Refactor introspection to ModelBuilder

This commit is contained in:
Raymond Feng 2013-08-07 14:51:32 -07:00
parent f3443959a1
commit 0a5c0ff77d
4 changed files with 70 additions and 51 deletions

View File

@ -1,6 +1,6 @@
var DataSource = require('../../loopback-datasource-juggler').DataSource; var DataSource = require('../../loopback-datasource-juggler').DataSource;
var ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder; var ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder;
var introspectType = require('../lib/introspection'); var introspectType = require('../lib/introspection')(ModelBuilder);
var ds = new DataSource('memory'); var ds = new DataSource('memory');

View File

@ -1,59 +1,60 @@
var ModelBuilder = require('./model-builder').ModelBuilder; module.exports = function getIntrospector(ModelBuilder) {
function introspectType(value) { function introspectType(value) {
// Unknown type, using Any // Unknown type, using Any
if (value === null || value === undefined) { if (value === null || value === undefined) {
return ModelBuilder.Any; return ModelBuilder.Any;
}
// Check registered schemaTypes
for (var t in ModelBuilder.schemaTypes) {
var st = ModelBuilder.schemaTypes[t];
if (st !== Object && st !== Array && (value instanceof st)) {
return t;
} }
}
var type = typeof value; // Check registered schemaTypes
if (type === 'string' || type === 'number' || type === 'boolean') { for (var t in ModelBuilder.schemaTypes) {
return type; var st = ModelBuilder.schemaTypes[t];
} if (st !== Object && st !== Array && (value instanceof st)) {
return t;
if (value instanceof Date) {
return 'date';
}
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
if (value[i] === null || value[i] === undefined) {
continue;
} }
var itemType = introspectType(value[i]); }
var type = typeof value;
if (type === 'string' || type === 'number' || type === 'boolean') {
return type;
}
if (value instanceof Date) {
return 'date';
}
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
if (value[i] === null || value[i] === undefined) {
continue;
}
var itemType = introspectType(value[i]);
if (itemType) {
return [itemType];
}
}
return 'array';
}
if (type === 'function') {
return value.constructor.name;
}
var properties = {};
for (var p in value) {
var itemType = introspectType(value[p]);
if (itemType) { if (itemType) {
return [itemType]; properties[p] = itemType;
} }
} }
return 'array'; if (Object.keys(properties).length === 0) {
} return 'object';
if (type === 'function') {
return value.constructor.name;
}
var properties = {};
for (var p in value) {
var itemType = introspectType(value[p]);
if (itemType) {
properties[p] = itemType;
} }
return properties;
} }
if(Object.keys(properties).length === 0) {
return 'object'; return introspectType;
}
return properties;
} }
module.exports = introspectType;

View File

@ -8,6 +8,11 @@ var List = require('./list.js');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var util = require('util'); var util = require('util');
// Set up types
require('./types')(ModelBuilder);
var introspect = require('./introspection')(ModelBuilder);
/** /**
* Export public API * Export public API
*/ */
@ -31,9 +36,6 @@ function ModelBuilder() {
util.inherits(ModelBuilder, EventEmitter); util.inherits(ModelBuilder, EventEmitter);
// Set up types
require('./types')(ModelBuilder);
/** /**
* Define class * Define class
* *
@ -516,5 +518,21 @@ ModelBuilder.prototype.buildModels = function (schemas) {
return models; return models;
} }
/**
* Introspect the json document to build a corresponding model
* @param name
* @param json
* @param options
* @returns {}
*/
ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) {
// Introspect the JSON document to generate a schema
var schema = introspect(json);
// Create a model for the generated schema
return this.define(name, schema, options);
}

View File

@ -1,6 +1,6 @@
var assert = require('assert'); var assert = require('assert');
var ModelBuilder = require('../lib/model-builder').ModelBuilder; var ModelBuilder = require('../lib/model-builder').ModelBuilder;
var introspectType = require('../lib/introspection'); var introspectType = require('../lib/introspection')(ModelBuilder);
var traverse = require('traverse'); var traverse = require('traverse');
describe('Introspection of model definitions from JSON', function() { describe('Introspection of model definitions from JSON', function() {