Skip to content

Commit 7c5e139

Browse files
authored
Add FormatSetFormatsProvider to ecschema-metadata (#8438)
1 parent 35744d9 commit 7c5e139

File tree

13 files changed

+547
-30
lines changed

13 files changed

+547
-30
lines changed

common/api/core-quantity.api.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ export interface FormatsChangedArgs {
306306
export interface FormatsProvider {
307307
// (undocumented)
308308
getFormat(name: string): Promise<FormatDefinition | undefined>;
309-
// (undocumented)
310309
onFormatsChanged: BeEvent<(args: FormatsChangedArgs) => void>;
311310
}
312311

@@ -415,9 +414,7 @@ export const isCustomFormatProps: (item: FormatProps) => item is CustomFormatPro
415414

416415
// @beta
417416
export interface MutableFormatsProvider extends FormatsProvider {
418-
// (undocumented)
419417
addFormat(name: string, format: FormatDefinition): Promise<void>;
420-
// (undocumented)
421418
removeFormat(name: string): Promise<void>;
422419
}
423420

common/api/ecschema-metadata.api.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { FormatsProvider } from '@itwin/core-quantity';
1414
import { FormatTraits } from '@itwin/core-quantity';
1515
import { FormatType } from '@itwin/core-quantity';
1616
import { FractionalPrecision } from '@itwin/core-quantity';
17+
import { MutableFormatsProvider } from '@itwin/core-quantity';
1718
import { ScientificType } from '@itwin/core-quantity';
1819
import { ShowSignOption } from '@itwin/core-quantity';
1920
import { UnitConversionProps } from '@itwin/core-quantity';
@@ -808,6 +809,20 @@ export interface FormatSet {
808809
name: string;
809810
}
810811

812+
// @beta
813+
export class FormatSetFormatsProvider implements MutableFormatsProvider {
814+
constructor(props: {
815+
formatSet: FormatSet;
816+
fallbackProvider?: FormatsProvider;
817+
});
818+
addFormat(name: string, format: FormatDefinition): Promise<void>;
819+
clearFallbackProvider(): void;
820+
getFormat(input: string): Promise<FormatDefinition | undefined>;
821+
// (undocumented)
822+
onFormatsChanged: BeEvent<(args: FormatsChangedArgs) => void>;
823+
removeFormat(name: string): Promise<void>;
824+
}
825+
811826
// @internal (undocumented)
812827
export function getFormatProps(format: Format | OverrideFormat): FormatProps;
813828

common/api/summary/ecschema-metadata.exports.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ preview;interface;EnumeratorProps
8989
public;class;Format
9090
preview;class;Format
9191
beta;interface;FormatSet
92+
beta;class;FormatSetFormatsProvider
9293
internal;function;getFormatProps
9394
public;interface;HasMixins
9495
preview;interface;HasMixins
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@itwin/core-quantity",
5+
"comment": "Add documentation to public methods",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@itwin/core-quantity"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@itwin/ecschema-metadata",
5+
"comment": "Add FormatSetFormatsProvider",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@itwin/ecschema-metadata"
10+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { BeEvent } from "@itwin/core-bentley";
2+
import { FormatDefinition, FormatsChangedArgs, FormatsProvider, MutableFormatsProvider } from "@itwin/core-quantity";
3+
import { FormatSet } from "../Deserialization/JsonProps";
4+
import { SchemaItem } from "../Metadata/SchemaItem";
5+
6+
/**
7+
* A mutable format provider that manages format definitions within a format set.
8+
* When formats are added or removed, the underlying format set is automatically updated.
9+
* @beta
10+
*/
11+
export class FormatSetFormatsProvider implements MutableFormatsProvider {
12+
public onFormatsChanged: BeEvent<(args: FormatsChangedArgs) => void> = new BeEvent<(args: FormatsChangedArgs) => void>();
13+
14+
private _formatSet: FormatSet;
15+
private _fallbackProvider?: FormatsProvider;
16+
17+
public constructor(props: {formatSet: FormatSet, fallbackProvider?: FormatsProvider}) {
18+
this._formatSet = props.formatSet;
19+
this._fallbackProvider = props.fallbackProvider;
20+
}
21+
22+
/**
23+
* Adds a format definition to the format set or updates an existing one.
24+
* @param name The name of the format to add or update
25+
* @param format The format definition to add or update
26+
*/
27+
public async addFormat(name: string, format: FormatDefinition): Promise<void> {
28+
this._formatSet.formats[name] = format;
29+
this.onFormatsChanged.raiseEvent({ formatsChanged: [name] });
30+
}
31+
32+
/**
33+
* Clears the fallback provider, if one is set.
34+
*/
35+
public clearFallbackProvider(): void {
36+
this._fallbackProvider = undefined;
37+
}
38+
39+
/**
40+
* Retrieves a format definition by its name from the format set. If not found, it checks the fallback provider to find the format, else returns undefined.
41+
*/
42+
public async getFormat(input: string): Promise<FormatDefinition | undefined> {
43+
// Normalizes any schemaItem names coming from node addon 'schemaName:schemaItemName' -> 'schemaName.schemaItemName'
44+
const [schemaName, itemName] = SchemaItem.parseFullName(input);
45+
46+
const name = (schemaName === "") ? itemName : `${schemaName}.${itemName}`;
47+
const format = this._formatSet.formats[name];
48+
if (format) return format;
49+
if (this._fallbackProvider) return this._fallbackProvider.getFormat(name);
50+
return undefined;
51+
}
52+
53+
/**
54+
* Removes a format definition from the format set.
55+
* @param name The name of the format to remove
56+
*/
57+
public async removeFormat(name: string): Promise<void> {
58+
delete this._formatSet.formats[name];
59+
this.onFormatsChanged.raiseEvent({ formatsChanged: [name] });
60+
}
61+
}

core/ecschema-metadata/src/SchemaFormatsProvider.ts renamed to core/ecschema-metadata/src/Formatting/SchemaFormatsProvider.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
* @module Metadata
77
*/
88

9-
import { ISchemaLocater, SchemaContext } from "./Context";
10-
import { SchemaItemKey, SchemaKey } from "./SchemaKey";
11-
import { SchemaItem } from "./Metadata/SchemaItem";
12-
import { Format } from "./Metadata/Format";
13-
import { SchemaItemFormatProps } from "./Deserialization/JsonProps";
9+
import { ISchemaLocater, SchemaContext } from "../Context";
10+
import { SchemaItemKey, SchemaKey } from "../SchemaKey";
11+
import { SchemaItem } from "../Metadata/SchemaItem";
12+
import { Format } from "../Metadata/Format";
13+
import { SchemaItemFormatProps } from "../Deserialization/JsonProps";
1414
import { BeEvent, Logger } from "@itwin/core-bentley";
15-
import { KindOfQuantity } from "./Metadata/KindOfQuantity";
16-
import { getFormatProps } from "./Metadata/OverrideFormat";
15+
import { KindOfQuantity } from "../Metadata/KindOfQuantity";
16+
import { getFormatProps } from "../Metadata/OverrideFormat";
1717
import { FormatDefinition, FormatProps, FormatsChangedArgs, FormatsProvider, UnitSystemKey } from "@itwin/core-quantity";
18-
import { Unit } from "./Metadata/Unit";
19-
import { InvertedUnit } from "./Metadata/InvertedUnit";
20-
import { Schema } from "./Metadata/Schema";
21-
import { UnitSystem } from "./Metadata/UnitSystem";
18+
import { Unit } from "../Metadata/Unit";
19+
import { InvertedUnit } from "../Metadata/InvertedUnit";
20+
import { Schema } from "../Metadata/Schema";
21+
import { UnitSystem } from "../Metadata/UnitSystem";
2222
const loggerCategory = "SchemaFormatsProvider";
2323
/**
2424
* Provides default formats and kind of quantities from a given SchemaContext or SchemaLocater.

core/ecschema-metadata/src/ecschema-metadata.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export * from "./UnitConversion/UnitConverter";
4545
export * from "./UnitProvider/SchemaUnitProvider";
4646
export * from "./Validation/SchemaWalker";
4747
export * from "./SchemaPartVisitorDelegate";
48-
export * from "./SchemaFormatsProvider";
48+
export * from "./Formatting/SchemaFormatsProvider";
49+
export * from "./Formatting/FormatSetFormatsProvider";
4950
export * from "./IncrementalLoading/ECSqlSchemaLocater";
5051
export * from "./IncrementalLoading/IncrementalSchemaLocater";
5152
export { CustomAttribute, CustomAttributeContainerProps} from "./Metadata/CustomAttribute";

0 commit comments

Comments
 (0)