UNPKG

@itwin/ecschema-metadata

Version:

ECObjects core concepts in typescript

122 lines 5.81 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.KindOfQuantityParser = exports.SchemaItemParser = void 0; const ECObjects_1 = require("../ECObjects"); const OverrideFormat_1 = require("../Metadata/OverrideFormat"); const SchemaItem_1 = require("../Metadata/SchemaItem"); const SchemaParser_1 = require("./SchemaParser"); /** * Parses SchemaItemProps JSON returned from an ECSql query and returns the correct SchemaItemProps JSON. * This is necessary as a small amount information (ie. CustomAttribute data) returned from the iModelDb * is in a different format than is required for a SchemaItemProps JSON object. * @internal */ class SchemaItemParser { _schema; _context; /** * Initializes a new SchemaItemParser. * @param schemaName The name the Schema containing the SchemaItems. * @param context The SchemaContext containing the Schema. */ constructor(schemaName, context) { this._schema = schemaName; this._context = context; } /** * Parses the given SchemaItemProps JSON returned from an ECSql query. * @param data The SchemaItemProps JSON as returned from an iModelDb. * @returns The corrected SchemaItemProps Json. */ async parse(data) { const props = data; props.schemaItemType = (0, ECObjects_1.parseSchemaItemType)(data.schemaItemType); props.customAttributes = props.customAttributes ? props.customAttributes.map((attr) => { return (0, SchemaParser_1.parseCustomAttribute)(attr); }) : undefined; if (!props.customAttributes || props.customAttributes.length === 0) delete props.customAttributes; return props; } /** * Helper method to resolve the SchemaItem's full name from the given rawTypeName. * If the SchemaItem is defined in the same Schema from which it is referenced, * the rawTypeName will be SchemaItem name ('PhysicalElement'). Otherwise, * the rawTypeName will have the schema alias ('bis:PhysicalElement'). * @param rawTypeName The name or aliased name of the SchemaItem. * @returns The full name of the SchemaItem, ie. 'BisCore.PhysicalElement' */ async getQualifiedTypeName(rawTypeName) { const nameParts = rawTypeName.split(":"); if (nameParts.length !== 2) { const [schemaName, itemName] = SchemaItem_1.SchemaItem.parseFullName(rawTypeName); if (!schemaName || schemaName === '') return `${this._schema}.${itemName}`; return rawTypeName; } const resolvedName = await this.resolveNameFromAlias(nameParts[0].toLocaleLowerCase()); if (!resolvedName) throw new Error(`No valid schema found for alias '${nameParts[0]}'`); return `${resolvedName}.${nameParts[1]}`; } async resolveNameFromAlias(alias) { for (const schema of this._context.getKnownSchemas()) { if (schema.alias === alias) return schema.schemaKey.name; } return undefined; } } exports.SchemaItemParser = SchemaItemParser; /** * Parses KindOfQuantityProps JSON returned from an ECSql query and returns the correct KindOfQuantityProps JSON. * This is necessary as a small amount information (ie. unqualified type names of presentationUnits) returned from * the iModelDb is in a different format than is required for a KindOfQuantityProps JSON object. * @internal */ class KindOfQuantityParser extends SchemaItemParser { /** * Parses the given KindOfQuantityProps JSON returned from an ECSql query. * @param data The KindOfQuantityProps JSON as returned from an iModelDb. * @returns The corrected KindOfQuantityProps Json. */ async parse(data) { const mutableProps = await super.parse(data); if (mutableProps.persistenceUnit) { mutableProps.persistenceUnit = await this.getQualifiedTypeName(mutableProps.persistenceUnit); } mutableProps.presentationUnits = await this.parsePresentationUnits(mutableProps); return mutableProps; } async parsePresentationUnits(props) { const presentationUnits = []; if (!props.presentationUnits) return []; for (const presentationUnit of props.presentationUnits) { const presFormatOverride = OverrideFormat_1.OverrideFormat.parseFormatString(presentationUnit); const formatString = await this.createOverrideFormatString(presFormatOverride); presentationUnits.push(formatString); } ; return presentationUnits; } async createOverrideFormatString(overrideFormatProps) { let formatFullName = await this.getQualifiedTypeName(overrideFormatProps.name); if (overrideFormatProps.precision) formatFullName += `(${overrideFormatProps.precision.toString()})`; if (undefined === overrideFormatProps.unitAndLabels) return formatFullName; for (const [unit, unitLabel] of overrideFormatProps.unitAndLabels) { const unitFullName = await this.getQualifiedTypeName(unit); if (undefined === unitLabel) formatFullName += `[${unitFullName}]`; else formatFullName += `[${unitFullName}|${unitLabel}]`; } return formatFullName; } } exports.KindOfQuantityParser = KindOfQuantityParser; //# sourceMappingURL=SchemaItemParsers.js.map