UNPKG

ern-api-gen

Version:

Electrode Native API generator

123 lines 3.9 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const properties = __importStar(require("./properties")); const beanUtils_1 = require("../java/beanUtils"); const ComposedModel_1 = __importDefault(require("./ComposedModel")); const values = obj => Object.keys(obj).map(key => { return obj[key]; }); /** * Best guess here. It is possible for json schema to refer * to types that we do not support. I.E, union types. * @param prop * @returns {*} */ let TYPES; let STR_TYPES; function resolve(prop = {}) { TYPES = TYPES || values(properties); STR_TYPES = STR_TYPES || TYPES.filter(t => t && t.TYPE === 'string'); if (prop == null) { return properties.NullProperty; } if (prop.format && prop.type === 'string') { for (const clz of STR_TYPES) { if (prop.format === clz.FORMAT) { return clz; } } } for (const clz of TYPES) { if (!prop.type) { if ('properties' in prop) { if (prop.additionalProperties) { return properties.MapProperty; } return properties.ObjectProperty; } if ('anyOf' in prop) { return properties.ObjectProperty; } if ('items' in prop) { return properties.ArrayProperty; } if ('$ref' in prop) { return properties.RefProperty; } if (prop.minimum || prop.maximum || prop.exclusiveMinimum || prop.exclusiveMaximum || prop.multipleOf) { return properties.NumberProperty; } if (prop.minLength || prop.maxLength || prop.pattern) { return properties.StringProperty; } if (prop.format) { for (const strClz of STR_TYPES) { if (strClz.FORMAT === prop.format) { return strClz; } } } } else if (clz.TYPE === prop.type) { if (prop.type === 'object') { return prop.additionalProperties ? properties.MapProperty : properties.ObjectProperty; } if (prop.format) { if (clz.FORMAT === prop.format) { return clz; } } else { return clz; } } } if (prop.allOf) { return ComposedModel_1.default; } throw new Error(`Could not resolve type ${prop.type} ${prop.format}`); } exports.resolve = resolve; function setup() { for (const { prototype, allowedProps } of values(properties)) { beanUtils_1.beanify(prototype, allowedProps); } } let hasSetup = false; function factory(prop, parent) { const TYPESF = values(properties); if (!hasSetup) { setup(); hasSetup = true; } for (const type of TYPESF) { if (prop instanceof type) { return prop; } } const PropertyClz = resolve(prop); if (!PropertyClz) { throw new Error(`Can not resolve property for ${prop}`); } const ret = beanUtils_1.apply(new PropertyClz(prop, parent), prop); return ret; } exports.factory = factory; exports.default = factory; //# sourceMappingURL=factory.js.map