UNPKG

@itwin/ecschema-metadata

Version:

ECObjects core concepts in typescript

109 lines 5.52 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaParser = void 0; exports.parseCustomAttribute = parseCustomAttribute; const Constants_1 = require("../Constants"); const ECObjects_1 = require("../ECObjects"); const ClassParsers_1 = require("./ClassParsers"); const SchemaItemParsers_1 = require("./SchemaItemParsers"); function clean(_key, value) { return value === null ? undefined : value; } /** * Parses SchemaProps JSON returned from an ECSql query and returns the correct SchemaProps JSON object. * This is necessary as a small amount information (ie. CustomAttributes, unqualified type names, etc.) * returned from the iModelDb is in a different format than is required for a given Schema or * SchemaItemProps JSON object. * @internal */ class SchemaParser { /** * Corrects the SchemaProps JSON returned from the query to a proper SchemaProps * JSON object. * @param schema The SchemaProps JSON object to parse. * @param context The SchemaContext that will contain the schema and it's references. * @returns The corrected SchemaProps JSON. */ static async parse(schema, context) { const props = schema; props.$schema = Constants_1.ECSchemaNamespaceUris.SCHEMAURL3_2_JSON, props.customAttributes = props.customAttributes ? props.customAttributes.map((attr) => { return parseCustomAttribute(attr); }) : undefined; props.label = props.label === null ? undefined : props.label; props.description = props.description === null ? undefined : props.description; if (props.items) { props.items = await this.parseItems(props.items, props.name, context); } if (!props.customAttributes || props.customAttributes.length === 0) delete props.customAttributes; const cleaned = JSON.parse(JSON.stringify(props, clean)); return cleaned; } /** * Parse the given SchemaItemProps array, as returned from an ECSql query, and returns the corrected SchemaItemProps. * @param schemaItems The SchemaItemProps array returned from an iModelDb. * @param schemaName The name of the Schema to which the SchemaItemProps belong. * @param context The SchemaContext containing the Schema. * @returns The corrected SchemaItemProps. */ static async parseSchemaItems(schemaItems, schemaName, context) { const items = []; for (const item of schemaItems) { const props = await this.parseItem(item, schemaName, context); const cleaned = JSON.parse(JSON.stringify(props, clean)); items.push(cleaned); } return items.length > 0 ? items : undefined; } static async parseItems(schemaItemProps, schemaName, context) { const items = {}; for (const itemProps of schemaItemProps) { const props = await this.parseItem(itemProps, schemaName, context); items[props.name] = props; delete props.name; } return Object.keys(items).length > 0 ? items : undefined; } static async parseItem(props, schemaName, context) { const schemaItem = "string" === typeof (props) ? JSON.parse(props) : props; const type = (0, ECObjects_1.parseSchemaItemType)(schemaItem.schemaItemType); switch (type) { case ECObjects_1.SchemaItemType.KindOfQuantity: const koqParser = new SchemaItemParsers_1.KindOfQuantityParser(schemaName, context); return koqParser.parse(schemaItem); case ECObjects_1.SchemaItemType.EntityClass: case ECObjects_1.SchemaItemType.StructClass: const classParser = new ClassParsers_1.ClassParser(schemaName, context); return classParser.parse(schemaItem); case ECObjects_1.SchemaItemType.RelationshipClass: const relationshipParser = new ClassParsers_1.RelationshipClassParser(schemaName, context); return relationshipParser.parse(schemaItem); case ECObjects_1.SchemaItemType.Mixin: const mixinParser = new ClassParsers_1.MixinParser(schemaName, context); return mixinParser.parse(schemaItem); case ECObjects_1.SchemaItemType.CustomAttributeClass: const caParser = new ClassParsers_1.CustomAttributeClassParser(schemaName, context); return caParser.parse(schemaItem); default: const itemParser = new SchemaItemParsers_1.SchemaItemParser(schemaName, context); return itemParser.parse(schemaItem); } } } exports.SchemaParser = SchemaParser; /** * Utility method to parse CustomAttribute data retrieved from a ECSql query. * @param customAttribute CustomAttribute data as retrieved from an iModel query. * @returns The CustomAttribute instance. * @internal */ function parseCustomAttribute(customAttribute) { return { ...customAttribute[customAttribute.ecClass], className: `${(customAttribute.ecSchema).split('.')[0]}.${customAttribute.ecClass}`, }; } //# sourceMappingURL=SchemaParser.js.map