2014-07-05 19:32:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
2014-07-06 14:50:24 +00:00
|
|
|
var _cloneDeep = require('lodash.clonedeep');
|
2014-07-10 18:17:26 +00:00
|
|
|
var translateDataTypeKeys = require('./translate-data-type-keys');
|
2014-07-05 19:32:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the modelHelper singleton.
|
|
|
|
*/
|
|
|
|
var modelHelper = module.exports = {
|
|
|
|
/**
|
|
|
|
* Given a class (from remotes.classes()), generate a model definition.
|
|
|
|
* This is used to generate the schema at the top of many endpoints.
|
|
|
|
* @param {Class} class Remote class.
|
|
|
|
* @return {Object} Associated model definition.
|
|
|
|
*/
|
|
|
|
generateModelDefinition: function generateModelDefinition(aClass) {
|
|
|
|
var def = aClass.ctor.definition;
|
|
|
|
var name = def.name;
|
|
|
|
|
|
|
|
var required = [];
|
2014-07-06 14:50:24 +00:00
|
|
|
// Don't modify original properties.
|
|
|
|
var properties = _cloneDeep(def.properties);
|
2014-07-05 19:32:00 +00:00
|
|
|
|
|
|
|
// Iterate through each property in the model definition.
|
2014-07-10 16:32:58 +00:00
|
|
|
// Types may be defined as constructors (e.g. String, Date, etc.),
|
|
|
|
// or as strings; getPropType() will take care of the conversion.
|
|
|
|
// See more on types:
|
|
|
|
// https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#431-primitives
|
|
|
|
Object.keys(properties).forEach(function(key) {
|
2014-07-06 14:50:24 +00:00
|
|
|
var prop = properties[key];
|
2014-07-05 19:32:00 +00:00
|
|
|
|
|
|
|
// Eke a type out of the constructors we were passed.
|
2014-07-10 16:32:58 +00:00
|
|
|
prop = modelHelper.LDLPropToSwaggerDataType(prop);
|
2014-07-05 19:32:00 +00:00
|
|
|
|
|
|
|
// Required props sit in a per-model array.
|
2014-07-10 16:32:58 +00:00
|
|
|
if (prop.required || (prop.id && !prop.generated)) {
|
2014-07-05 19:32:00 +00:00
|
|
|
required.push(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change mismatched keys.
|
2014-07-10 18:17:26 +00:00
|
|
|
prop = translateDataTypeKeys(prop);
|
2014-07-10 16:32:58 +00:00
|
|
|
|
|
|
|
// Assign this back to the properties object.
|
|
|
|
properties[key] = prop;
|
2014-07-05 19:32:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var out = {};
|
|
|
|
out[name] = {
|
|
|
|
id: name,
|
2014-07-06 14:50:24 +00:00
|
|
|
properties: properties,
|
2014-07-05 19:32:00 +00:00
|
|
|
required: required
|
|
|
|
};
|
|
|
|
return out;
|
2014-07-10 16:32:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a propType (which may be a function, string, or array),
|
|
|
|
* get a string type.
|
|
|
|
* @param {*} propType Prop type description.
|
|
|
|
* @return {String} Prop type string.
|
|
|
|
*/
|
|
|
|
getPropType: function getPropType(propType) {
|
|
|
|
if (typeof propType === 'function') {
|
|
|
|
propType = propType.name.toLowerCase();
|
|
|
|
} else if(Array.isArray(propType)) {
|
|
|
|
propType = 'array';
|
|
|
|
}
|
|
|
|
return propType;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Converts a prop defined with the LDL spec to one conforming to the
|
|
|
|
// Swagger spec.
|
|
|
|
// https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#431-primitives
|
|
|
|
LDLPropToSwaggerDataType: function LDLPropToSwaggerDataType(prop) {
|
|
|
|
var out = _cloneDeep(prop);
|
|
|
|
out.type = modelHelper.getPropType(out.type);
|
|
|
|
|
|
|
|
if (out.type === 'array') {
|
|
|
|
var arrayProp = prop.type[0];
|
|
|
|
if (!arrayProp.type) arrayProp = {type: arrayProp};
|
|
|
|
out.items = modelHelper.LDLPropToSwaggerDataType(arrayProp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out.type === 'date') {
|
|
|
|
out.type = 'string';
|
|
|
|
out.format = 'date';
|
|
|
|
} else if (out.type === 'buffer') {
|
|
|
|
out.type = 'string';
|
|
|
|
out.format = 'byte';
|
|
|
|
} else if (out.type === 'number') {
|
|
|
|
out.format = 'double'; // Since all JS numbers are doubles
|
|
|
|
}
|
|
|
|
return out;
|
2014-07-05 19:32:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-10 16:32:58 +00:00
|
|
|
|