2013-08-07 21:51:32 +00:00
|
|
|
module.exports = function getIntrospector(ModelBuilder) {
|
2013-07-26 20:06:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
function introspectType(value) {
|
2013-07-26 20:06:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Unknown type, using Any
|
|
|
|
if (value === null || value === undefined) {
|
|
|
|
return ModelBuilder.Any;
|
|
|
|
}
|
2013-07-26 20:06:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// Check registered schemaTypes
|
|
|
|
for (var t in ModelBuilder.schemaTypes) {
|
|
|
|
var st = ModelBuilder.schemaTypes[t];
|
|
|
|
if (st !== Object && st !== Array && (value instanceof st)) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 20:06:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var type = typeof value;
|
|
|
|
if (type === 'string' || type === 'number' || type === 'boolean') {
|
|
|
|
return type;
|
|
|
|
}
|
2013-07-26 20:06:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (value instanceof Date) {
|
|
|
|
return 'date';
|
|
|
|
}
|
2013-07-26 20:06:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (Array.isArray(value)) {
|
|
|
|
for (var i = 0; i < value.length; i++) {
|
|
|
|
if (value[i] === null || value[i] === undefined) {
|
|
|
|
continue;
|
2013-07-26 20:06:43 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
var itemType = introspectType(value[i]);
|
|
|
|
if (itemType) {
|
|
|
|
return [itemType];
|
2013-08-07 21:51:32 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
return 'array';
|
|
|
|
}
|
2013-07-26 20:06:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (type === 'function') {
|
|
|
|
return value.constructor.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
var properties = {};
|
|
|
|
for (var p in value) {
|
|
|
|
var itemType = introspectType(value[p]);
|
|
|
|
if (itemType) {
|
|
|
|
properties[p] = itemType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Object.keys(properties).length === 0) {
|
|
|
|
return 'object';
|
2013-07-26 20:06:43 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
return properties;
|
|
|
|
}
|
2013-07-26 20:06:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
return introspectType;
|
2013-08-07 21:51:32 +00:00
|
|
|
}
|
2013-07-26 20:06:43 +00:00
|
|
|
|
|
|
|
|