UNPKG

@itwin/ecschema-metadata

Version:

ECObjects core concepts in typescript

226 lines • 9.32 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.SchemaItemKey = exports.SchemaKey = exports.ECVersion = void 0; const ECObjects_1 = require("./ECObjects"); const Exception_1 = require("./Exception"); const ECName_1 = require("./ECName"); /** * @public @preview */ class ECVersion { _read = 0; _write = 0; _minor = 0; /** * Using a version with all zero is invalid * for a schema, but it can be used to indicate * "no version has been specified" when locating things */ static NO_VERSION = new ECVersion(0, 0, 0); /** * The constructor will throw an ECSchemaError if any of the parameters below are above the threshold. * @param read Can support up to 999. * @param write Can support up to 999. * @param minor Can support up to 9999999. * */ constructor(read, write, minor) { if (undefined !== read) this._read = read; if (undefined !== write) this._write = write; if (undefined !== minor) this._minor = minor; if (this._read > 999 || this._read < 0 || this._write > 999 || this._write < 0 || this._minor > 9999999 || this._minor < 0) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECVersion); } get read() { return this._read; } get write() { return this._write; } get minor() { return this._minor; } /** * Creates a string, in the format 'RR.ww.mm', representing this ECVersion. * @note The default is to pad with zeroes. * @param padZeroes If true, the returned string will strictly follow `RR.ww.mm` and add leading zeroes if necessary. */ toString(padZeroes = true) { if (!padZeroes) return `${this.read}.${this.write}.${this.minor}`; const padWithZeroes = (num) => { return (num < 10 ? "0" : "") + num; }; return `${padWithZeroes(this.read)}.${padWithZeroes(this.write)}.${padWithZeroes(this.minor)}`; } /** * Given a valid version string the * @param versionString A valid version string of the format, 'RR.ww.mm'. */ static fromString(versionString) { const [read, write, minor] = versionString.split("."); if (!read) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECVersion, `The read version is missing from version string, ${versionString}`); if (!write) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECVersion, `The write version is missing from version string, ${versionString}`); if (!minor) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECVersion, `The minor version is missing from version string, ${versionString}`); return new ECVersion(+read, +write, +minor); } /** * Compares two schema versions. * @param rhs The schema to compare. * @return A negative number if this schema version is less than the given version, a positive number if greater, and 0 if are equivalent. */ compare(rhv) { if (this.read !== rhv.read) return this.read - rhv.read; if (this.write !== rhv.write) return this.write - rhv.write; return this.minor - rhv.minor; } } exports.ECVersion = ECVersion; /** * The SchemaKey contains a Schemas name and version. * @public @preview */ class SchemaKey { _name; _version; constructor(name, readOrVersion, writeVersion, minorVersion) { this._name = new ECName_1.ECName(name); if (readOrVersion !== undefined && typeof (readOrVersion) !== "number") this._version = readOrVersion; else this._version = new ECVersion(readOrVersion, writeVersion, minorVersion); } get version() { return this._version; } get name() { return this._name.name; } get readVersion() { return this.version.read; } get writeVersion() { return this.version.write; } get minorVersion() { return this.version.minor; } /** * Creates a string, in the format 'RR.ww.mm', representing this SchemaKey. * @note The default is to pad the full name with zeroes. * @param padZeroes If true, the returned string will strictly follow `Name.RR.ww.mm` and add leading zeroes if necessary. */ toString(padZeroes = true) { return `${this.name}.${this.version.toString(padZeroes)}`; } static parseString(fullName) { const keyPieces = fullName.split("."); if (keyPieces.length !== 4) throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECName); const schemaName = keyPieces[0]; const readVer = Number(keyPieces[1]); const writeVer = Number(keyPieces[2]); const minorVer = Number(keyPieces[3]); return new SchemaKey(schemaName, new ECVersion(readVer, writeVer, minorVer)); } /** * Compares two schema names, case-insensitive. * @return True if they match; otherwise, false. */ compareByName(rhs) { if (undefined === rhs) return false; if (typeof (rhs) === "string") return rhs.toLowerCase() === this.name.toLowerCase(); return rhs.name.toLowerCase() === this.name.toLowerCase(); } /** * Compares two schema versions. * @param rhs The schema to compare. * @return A negative number if this schema version is less than the given version, a positive number if greater, and 0 if are equivalent. */ compareByVersion(rhs) { return this.version.compare(rhs.version); } /** * * @param rhs The SchemaKey to compare with * @param matchType The match type to use for comparison. */ matches(rhs, matchType = ECObjects_1.SchemaMatchType.Exact) { switch (matchType) { case ECObjects_1.SchemaMatchType.Identical: case ECObjects_1.SchemaMatchType.Exact: return this.compareByName(rhs.name) && this.readVersion === rhs.readVersion && this.writeVersion === rhs.writeVersion && this.minorVersion === rhs.minorVersion; case ECObjects_1.SchemaMatchType.LatestReadCompatible: if (!this.compareByName(rhs.name)) return false; if (rhs.readVersion !== this.readVersion) return false; if (this.writeVersion === rhs.writeVersion) return this.minorVersion >= rhs.minorVersion; return this.writeVersion > rhs.writeVersion; case ECObjects_1.SchemaMatchType.LatestWriteCompatible: return this.compareByName(rhs.name) && this.readVersion === rhs.readVersion && this.writeVersion === rhs.writeVersion && this.minorVersion >= rhs.minorVersion; case ECObjects_1.SchemaMatchType.Latest: return this.compareByName(rhs.name); default: return false; } } /** * Deserializes a SchemaKeyProps JSON object into a SchemaKey object. * @param props SchemaKeyProps * @returns A SchemaKey object. */ static fromJSON(props) { return new SchemaKey(props.name, props.read, props.write, props.minor); } /** * Save this SchemaKey's properties to an object for serializing to JSON. */ toJSON() { return { name: this.name, read: this.readVersion, write: this.writeVersion, minor: this.minorVersion, }; } } exports.SchemaKey = SchemaKey; /** * The SchemaItemKey contains a SchemaItem's name and SchemaKey. * @public @preview */ class SchemaItemKey { _name; _schemaKey; constructor(name, schema) { this._name = new ECName_1.ECName(name); this._schemaKey = schema; } get schemaKey() { return this._schemaKey; } get name() { return this._name.name; } get schemaName() { return this.schemaKey.name; } /** Returns the name in the format, {schemaName}.{name}. */ get fullName() { return `${this.schemaName}.${this.name}`; } /** * Checks whether this SchemaItemKey matches the one provided. * @param rhs The SchemaItemKey to compare to this. */ // TODO: Need to add a match type matches(rhs) { if (rhs.name !== this.name) return false; if (!rhs.schemaKey.matches(this.schemaKey, ECObjects_1.SchemaMatchType.Latest)) return false; return true; } matchesFullName(name) { const schemaVersion = this.schemaKey.version.toString().replace(/\./g, "\\."); const fullNameRegex = new RegExp(`^${this.schemaName}(\\.${schemaVersion})?[.:]${this.name}$`, "i"); return fullNameRegex.test(name); } } exports.SchemaItemKey = SchemaItemKey; //# sourceMappingURL=SchemaKey.js.map