@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
180 lines • 8.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElementChildAttributeSerializer = exports.ElementChildAttributeParser = void 0;
exports.ElementChildAttribute = ElementChildAttribute;
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 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");
const add_serializer_to_metadata_1 = require("./utilities/add-serializer-to-metadata");
let ElementChildAttributeParser = class ElementChildAttributeParser {
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) {
var _a;
if (element.hasChild(this.tag)) {
element = element.getChild(this.tag);
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 (element.has(this.attribute)) {
const rawValue = element.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);
}
}
else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
parsedElement[this.propertyKey] = value;
}
}
else if (this.required) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (parsedElement[this.propertyKey] === undefined) {
throw new error_1.RxapXmlParserValidateRequiredError(`Element <${element.name}> child <${this.tag}> attribute is required!`, parsedElement.__tag);
}
}
return parsedElement;
}
};
exports.ElementChildAttributeParser = ElementChildAttributeParser;
exports.ElementChildAttributeParser = ElementChildAttributeParser = tslib_1.__decorate([
(0, mixin_1.Mixin)(attribute_element_mixin_1.AttributeElementMixin, tag_element_mixin_1.TagElementMixin)
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
,
tslib_1.__metadata("design:paramtypes", [String, Object])
], ElementChildAttributeParser);
let ElementChildAttributeSerializer = class ElementChildAttributeSerializer {
constructor(propertyKey, options) {
this.propertyKey = propertyKey;
this.options = options;
this.serialize = this.serialize.bind(this);
Reflect.set(this.serialize, 'propertyKey', propertyKey);
}
serialize(xmlParser, element, parsedElement) {
// @ts-expect-error the propertyKey is set by the property decorator
const child = parsedElement[this.propertyKey];
if (child !== undefined) {
if (!element.hasChild(this.tag)) {
element.addChild(this.tag);
}
const childElement = element.getChild(this.tag);
childElement.set(this.attribute, child);
}
else if (this.required) {
throw new error_1.RxapXmlParserValidateRequiredError(`Element <${element.name}> child <${this.tag}> raw content is required!`, parsedElement.__tag);
}
}
};
exports.ElementChildAttributeSerializer = ElementChildAttributeSerializer;
exports.ElementChildAttributeSerializer = ElementChildAttributeSerializer = tslib_1.__decorate([
(0, mixin_1.Mixin)(attribute_element_mixin_1.AttributeElementMixin, tag_element_mixin_1.TagElementMixin)
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
,
tslib_1.__metadata("design:paramtypes", [String, Object])
], ElementChildAttributeSerializer);
function ElementChildAttribute(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 ElementChildAttributeParser(propertyKey, optionsWithDefaults);
(0, add_parser_to_metadata_1.AddParserToMetadata)(parser, target);
const serializer = new ElementChildAttributeSerializer(propertyKey, options);
(0, add_serializer_to_metadata_1.AddSerializerToMetadata)(serializer, target);
if (optionsWithDefaults.required) {
(0, required_property_1.RequiredProperty)()(target, propertyKey);
}
};
}
//# sourceMappingURL=element-child-attribute.js.map