UNPKG

@rxap/xml-parser

Version:

Provides a set of decorators and services for parsing and serializing XML documents into TypeScript classes. It simplifies the process of mapping XML elements and attributes to class properties, handling data validation, and serializing objects back into

160 lines 7.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElementChildrenAttributeParser = void 0; exports.ElementChildrenAttribute = ElementChildrenAttribute; const tslib_1 = require("tslib"); const mixin_1 = require("@rxap/mixin"); const reflect_metadata_1 = require("@rxap/reflect-metadata"); const utilities_1 = require("@rxap/utilities"); const error_1 = require("../error"); const metadata_keys_1 = require("./metadata-keys"); const attribute_element_mixin_1 = require("./mixins/attribute-element.mixin"); const children_element_mixin_1 = require("./mixins/children-element.mixin"); const tag_element_mixin_1 = require("./mixins/tag-element.mixin"); const required_property_1 = require("./required-property"); const add_parser_to_metadata_1 = require("./utilities/add-parser-to-metadata"); let ElementChildrenAttributeParser = class ElementChildrenAttributeParser { constructor(propertyKey, options) { this.propertyKey = propertyKey; this.options = options; this.parse = this.parse.bind(this); Reflect.set(this.parse, 'propertyKey', propertyKey); } parse(xmlParser, element, parsedElement) { const rxapElementChildren = this.getChildren(element); if (!rxapElementChildren) { throw new error_1.RxapXmlParserValidateRequiredError(`The child group element '${this.options.group}' is required for ${parsedElement.__tag}!`, parsedElement.__tag); } const children = rxapElementChildren.filter(child => child.hasName(this.tag)); if (this.required && children.length === 0) { throw new error_1.RxapXmlParserValidateRequiredError(`Some element child <${this.tag}> is required in <${parsedElement.__tag}>!`, parsedElement.__tag); } if (this.min !== null && this.min > children.length) { throw new error_1.RxapXmlParserValidateError(`Element child <${this.tag}> should be at least ${this.min} in <${parsedElement.__tag}>!`, parsedElement.__tag); } if (this.max !== null && this.max > children.length) { throw new error_1.RxapXmlParserValidateError(`Element child <${this.tag}> should be at most ${this.max} in <${parsedElement.__tag}>!`, parsedElement.__tag); } if (!(0, utilities_1.hasIndexSignature)(parsedElement)) { throw new Error('The parsed element has no index signature'); } if (!Array.isArray(parsedElement[this.propertyKey])) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore parsedElement[this.propertyKey] = []; } parsedElement[this.propertyKey] .push(...children.map(child => { var _a; let value = (_a = this.defaultValue) !== null && _a !== void 0 ? _a : // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore parsedElement[this.propertyKey]; if (child.has(this.attribute)) { const rawValue = child.get(this.attribute, undefined, true); value = this.parseValue(rawValue); } if (value === undefined) { if (this.required) { throw new error_1.RxapXmlParserValidateRequiredError(`The attribute '${this.attribute}' is required for child <${parsedElement.__tag}>`, parsedElement.__tag, this.attribute); } } return value; })); return parsedElement; } }; exports.ElementChildrenAttributeParser = ElementChildrenAttributeParser; exports.ElementChildrenAttributeParser = ElementChildrenAttributeParser = tslib_1.__decorate([ (0, mixin_1.Mixin)(attribute_element_mixin_1.AttributeElementMixin, tag_element_mixin_1.TagElementMixin, children_element_mixin_1.ChildrenElementMixin) // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging , tslib_1.__metadata("design:paramtypes", [String, Object]) ], ElementChildrenAttributeParser); function ElementChildrenAttribute(optionsOrTag, optionsOrAttribute, withOptions) { return function (target, propertyKey) { var _a, _b, _c; let tag; let attribute; let options; // if the options parameter is not defined. Then try to parse the possible input if (!withOptions) { // possible input: // tag, attribute // tag, options // options if (typeof optionsOrAttribute === 'string') { // possible input: // tag, attribute if (typeof optionsOrTag !== 'string') { throw new Error('Invalid input. If the second parameter is a string the first must also be a string'); } tag = optionsOrTag; attribute = optionsOrAttribute; options = { tag, attribute, }; } else if (optionsOrAttribute === undefined) { // possible input // tag // options if (typeof optionsOrTag === 'string') { // possible input // tag tag = optionsOrTag; attribute = propertyKey; options = { tag, attribute, }; } else if (optionsOrTag === undefined) { throw new Error('Invalid input. If the first parameter is undefined the second must be a string or object'); } else { // possible input // options tag = optionsOrTag.tag; attribute = (_a = optionsOrTag.attribute) !== null && _a !== void 0 ? _a : propertyKey; options = Object.assign(Object.assign({}, optionsOrTag), { tag, attribute }); } } else { // possible input // tag, options if (typeof optionsOrTag !== 'string') { throw new Error('Invalid input. If the second parameter is a object the first must also be a string'); } tag = optionsOrTag; attribute = (_b = optionsOrAttribute.attribute) !== null && _b !== void 0 ? _b : propertyKey; options = Object.assign(Object.assign({}, optionsOrAttribute), { tag, attribute }); } } else { // possible input // tag, attribute, options if (typeof optionsOrAttribute !== 'string') { throw new Error('Invalid input. If the third parameter is a object the second must also be a string'); } if (typeof optionsOrTag !== 'string') { throw new Error('Invalid input. If the third parameter is a object the first must also be a string'); } tag = optionsOrTag; attribute = optionsOrAttribute; options = Object.assign(Object.assign({}, withOptions), { tag, attribute }); } options = (0, utilities_1.deepMerge)(options, (_c = (0, reflect_metadata_1.getMetadata)(metadata_keys_1.ElementParserMetaData.OPTIONS, target, propertyKey)) !== null && _c !== void 0 ? _c : {}); const optionsWithDefaults = Object.assign({ attribute: propertyKey }, options); const parser = new ElementChildrenAttributeParser(propertyKey, optionsWithDefaults); (0, add_parser_to_metadata_1.AddParserToMetadata)(parser, target); if (optionsWithDefaults.required) { (0, required_property_1.RequiredProperty)()(target, propertyKey); } }; } //# sourceMappingURL=element-children-attribute.js.map