{"size": 2104, "ext": "js", "max_stars_repo_path": "schemas/WixSchemaParser.js", "max_stars_repo_name": "urish/openapi-node", "max_stars_repo_stars_event_min_datetime": "2015-05-26T08:26:46.000Z", "max_stars_repo_stars_event_max_datetime": "2015-05-26T08:26:46.000Z", "max_issues_repo_path": "schemas/WixSchemaParser.js", "max_issues_repo_name": "urish/openapi-node", "max_issues_repo_issues_event_min_datetime": "2015-09-13T20:19:51.000Z", "max_issues_repo_issues_event_max_datetime": "2016-11-23T22:57:35.000Z", "max_forks_repo_path": "schemas/WixSchemaParser.js", "max_forks_repo_name": "urish/openapi-node", "max_forks_repo_forks_event_min_datetime": "2015-02-03T14:53:09.000Z", "max_forks_repo_forks_event_max_datetime": "2016-03-15T18:01:14.000Z"}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": 7, "avg_line_length": 36.9122807018, "max_line_length": 82, "alphanum_fraction": 0.5826996198}
var _ = require('lodash-node');
var RuntimeVisitor = require('./visitors/RuntimeVisitor.js');

function WixSchemaParser(visitor) {
    this.visitor = visitor;
    if(this.visitor === undefined) {
        this.visitor = new RuntimeVisitor();
    }
}

WixSchemaParser.prototype = {
    toWixSchemaObject : function(jsonObj, name) {
        var jsonSchema = typeof jsonObj === 'object' ? jsonObj : require(jsonObj);
        this.visitor.startVisiting(name);
        this.parseObject(name, jsonSchema);
        return this.visitor.endVisiting(name);
    },
    handleType : function(type, propertyName, schemaItem) {
        if(type === 'string') {
            var enumProperties = null;
            if(schemaItem.hasOwnProperty('enum')) {
                enumProperties = schemaItem.enum;
            }
            this.visitor.stringPropEncountered(propertyName, enumProperties);
        } else if(type === 'array') {
            if(schemaItem.hasOwnProperty('items')) {
                this.parseObject(propertyName, schemaItem.items, true);
                this.visitor.arrayItemEncountered(propertyName);
            }
        } else if(type === 'object') {
            this.parseObject(propertyName, schemaItem, false);
        } else { //no other special handling right now
            this.visitor.otherPropEncountered(propertyName);
        }
    },
    parseObject : function(name, schemaItem, isArray) {
        if(schemaItem.hasOwnProperty('properties')) {
            var props = schemaItem.properties;
            if(props !== null) {
                this.visitor.objectEncountered(name, isArray);
                for (var prop in props) {
                    if (props[prop].hasOwnProperty('type')) {
                        this.handleType(props[prop].type, prop, props[prop]);
                    }
                }
                this.visitor.objectFinished(name, isArray);
            }
        }
    }
};

module.exports = {
    parse : function(jsonObj, name, visitor) {
        var parser = new WixSchemaParser(visitor);
        return parser.toWixSchemaObject(jsonObj, name);
    }
}