UNPKG

@itwin/ecschema-metadata

Version:

ECObjects core concepts in typescript

152 lines 6.36 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. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Metadata */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MutableConstant = exports.Constant = void 0; const DelayedPromise_1 = require("../DelayedPromise"); const XmlSerializationUtils_1 = require("../Deserialization/XmlSerializationUtils"); const ECObjects_1 = require("../ECObjects"); const Exception_1 = require("../Exception"); const Phenomenon_1 = require("./Phenomenon"); const SchemaItem_1 = require("./SchemaItem"); /** * A Constant is a specific type of Unit that represents a number. * @public @preview */ class Constant extends SchemaItem_1.SchemaItem { schemaItemType = Constant.schemaItemType; /** @internal */ static get schemaItemType() { return ECObjects_1.SchemaItemType.Constant; } _phenomenon; _definition; _numerator; _denominator; /** @internal */ constructor(schema, name) { super(schema, name); this._definition = ""; } get phenomenon() { return this._phenomenon; } get definition() { return this._definition; } get numerator() { return this._numerator ?? 1.0; } get denominator() { return this._denominator ?? 1.0; } get hasNumerator() { return (this._numerator !== undefined); } get hasDenominator() { return (this._denominator !== undefined); } /** * Save this Constants properties to an object for serializing to JSON. * @param standalone Serialization includes only this object (as opposed to the full schema). * @param includeSchemaVersion Include the Schema's version information in the serialized object. */ toJSON(standalone = false, includeSchemaVersion = false) { const schemaJson = super.toJSON(standalone, includeSchemaVersion); if (this.phenomenon !== undefined) schemaJson.phenomenon = this.phenomenon.fullName; schemaJson.definition = this.definition; if (this.hasNumerator) schemaJson.numerator = this.numerator; if (this.hasDenominator) schemaJson.denominator = this.denominator; return schemaJson; } /** @internal */ async toXml(schemaXml) { const itemElement = await super.toXml(schemaXml); itemElement.setAttribute("definition", this.definition); if (this.hasNumerator) itemElement.setAttribute("numerator", this.numerator.toString()); if (this.hasDenominator) itemElement.setAttribute("denominator", this.denominator.toString()); const phenomenon = await this.phenomenon; if (undefined !== phenomenon) { const phenomenonName = XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, phenomenon.schema, phenomenon.name); itemElement.setAttribute("phenomenon", phenomenonName); } return itemElement; } fromJSONSync(constantProps) { super.fromJSONSync(constantProps); const schemaItemKey = this.schema.getSchemaItemKey(constantProps.phenomenon); if (!schemaItemKey) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the phenomenon ${constantProps.phenomenon}.`); this._phenomenon = new DelayedPromise_1.DelayedPromiseWithProps(schemaItemKey, async () => { const phenom = await this.schema.lookupItem(schemaItemKey, Phenomenon_1.Phenomenon); if (undefined === phenom) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the phenomenon ${constantProps.phenomenon}.`); return phenom; }); if (this._definition !== "" && constantProps.definition.toLowerCase() !== this._definition.toLowerCase()) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `The Constant ${this.name} has an invalid 'definition' attribute.`); else if (this._definition === "") this._definition = constantProps.definition; if (undefined !== constantProps.numerator) { if (constantProps.numerator !== this._numerator) this._numerator = constantProps.numerator; } if (undefined !== constantProps.denominator) { if (constantProps.denominator !== this._denominator) this._denominator = constantProps.denominator; } } async fromJSON(constantProps) { this.fromJSONSync(constantProps); } /** * @param phenomenon A LazyLoadedPhenomenon. * @internal */ setPhenomenon(phenomenon) { this._phenomenon = phenomenon; } /** * @internal */ setDefinition(definition) { this._definition = definition; } /** * @internal */ setNumerator(numerator) { this._numerator = numerator; } /** * @internal */ setDenominator(denominator) { this._denominator = denominator; } /** * Type guard to check if the SchemaItem is of type Constant. * @param item The SchemaItem to check. * @returns True if the item is a Constant, false otherwise. */ static isConstant(item) { if (item && item.schemaItemType === ECObjects_1.SchemaItemType.Constant) return true; return false; } /** * Type assertion to check if the SchemaItem is of type Constant. * @param item The SchemaItem to check. * @returns The item cast to Constant if it is a Constant, undefined otherwise. * @internal */ static assertIsConstant(item) { if (!this.isConstant(item)) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidSchemaItemType, `Expected '${ECObjects_1.SchemaItemType.Constant}' (Constant)`); } } exports.Constant = Constant; /** * @internal * An abstract class used for schema editing. */ class MutableConstant extends Constant { } exports.MutableConstant = MutableConstant; //# sourceMappingURL=Constant.js.map