UNPKG

@allgemein/schema-api

Version:
133 lines 4.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaUtils = void 0; const lodash_1 = require("lodash"); const Constants_1 = require("./Constants"); const IClassRef_1 = require("../api/IClassRef"); const IEntityRef_1 = require("../api/IEntityRef"); const base_1 = require("@allgemein/base"); let anonId = 0; class SchemaUtils { /** * convert an json entry to an instance of given entityRef type * * - with options can be set pre/post processing for build * - beforeBuild - preprocess of an object * - afterBuild - postprocess of an object * - createAndCopy - if true the only the instance is created, all properties are passed by assign * * @param entityRef * @param data * @param options: IBuildOptions * */ static transform(entityRef, data, options = {}) { let object = entityRef.create(!(0, lodash_1.isUndefined)(options.skipClassNamespaceInfo) ? !options.skipClassNamespaceInfo : true); if (options.beforeBuild) { options.beforeBuild(entityRef, data, object, options); } if (!(0, lodash_1.get)(options, Constants_1.OPT_CREAT_AND_COPY, false)) { for (let p of entityRef.getPropertyRefs()) { if (((0, lodash_1.isNull)(data[p.name]) || (0, lodash_1.isUndefined)(data[p.name]))) { //object[p.name] = null; continue; } if (p.isReference()) { let ref = p.getTargetRef(); if (p.isCollection() || (0, lodash_1.isArray)(data[p.name])) { object[p.name] = []; for (let i = 0; i < data[p.name].length; i++) { object[p.name][i] = ref.build(data[p.name][i], options); } } else { object[p.name] = ref.build(data[p.name], options); } } else { if (p.isCollection() && ((0, lodash_1.isArray)(data[p.name]) || (0, lodash_1.isSet)(data[p.name]))) { object[p.name] = []; for (let i = 0; i < data[p.name].length; i++) { let v = data[p.name][i]; if (v) { object[p.name][i] = p.convert(v, options); } else { object[p.name][i] = null; } } } else if (p.isCollection() && !((0, lodash_1.isArray)(data[p.name]) || (0, lodash_1.isSet)(data[p.name]))) { throw new base_1.NotYetImplementedError(); } else { object[p.name] = p.convert(data[p.name], options); } } } } else { (0, lodash_1.assign)(object, data); } if (options.afterBuild) { options.afterBuild(entityRef, data, object, options); } return object; } /** * Create a class of given name * * @param str */ static clazz(str) { const X = this.clazzAnonymous(); Object.defineProperty(X, Constants_1.C_PROP_NAME, { value: str }); return X; } /** * Create a class of given name * * @param str */ static clazzAnonymous() { const X = new Function(); const id = anonId++; Object.defineProperty(X, 'anonId', { value: id }); return X; } /** * Return inherited class if present else null will be delivered * * @param klass */ static getInherited(klass) { const proto = Reflect.getPrototypeOf(klass); if (proto.name && !(0, lodash_1.isEmpty)(proto.name) && proto.name !== Object.name && proto.name !== klass.name) { return proto; } return null; } static getFunction(klass) { if ((0, IEntityRef_1.isEntityRef)(klass)) { return klass.getClassRef().getClass(true); } else if ((0, IClassRef_1.isClassRef)(klass)) { return klass.getClass(true); } else if ((0, lodash_1.isFunction)(klass)) { return klass; } throw new Error('no class found in ' + klass); } static normValue(value) { try { return JSON.parse(JSON.stringify(value)); } catch (err) { console.error(err); } return 'undefined'; } } exports.SchemaUtils = SchemaUtils; //# sourceMappingURL=SchemaUtils.js.map