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

41 lines 2.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AddParserToMetadata = AddParserToMetadata; const reflect_metadata_1 = require("@rxap/reflect-metadata"); const metadata_keys_1 = require("../metadata-keys"); const utilities_1 = require("../utilities"); /** * Adds a parser to the metadata of the specified target. This function first retrieves all existing parsers, * filters them based on certain conditions, and then appends the new parser to the metadata. * * @param {ElementParser} parser - The parser instance to be added. This parser should have a `parse` method and a `propertyKey`. * @param {any} target - The target object to which the parser will be added. This is typically a class or a class instance. * * The function operates in two main steps: * 1. It retrieves all element parsers associated with the constructor of the target, filters out any existing parsers that * should not be overwritten, and then adds the new parser's `parse` method to the `ElementParserMetaData.PARSER` metadata key. * 2. It retrieves all element parser instances associated with the constructor of the target, filters out any existing parser * instances that have the same `propertyKey` as the new parser, and then adds the new parser instance to the * `ElementParserMetaData.PARSER_INSTANCE` metadata key. * * Note: The function currently does not handle overwriting existing parsers with the same `propertyKey`. This functionality * needs to be tested and potentially implemented. */ function AddParserToMetadata(parser, target) { const addedParser = (0, utilities_1.GetAllElementParser)(target.constructor) .filter(p => { const propertyKey = Reflect.get(p, 'propertyKey'); return !propertyKey || propertyKey !== parser.propertyKey; }); const existingOwnAddedParser = (0, utilities_1.GetAllOwnElementParser)(target.constructor).filter(p => { const propertyKey = Reflect.get(p, 'propertyKey'); return propertyKey === parser.propertyKey; }); (0, reflect_metadata_1.setMetadata)(metadata_keys_1.ElementParserMetaData.PARSER, [...existingOwnAddedParser, ...addedParser, parser.parse], target.constructor); const addedElementParser = (0, utilities_1.GetAllElementParserInstances)(target.constructor) .filter(p => p.propertyKey !== parser.propertyKey); const existingOwnAddedElementParser = (0, utilities_1.GetAllOwnElementParserInstances)(target.constructor) .filter(p => p.propertyKey === parser.propertyKey); (0, reflect_metadata_1.setMetadata)(metadata_keys_1.ElementParserMetaData.PARSER_INSTANCE, [...existingOwnAddedElementParser, ...addedElementParser, parser], target); } //# sourceMappingURL=add-parser-to-metadata.js.map