UNPKG

@opra/common

Version:
76 lines (75 loc) 2.18 kB
import 'reflect-metadata'; import { omitUndefined } from '@jsopen/objects'; import { asMutable } from 'ts-gems'; import { vg } from 'valgen'; import { OpraSchema } from '../../schema/index.js'; import { DATATYPE_METADATA, DECORATOR } from '../constants.js'; import { DataType } from './data-type.js'; /** * @class ArrayType */ export const ArrayType = function (...args) { // ArrayType factory if (!this) { return ArrayType[DECORATOR].apply(undefined, args); } // Constructor const [owner, initArgs, context] = args; DataType.call(this, owner, initArgs, context); const _this = asMutable(this); _this.kind = OpraSchema.ArrayType.Kind; _this.type = initArgs.type; _this.minOccurs = initArgs.minOccurs; _this.maxOccurs = initArgs.maxOccurs; }; /** * * @class ArrayType */ class ArrayTypeClass extends DataType { toJSON(options) { const superJson = super.toJSON(options); const typeName = this.node.getDataTypeNameWithNs(this.type); return omitUndefined({ ...superJson, kind: this.kind, type: typeName ? typeName : this.type.toJSON(options), minOccurs: this.minOccurs, maxOccurs: this.maxOccurs, }); } generateCodec(codec, options, properties) { let fn = this.type.generateCodec(codec, options, properties); fn = vg.isArray(fn); const fns = []; if (this.minOccurs) fns.push(vg.lengthMin(this.minOccurs)); if (this.maxOccurs) fns.push(vg.lengthMax(this.maxOccurs)); if (fns.length > 0) return vg.pipe([fn, ...fns], { returnIndex: 0 }); return fn; } extendsFrom() { return false; } _locateBase() { return; } } ArrayType.prototype = ArrayTypeClass.prototype; ArrayType[DECORATOR] = ArrayTypeFactory; /** * */ function ArrayTypeFactory(clasRefs, options) { class ArrayClass { } const metadata = { ...options, kind: OpraSchema.ArrayType.Kind, type: clasRefs, }; Reflect.defineMetadata(DATATYPE_METADATA, metadata, ArrayClass); return ArrayClass; }