@itwin/ecschema-metadata 5.7.0-dev.9 → 5.8.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -1
- package/lib/cjs/Formatting/SchemaFormatsProvider.d.ts +6 -5
- package/lib/cjs/Formatting/SchemaFormatsProvider.d.ts.map +1 -1
- package/lib/cjs/Formatting/SchemaFormatsProvider.js +35 -29
- package/lib/cjs/Formatting/SchemaFormatsProvider.js.map +1 -1
- package/lib/esm/Formatting/SchemaFormatsProvider.d.ts +6 -5
- package/lib/esm/Formatting/SchemaFormatsProvider.d.ts.map +1 -1
- package/lib/esm/Formatting/SchemaFormatsProvider.js +35 -29
- package/lib/esm/Formatting/SchemaFormatsProvider.js.map +1 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
# Change Log - @itwin/ecschema-metadata
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Fri, 13 Feb 2026 17:13:15 GMT and should not be manually modified.
|
|
4
|
+
|
|
5
|
+
## 5.6.1
|
|
6
|
+
Fri, 13 Feb 2026 17:11:47 GMT
|
|
7
|
+
|
|
8
|
+
_Version update only_
|
|
4
9
|
|
|
5
10
|
## 5.6.0
|
|
6
11
|
Thu, 05 Feb 2026 16:12:37 GMT
|
|
@@ -10,7 +10,7 @@ import { FormatDefinition, FormatsChangedArgs, FormatsProvider, UnitSystemKey }
|
|
|
10
10
|
*/
|
|
11
11
|
export declare class SchemaFormatsProvider implements FormatsProvider {
|
|
12
12
|
private _context;
|
|
13
|
-
private _unitSystem
|
|
13
|
+
private _unitSystem?;
|
|
14
14
|
private _formatsRetrieved;
|
|
15
15
|
onFormatsChanged: BeEvent<(args: FormatsChangedArgs) => void>;
|
|
16
16
|
/**
|
|
@@ -18,12 +18,13 @@ export declare class SchemaFormatsProvider implements FormatsProvider {
|
|
|
18
18
|
* @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext
|
|
19
19
|
* class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be
|
|
20
20
|
* created and the locater will be added.
|
|
21
|
-
* @param unitSystem
|
|
21
|
+
* @param unitSystem Optional unit system used to lookup a default format through a schema specific algorithm, when the format retrieved is associated with a KindOfQuantity.
|
|
22
|
+
* If not provided, the default presentation format will be used directly without matching unit systems.
|
|
22
23
|
*/
|
|
23
|
-
constructor(contextOrLocater: ISchemaLocater, unitSystem
|
|
24
|
+
constructor(contextOrLocater: ISchemaLocater, unitSystem?: UnitSystemKey);
|
|
24
25
|
get context(): SchemaContext;
|
|
25
|
-
get unitSystem(): UnitSystemKey;
|
|
26
|
-
set unitSystem(unitSystem: UnitSystemKey);
|
|
26
|
+
get unitSystem(): UnitSystemKey | undefined;
|
|
27
|
+
set unitSystem(unitSystem: UnitSystemKey | undefined);
|
|
27
28
|
private clear;
|
|
28
29
|
/** When using a presentation unit from a KindOfQuantity, the label and description should come from the KindOfQuantity */
|
|
29
30
|
private convertToFormatDefinition;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaFormatsProvider.d.ts","sourceRoot":"","sources":["../../../src/Formatting/SchemaFormatsProvider.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAK3D,OAAO,EAAE,OAAO,EAAU,MAAM,qBAAqB,CAAC;AAGtD,OAAO,EAAE,gBAAgB,EAAe,kBAAkB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAMzH;;;GAGG;AACH,qBAAa,qBAAsB,YAAW,eAAe;IAC3D,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,WAAW,CAAgB;
|
|
1
|
+
{"version":3,"file":"SchemaFormatsProvider.d.ts","sourceRoot":"","sources":["../../../src/Formatting/SchemaFormatsProvider.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAK3D,OAAO,EAAE,OAAO,EAAU,MAAM,qBAAqB,CAAC;AAGtD,OAAO,EAAE,gBAAgB,EAAe,kBAAkB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAMzH;;;GAGG;AACH,qBAAa,qBAAsB,YAAW,eAAe;IAC3D,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,WAAW,CAAC,CAAgB;IACpC,OAAO,CAAC,iBAAiB,CAA0B;IAC5C,gBAAgB,iBAAsB,kBAAkB,KAAK,IAAI,EAAI;IAC5E;;;;;;;OAOG;gBACS,gBAAgB,EAAE,cAAc,EAAE,UAAU,CAAC,EAAE,aAAa;IAUxE,IAAW,OAAO,kBAA4B;IAC9C,IAAW,UAAU,IAEa,aAAa,GAAG,SAAS,CAFP;IAEpD,IAAW,UAAU,CAAC,UAAU,EAAE,aAAa,GAAG,SAAS,EAG1D;IAED,OAAO,CAAC,KAAK;IAMb,0HAA0H;IAC1H,OAAO,CAAC,yBAAyB;YAenB,iCAAiC;IAyD/C;;;;;;OAMG;IACU,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC;CA8B5E"}
|
|
@@ -30,7 +30,8 @@ class SchemaFormatsProvider {
|
|
|
30
30
|
* @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext
|
|
31
31
|
* class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be
|
|
32
32
|
* created and the locater will be added.
|
|
33
|
-
* @param unitSystem
|
|
33
|
+
* @param unitSystem Optional unit system used to lookup a default format through a schema specific algorithm, when the format retrieved is associated with a KindOfQuantity.
|
|
34
|
+
* If not provided, the default presentation format will be used directly without matching unit systems.
|
|
34
35
|
*/
|
|
35
36
|
constructor(contextOrLocater, unitSystem) {
|
|
36
37
|
if (contextOrLocater instanceof Context_1.SchemaContext) {
|
|
@@ -79,39 +80,44 @@ class SchemaFormatsProvider {
|
|
|
79
80
|
if (!kindOfQuantity) {
|
|
80
81
|
return undefined;
|
|
81
82
|
}
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
for (const
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
83
|
+
// If a unit system is provided, find the first presentation format that matches it.
|
|
84
|
+
if (this._unitSystem) {
|
|
85
|
+
const unitSystemMatchers = getUnitSystemGroupMatchers(this._unitSystem);
|
|
86
|
+
const presentationFormats = kindOfQuantity.presentationFormats;
|
|
87
|
+
for (const matcher of unitSystemMatchers) {
|
|
88
|
+
for (const lazyFormat of presentationFormats) {
|
|
89
|
+
const format = await lazyFormat;
|
|
90
|
+
const unit = await (format.units && format.units[0][0]);
|
|
91
|
+
if (!unit) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const currentUnitSystem = await unit.unitSystem;
|
|
95
|
+
if (currentUnitSystem && matcher(currentUnitSystem)) {
|
|
96
|
+
this._formatsRetrieved.add(itemKey.fullName);
|
|
97
|
+
const props = (0, OverrideFormat_1.getFormatProps)(format);
|
|
98
|
+
return this.convertToFormatDefinition(props, kindOfQuantity);
|
|
99
|
+
}
|
|
97
100
|
}
|
|
98
101
|
}
|
|
102
|
+
// If no matching presentation format was found, fall back to persistence unit format
|
|
103
|
+
// only if it matches the requested unit system.
|
|
104
|
+
const persistenceUnit = await kindOfQuantity.persistenceUnit;
|
|
105
|
+
const persistenceUnitSystem = await persistenceUnit?.unitSystem;
|
|
106
|
+
if (persistenceUnit && persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {
|
|
107
|
+
this._formatsRetrieved.add(itemKey.fullName);
|
|
108
|
+
const props = getPersistenceUnitFormatProps(persistenceUnit);
|
|
109
|
+
return this.convertToFormatDefinition(props, kindOfQuantity);
|
|
110
|
+
}
|
|
99
111
|
}
|
|
100
|
-
// If no
|
|
101
|
-
|
|
102
|
-
const persistenceUnitSystem = await persistenceUnit?.unitSystem;
|
|
103
|
-
if (persistenceUnit && persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {
|
|
104
|
-
this._formatsRetrieved.add(itemKey.fullName);
|
|
105
|
-
const props = getPersistenceUnitFormatProps(persistenceUnit);
|
|
106
|
-
return this.convertToFormatDefinition(props, kindOfQuantity);
|
|
107
|
-
}
|
|
112
|
+
// If no unit system was provided, or no matching format was found, use the default presentation format.
|
|
113
|
+
// Unit conversion from persistence unit to presentation unit will be handled by FormatterSpec.
|
|
108
114
|
const defaultFormat = kindOfQuantity.defaultPresentationFormat;
|
|
109
|
-
if (
|
|
110
|
-
|
|
115
|
+
if (defaultFormat) {
|
|
116
|
+
this._formatsRetrieved.add(itemKey.fullName);
|
|
117
|
+
const defaultProps = (0, OverrideFormat_1.getFormatProps)(await defaultFormat);
|
|
118
|
+
return this.convertToFormatDefinition(defaultProps, kindOfQuantity);
|
|
111
119
|
}
|
|
112
|
-
|
|
113
|
-
const defaultProps = (0, OverrideFormat_1.getFormatProps)(await defaultFormat);
|
|
114
|
-
return this.convertToFormatDefinition(defaultProps, kindOfQuantity);
|
|
120
|
+
return undefined;
|
|
115
121
|
}
|
|
116
122
|
/**
|
|
117
123
|
* Retrieves a Format from a SchemaContext. If the format is part of a KindOfQuantity, the first presentation format in the KindOfQuantity that matches the current unit system will be retrieved.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaFormatsProvider.js","sourceRoot":"","sources":["../../../src/Formatting/SchemaFormatsProvider.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;AAC/F;;GAEG;;;AAEH,wCAA2D;AAC3D,4CAAwD;AACxD,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---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Metadata\r\n */\r\n\r\nimport { ISchemaLocater, SchemaContext } from \"../Context\";\r\nimport { SchemaItemKey, SchemaKey } from \"../SchemaKey\";\r\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\r\nimport { Format } from \"../Metadata/Format\";\r\nimport { SchemaItemFormatProps } from \"../Deserialization/JsonProps\";\r\nimport { BeEvent, Logger } from \"@itwin/core-bentley\";\r\nimport { KindOfQuantity } from \"../Metadata/KindOfQuantity\";\r\nimport { getFormatProps } from \"../Metadata/OverrideFormat\";\r\nimport { FormatDefinition, FormatProps, FormatsChangedArgs, FormatsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\r\nimport { Unit } from \"../Metadata/Unit\";\r\nimport { InvertedUnit } from \"../Metadata/InvertedUnit\";\r\nimport { Schema } from \"../Metadata/Schema\";\r\nimport { UnitSystem } from \"../Metadata/UnitSystem\";\r\nconst loggerCategory = \"SchemaFormatsProvider\";\r\n/**\r\n * Provides default formats and kind of quantities from a given SchemaContext or SchemaLocater.\r\n * @beta\r\n */\r\nexport class SchemaFormatsProvider implements FormatsProvider {\r\n private _context: SchemaContext;\r\n private _unitSystem: UnitSystemKey;\r\n private _formatsRetrieved: Set<string> = new Set();\r\n public onFormatsChanged = new BeEvent<(args: FormatsChangedArgs) => void>();\r\n /**\r\n *\r\n * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext\r\n * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be\r\n * created and the locater will be added.\r\n * @param unitSystem Used to lookup a default format through a schema specific algorithm, when the format retrieved is associated with a KindOfQuantity.\r\n */\r\n constructor(contextOrLocater: ISchemaLocater, unitSystem: UnitSystemKey) {\r\n if (contextOrLocater instanceof SchemaContext) {\r\n this._context = contextOrLocater;\r\n } else {\r\n this._context = new SchemaContext();\r\n this._context.addLocater(contextOrLocater);\r\n }\r\n this._unitSystem = unitSystem;\r\n }\r\n\r\n public get context() { return this._context; }\r\n public get unitSystem() { return this._unitSystem; }\r\n\r\n public set unitSystem(unitSystem: UnitSystemKey) {\r\n this._unitSystem = unitSystem;\r\n this.clear();\r\n }\r\n\r\n private clear(): void {\r\n const formatsChanged = Array.from(this._formatsRetrieved);\r\n this._formatsRetrieved.clear();\r\n this.onFormatsChanged.raiseEvent({ formatsChanged });\r\n }\r\n\r\n /** When using a presentation unit from a KindOfQuantity, the label and description should come from the KindOfQuantity */\r\n private convertToFormatDefinition(format: SchemaItemFormatProps, kindOfQuantity: KindOfQuantity): FormatDefinition {\r\n // Destructure all properties except 'rest'\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n const { name, label, description, $schema, schema, schemaVersion, schemaItemType,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n customAttributes, originalECSpecMajorVersion, originalECSpecMinorVersion, ...rest } = format;\r\n\r\n return {\r\n ...rest,\r\n name: kindOfQuantity.fullName,\r\n label: kindOfQuantity.label ?? format.label,\r\n description: kindOfQuantity.description ?? format.description,\r\n }\r\n }\r\n\r\n private async getKindOfQuantityFormatFromSchema(itemKey: SchemaItemKey): Promise<FormatDefinition | undefined> {\r\n let kindOfQuantity: KindOfQuantity | undefined;\r\n try {\r\n kindOfQuantity = await this._context.getSchemaItem(itemKey, KindOfQuantity);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find KindOfQuantity ${itemKey.fullName}`);\r\n return undefined;\r\n }\r\n\r\n if (!kindOfQuantity) {\r\n return undefined;\r\n }\r\n\r\n // Find the first presentation format that matches the provided unit system.\r\n const unitSystemMatchers = getUnitSystemGroupMatchers(this._unitSystem);\r\n const presentationFormats = kindOfQuantity.presentationFormats;\r\n for (const matcher of unitSystemMatchers) {\r\n for (const lazyFormat of presentationFormats) {\r\n const format = await lazyFormat;\r\n const unit = await (format.units && format.units[0][0]);\r\n if (!unit) {\r\n continue;\r\n }\r\n const currentUnitSystem = await unit.unitSystem;\r\n if (currentUnitSystem && matcher(currentUnitSystem)) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const props = getFormatProps(format);\r\n return this.convertToFormatDefinition(props, kindOfQuantity);\r\n }\r\n }\r\n }\r\n\r\n // If no matching presentation format was found, use persistence unit format if it matches unit system.\r\n const persistenceUnit = await kindOfQuantity.persistenceUnit;\r\n const persistenceUnitSystem = await persistenceUnit?.unitSystem;\r\n if (persistenceUnit && persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const props = getPersistenceUnitFormatProps(persistenceUnit);\r\n return this.convertToFormatDefinition(props, kindOfQuantity);\r\n }\r\n\r\n const defaultFormat = kindOfQuantity.defaultPresentationFormat;\r\n if (!defaultFormat) {\r\n return undefined;\r\n }\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const defaultProps = getFormatProps(await defaultFormat);\r\n return this.convertToFormatDefinition(defaultProps, kindOfQuantity);\r\n }\r\n\r\n\r\n /**\r\n * Retrieves a Format from a SchemaContext. If the format is part of a KindOfQuantity, the first presentation format in the KindOfQuantity that matches the current unit system will be retrieved.\r\n * If no presentation format matches the current unit system, the persistence unit format will be retrieved if it matches the current unit system.\r\n * Else, the default presentation format will be retrieved.\r\n * @param name The full name of the Format or KindOfQuantity.\r\n * @returns\r\n */\r\n public async getFormat(name: string): Promise<FormatDefinition | undefined> {\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(name);\r\n const schemaKey = new SchemaKey(schemaName);\r\n let schema: Schema | undefined;\r\n try {\r\n schema = await this._context.getSchema(schemaKey);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find schema ${schemaName}`);\r\n return undefined;\r\n }\r\n if (!schema) {\r\n return undefined;\r\n }\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n\r\n if (schema.name === \"Formats\") {\r\n let format: Format | undefined;\r\n try {\r\n format = await this._context.getSchemaItem(itemKey, Format);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find Format ${itemKey.fullName}`);\r\n return undefined;\r\n }\r\n if (!format) {\r\n return undefined;\r\n }\r\n return format.toJSON(true);\r\n }\r\n return this.getKindOfQuantityFormatFromSchema(itemKey);\r\n }\r\n}\r\n\r\nfunction getUnitSystemGroupMatchers(groupKey?: UnitSystemKey) {\r\n function createMatcher(name: string | string[]) {\r\n const names = Array.isArray(name) ? name : [name];\r\n return (unitSystem: UnitSystem) => names.some((n) => n === unitSystem.name.toUpperCase());\r\n }\r\n switch (groupKey) {\r\n case \"imperial\":\r\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"metric\":\r\n return [[\"SI\", \"METRIC\"], \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"usCustomary\":\r\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"usSurvey\":\r\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n }\r\n return [];\r\n}\r\n\r\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\r\n // Same as Format \"DefaultRealU\" in Formats ecschema\r\n return {\r\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\r\n precision: 6,\r\n type: \"Decimal\",\r\n composite: {\r\n units: [\r\n {\r\n name: persistenceUnit.fullName,\r\n label: persistenceUnit.label,\r\n },\r\n ],\r\n },\r\n };\r\n}"]}
|
|
1
|
+
{"version":3,"file":"SchemaFormatsProvider.js","sourceRoot":"","sources":["../../../src/Formatting/SchemaFormatsProvider.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;AAC/F;;GAEG;;;AAEH,wCAA2D;AA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AAE,eAAe,CAAC,QAAQ;oBAC9B,KAAK,EAAE,eAAe,CAAC,KAAK;iBAC7B;aACF;SACF;KACF,CAAC;AACJ,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Metadata\r\n */\r\n\r\nimport { ISchemaLocater, SchemaContext } from \"../Context\";\r\nimport { SchemaItemKey, SchemaKey } from \"../SchemaKey\";\r\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\r\nimport { Format } from \"../Metadata/Format\";\r\nimport { SchemaItemFormatProps } from \"../Deserialization/JsonProps\";\r\nimport { BeEvent, Logger } from \"@itwin/core-bentley\";\r\nimport { KindOfQuantity } from \"../Metadata/KindOfQuantity\";\r\nimport { getFormatProps } from \"../Metadata/OverrideFormat\";\r\nimport { FormatDefinition, FormatProps, FormatsChangedArgs, FormatsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\r\nimport { Unit } from \"../Metadata/Unit\";\r\nimport { InvertedUnit } from \"../Metadata/InvertedUnit\";\r\nimport { Schema } from \"../Metadata/Schema\";\r\nimport { UnitSystem } from \"../Metadata/UnitSystem\";\r\nconst loggerCategory = \"SchemaFormatsProvider\";\r\n/**\r\n * Provides default formats and kind of quantities from a given SchemaContext or SchemaLocater.\r\n * @beta\r\n */\r\nexport class SchemaFormatsProvider implements FormatsProvider {\r\n private _context: SchemaContext;\r\n private _unitSystem?: UnitSystemKey;\r\n private _formatsRetrieved: Set<string> = new Set();\r\n public onFormatsChanged = new BeEvent<(args: FormatsChangedArgs) => void>();\r\n /**\r\n *\r\n * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext\r\n * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be\r\n * created and the locater will be added.\r\n * @param unitSystem Optional unit system used to lookup a default format through a schema specific algorithm, when the format retrieved is associated with a KindOfQuantity.\r\n * If not provided, the default presentation format will be used directly without matching unit systems.\r\n */\r\n constructor(contextOrLocater: ISchemaLocater, unitSystem?: UnitSystemKey) {\r\n if (contextOrLocater instanceof SchemaContext) {\r\n this._context = contextOrLocater;\r\n } else {\r\n this._context = new SchemaContext();\r\n this._context.addLocater(contextOrLocater);\r\n }\r\n this._unitSystem = unitSystem;\r\n }\r\n\r\n public get context() { return this._context; }\r\n public get unitSystem() { return this._unitSystem; }\r\n\r\n public set unitSystem(unitSystem: UnitSystemKey | undefined) {\r\n this._unitSystem = unitSystem;\r\n this.clear();\r\n }\r\n\r\n private clear(): void {\r\n const formatsChanged = Array.from(this._formatsRetrieved);\r\n this._formatsRetrieved.clear();\r\n this.onFormatsChanged.raiseEvent({ formatsChanged });\r\n }\r\n\r\n /** When using a presentation unit from a KindOfQuantity, the label and description should come from the KindOfQuantity */\r\n private convertToFormatDefinition(format: SchemaItemFormatProps, kindOfQuantity: KindOfQuantity): FormatDefinition {\r\n // Destructure all properties except 'rest'\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n const { name, label, description, $schema, schema, schemaVersion, schemaItemType,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n customAttributes, originalECSpecMajorVersion, originalECSpecMinorVersion, ...rest } = format;\r\n\r\n return {\r\n ...rest,\r\n name: kindOfQuantity.fullName,\r\n label: kindOfQuantity.label ?? format.label,\r\n description: kindOfQuantity.description ?? format.description,\r\n }\r\n }\r\n\r\n private async getKindOfQuantityFormatFromSchema(itemKey: SchemaItemKey): Promise<FormatDefinition | undefined> {\r\n let kindOfQuantity: KindOfQuantity | undefined;\r\n try {\r\n kindOfQuantity = await this._context.getSchemaItem(itemKey, KindOfQuantity);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find KindOfQuantity ${itemKey.fullName}`);\r\n return undefined;\r\n }\r\n\r\n if (!kindOfQuantity) {\r\n return undefined;\r\n }\r\n\r\n // If a unit system is provided, find the first presentation format that matches it.\r\n if (this._unitSystem) {\r\n const unitSystemMatchers = getUnitSystemGroupMatchers(this._unitSystem);\r\n const presentationFormats = kindOfQuantity.presentationFormats;\r\n for (const matcher of unitSystemMatchers) {\r\n for (const lazyFormat of presentationFormats) {\r\n const format = await lazyFormat;\r\n const unit = await (format.units && format.units[0][0]);\r\n if (!unit) {\r\n continue;\r\n }\r\n const currentUnitSystem = await unit.unitSystem;\r\n if (currentUnitSystem && matcher(currentUnitSystem)) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const props = getFormatProps(format);\r\n return this.convertToFormatDefinition(props, kindOfQuantity);\r\n }\r\n }\r\n }\r\n\r\n // If no matching presentation format was found, fall back to persistence unit format\r\n // only if it matches the requested unit system.\r\n const persistenceUnit = await kindOfQuantity.persistenceUnit;\r\n const persistenceUnitSystem = await persistenceUnit?.unitSystem;\r\n if (persistenceUnit && persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const props = getPersistenceUnitFormatProps(persistenceUnit);\r\n return this.convertToFormatDefinition(props, kindOfQuantity);\r\n }\r\n }\r\n\r\n // If no unit system was provided, or no matching format was found, use the default presentation format.\r\n // Unit conversion from persistence unit to presentation unit will be handled by FormatterSpec.\r\n const defaultFormat = kindOfQuantity.defaultPresentationFormat;\r\n if (defaultFormat) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const defaultProps = getFormatProps(await defaultFormat);\r\n return this.convertToFormatDefinition(defaultProps, kindOfQuantity);\r\n }\r\n\r\n return undefined;\r\n }\r\n\r\n\r\n /**\r\n * Retrieves a Format from a SchemaContext. If the format is part of a KindOfQuantity, the first presentation format in the KindOfQuantity that matches the current unit system will be retrieved.\r\n * If no presentation format matches the current unit system, the persistence unit format will be retrieved if it matches the current unit system.\r\n * Else, the default presentation format will be retrieved.\r\n * @param name The full name of the Format or KindOfQuantity.\r\n * @returns\r\n */\r\n public async getFormat(name: string): Promise<FormatDefinition | undefined> {\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(name);\r\n const schemaKey = new SchemaKey(schemaName);\r\n let schema: Schema | undefined;\r\n try {\r\n schema = await this._context.getSchema(schemaKey);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find schema ${schemaName}`);\r\n return undefined;\r\n }\r\n if (!schema) {\r\n return undefined;\r\n }\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n\r\n if (schema.name === \"Formats\") {\r\n let format: Format | undefined;\r\n try {\r\n format = await this._context.getSchemaItem(itemKey, Format);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find Format ${itemKey.fullName}`);\r\n return undefined;\r\n }\r\n if (!format) {\r\n return undefined;\r\n }\r\n return format.toJSON(true);\r\n }\r\n return this.getKindOfQuantityFormatFromSchema(itemKey);\r\n }\r\n}\r\n\r\nfunction getUnitSystemGroupMatchers(groupKey?: UnitSystemKey) {\r\n function createMatcher(name: string | string[]) {\r\n const names = Array.isArray(name) ? name : [name];\r\n return (unitSystem: UnitSystem) => names.some((n) => n === unitSystem.name.toUpperCase());\r\n }\r\n switch (groupKey) {\r\n case \"imperial\":\r\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"metric\":\r\n return [[\"SI\", \"METRIC\"], \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"usCustomary\":\r\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"usSurvey\":\r\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n }\r\n return [];\r\n}\r\n\r\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\r\n // Same as Format \"DefaultRealU\" in Formats ecschema\r\n return {\r\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\r\n precision: 6,\r\n type: \"Decimal\",\r\n composite: {\r\n units: [\r\n {\r\n name: persistenceUnit.fullName,\r\n label: persistenceUnit.label,\r\n },\r\n ],\r\n },\r\n };\r\n}"]}
|
|
@@ -10,7 +10,7 @@ import { FormatDefinition, FormatsChangedArgs, FormatsProvider, UnitSystemKey }
|
|
|
10
10
|
*/
|
|
11
11
|
export declare class SchemaFormatsProvider implements FormatsProvider {
|
|
12
12
|
private _context;
|
|
13
|
-
private _unitSystem
|
|
13
|
+
private _unitSystem?;
|
|
14
14
|
private _formatsRetrieved;
|
|
15
15
|
onFormatsChanged: BeEvent<(args: FormatsChangedArgs) => void>;
|
|
16
16
|
/**
|
|
@@ -18,12 +18,13 @@ export declare class SchemaFormatsProvider implements FormatsProvider {
|
|
|
18
18
|
* @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext
|
|
19
19
|
* class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be
|
|
20
20
|
* created and the locater will be added.
|
|
21
|
-
* @param unitSystem
|
|
21
|
+
* @param unitSystem Optional unit system used to lookup a default format through a schema specific algorithm, when the format retrieved is associated with a KindOfQuantity.
|
|
22
|
+
* If not provided, the default presentation format will be used directly without matching unit systems.
|
|
22
23
|
*/
|
|
23
|
-
constructor(contextOrLocater: ISchemaLocater, unitSystem
|
|
24
|
+
constructor(contextOrLocater: ISchemaLocater, unitSystem?: UnitSystemKey);
|
|
24
25
|
get context(): SchemaContext;
|
|
25
|
-
get unitSystem(): UnitSystemKey;
|
|
26
|
-
set unitSystem(unitSystem: UnitSystemKey);
|
|
26
|
+
get unitSystem(): UnitSystemKey | undefined;
|
|
27
|
+
set unitSystem(unitSystem: UnitSystemKey | undefined);
|
|
27
28
|
private clear;
|
|
28
29
|
/** When using a presentation unit from a KindOfQuantity, the label and description should come from the KindOfQuantity */
|
|
29
30
|
private convertToFormatDefinition;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaFormatsProvider.d.ts","sourceRoot":"","sources":["../../../src/Formatting/SchemaFormatsProvider.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAK3D,OAAO,EAAE,OAAO,EAAU,MAAM,qBAAqB,CAAC;AAGtD,OAAO,EAAE,gBAAgB,EAAe,kBAAkB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAMzH;;;GAGG;AACH,qBAAa,qBAAsB,YAAW,eAAe;IAC3D,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,WAAW,CAAgB;
|
|
1
|
+
{"version":3,"file":"SchemaFormatsProvider.d.ts","sourceRoot":"","sources":["../../../src/Formatting/SchemaFormatsProvider.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAK3D,OAAO,EAAE,OAAO,EAAU,MAAM,qBAAqB,CAAC;AAGtD,OAAO,EAAE,gBAAgB,EAAe,kBAAkB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAMzH;;;GAGG;AACH,qBAAa,qBAAsB,YAAW,eAAe;IAC3D,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,WAAW,CAAC,CAAgB;IACpC,OAAO,CAAC,iBAAiB,CAA0B;IAC5C,gBAAgB,iBAAsB,kBAAkB,KAAK,IAAI,EAAI;IAC5E;;;;;;;OAOG;gBACS,gBAAgB,EAAE,cAAc,EAAE,UAAU,CAAC,EAAE,aAAa;IAUxE,IAAW,OAAO,kBAA4B;IAC9C,IAAW,UAAU,IAEa,aAAa,GAAG,SAAS,CAFP;IAEpD,IAAW,UAAU,CAAC,UAAU,EAAE,aAAa,GAAG,SAAS,EAG1D;IAED,OAAO,CAAC,KAAK;IAMb,0HAA0H;IAC1H,OAAO,CAAC,yBAAyB;YAenB,iCAAiC;IAyD/C;;;;;;OAMG;IACU,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC;CA8B5E"}
|
|
@@ -27,7 +27,8 @@ export class SchemaFormatsProvider {
|
|
|
27
27
|
* @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext
|
|
28
28
|
* class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be
|
|
29
29
|
* created and the locater will be added.
|
|
30
|
-
* @param unitSystem
|
|
30
|
+
* @param unitSystem Optional unit system used to lookup a default format through a schema specific algorithm, when the format retrieved is associated with a KindOfQuantity.
|
|
31
|
+
* If not provided, the default presentation format will be used directly without matching unit systems.
|
|
31
32
|
*/
|
|
32
33
|
constructor(contextOrLocater, unitSystem) {
|
|
33
34
|
if (contextOrLocater instanceof SchemaContext) {
|
|
@@ -76,39 +77,44 @@ export class SchemaFormatsProvider {
|
|
|
76
77
|
if (!kindOfQuantity) {
|
|
77
78
|
return undefined;
|
|
78
79
|
}
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
for (const
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
80
|
+
// If a unit system is provided, find the first presentation format that matches it.
|
|
81
|
+
if (this._unitSystem) {
|
|
82
|
+
const unitSystemMatchers = getUnitSystemGroupMatchers(this._unitSystem);
|
|
83
|
+
const presentationFormats = kindOfQuantity.presentationFormats;
|
|
84
|
+
for (const matcher of unitSystemMatchers) {
|
|
85
|
+
for (const lazyFormat of presentationFormats) {
|
|
86
|
+
const format = await lazyFormat;
|
|
87
|
+
const unit = await (format.units && format.units[0][0]);
|
|
88
|
+
if (!unit) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const currentUnitSystem = await unit.unitSystem;
|
|
92
|
+
if (currentUnitSystem && matcher(currentUnitSystem)) {
|
|
93
|
+
this._formatsRetrieved.add(itemKey.fullName);
|
|
94
|
+
const props = getFormatProps(format);
|
|
95
|
+
return this.convertToFormatDefinition(props, kindOfQuantity);
|
|
96
|
+
}
|
|
94
97
|
}
|
|
95
98
|
}
|
|
99
|
+
// If no matching presentation format was found, fall back to persistence unit format
|
|
100
|
+
// only if it matches the requested unit system.
|
|
101
|
+
const persistenceUnit = await kindOfQuantity.persistenceUnit;
|
|
102
|
+
const persistenceUnitSystem = await persistenceUnit?.unitSystem;
|
|
103
|
+
if (persistenceUnit && persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {
|
|
104
|
+
this._formatsRetrieved.add(itemKey.fullName);
|
|
105
|
+
const props = getPersistenceUnitFormatProps(persistenceUnit);
|
|
106
|
+
return this.convertToFormatDefinition(props, kindOfQuantity);
|
|
107
|
+
}
|
|
96
108
|
}
|
|
97
|
-
// If no
|
|
98
|
-
|
|
99
|
-
const persistenceUnitSystem = await persistenceUnit?.unitSystem;
|
|
100
|
-
if (persistenceUnit && persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {
|
|
101
|
-
this._formatsRetrieved.add(itemKey.fullName);
|
|
102
|
-
const props = getPersistenceUnitFormatProps(persistenceUnit);
|
|
103
|
-
return this.convertToFormatDefinition(props, kindOfQuantity);
|
|
104
|
-
}
|
|
109
|
+
// If no unit system was provided, or no matching format was found, use the default presentation format.
|
|
110
|
+
// Unit conversion from persistence unit to presentation unit will be handled by FormatterSpec.
|
|
105
111
|
const defaultFormat = kindOfQuantity.defaultPresentationFormat;
|
|
106
|
-
if (
|
|
107
|
-
|
|
112
|
+
if (defaultFormat) {
|
|
113
|
+
this._formatsRetrieved.add(itemKey.fullName);
|
|
114
|
+
const defaultProps = getFormatProps(await defaultFormat);
|
|
115
|
+
return this.convertToFormatDefinition(defaultProps, kindOfQuantity);
|
|
108
116
|
}
|
|
109
|
-
|
|
110
|
-
const defaultProps = getFormatProps(await defaultFormat);
|
|
111
|
-
return this.convertToFormatDefinition(defaultProps, kindOfQuantity);
|
|
117
|
+
return undefined;
|
|
112
118
|
}
|
|
113
119
|
/**
|
|
114
120
|
* Retrieves a Format from a SchemaContext. If the format is part of a KindOfQuantity, the first presentation format in the KindOfQuantity that matches the current unit system will be retrieved.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaFormatsProvider.js","sourceRoot":"","sources":["../../../src/Formatting/SchemaFormatsProvider.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH,OAAO,EAAkB,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAM5D,MAAM,cAAc,GAAG,uBAAuB,CAAC;AAC/C;;;GAGG;AACH,MAAM,OAAO,qBAAqB;IACxB,QAAQ,CAAgB;IACxB,WAAW,CAAgB;IAC3B,iBAAiB,GAAgB,IAAI,GAAG,EAAE,CAAC;IAC5C,gBAAgB,GAAG,IAAI,OAAO,EAAsC,CAAC;IAC5E;;;;;;OAMG;IACH,YAAY,gBAAgC,EAAE,UAAyB;QACrE,IAAI,gBAAgB,YAAY,aAAa,EAAE,CAAC;YAC9C,IAAI,CAAC,QAAQ,GAAG,gBAAgB,CAAC;QACnC,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,QAAQ,GAAG,IAAI,aAAa,EAAE,CAAC;YACpC,IAAI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EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC3E,KAAK,aAAa;YAChB,OAAO,CAAC,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACrE,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IACnF,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,SAAS,6BAA6B,CAAC,eAAoC;IACzE,oDAAoD;IACpD,OAAO;QACL,YAAY,EAAE,CAAC,gBAAgB,EAAE,kBAAkB,EAAE,eAAe,CAAC;QACrE,SAAS,EAAE,CAAC;QACZ,IAAI,EAAE,SAAS;QACf,SAAS,EAAE;YACT,KAAK,EAAE;gBACL;oBACE,IAAI,EAAE,eAAe,CAAC,QAAQ;oBAC9B,KAAK,EAAE,eAAe,CAAC,KAAK;iBAC7B;aACF;SACF;KACF,CAAC;AACJ,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Metadata\r\n */\r\n\r\nimport { ISchemaLocater, SchemaContext } from \"../Context\";\r\nimport { SchemaItemKey, SchemaKey } from \"../SchemaKey\";\r\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\r\nimport { Format } from \"../Metadata/Format\";\r\nimport { SchemaItemFormatProps } from \"../Deserialization/JsonProps\";\r\nimport { BeEvent, Logger } from \"@itwin/core-bentley\";\r\nimport { KindOfQuantity } from \"../Metadata/KindOfQuantity\";\r\nimport { getFormatProps } from \"../Metadata/OverrideFormat\";\r\nimport { FormatDefinition, FormatProps, FormatsChangedArgs, FormatsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\r\nimport { Unit } from \"../Metadata/Unit\";\r\nimport { InvertedUnit } from \"../Metadata/InvertedUnit\";\r\nimport { Schema } from \"../Metadata/Schema\";\r\nimport { UnitSystem } from \"../Metadata/UnitSystem\";\r\nconst loggerCategory = \"SchemaFormatsProvider\";\r\n/**\r\n * Provides default formats and kind of quantities from a given SchemaContext or SchemaLocater.\r\n * @beta\r\n */\r\nexport class SchemaFormatsProvider implements FormatsProvider {\r\n private _context: SchemaContext;\r\n private _unitSystem: UnitSystemKey;\r\n private _formatsRetrieved: Set<string> = new Set();\r\n public onFormatsChanged = new BeEvent<(args: FormatsChangedArgs) => void>();\r\n /**\r\n *\r\n * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext\r\n * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be\r\n * created and the locater will be added.\r\n * @param unitSystem Used to lookup a default format through a schema specific algorithm, when the format retrieved is associated with a KindOfQuantity.\r\n */\r\n constructor(contextOrLocater: ISchemaLocater, unitSystem: UnitSystemKey) {\r\n if (contextOrLocater instanceof SchemaContext) {\r\n this._context = contextOrLocater;\r\n } else {\r\n this._context = new SchemaContext();\r\n this._context.addLocater(contextOrLocater);\r\n }\r\n this._unitSystem = unitSystem;\r\n }\r\n\r\n public get context() { return this._context; }\r\n public get unitSystem() { return this._unitSystem; }\r\n\r\n public set unitSystem(unitSystem: UnitSystemKey) {\r\n this._unitSystem = unitSystem;\r\n this.clear();\r\n }\r\n\r\n private clear(): void {\r\n const formatsChanged = Array.from(this._formatsRetrieved);\r\n this._formatsRetrieved.clear();\r\n this.onFormatsChanged.raiseEvent({ formatsChanged });\r\n }\r\n\r\n /** When using a presentation unit from a KindOfQuantity, the label and description should come from the KindOfQuantity */\r\n private convertToFormatDefinition(format: SchemaItemFormatProps, kindOfQuantity: KindOfQuantity): FormatDefinition {\r\n // Destructure all properties except 'rest'\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n const { name, label, description, $schema, schema, schemaVersion, schemaItemType,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n customAttributes, originalECSpecMajorVersion, originalECSpecMinorVersion, ...rest } = format;\r\n\r\n return {\r\n ...rest,\r\n name: kindOfQuantity.fullName,\r\n label: kindOfQuantity.label ?? format.label,\r\n description: kindOfQuantity.description ?? format.description,\r\n }\r\n }\r\n\r\n private async getKindOfQuantityFormatFromSchema(itemKey: SchemaItemKey): Promise<FormatDefinition | undefined> {\r\n let kindOfQuantity: KindOfQuantity | undefined;\r\n try {\r\n kindOfQuantity = await this._context.getSchemaItem(itemKey, KindOfQuantity);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find KindOfQuantity ${itemKey.fullName}`);\r\n return undefined;\r\n }\r\n\r\n if (!kindOfQuantity) {\r\n return undefined;\r\n }\r\n\r\n // Find the first presentation format that matches the provided unit system.\r\n const unitSystemMatchers = getUnitSystemGroupMatchers(this._unitSystem);\r\n const presentationFormats = kindOfQuantity.presentationFormats;\r\n for (const matcher of unitSystemMatchers) {\r\n for (const lazyFormat of presentationFormats) {\r\n const format = await lazyFormat;\r\n const unit = await (format.units && format.units[0][0]);\r\n if (!unit) {\r\n continue;\r\n }\r\n const currentUnitSystem = await unit.unitSystem;\r\n if (currentUnitSystem && matcher(currentUnitSystem)) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const props = getFormatProps(format);\r\n return this.convertToFormatDefinition(props, kindOfQuantity);\r\n }\r\n }\r\n }\r\n\r\n // If no matching presentation format was found, use persistence unit format if it matches unit system.\r\n const persistenceUnit = await kindOfQuantity.persistenceUnit;\r\n const persistenceUnitSystem = await persistenceUnit?.unitSystem;\r\n if (persistenceUnit && persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const props = getPersistenceUnitFormatProps(persistenceUnit);\r\n return this.convertToFormatDefinition(props, kindOfQuantity);\r\n }\r\n\r\n const defaultFormat = kindOfQuantity.defaultPresentationFormat;\r\n if (!defaultFormat) {\r\n return undefined;\r\n }\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const defaultProps = getFormatProps(await defaultFormat);\r\n return this.convertToFormatDefinition(defaultProps, kindOfQuantity);\r\n }\r\n\r\n\r\n /**\r\n * Retrieves a Format from a SchemaContext. If the format is part of a KindOfQuantity, the first presentation format in the KindOfQuantity that matches the current unit system will be retrieved.\r\n * If no presentation format matches the current unit system, the persistence unit format will be retrieved if it matches the current unit system.\r\n * Else, the default presentation format will be retrieved.\r\n * @param name The full name of the Format or KindOfQuantity.\r\n * @returns\r\n */\r\n public async getFormat(name: string): Promise<FormatDefinition | undefined> {\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(name);\r\n const schemaKey = new SchemaKey(schemaName);\r\n let schema: Schema | undefined;\r\n try {\r\n schema = await this._context.getSchema(schemaKey);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find schema ${schemaName}`);\r\n return undefined;\r\n }\r\n if (!schema) {\r\n return undefined;\r\n }\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n\r\n if (schema.name === \"Formats\") {\r\n let format: Format | undefined;\r\n try {\r\n format = await this._context.getSchemaItem(itemKey, Format);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find Format ${itemKey.fullName}`);\r\n return undefined;\r\n }\r\n if (!format) {\r\n return undefined;\r\n }\r\n return format.toJSON(true);\r\n }\r\n return this.getKindOfQuantityFormatFromSchema(itemKey);\r\n }\r\n}\r\n\r\nfunction getUnitSystemGroupMatchers(groupKey?: UnitSystemKey) {\r\n function createMatcher(name: string | string[]) {\r\n const names = Array.isArray(name) ? name : [name];\r\n return (unitSystem: UnitSystem) => names.some((n) => n === unitSystem.name.toUpperCase());\r\n }\r\n switch (groupKey) {\r\n case \"imperial\":\r\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"metric\":\r\n return [[\"SI\", \"METRIC\"], \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"usCustomary\":\r\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"usSurvey\":\r\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n }\r\n return [];\r\n}\r\n\r\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\r\n // Same as Format \"DefaultRealU\" in Formats ecschema\r\n return {\r\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\r\n precision: 6,\r\n type: \"Decimal\",\r\n composite: {\r\n units: [\r\n {\r\n name: persistenceUnit.fullName,\r\n label: persistenceUnit.label,\r\n },\r\n ],\r\n },\r\n };\r\n}"]}
|
|
1
|
+
{"version":3,"file":"SchemaFormatsProvider.js","sourceRoot":"","sources":["../../../src/Formatting/SchemaFormatsProvider.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH,OAAO,EAAkB,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAM5D,MAAM,cAAc,GAAG,uBAAuB,CAAC;AAC/C;;;GAGG;AACH,MAAM,OAAO,qBAAqB;IACxB,QAAQ,CAAgB;IACxB,WAAW,CAAiB;IAC5B,iBAAiB,GAAgB,IAAI,GAAG,EAAE,CAAC;IAC5C,gBAAgB,GAAG,IAA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AAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,UAAU,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;IAC5F,CAAC;IACD,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACjF,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC3E,KAAK,aAAa;YAChB,OAAO,CAAC,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACrE,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IACnF,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,SAAS,6BAA6B,CAAC,eAAoC;IACzE,oDAAoD;IACpD,OAAO;QACL,YAAY,EAAE,CAAC,gBAAgB,EAAE,kBAAkB,EAAE,eAAe,CAAC;QACrE,SAAS,EAAE,CAAC;QACZ,IAAI,EAAE,SAAS;QACf,SAAS,EAAE;YACT,KAAK,EAAE;gBACL;oBACE,IAAI,EAAE,eAAe,CAAC,QAAQ;oBAC9B,KAAK,EAAE,eAAe,CAAC,KAAK;iBAC7B;aACF;SACF;KACF,CAAC;AACJ,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Metadata\r\n */\r\n\r\nimport { ISchemaLocater, SchemaContext } from \"../Context\";\r\nimport { SchemaItemKey, SchemaKey } from \"../SchemaKey\";\r\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\r\nimport { Format } from \"../Metadata/Format\";\r\nimport { SchemaItemFormatProps } from \"../Deserialization/JsonProps\";\r\nimport { BeEvent, Logger } from \"@itwin/core-bentley\";\r\nimport { KindOfQuantity } from \"../Metadata/KindOfQuantity\";\r\nimport { getFormatProps } from \"../Metadata/OverrideFormat\";\r\nimport { FormatDefinition, FormatProps, FormatsChangedArgs, FormatsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\r\nimport { Unit } from \"../Metadata/Unit\";\r\nimport { InvertedUnit } from \"../Metadata/InvertedUnit\";\r\nimport { Schema } from \"../Metadata/Schema\";\r\nimport { UnitSystem } from \"../Metadata/UnitSystem\";\r\nconst loggerCategory = \"SchemaFormatsProvider\";\r\n/**\r\n * Provides default formats and kind of quantities from a given SchemaContext or SchemaLocater.\r\n * @beta\r\n */\r\nexport class SchemaFormatsProvider implements FormatsProvider {\r\n private _context: SchemaContext;\r\n private _unitSystem?: UnitSystemKey;\r\n private _formatsRetrieved: Set<string> = new Set();\r\n public onFormatsChanged = new BeEvent<(args: FormatsChangedArgs) => void>();\r\n /**\r\n *\r\n * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext\r\n * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be\r\n * created and the locater will be added.\r\n * @param unitSystem Optional unit system used to lookup a default format through a schema specific algorithm, when the format retrieved is associated with a KindOfQuantity.\r\n * If not provided, the default presentation format will be used directly without matching unit systems.\r\n */\r\n constructor(contextOrLocater: ISchemaLocater, unitSystem?: UnitSystemKey) {\r\n if (contextOrLocater instanceof SchemaContext) {\r\n this._context = contextOrLocater;\r\n } else {\r\n this._context = new SchemaContext();\r\n this._context.addLocater(contextOrLocater);\r\n }\r\n this._unitSystem = unitSystem;\r\n }\r\n\r\n public get context() { return this._context; }\r\n public get unitSystem() { return this._unitSystem; }\r\n\r\n public set unitSystem(unitSystem: UnitSystemKey | undefined) {\r\n this._unitSystem = unitSystem;\r\n this.clear();\r\n }\r\n\r\n private clear(): void {\r\n const formatsChanged = Array.from(this._formatsRetrieved);\r\n this._formatsRetrieved.clear();\r\n this.onFormatsChanged.raiseEvent({ formatsChanged });\r\n }\r\n\r\n /** When using a presentation unit from a KindOfQuantity, the label and description should come from the KindOfQuantity */\r\n private convertToFormatDefinition(format: SchemaItemFormatProps, kindOfQuantity: KindOfQuantity): FormatDefinition {\r\n // Destructure all properties except 'rest'\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n const { name, label, description, $schema, schema, schemaVersion, schemaItemType,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n customAttributes, originalECSpecMajorVersion, originalECSpecMinorVersion, ...rest } = format;\r\n\r\n return {\r\n ...rest,\r\n name: kindOfQuantity.fullName,\r\n label: kindOfQuantity.label ?? format.label,\r\n description: kindOfQuantity.description ?? format.description,\r\n }\r\n }\r\n\r\n private async getKindOfQuantityFormatFromSchema(itemKey: SchemaItemKey): Promise<FormatDefinition | undefined> {\r\n let kindOfQuantity: KindOfQuantity | undefined;\r\n try {\r\n kindOfQuantity = await this._context.getSchemaItem(itemKey, KindOfQuantity);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find KindOfQuantity ${itemKey.fullName}`);\r\n return undefined;\r\n }\r\n\r\n if (!kindOfQuantity) {\r\n return undefined;\r\n }\r\n\r\n // If a unit system is provided, find the first presentation format that matches it.\r\n if (this._unitSystem) {\r\n const unitSystemMatchers = getUnitSystemGroupMatchers(this._unitSystem);\r\n const presentationFormats = kindOfQuantity.presentationFormats;\r\n for (const matcher of unitSystemMatchers) {\r\n for (const lazyFormat of presentationFormats) {\r\n const format = await lazyFormat;\r\n const unit = await (format.units && format.units[0][0]);\r\n if (!unit) {\r\n continue;\r\n }\r\n const currentUnitSystem = await unit.unitSystem;\r\n if (currentUnitSystem && matcher(currentUnitSystem)) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const props = getFormatProps(format);\r\n return this.convertToFormatDefinition(props, kindOfQuantity);\r\n }\r\n }\r\n }\r\n\r\n // If no matching presentation format was found, fall back to persistence unit format\r\n // only if it matches the requested unit system.\r\n const persistenceUnit = await kindOfQuantity.persistenceUnit;\r\n const persistenceUnitSystem = await persistenceUnit?.unitSystem;\r\n if (persistenceUnit && persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const props = getPersistenceUnitFormatProps(persistenceUnit);\r\n return this.convertToFormatDefinition(props, kindOfQuantity);\r\n }\r\n }\r\n\r\n // If no unit system was provided, or no matching format was found, use the default presentation format.\r\n // Unit conversion from persistence unit to presentation unit will be handled by FormatterSpec.\r\n const defaultFormat = kindOfQuantity.defaultPresentationFormat;\r\n if (defaultFormat) {\r\n this._formatsRetrieved.add(itemKey.fullName);\r\n const defaultProps = getFormatProps(await defaultFormat);\r\n return this.convertToFormatDefinition(defaultProps, kindOfQuantity);\r\n }\r\n\r\n return undefined;\r\n }\r\n\r\n\r\n /**\r\n * Retrieves a Format from a SchemaContext. If the format is part of a KindOfQuantity, the first presentation format in the KindOfQuantity that matches the current unit system will be retrieved.\r\n * If no presentation format matches the current unit system, the persistence unit format will be retrieved if it matches the current unit system.\r\n * Else, the default presentation format will be retrieved.\r\n * @param name The full name of the Format or KindOfQuantity.\r\n * @returns\r\n */\r\n public async getFormat(name: string): Promise<FormatDefinition | undefined> {\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(name);\r\n const schemaKey = new SchemaKey(schemaName);\r\n let schema: Schema | undefined;\r\n try {\r\n schema = await this._context.getSchema(schemaKey);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find schema ${schemaName}`);\r\n return undefined;\r\n }\r\n if (!schema) {\r\n return undefined;\r\n }\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n\r\n if (schema.name === \"Formats\") {\r\n let format: Format | undefined;\r\n try {\r\n format = await this._context.getSchemaItem(itemKey, Format);\r\n } catch {\r\n Logger.logError(loggerCategory, `Failed to find Format ${itemKey.fullName}`);\r\n return undefined;\r\n }\r\n if (!format) {\r\n return undefined;\r\n }\r\n return format.toJSON(true);\r\n }\r\n return this.getKindOfQuantityFormatFromSchema(itemKey);\r\n }\r\n}\r\n\r\nfunction getUnitSystemGroupMatchers(groupKey?: UnitSystemKey) {\r\n function createMatcher(name: string | string[]) {\r\n const names = Array.isArray(name) ? name : [name];\r\n return (unitSystem: UnitSystem) => names.some((n) => n === unitSystem.name.toUpperCase());\r\n }\r\n switch (groupKey) {\r\n case \"imperial\":\r\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"metric\":\r\n return [[\"SI\", \"METRIC\"], \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"usCustomary\":\r\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n case \"usSurvey\":\r\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\r\n }\r\n return [];\r\n}\r\n\r\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\r\n // Same as Format \"DefaultRealU\" in Formats ecschema\r\n return {\r\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\r\n precision: 6,\r\n type: \"Decimal\",\r\n composite: {\r\n units: [\r\n {\r\n name: persistenceUnit.fullName,\r\n label: persistenceUnit.label,\r\n },\r\n ],\r\n },\r\n };\r\n}"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@itwin/ecschema-metadata",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.8.0-dev.1",
|
|
4
4
|
"description": "ECObjects core concepts in typescript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "lib/cjs/ecschema-metadata.js",
|
|
@@ -41,13 +41,13 @@
|
|
|
41
41
|
"rimraf": "^6.0.1",
|
|
42
42
|
"typescript": "~5.6.2",
|
|
43
43
|
"vitest": "^3.0.6",
|
|
44
|
-
"@itwin/build-tools": "5.
|
|
45
|
-
"@itwin/core-
|
|
46
|
-
"@itwin/core-
|
|
44
|
+
"@itwin/build-tools": "5.8.0-dev.1",
|
|
45
|
+
"@itwin/core-quantity": "5.8.0-dev.1",
|
|
46
|
+
"@itwin/core-bentley": "5.8.0-dev.1"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@itwin/core-bentley": "5.
|
|
50
|
-
"@itwin/core-quantity": "5.
|
|
49
|
+
"@itwin/core-bentley": "5.8.0-dev.1",
|
|
50
|
+
"@itwin/core-quantity": "5.8.0-dev.1"
|
|
51
51
|
},
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "npm run -s build:cjs && npm run -s build:esm",
|