UNPKG

@opra/common

Version:
122 lines (121 loc) 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimpleType = void 0; require("reflect-metadata"); const objects_1 = require("@jsopen/objects"); const ts_gems_1 = require("ts-gems"); const valgen_1 = require("valgen"); const index_js_1 = require("../../helpers/index.js"); const index_js_2 = require("../../schema/index.js"); const constants_js_1 = require("../constants.js"); const simple_type_decorator_js_1 = require("../decorators/simple-type.decorator.js"); const data_type_js_1 = require("./data-type.js"); /** * SimpleType constructor */ exports.SimpleType = function (...args) { // Decorator if (!this) return exports.SimpleType[constants_js_1.DECORATOR](...args); // Constructor const [owner, initArgs, context] = args; data_type_js_1.DataType.call(this, owner, initArgs, context); const _this = (0, ts_gems_1.asMutable)(this); _this.kind = index_js_2.OpraSchema.SimpleType.Kind; if (initArgs.base) { // noinspection SuspiciousTypeOfGuard if (!(initArgs.base instanceof exports.SimpleType)) { throw new TypeError(`"${initArgs.base.kind}" can't be set as base for a "${this.kind}"`); } _this.base = initArgs.base; } _this.properties = initArgs.properties; _this.ownNameMappings = { ...initArgs.nameMappings }; _this.nameMappings = { ..._this.base?.nameMappings, ...initArgs.nameMappings, }; _this.ownAttributes = (0, index_js_1.cloneObject)(initArgs.attributes || {}); _this.attributes = _this.base ? (0, index_js_1.cloneObject)(_this.base.attributes) : {}; if (_this.ownAttributes) { for (const [k, el] of Object.entries(_this.ownAttributes)) { if (_this.attributes[k]?.sealed) throw new TypeError(`Sealed attribute "${k}" can not be overwritten`); _this.attributes[k] = el; } } _this._generateDecoder = initArgs.generateDecoder; _this._generateEncoder = initArgs.generateEncoder; }; /** * * @class SimpleType */ class SimpleTypeClass extends data_type_js_1.DataType { extendsFrom(baseType) { if (!(baseType instanceof data_type_js_1.DataType)) baseType = this.node.getDataType(baseType); if (!(baseType instanceof exports.SimpleType)) return false; if (baseType === this) return true; return !!this.base?.extendsFrom(baseType); } generateCodec(codec, options, properties) { const prop = { ...this.properties, ...properties, }; if (codec === 'decode') { let t = this; while (t) { if (t._generateDecoder) return t._generateDecoder(prop, options?.documentElement || this.owner, options?.scope); t = this.base; } return valgen_1.isAny; } let t = this; while (t) { if (t._generateEncoder) return t._generateEncoder(prop, options?.documentElement || this.owner, options?.scope); t = this.base; } return valgen_1.isAny; } toJSON(options) { const superJson = super.toJSON(options); const baseName = this.base ? this.node.getDataTypeNameWithNs(this.base) : undefined; const attributes = (0, objects_1.omitUndefined)(this.ownAttributes); let properties; if (this.properties && typeof this.properties.toJSON === 'function') { properties = this.properties.toJSON(this.properties, this.owner, options); } else properties = this.properties ? (0, index_js_1.cloneObject)(this.properties) : {}; const out = { ...superJson, kind: this.kind, base: baseName, attributes: attributes && Object.keys(attributes).length ? attributes : undefined, properties: Object.keys(properties).length ? properties : undefined, }; if (Object.keys(this.ownNameMappings).length) out.nameMappings = { ...this.ownNameMappings }; return (0, objects_1.omitUndefined)(out, true); } _locateBase(callback) { if (!this.base) return; if (callback(this.base)) return this.base; if (this.base._locateBase) return this.base._locateBase(callback); } } exports.SimpleType.prototype = SimpleTypeClass.prototype; Object.assign(exports.SimpleType, simple_type_decorator_js_1.SimpleTypeDecoratorFactory); exports.SimpleType[constants_js_1.DECORATOR] = simple_type_decorator_js_1.SimpleTypeDecoratorFactory; exports.SimpleType.Attribute = simple_type_decorator_js_1.AttributeDecoratorFactory;