UNPKG

@calf/serializable

Version:

Serializable module of Calf framework.

110 lines (109 loc) 4 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaParser = void 0; // Parsers var base_parser_1 = require("./base.parser"); var entity_parser_1 = require("./entity.parser"); var property_parser_1 = require("./property.parser"); /** * Schema parser */ var SchemaParser = /** @class */ (function (_super) { __extends(SchemaParser, _super); function SchemaParser() { return _super !== null && _super.apply(this, arguments) || this; } /** * Parse target * @param target */ SchemaParser.prototype.parse = function (target) { var _this = this; // Initialize schema var schema = { entity: {}, properties: {} }; // Init entity parser var eParser = new entity_parser_1.EntityParser(); // Get entity definition schema.entity = eParser.parse(target); // Check for extends if (schema.entity && schema.entity.extends && schema.entity.extends.length) { // Process each entity schema.entity.extends.forEach(function (extend) { return _this.mapProperties(extend, schema); }); } // Map properties this.mapProperties(target, schema); // Return schema return schema; }; /** * Map properties * @param target * @param schema */ SchemaParser.prototype.mapProperties = function (target, schema) { // Init property parser var pParser = new property_parser_1.PropertyParser(); // Get target instance, otherwise we wont get access // to its properties var instance = new target(); // And keep original var original = instance; // Initialize array of property names var propertyNames = this.getPropertyNames(instance); // Iterate each property name to get property definition propertyNames.forEach(function (name) { // Get definition var pDefinition = pParser.parse(original, name); // Check if any definition was found if (!pDefinition) { return; } // Assign definition to schema // This condition is not needed, but typescript compilers // does not like it when it is not there if (schema.properties) { schema.properties[name] = pDefinition; } }); }; /** * Get property names * @param target */ SchemaParser.prototype.getPropertyNames = function (target) { // Init result var names = []; // Get all properties, inluding those from prototypes for (; target != null; target = Object.getPrototypeOf(target)) { // Get own property names var op = Object.getOwnPropertyNames(target); // Iterate names for (var index = 0; index < op.length; index++) { // Add name to array if it is not already there if (names.indexOf(op[index]) == -1) { names.push(op[index]); } } } // Return names return names; }; return SchemaParser; }(base_parser_1.BaseParser)); exports.SchemaParser = SchemaParser;