@itwin/ecschema-metadata 3.5.0-dev.16 → 3.5.0-dev.19

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.
Files changed (141) hide show
  1. package/CHANGELOG.md +6 -1
  2. package/lib/cjs/Constants.d.ts +23 -23
  3. package/lib/cjs/Constants.js +33 -33
  4. package/lib/cjs/Constants.js.map +1 -1
  5. package/lib/cjs/Context.d.ts +139 -139
  6. package/lib/cjs/Context.js +204 -204
  7. package/lib/cjs/DelayedPromise.d.ts +88 -88
  8. package/lib/cjs/DelayedPromise.js +91 -91
  9. package/lib/cjs/DelayedPromise.js.map +1 -1
  10. package/lib/cjs/Deserialization/AbstractParser.d.ts +45 -45
  11. package/lib/cjs/Deserialization/AbstractParser.js +11 -11
  12. package/lib/cjs/Deserialization/AbstractParser.js.map +1 -1
  13. package/lib/cjs/Deserialization/Helper.d.ts +251 -251
  14. package/lib/cjs/Deserialization/Helper.js +843 -843
  15. package/lib/cjs/Deserialization/Helper.js.map +1 -1
  16. package/lib/cjs/Deserialization/JsonParser.d.ts +165 -165
  17. package/lib/cjs/Deserialization/JsonParser.js +680 -680
  18. package/lib/cjs/Deserialization/JsonParser.js.map +1 -1
  19. package/lib/cjs/Deserialization/JsonProps.d.ts +282 -282
  20. package/lib/cjs/Deserialization/JsonProps.js +9 -9
  21. package/lib/cjs/Deserialization/JsonProps.js.map +1 -1
  22. package/lib/cjs/Deserialization/SchemaGraphUtil.d.ts +34 -34
  23. package/lib/cjs/Deserialization/SchemaGraphUtil.js +77 -77
  24. package/lib/cjs/Deserialization/SchemaGraphUtil.js.map +1 -1
  25. package/lib/cjs/Deserialization/XmlParser.d.ts +85 -85
  26. package/lib/cjs/Deserialization/XmlParser.js +973 -973
  27. package/lib/cjs/Deserialization/XmlParser.js.map +1 -1
  28. package/lib/cjs/Deserialization/XmlSerializationUtils.d.ts +54 -54
  29. package/lib/cjs/Deserialization/XmlSerializationUtils.js +174 -174
  30. package/lib/cjs/Deserialization/XmlSerializationUtils.js.map +1 -1
  31. package/lib/cjs/ECName.d.ts +30 -30
  32. package/lib/cjs/ECName.js +85 -85
  33. package/lib/cjs/ECName.js.map +1 -1
  34. package/lib/cjs/ECObjects.d.ts +162 -162
  35. package/lib/cjs/ECObjects.js +453 -453
  36. package/lib/cjs/ECObjects.js.map +1 -1
  37. package/lib/cjs/Exception.d.ts +43 -43
  38. package/lib/cjs/Exception.js +86 -86
  39. package/lib/cjs/Exception.js.map +1 -1
  40. package/lib/cjs/Interfaces.d.ts +66 -66
  41. package/lib/cjs/Interfaces.js +9 -9
  42. package/lib/cjs/Interfaces.js.map +1 -1
  43. package/lib/cjs/Metadata/Class.d.ts +243 -243
  44. package/lib/cjs/Metadata/Class.js +558 -558
  45. package/lib/cjs/Metadata/Class.js.map +1 -1
  46. package/lib/cjs/Metadata/Constant.d.ts +64 -64
  47. package/lib/cjs/Metadata/Constant.js +123 -123
  48. package/lib/cjs/Metadata/Constant.js.map +1 -1
  49. package/lib/cjs/Metadata/CustomAttribute.d.ts +23 -23
  50. package/lib/cjs/Metadata/CustomAttribute.js +27 -27
  51. package/lib/cjs/Metadata/CustomAttribute.js.map +1 -1
  52. package/lib/cjs/Metadata/CustomAttributeClass.d.ts +39 -39
  53. package/lib/cjs/Metadata/CustomAttributeClass.js +68 -68
  54. package/lib/cjs/Metadata/CustomAttributeClass.js.map +1 -1
  55. package/lib/cjs/Metadata/EntityClass.d.ts +77 -77
  56. package/lib/cjs/Metadata/EntityClass.js +230 -230
  57. package/lib/cjs/Metadata/EntityClass.js.map +1 -1
  58. package/lib/cjs/Metadata/Enumeration.d.ts +86 -86
  59. package/lib/cjs/Metadata/Enumeration.js +159 -159
  60. package/lib/cjs/Metadata/Enumeration.js.map +1 -1
  61. package/lib/cjs/Metadata/Format.d.ts +98 -98
  62. package/lib/cjs/Metadata/Format.js +242 -242
  63. package/lib/cjs/Metadata/Format.js.map +1 -1
  64. package/lib/cjs/Metadata/InvertedUnit.d.ts +49 -49
  65. package/lib/cjs/Metadata/InvertedUnit.js +97 -97
  66. package/lib/cjs/Metadata/InvertedUnit.js.map +1 -1
  67. package/lib/cjs/Metadata/KindOfQuantity.d.ts +65 -65
  68. package/lib/cjs/Metadata/KindOfQuantity.js +225 -225
  69. package/lib/cjs/Metadata/KindOfQuantity.js.map +1 -1
  70. package/lib/cjs/Metadata/Mixin.d.ts +54 -54
  71. package/lib/cjs/Metadata/Mixin.js +113 -113
  72. package/lib/cjs/Metadata/Mixin.js.map +1 -1
  73. package/lib/cjs/Metadata/OverrideFormat.d.ts +55 -55
  74. package/lib/cjs/Metadata/OverrideFormat.js +88 -88
  75. package/lib/cjs/Metadata/OverrideFormat.js.map +1 -1
  76. package/lib/cjs/Metadata/Phenomenon.d.ts +33 -33
  77. package/lib/cjs/Metadata/Phenomenon.js +59 -59
  78. package/lib/cjs/Metadata/Phenomenon.js.map +1 -1
  79. package/lib/cjs/Metadata/Property.d.ts +197 -197
  80. package/lib/cjs/Metadata/Property.js +491 -491
  81. package/lib/cjs/Metadata/Property.js.map +1 -1
  82. package/lib/cjs/Metadata/PropertyCategory.d.ts +39 -39
  83. package/lib/cjs/Metadata/PropertyCategory.js +61 -61
  84. package/lib/cjs/Metadata/PropertyCategory.js.map +1 -1
  85. package/lib/cjs/Metadata/RelationshipClass.d.ts +152 -152
  86. package/lib/cjs/Metadata/RelationshipClass.js +349 -349
  87. package/lib/cjs/Metadata/RelationshipClass.js.map +1 -1
  88. package/lib/cjs/Metadata/Schema.d.ts +291 -291
  89. package/lib/cjs/Metadata/Schema.js +520 -520
  90. package/lib/cjs/Metadata/Schema.js.map +1 -1
  91. package/lib/cjs/Metadata/SchemaItem.d.ts +61 -61
  92. package/lib/cjs/Metadata/SchemaItem.js +136 -136
  93. package/lib/cjs/Metadata/SchemaItem.js.map +1 -1
  94. package/lib/cjs/Metadata/Unit.d.ts +76 -76
  95. package/lib/cjs/Metadata/Unit.js +162 -162
  96. package/lib/cjs/Metadata/UnitSystem.d.ts +20 -20
  97. package/lib/cjs/Metadata/UnitSystem.js +29 -29
  98. package/lib/cjs/Metadata/UnitSystem.js.map +1 -1
  99. package/lib/cjs/PropertyTypes.d.ts +49 -49
  100. package/lib/cjs/PropertyTypes.js +91 -91
  101. package/lib/cjs/PropertyTypes.js.map +1 -1
  102. package/lib/cjs/SchemaJsonLocater.d.ts +34 -34
  103. package/lib/cjs/SchemaJsonLocater.js +42 -42
  104. package/lib/cjs/SchemaJsonLocater.js.map +1 -1
  105. package/lib/cjs/SchemaKey.d.ts +110 -110
  106. package/lib/cjs/SchemaKey.js +219 -219
  107. package/lib/cjs/SchemaKey.js.map +1 -1
  108. package/lib/cjs/SchemaLoader.d.ts +23 -23
  109. package/lib/cjs/SchemaLoader.js +47 -47
  110. package/lib/cjs/SchemaLoader.js.map +1 -1
  111. package/lib/cjs/SchemaPartVisitorDelegate.d.ts +274 -274
  112. package/lib/cjs/SchemaPartVisitorDelegate.js +212 -212
  113. package/lib/cjs/SchemaPartVisitorDelegate.js.map +1 -1
  114. package/lib/cjs/UnitConversion/Graph.d.ts +34 -34
  115. package/lib/cjs/UnitConversion/Graph.js +83 -83
  116. package/lib/cjs/UnitConversion/Graph.js.map +1 -1
  117. package/lib/cjs/UnitConversion/Parser.d.ts +8 -8
  118. package/lib/cjs/UnitConversion/Parser.js +42 -42
  119. package/lib/cjs/UnitConversion/Parser.js.map +1 -1
  120. package/lib/cjs/UnitConversion/UnitConversion.d.ts +45 -45
  121. package/lib/cjs/UnitConversion/UnitConversion.js +77 -77
  122. package/lib/cjs/UnitConversion/UnitConversion.js.map +1 -1
  123. package/lib/cjs/UnitConversion/UnitConverter.d.ts +39 -39
  124. package/lib/cjs/UnitConversion/UnitConverter.js +116 -116
  125. package/lib/cjs/UnitConversion/UnitConverter.js.map +1 -1
  126. package/lib/cjs/UnitConversion/UnitTree.d.ts +43 -43
  127. package/lib/cjs/UnitConversion/UnitTree.js +169 -169
  128. package/lib/cjs/UnitConversion/UnitTree.js.map +1 -1
  129. package/lib/cjs/UnitProvider/SchemaUnitProvider.d.ts +77 -77
  130. package/lib/cjs/UnitProvider/SchemaUnitProvider.js +236 -236
  131. package/lib/cjs/UnitProvider/SchemaUnitProvider.js.map +1 -1
  132. package/lib/cjs/Validation/SchemaWalker.d.ts +23 -23
  133. package/lib/cjs/Validation/SchemaWalker.js +53 -53
  134. package/lib/cjs/Validation/SchemaWalker.js.map +1 -1
  135. package/lib/cjs/ecschema-metadata.d.ts +51 -51
  136. package/lib/cjs/ecschema-metadata.js +96 -96
  137. package/lib/cjs/ecschema-metadata.js.map +1 -1
  138. package/lib/cjs/utils/SchemaGraph.d.ts +25 -25
  139. package/lib/cjs/utils/SchemaGraph.js +65 -65
  140. package/lib/cjs/utils/SchemaGraph.js.map +1 -1
  141. package/package.json +7 -7
@@ -1,559 +1,559 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module Metadata
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.MutableClass = exports.MutableStructClass = exports.StructClass = exports.ECClass = void 0;
11
- const core_bentley_1 = require("@itwin/core-bentley");
12
- const DelayedPromise_1 = require("../DelayedPromise");
13
- const XmlSerializationUtils_1 = require("../Deserialization/XmlSerializationUtils");
14
- const ECObjects_1 = require("../ECObjects");
15
- const Exception_1 = require("../Exception");
16
- const SchemaKey_1 = require("../SchemaKey");
17
- const CustomAttribute_1 = require("./CustomAttribute");
18
- const Property_1 = require("./Property");
19
- const SchemaItem_1 = require("./SchemaItem");
20
- /**
21
- * A common abstract class for all of the ECClass types.
22
- * @beta
23
- */
24
- class ECClass extends SchemaItem_1.SchemaItem {
25
- constructor(schema, name, modifier) {
26
- super(schema, name);
27
- if (modifier)
28
- this._modifier = modifier;
29
- else
30
- this._modifier = ECObjects_1.ECClassModifier.None;
31
- }
32
- get modifier() { return this._modifier; }
33
- get baseClass() { return this._baseClass; }
34
- set baseClass(baseClass) { this._baseClass = baseClass; }
35
- get properties() { var _a; return (_a = this._properties) === null || _a === void 0 ? void 0 : _a.values(); }
36
- get customAttributes() { return this._customAttributes; }
37
- /**
38
- * Convenience method for adding an already loaded ECProperty used by create*Property methods.
39
- * @param prop The property to add.
40
- * @return The property that was added.
41
- */
42
- addProperty(prop) {
43
- if (!this._properties)
44
- this._properties = new Map();
45
- this._properties.set(prop.name.toUpperCase(), prop);
46
- return prop;
47
- }
48
- /**
49
- * Deletes a property from within this class.
50
- * @param name The property name to delete, lookup is case-insensitive
51
- * @alpha
52
- */
53
- async deleteProperty(name) {
54
- if (this._properties) {
55
- const property = await this.getProperty(name);
56
- if (property)
57
- this._properties.delete(name.toUpperCase());
58
- }
59
- }
60
- /**
61
- * Deletes a property from within this class.
62
- * @param name The property name to delete, lookup is case-insensitive
63
- * @alpha
64
- */
65
- deletePropertySync(name) {
66
- if (this._properties) {
67
- const property = this.getPropertySync(name);
68
- if (property)
69
- this._properties.delete(name.toUpperCase());
70
- }
71
- }
72
- getBaseClassSync() {
73
- if (!this.baseClass) {
74
- return undefined;
75
- }
76
- return this.schema.lookupItemSync(this.baseClass);
77
- }
78
- /**
79
- * Searches, case-insensitive, for a local ECProperty with the name provided.
80
- * @param name
81
- */
82
- async getProperty(name, includeInherited = false) {
83
- if (this._properties) {
84
- const upperKey = name.toUpperCase();
85
- const property = this._properties.get(upperKey);
86
- if (property)
87
- return property;
88
- }
89
- if (!includeInherited) {
90
- return undefined;
91
- }
92
- return this.getInheritedProperty(name);
93
- }
94
- /**
95
- * Searches, case-insensitive, for a local ECProperty with the name provided.
96
- * @param name
97
- */
98
- getPropertySync(name, includeInherited = false) {
99
- if (this._properties) {
100
- const upperKey = name.toUpperCase();
101
- const property = this._properties.get(upperKey);
102
- if (property)
103
- return property;
104
- }
105
- if (!includeInherited) {
106
- return undefined;
107
- }
108
- return this.getInheritedPropertySync(name);
109
- }
110
- /**
111
- * Searches the base class, if one exists, for the property with the name provided.
112
- * @param name The name of the inherited property to find.
113
- */
114
- async getInheritedProperty(name) {
115
- if (this.baseClass) {
116
- const baseClassObj = await this.baseClass;
117
- return baseClassObj.getProperty(name, true);
118
- }
119
- return undefined;
120
- }
121
- /**
122
- * Searches the base class, if one exists, for the property with the name provided.
123
- * @param name The name of the inherited property to find.
124
- */
125
- getInheritedPropertySync(name) {
126
- const baseClassObj = this.getBaseClassSync();
127
- if (baseClassObj)
128
- return baseClassObj.getPropertySync(name, true);
129
- return undefined;
130
- }
131
- async createPrimitiveProperty(name, primitiveType) {
132
- if (await this.getProperty(name))
133
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
134
- const propType = await this.loadPrimitiveType(primitiveType, this.schema);
135
- if (typeof (propType) === "number")
136
- return this.addProperty(new Property_1.PrimitiveProperty(this, name, propType));
137
- return this.addProperty(new Property_1.EnumerationProperty(this, name, new DelayedPromise_1.DelayedPromiseWithProps(propType.key, async () => propType)));
138
- }
139
- createPrimitivePropertySync(name, primitiveType) {
140
- if (this.getPropertySync(name))
141
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
142
- const propType = this.loadPrimitiveTypeSync(primitiveType, this.schema);
143
- if (typeof (propType) === "number")
144
- return this.addProperty(new Property_1.PrimitiveProperty(this, name, propType));
145
- return this.addProperty(new Property_1.EnumerationProperty(this, name, new DelayedPromise_1.DelayedPromiseWithProps(propType.key, async () => propType)));
146
- }
147
- async createPrimitiveArrayProperty(name, primitiveType) {
148
- if (await this.getProperty(name))
149
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
150
- const propType = await this.loadPrimitiveType(primitiveType, this.schema);
151
- if (typeof (propType) === "number")
152
- return this.addProperty(new Property_1.PrimitiveArrayProperty(this, name, propType));
153
- return this.addProperty(new Property_1.EnumerationArrayProperty(this, name, new DelayedPromise_1.DelayedPromiseWithProps(propType.key, async () => propType)));
154
- }
155
- createPrimitiveArrayPropertySync(name, primitiveType) {
156
- if (this.getPropertySync(name))
157
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
158
- const propType = this.loadPrimitiveTypeSync(primitiveType, this.schema);
159
- if (typeof (propType) === "number")
160
- return this.addProperty(new Property_1.PrimitiveArrayProperty(this, name, propType));
161
- return this.addProperty(new Property_1.EnumerationArrayProperty(this, name, new DelayedPromise_1.DelayedPromiseWithProps(propType.key, async () => propType)));
162
- }
163
- /**
164
- *
165
- * @param name The name of property to create.
166
- * @param structType The struct type of property to create.
167
- */
168
- async createStructProperty(name, structType) {
169
- if (await this.getProperty(name))
170
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
171
- return this.addProperty(new Property_1.StructProperty(this, name, await this.loadStructType(structType, this.schema)));
172
- }
173
- /**
174
- *
175
- * @param name The name of property to create.
176
- * @param structType The struct type of property to create.
177
- */
178
- createStructPropertySync(name, structType) {
179
- if (this.getPropertySync(name))
180
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
181
- return this.addProperty(new Property_1.StructProperty(this, name, this.loadStructTypeSync(structType, this.schema)));
182
- }
183
- /**
184
- *
185
- * @param name
186
- * @param type
187
- */
188
- async createStructArrayProperty(name, structType) {
189
- if (await this.getProperty(name))
190
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
191
- return this.addProperty(new Property_1.StructArrayProperty(this, name, await this.loadStructType(structType, this.schema)));
192
- }
193
- /**
194
- *
195
- * @param name
196
- * @param type
197
- */
198
- createStructArrayPropertySync(name, structType) {
199
- if (this.getPropertySync(name))
200
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
201
- return this.addProperty(new Property_1.StructArrayProperty(this, name, this.loadStructTypeSync(structType, this.schema)));
202
- }
203
- async loadStructType(structType, schema) {
204
- let correctType;
205
- if (typeof (structType) === "string") {
206
- correctType = await schema.lookupItem(structType);
207
- }
208
- else
209
- correctType = structType;
210
- if (!correctType)
211
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided Struct type, ${structType}, is not a valid StructClass.`);
212
- return correctType;
213
- }
214
- loadStructTypeSync(structType, schema) {
215
- let correctType;
216
- if (typeof (structType) === "string") {
217
- correctType = schema.lookupItemSync(structType);
218
- }
219
- else
220
- correctType = structType;
221
- if (!correctType)
222
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided Struct type, ${structType}, is not a valid StructClass.`);
223
- return correctType;
224
- }
225
- async loadPrimitiveType(primitiveType, schema) {
226
- if (primitiveType === undefined)
227
- return ECObjects_1.PrimitiveType.Integer;
228
- if (typeof (primitiveType) === "string") {
229
- let resolvedType = (0, ECObjects_1.parsePrimitiveType)(primitiveType);
230
- if (!resolvedType) {
231
- resolvedType = await schema.lookupItem(primitiveType);
232
- }
233
- if (resolvedType === undefined)
234
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided primitive type, ${primitiveType}, is not a valid PrimitiveType or Enumeration.`);
235
- // If resolvedType is a SchemaItem, make sure it is an Enumeration- if not, throw an error
236
- if (typeof (resolvedType) !== "number" && resolvedType.schemaItemType !== ECObjects_1.SchemaItemType.Enumeration)
237
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided primitive type, ${primitiveType}, is not a valid PrimitiveType or Enumeration.`);
238
- return resolvedType;
239
- }
240
- return primitiveType;
241
- }
242
- loadPrimitiveTypeSync(primitiveType, schema) {
243
- if (primitiveType === undefined)
244
- return ECObjects_1.PrimitiveType.Integer;
245
- if (typeof (primitiveType) === "string") {
246
- let resolvedType = (0, ECObjects_1.parsePrimitiveType)(primitiveType);
247
- if (!resolvedType) {
248
- resolvedType = schema.lookupItemSync(primitiveType);
249
- }
250
- if (resolvedType === undefined)
251
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided primitive type, ${primitiveType}, is not a valid PrimitiveType or Enumeration.`);
252
- return resolvedType;
253
- }
254
- return primitiveType;
255
- }
256
- /**
257
- * Save this Classes properties to an object for serializing to JSON.
258
- * @param standalone Serialization includes only this object (as opposed to the full schema).
259
- * @param includeSchemaVersion Include the Schema's version information in the serialized object.
260
- */
261
- toJSON(standalone = false, includeSchemaVersion = false) {
262
- const schemaJson = super.toJSON(standalone, includeSchemaVersion);
263
- const isMixin = ECObjects_1.SchemaItemType.Mixin === this.schemaItemType;
264
- const isRelationship = ECObjects_1.SchemaItemType.RelationshipClass === this.schemaItemType;
265
- if (!isMixin && (ECObjects_1.ECClassModifier.None !== this.modifier || isRelationship))
266
- schemaJson.modifier = (0, ECObjects_1.classModifierToString)(this.modifier);
267
- if (this.baseClass !== undefined)
268
- schemaJson.baseClass = this.baseClass.fullName;
269
- if (this._properties !== undefined && this._properties.size > 0)
270
- schemaJson.properties = [...this.properties].map((prop) => prop.toJSON());
271
- const customAttributes = (0, CustomAttribute_1.serializeCustomAttributes)(this.customAttributes);
272
- if (customAttributes !== undefined)
273
- schemaJson.customAttributes = customAttributes;
274
- return schemaJson;
275
- }
276
- /** @internal */
277
- async toXml(schemaXml) {
278
- const itemElement = await super.toXml(schemaXml);
279
- if (undefined !== this.modifier)
280
- itemElement.setAttribute("modifier", (0, ECObjects_1.classModifierToString)(this.modifier));
281
- if (undefined !== this.baseClass) {
282
- const baseClass = await this.baseClass;
283
- const baseClassElement = schemaXml.createElement("BaseClass");
284
- const baseClassName = XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, baseClass.schema, baseClass.name);
285
- baseClassElement.textContent = baseClassName;
286
- itemElement.appendChild(baseClassElement);
287
- }
288
- if (undefined !== this.properties) {
289
- for (const prop of this.properties) {
290
- const propXml = await prop.toXml(schemaXml);
291
- itemElement.appendChild(propXml);
292
- }
293
- }
294
- if (this._customAttributes) {
295
- const caContainerElement = schemaXml.createElement("ECCustomAttributes");
296
- for (const [name, attribute] of this._customAttributes) {
297
- const caElement = await XmlSerializationUtils_1.XmlSerializationUtils.writeCustomAttribute(name, attribute, schemaXml, this.schema);
298
- caContainerElement.appendChild(caElement);
299
- }
300
- itemElement.appendChild(caContainerElement);
301
- }
302
- return itemElement;
303
- }
304
- fromJSONSync(classProps) {
305
- super.fromJSONSync(classProps);
306
- if (undefined !== classProps.modifier) {
307
- const modifier = (0, ECObjects_1.parseClassModifier)(classProps.modifier);
308
- if (undefined === modifier)
309
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidModifier, `The string '${classProps.modifier}' is not a valid ECClassModifier.`);
310
- this._modifier = modifier;
311
- }
312
- if (undefined !== classProps.baseClass) {
313
- const ecClassSchemaItemKey = this.schema.getSchemaItemKey(classProps.baseClass);
314
- if (!ecClassSchemaItemKey)
315
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `Unable to locate the baseClass ${classProps.baseClass}.`);
316
- this._baseClass = new DelayedPromise_1.DelayedPromiseWithProps(ecClassSchemaItemKey, async () => {
317
- const baseClass = await this.schema.lookupItem(ecClassSchemaItemKey);
318
- if (undefined === baseClass)
319
- throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `Unable to locate the baseClass ${classProps.baseClass}.`);
320
- return baseClass;
321
- });
322
- }
323
- }
324
- async fromJSON(classProps) {
325
- this.fromJSONSync(classProps);
326
- }
327
- addCustomAttribute(customAttribute) {
328
- if (!this._customAttributes)
329
- this._customAttributes = new Map();
330
- this._customAttributes.set(customAttribute.className, customAttribute);
331
- }
332
- /**
333
- * Iterates (recursively) over all base classes and mixins, in "property override" order.
334
- * This is essentially a depth-first traversal through the inheritance tree.
335
- */
336
- async *getAllBaseClasses() {
337
- const baseClasses = [this];
338
- const addBaseClasses = async (ecClass) => {
339
- if (ECObjects_1.SchemaItemType.EntityClass === ecClass.schemaItemType) {
340
- for (let i = (ecClass).mixins.length - 1; i >= 0; i--) {
341
- baseClasses.push(await (ecClass).mixins[i]);
342
- }
343
- }
344
- if (ecClass.baseClass)
345
- baseClasses.push(await ecClass.baseClass);
346
- };
347
- while (baseClasses.length > 0) {
348
- const baseClass = baseClasses.pop();
349
- await addBaseClasses(baseClass);
350
- if (baseClass !== this)
351
- yield baseClass;
352
- }
353
- }
354
- *getAllBaseClassesSync() {
355
- const baseClasses = [this];
356
- const addBaseClasses = (ecClass) => {
357
- if (ECObjects_1.SchemaItemType.EntityClass === ecClass.schemaItemType) {
358
- for (const m of Array.from(ecClass.getMixinsSync()).reverse()) {
359
- baseClasses.push(m);
360
- }
361
- }
362
- const baseClass = ecClass.getBaseClassSync();
363
- if (baseClass)
364
- baseClasses.push(baseClass);
365
- };
366
- while (baseClasses.length > 0) {
367
- const baseClass = baseClasses.pop();
368
- addBaseClasses(baseClass);
369
- if (baseClass !== this)
370
- yield baseClass;
371
- }
372
- }
373
- static mergeProperties(target, existingValues, propertiesToMerge, overwriteExisting) {
374
- for (const property of propertiesToMerge) {
375
- const upperCaseName = property.name.toUpperCase();
376
- const existing = existingValues.get(upperCaseName);
377
- if (existing !== undefined) {
378
- if (overwriteExisting) {
379
- target[existing] = property;
380
- }
381
- }
382
- else {
383
- existingValues.set(upperCaseName, target.length);
384
- target.push(property);
385
- }
386
- }
387
- }
388
- async buildPropertyCache(result, existingValues, resetBaseCaches = false) {
389
- if (!existingValues) {
390
- existingValues = new Map();
391
- }
392
- if (this.baseClass) {
393
- ECClass.mergeProperties(result, existingValues, await (await this.baseClass).getProperties(resetBaseCaches), false);
394
- }
395
- if (!this.properties)
396
- return;
397
- ECClass.mergeProperties(result, existingValues, [...this.properties], true);
398
- }
399
- buildPropertyCacheSync(result, existingValues, resetBaseCaches = false) {
400
- if (!existingValues) {
401
- existingValues = new Map();
402
- }
403
- const baseClass = this.getBaseClassSync();
404
- if (baseClass) {
405
- ECClass.mergeProperties(result, existingValues, baseClass.getPropertiesSync(resetBaseCaches), false);
406
- }
407
- if (!this.properties)
408
- return;
409
- ECClass.mergeProperties(result, existingValues, [...this.properties], true);
410
- }
411
- /**
412
- * Iterates all properties, including the ones merged from base classes and mixins. To obtain only local properties, use the 'properties' field.
413
- * Since this is an expensive operation, results will be cached after first call.
414
- * @param resetCache if true, any previously cached results will be dropped and cache will be rebuilt
415
- */
416
- getPropertiesSync(resetCache = false) {
417
- if (!this._mergedPropertyCache || resetCache) {
418
- this._mergedPropertyCache = [];
419
- this.buildPropertyCacheSync(this._mergedPropertyCache, undefined, resetCache);
420
- }
421
- return this._mergedPropertyCache;
422
- }
423
- /**
424
- * Iterates all properties, including the ones merged from base classes and mixins. To obtain only local properties, use the 'properties' field.
425
- * Since this is an expensive operation, results will be cached after first call.
426
- * @param resetCache if true, any previously cached results will be dropped and cache will be rebuilt
427
- */
428
- async getProperties(resetCache = false) {
429
- if (!this._mergedPropertyCache || resetCache) {
430
- this._mergedPropertyCache = [];
431
- await this.buildPropertyCache(this._mergedPropertyCache, undefined, resetCache);
432
- }
433
- return this._mergedPropertyCache;
434
- }
435
- /**
436
- * Retrieve all custom attributes in the current class and its bases
437
- * This is the async version of getCustomAttributesSync()
438
- */
439
- async getCustomAttributes() {
440
- return this.getCustomAttributesSync();
441
- }
442
- /**
443
- * Retrieve all custom attributes in the current class and its bases.
444
- */
445
- getCustomAttributesSync() {
446
- let customAttributes = this._customAttributes;
447
- if (undefined === customAttributes) {
448
- customAttributes = new Map();
449
- }
450
- this.traverseBaseClassesSync((ecClass) => {
451
- if (undefined === ecClass.customAttributes)
452
- return false;
453
- for (const [className, customAttribute] of ecClass.customAttributes) {
454
- if (customAttributes.has(className))
455
- continue;
456
- customAttributes.set(className, customAttribute);
457
- }
458
- return false;
459
- });
460
- return customAttributes;
461
- }
462
- /**
463
- * Asynchronously traverses through the inheritance tree, using depth-first traversal, calling the given callback
464
- * function for each base class encountered.
465
- * @param callback The function to call for each base class in the hierarchy.
466
- * @param arg An argument that will be passed as the second parameter to the callback function.
467
- */
468
- async traverseBaseClasses(callback, arg) {
469
- for await (const baseClass of this.getAllBaseClasses()) {
470
- if (callback(baseClass, arg))
471
- return true;
472
- }
473
- return false;
474
- }
475
- /**
476
- * Synchronously traverses through the inheritance tree, using depth-first traversal, calling the given callback
477
- * function for each base class encountered.
478
- * @param callback The function to call for each base class in the hierarchy.
479
- * @param arg An argument that will be passed as the second parameter to the callback function.
480
- */
481
- traverseBaseClassesSync(callback, arg) {
482
- const baseClasses = this.getAllBaseClassesSync();
483
- if (!baseClasses)
484
- return false;
485
- for (const baseClass of baseClasses) {
486
- if (callback(baseClass, arg))
487
- return true;
488
- }
489
- return false;
490
- }
491
- async is(targetClass, schemaName) {
492
- if (schemaName !== undefined) {
493
- (0, core_bentley_1.assert)(typeof (targetClass) === "string", "Expected targetClass of type string because schemaName was specified");
494
- const key = new SchemaKey_1.SchemaItemKey(targetClass, new SchemaKey_1.SchemaKey(schemaName));
495
- if (SchemaItem_1.SchemaItem.equalByKey(this, key))
496
- return true;
497
- return this.traverseBaseClasses(SchemaItem_1.SchemaItem.equalByKey, key);
498
- }
499
- else {
500
- (0, core_bentley_1.assert)(ECClass.isECClass(targetClass), "Expected targetClass to be of type ECClass");
501
- if (SchemaItem_1.SchemaItem.equalByKey(this, targetClass))
502
- return true;
503
- return this.traverseBaseClasses(SchemaItem_1.SchemaItem.equalByKey, targetClass);
504
- }
505
- }
506
- /**
507
- * A synchronous version of the [[ECClass.is]], indicating if the targetClass is of this type.
508
- * @param targetClass The class to check.
509
- */
510
- isSync(targetClass) {
511
- if (SchemaItem_1.SchemaItem.equalByKey(this, targetClass))
512
- return true;
513
- return this.traverseBaseClassesSync(SchemaItem_1.SchemaItem.equalByKey, targetClass);
514
- }
515
- /**
516
- * @internal
517
- */
518
- static isECClass(object) {
519
- if (!SchemaItem_1.SchemaItem.isSchemaItem(object))
520
- return false;
521
- return object.schemaItemType === ECObjects_1.SchemaItemType.EntityClass || object.schemaItemType === ECObjects_1.SchemaItemType.Mixin || object.schemaItemType === ECObjects_1.SchemaItemType.RelationshipClass ||
522
- object.schemaItemType === ECObjects_1.SchemaItemType.StructClass || object.schemaItemType === ECObjects_1.SchemaItemType.CustomAttributeClass;
523
- }
524
- /**
525
- * @alpha
526
- * A setter method used specifically for schema editing.
527
- * @param modifier
528
- */
529
- setModifier(modifier) {
530
- this._modifier = modifier;
531
- }
532
- }
533
- exports.ECClass = ECClass;
534
- /**
535
- * A Typescript class representation of an ECStructClass.
536
- * @beta
537
- */
538
- class StructClass extends ECClass {
539
- constructor(schema, name, modifier) {
540
- super(schema, name, modifier);
541
- this.schemaItemType = ECObjects_1.SchemaItemType.StructClass;
542
- }
543
- }
544
- exports.StructClass = StructClass;
545
- /**
546
- * @internal
547
- * An abstract class used for schema editing.
548
- */
549
- class MutableStructClass extends StructClass {
550
- }
551
- exports.MutableStructClass = MutableStructClass;
552
- /**
553
- * Hackish approach that works like a "friend class" so we can access protected members without making them public.
554
- * @internal
555
- */
556
- class MutableClass extends ECClass {
557
- }
558
- exports.MutableClass = MutableClass;
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module Metadata
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.MutableClass = exports.MutableStructClass = exports.StructClass = exports.ECClass = void 0;
11
+ const core_bentley_1 = require("@itwin/core-bentley");
12
+ const DelayedPromise_1 = require("../DelayedPromise");
13
+ const XmlSerializationUtils_1 = require("../Deserialization/XmlSerializationUtils");
14
+ const ECObjects_1 = require("../ECObjects");
15
+ const Exception_1 = require("../Exception");
16
+ const SchemaKey_1 = require("../SchemaKey");
17
+ const CustomAttribute_1 = require("./CustomAttribute");
18
+ const Property_1 = require("./Property");
19
+ const SchemaItem_1 = require("./SchemaItem");
20
+ /**
21
+ * A common abstract class for all of the ECClass types.
22
+ * @beta
23
+ */
24
+ class ECClass extends SchemaItem_1.SchemaItem {
25
+ constructor(schema, name, modifier) {
26
+ super(schema, name);
27
+ if (modifier)
28
+ this._modifier = modifier;
29
+ else
30
+ this._modifier = ECObjects_1.ECClassModifier.None;
31
+ }
32
+ get modifier() { return this._modifier; }
33
+ get baseClass() { return this._baseClass; }
34
+ set baseClass(baseClass) { this._baseClass = baseClass; }
35
+ get properties() { var _a; return (_a = this._properties) === null || _a === void 0 ? void 0 : _a.values(); }
36
+ get customAttributes() { return this._customAttributes; }
37
+ /**
38
+ * Convenience method for adding an already loaded ECProperty used by create*Property methods.
39
+ * @param prop The property to add.
40
+ * @return The property that was added.
41
+ */
42
+ addProperty(prop) {
43
+ if (!this._properties)
44
+ this._properties = new Map();
45
+ this._properties.set(prop.name.toUpperCase(), prop);
46
+ return prop;
47
+ }
48
+ /**
49
+ * Deletes a property from within this class.
50
+ * @param name The property name to delete, lookup is case-insensitive
51
+ * @alpha
52
+ */
53
+ async deleteProperty(name) {
54
+ if (this._properties) {
55
+ const property = await this.getProperty(name);
56
+ if (property)
57
+ this._properties.delete(name.toUpperCase());
58
+ }
59
+ }
60
+ /**
61
+ * Deletes a property from within this class.
62
+ * @param name The property name to delete, lookup is case-insensitive
63
+ * @alpha
64
+ */
65
+ deletePropertySync(name) {
66
+ if (this._properties) {
67
+ const property = this.getPropertySync(name);
68
+ if (property)
69
+ this._properties.delete(name.toUpperCase());
70
+ }
71
+ }
72
+ getBaseClassSync() {
73
+ if (!this.baseClass) {
74
+ return undefined;
75
+ }
76
+ return this.schema.lookupItemSync(this.baseClass);
77
+ }
78
+ /**
79
+ * Searches, case-insensitive, for a local ECProperty with the name provided.
80
+ * @param name
81
+ */
82
+ async getProperty(name, includeInherited = false) {
83
+ if (this._properties) {
84
+ const upperKey = name.toUpperCase();
85
+ const property = this._properties.get(upperKey);
86
+ if (property)
87
+ return property;
88
+ }
89
+ if (!includeInherited) {
90
+ return undefined;
91
+ }
92
+ return this.getInheritedProperty(name);
93
+ }
94
+ /**
95
+ * Searches, case-insensitive, for a local ECProperty with the name provided.
96
+ * @param name
97
+ */
98
+ getPropertySync(name, includeInherited = false) {
99
+ if (this._properties) {
100
+ const upperKey = name.toUpperCase();
101
+ const property = this._properties.get(upperKey);
102
+ if (property)
103
+ return property;
104
+ }
105
+ if (!includeInherited) {
106
+ return undefined;
107
+ }
108
+ return this.getInheritedPropertySync(name);
109
+ }
110
+ /**
111
+ * Searches the base class, if one exists, for the property with the name provided.
112
+ * @param name The name of the inherited property to find.
113
+ */
114
+ async getInheritedProperty(name) {
115
+ if (this.baseClass) {
116
+ const baseClassObj = await this.baseClass;
117
+ return baseClassObj.getProperty(name, true);
118
+ }
119
+ return undefined;
120
+ }
121
+ /**
122
+ * Searches the base class, if one exists, for the property with the name provided.
123
+ * @param name The name of the inherited property to find.
124
+ */
125
+ getInheritedPropertySync(name) {
126
+ const baseClassObj = this.getBaseClassSync();
127
+ if (baseClassObj)
128
+ return baseClassObj.getPropertySync(name, true);
129
+ return undefined;
130
+ }
131
+ async createPrimitiveProperty(name, primitiveType) {
132
+ if (await this.getProperty(name))
133
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
134
+ const propType = await this.loadPrimitiveType(primitiveType, this.schema);
135
+ if (typeof (propType) === "number")
136
+ return this.addProperty(new Property_1.PrimitiveProperty(this, name, propType));
137
+ return this.addProperty(new Property_1.EnumerationProperty(this, name, new DelayedPromise_1.DelayedPromiseWithProps(propType.key, async () => propType)));
138
+ }
139
+ createPrimitivePropertySync(name, primitiveType) {
140
+ if (this.getPropertySync(name))
141
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
142
+ const propType = this.loadPrimitiveTypeSync(primitiveType, this.schema);
143
+ if (typeof (propType) === "number")
144
+ return this.addProperty(new Property_1.PrimitiveProperty(this, name, propType));
145
+ return this.addProperty(new Property_1.EnumerationProperty(this, name, new DelayedPromise_1.DelayedPromiseWithProps(propType.key, async () => propType)));
146
+ }
147
+ async createPrimitiveArrayProperty(name, primitiveType) {
148
+ if (await this.getProperty(name))
149
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
150
+ const propType = await this.loadPrimitiveType(primitiveType, this.schema);
151
+ if (typeof (propType) === "number")
152
+ return this.addProperty(new Property_1.PrimitiveArrayProperty(this, name, propType));
153
+ return this.addProperty(new Property_1.EnumerationArrayProperty(this, name, new DelayedPromise_1.DelayedPromiseWithProps(propType.key, async () => propType)));
154
+ }
155
+ createPrimitiveArrayPropertySync(name, primitiveType) {
156
+ if (this.getPropertySync(name))
157
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
158
+ const propType = this.loadPrimitiveTypeSync(primitiveType, this.schema);
159
+ if (typeof (propType) === "number")
160
+ return this.addProperty(new Property_1.PrimitiveArrayProperty(this, name, propType));
161
+ return this.addProperty(new Property_1.EnumerationArrayProperty(this, name, new DelayedPromise_1.DelayedPromiseWithProps(propType.key, async () => propType)));
162
+ }
163
+ /**
164
+ *
165
+ * @param name The name of property to create.
166
+ * @param structType The struct type of property to create.
167
+ */
168
+ async createStructProperty(name, structType) {
169
+ if (await this.getProperty(name))
170
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
171
+ return this.addProperty(new Property_1.StructProperty(this, name, await this.loadStructType(structType, this.schema)));
172
+ }
173
+ /**
174
+ *
175
+ * @param name The name of property to create.
176
+ * @param structType The struct type of property to create.
177
+ */
178
+ createStructPropertySync(name, structType) {
179
+ if (this.getPropertySync(name))
180
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
181
+ return this.addProperty(new Property_1.StructProperty(this, name, this.loadStructTypeSync(structType, this.schema)));
182
+ }
183
+ /**
184
+ *
185
+ * @param name
186
+ * @param type
187
+ */
188
+ async createStructArrayProperty(name, structType) {
189
+ if (await this.getProperty(name))
190
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
191
+ return this.addProperty(new Property_1.StructArrayProperty(this, name, await this.loadStructType(structType, this.schema)));
192
+ }
193
+ /**
194
+ *
195
+ * @param name
196
+ * @param type
197
+ */
198
+ createStructArrayPropertySync(name, structType) {
199
+ if (this.getPropertySync(name))
200
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.DuplicateProperty, `An ECProperty with the name ${name} already exists in the class ${this.name}.`);
201
+ return this.addProperty(new Property_1.StructArrayProperty(this, name, this.loadStructTypeSync(structType, this.schema)));
202
+ }
203
+ async loadStructType(structType, schema) {
204
+ let correctType;
205
+ if (typeof (structType) === "string") {
206
+ correctType = await schema.lookupItem(structType);
207
+ }
208
+ else
209
+ correctType = structType;
210
+ if (!correctType)
211
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided Struct type, ${structType}, is not a valid StructClass.`);
212
+ return correctType;
213
+ }
214
+ loadStructTypeSync(structType, schema) {
215
+ let correctType;
216
+ if (typeof (structType) === "string") {
217
+ correctType = schema.lookupItemSync(structType);
218
+ }
219
+ else
220
+ correctType = structType;
221
+ if (!correctType)
222
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided Struct type, ${structType}, is not a valid StructClass.`);
223
+ return correctType;
224
+ }
225
+ async loadPrimitiveType(primitiveType, schema) {
226
+ if (primitiveType === undefined)
227
+ return ECObjects_1.PrimitiveType.Integer;
228
+ if (typeof (primitiveType) === "string") {
229
+ let resolvedType = (0, ECObjects_1.parsePrimitiveType)(primitiveType);
230
+ if (!resolvedType) {
231
+ resolvedType = await schema.lookupItem(primitiveType);
232
+ }
233
+ if (resolvedType === undefined)
234
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided primitive type, ${primitiveType}, is not a valid PrimitiveType or Enumeration.`);
235
+ // If resolvedType is a SchemaItem, make sure it is an Enumeration- if not, throw an error
236
+ if (typeof (resolvedType) !== "number" && resolvedType.schemaItemType !== ECObjects_1.SchemaItemType.Enumeration)
237
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided primitive type, ${primitiveType}, is not a valid PrimitiveType or Enumeration.`);
238
+ return resolvedType;
239
+ }
240
+ return primitiveType;
241
+ }
242
+ loadPrimitiveTypeSync(primitiveType, schema) {
243
+ if (primitiveType === undefined)
244
+ return ECObjects_1.PrimitiveType.Integer;
245
+ if (typeof (primitiveType) === "string") {
246
+ let resolvedType = (0, ECObjects_1.parsePrimitiveType)(primitiveType);
247
+ if (!resolvedType) {
248
+ resolvedType = schema.lookupItemSync(primitiveType);
249
+ }
250
+ if (resolvedType === undefined)
251
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidType, `The provided primitive type, ${primitiveType}, is not a valid PrimitiveType or Enumeration.`);
252
+ return resolvedType;
253
+ }
254
+ return primitiveType;
255
+ }
256
+ /**
257
+ * Save this Classes properties to an object for serializing to JSON.
258
+ * @param standalone Serialization includes only this object (as opposed to the full schema).
259
+ * @param includeSchemaVersion Include the Schema's version information in the serialized object.
260
+ */
261
+ toJSON(standalone = false, includeSchemaVersion = false) {
262
+ const schemaJson = super.toJSON(standalone, includeSchemaVersion);
263
+ const isMixin = ECObjects_1.SchemaItemType.Mixin === this.schemaItemType;
264
+ const isRelationship = ECObjects_1.SchemaItemType.RelationshipClass === this.schemaItemType;
265
+ if (!isMixin && (ECObjects_1.ECClassModifier.None !== this.modifier || isRelationship))
266
+ schemaJson.modifier = (0, ECObjects_1.classModifierToString)(this.modifier);
267
+ if (this.baseClass !== undefined)
268
+ schemaJson.baseClass = this.baseClass.fullName;
269
+ if (this._properties !== undefined && this._properties.size > 0)
270
+ schemaJson.properties = [...this.properties].map((prop) => prop.toJSON());
271
+ const customAttributes = (0, CustomAttribute_1.serializeCustomAttributes)(this.customAttributes);
272
+ if (customAttributes !== undefined)
273
+ schemaJson.customAttributes = customAttributes;
274
+ return schemaJson;
275
+ }
276
+ /** @internal */
277
+ async toXml(schemaXml) {
278
+ const itemElement = await super.toXml(schemaXml);
279
+ if (undefined !== this.modifier)
280
+ itemElement.setAttribute("modifier", (0, ECObjects_1.classModifierToString)(this.modifier));
281
+ if (undefined !== this.baseClass) {
282
+ const baseClass = await this.baseClass;
283
+ const baseClassElement = schemaXml.createElement("BaseClass");
284
+ const baseClassName = XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, baseClass.schema, baseClass.name);
285
+ baseClassElement.textContent = baseClassName;
286
+ itemElement.appendChild(baseClassElement);
287
+ }
288
+ if (undefined !== this.properties) {
289
+ for (const prop of this.properties) {
290
+ const propXml = await prop.toXml(schemaXml);
291
+ itemElement.appendChild(propXml);
292
+ }
293
+ }
294
+ if (this._customAttributes) {
295
+ const caContainerElement = schemaXml.createElement("ECCustomAttributes");
296
+ for (const [name, attribute] of this._customAttributes) {
297
+ const caElement = await XmlSerializationUtils_1.XmlSerializationUtils.writeCustomAttribute(name, attribute, schemaXml, this.schema);
298
+ caContainerElement.appendChild(caElement);
299
+ }
300
+ itemElement.appendChild(caContainerElement);
301
+ }
302
+ return itemElement;
303
+ }
304
+ fromJSONSync(classProps) {
305
+ super.fromJSONSync(classProps);
306
+ if (undefined !== classProps.modifier) {
307
+ const modifier = (0, ECObjects_1.parseClassModifier)(classProps.modifier);
308
+ if (undefined === modifier)
309
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidModifier, `The string '${classProps.modifier}' is not a valid ECClassModifier.`);
310
+ this._modifier = modifier;
311
+ }
312
+ if (undefined !== classProps.baseClass) {
313
+ const ecClassSchemaItemKey = this.schema.getSchemaItemKey(classProps.baseClass);
314
+ if (!ecClassSchemaItemKey)
315
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `Unable to locate the baseClass ${classProps.baseClass}.`);
316
+ this._baseClass = new DelayedPromise_1.DelayedPromiseWithProps(ecClassSchemaItemKey, async () => {
317
+ const baseClass = await this.schema.lookupItem(ecClassSchemaItemKey);
318
+ if (undefined === baseClass)
319
+ throw new Exception_1.ECObjectsError(Exception_1.ECObjectsStatus.InvalidECJson, `Unable to locate the baseClass ${classProps.baseClass}.`);
320
+ return baseClass;
321
+ });
322
+ }
323
+ }
324
+ async fromJSON(classProps) {
325
+ this.fromJSONSync(classProps);
326
+ }
327
+ addCustomAttribute(customAttribute) {
328
+ if (!this._customAttributes)
329
+ this._customAttributes = new Map();
330
+ this._customAttributes.set(customAttribute.className, customAttribute);
331
+ }
332
+ /**
333
+ * Iterates (recursively) over all base classes and mixins, in "property override" order.
334
+ * This is essentially a depth-first traversal through the inheritance tree.
335
+ */
336
+ async *getAllBaseClasses() {
337
+ const baseClasses = [this];
338
+ const addBaseClasses = async (ecClass) => {
339
+ if (ECObjects_1.SchemaItemType.EntityClass === ecClass.schemaItemType) {
340
+ for (let i = (ecClass).mixins.length - 1; i >= 0; i--) {
341
+ baseClasses.push(await (ecClass).mixins[i]);
342
+ }
343
+ }
344
+ if (ecClass.baseClass)
345
+ baseClasses.push(await ecClass.baseClass);
346
+ };
347
+ while (baseClasses.length > 0) {
348
+ const baseClass = baseClasses.pop();
349
+ await addBaseClasses(baseClass);
350
+ if (baseClass !== this)
351
+ yield baseClass;
352
+ }
353
+ }
354
+ *getAllBaseClassesSync() {
355
+ const baseClasses = [this];
356
+ const addBaseClasses = (ecClass) => {
357
+ if (ECObjects_1.SchemaItemType.EntityClass === ecClass.schemaItemType) {
358
+ for (const m of Array.from(ecClass.getMixinsSync()).reverse()) {
359
+ baseClasses.push(m);
360
+ }
361
+ }
362
+ const baseClass = ecClass.getBaseClassSync();
363
+ if (baseClass)
364
+ baseClasses.push(baseClass);
365
+ };
366
+ while (baseClasses.length > 0) {
367
+ const baseClass = baseClasses.pop();
368
+ addBaseClasses(baseClass);
369
+ if (baseClass !== this)
370
+ yield baseClass;
371
+ }
372
+ }
373
+ static mergeProperties(target, existingValues, propertiesToMerge, overwriteExisting) {
374
+ for (const property of propertiesToMerge) {
375
+ const upperCaseName = property.name.toUpperCase();
376
+ const existing = existingValues.get(upperCaseName);
377
+ if (existing !== undefined) {
378
+ if (overwriteExisting) {
379
+ target[existing] = property;
380
+ }
381
+ }
382
+ else {
383
+ existingValues.set(upperCaseName, target.length);
384
+ target.push(property);
385
+ }
386
+ }
387
+ }
388
+ async buildPropertyCache(result, existingValues, resetBaseCaches = false) {
389
+ if (!existingValues) {
390
+ existingValues = new Map();
391
+ }
392
+ if (this.baseClass) {
393
+ ECClass.mergeProperties(result, existingValues, await (await this.baseClass).getProperties(resetBaseCaches), false);
394
+ }
395
+ if (!this.properties)
396
+ return;
397
+ ECClass.mergeProperties(result, existingValues, [...this.properties], true);
398
+ }
399
+ buildPropertyCacheSync(result, existingValues, resetBaseCaches = false) {
400
+ if (!existingValues) {
401
+ existingValues = new Map();
402
+ }
403
+ const baseClass = this.getBaseClassSync();
404
+ if (baseClass) {
405
+ ECClass.mergeProperties(result, existingValues, baseClass.getPropertiesSync(resetBaseCaches), false);
406
+ }
407
+ if (!this.properties)
408
+ return;
409
+ ECClass.mergeProperties(result, existingValues, [...this.properties], true);
410
+ }
411
+ /**
412
+ * Iterates all properties, including the ones merged from base classes and mixins. To obtain only local properties, use the 'properties' field.
413
+ * Since this is an expensive operation, results will be cached after first call.
414
+ * @param resetCache if true, any previously cached results will be dropped and cache will be rebuilt
415
+ */
416
+ getPropertiesSync(resetCache = false) {
417
+ if (!this._mergedPropertyCache || resetCache) {
418
+ this._mergedPropertyCache = [];
419
+ this.buildPropertyCacheSync(this._mergedPropertyCache, undefined, resetCache);
420
+ }
421
+ return this._mergedPropertyCache;
422
+ }
423
+ /**
424
+ * Iterates all properties, including the ones merged from base classes and mixins. To obtain only local properties, use the 'properties' field.
425
+ * Since this is an expensive operation, results will be cached after first call.
426
+ * @param resetCache if true, any previously cached results will be dropped and cache will be rebuilt
427
+ */
428
+ async getProperties(resetCache = false) {
429
+ if (!this._mergedPropertyCache || resetCache) {
430
+ this._mergedPropertyCache = [];
431
+ await this.buildPropertyCache(this._mergedPropertyCache, undefined, resetCache);
432
+ }
433
+ return this._mergedPropertyCache;
434
+ }
435
+ /**
436
+ * Retrieve all custom attributes in the current class and its bases
437
+ * This is the async version of getCustomAttributesSync()
438
+ */
439
+ async getCustomAttributes() {
440
+ return this.getCustomAttributesSync();
441
+ }
442
+ /**
443
+ * Retrieve all custom attributes in the current class and its bases.
444
+ */
445
+ getCustomAttributesSync() {
446
+ let customAttributes = this._customAttributes;
447
+ if (undefined === customAttributes) {
448
+ customAttributes = new Map();
449
+ }
450
+ this.traverseBaseClassesSync((ecClass) => {
451
+ if (undefined === ecClass.customAttributes)
452
+ return false;
453
+ for (const [className, customAttribute] of ecClass.customAttributes) {
454
+ if (customAttributes.has(className))
455
+ continue;
456
+ customAttributes.set(className, customAttribute);
457
+ }
458
+ return false;
459
+ });
460
+ return customAttributes;
461
+ }
462
+ /**
463
+ * Asynchronously traverses through the inheritance tree, using depth-first traversal, calling the given callback
464
+ * function for each base class encountered.
465
+ * @param callback The function to call for each base class in the hierarchy.
466
+ * @param arg An argument that will be passed as the second parameter to the callback function.
467
+ */
468
+ async traverseBaseClasses(callback, arg) {
469
+ for await (const baseClass of this.getAllBaseClasses()) {
470
+ if (callback(baseClass, arg))
471
+ return true;
472
+ }
473
+ return false;
474
+ }
475
+ /**
476
+ * Synchronously traverses through the inheritance tree, using depth-first traversal, calling the given callback
477
+ * function for each base class encountered.
478
+ * @param callback The function to call for each base class in the hierarchy.
479
+ * @param arg An argument that will be passed as the second parameter to the callback function.
480
+ */
481
+ traverseBaseClassesSync(callback, arg) {
482
+ const baseClasses = this.getAllBaseClassesSync();
483
+ if (!baseClasses)
484
+ return false;
485
+ for (const baseClass of baseClasses) {
486
+ if (callback(baseClass, arg))
487
+ return true;
488
+ }
489
+ return false;
490
+ }
491
+ async is(targetClass, schemaName) {
492
+ if (schemaName !== undefined) {
493
+ (0, core_bentley_1.assert)(typeof (targetClass) === "string", "Expected targetClass of type string because schemaName was specified");
494
+ const key = new SchemaKey_1.SchemaItemKey(targetClass, new SchemaKey_1.SchemaKey(schemaName));
495
+ if (SchemaItem_1.SchemaItem.equalByKey(this, key))
496
+ return true;
497
+ return this.traverseBaseClasses(SchemaItem_1.SchemaItem.equalByKey, key);
498
+ }
499
+ else {
500
+ (0, core_bentley_1.assert)(ECClass.isECClass(targetClass), "Expected targetClass to be of type ECClass");
501
+ if (SchemaItem_1.SchemaItem.equalByKey(this, targetClass))
502
+ return true;
503
+ return this.traverseBaseClasses(SchemaItem_1.SchemaItem.equalByKey, targetClass);
504
+ }
505
+ }
506
+ /**
507
+ * A synchronous version of the [[ECClass.is]], indicating if the targetClass is of this type.
508
+ * @param targetClass The class to check.
509
+ */
510
+ isSync(targetClass) {
511
+ if (SchemaItem_1.SchemaItem.equalByKey(this, targetClass))
512
+ return true;
513
+ return this.traverseBaseClassesSync(SchemaItem_1.SchemaItem.equalByKey, targetClass);
514
+ }
515
+ /**
516
+ * @internal
517
+ */
518
+ static isECClass(object) {
519
+ if (!SchemaItem_1.SchemaItem.isSchemaItem(object))
520
+ return false;
521
+ return object.schemaItemType === ECObjects_1.SchemaItemType.EntityClass || object.schemaItemType === ECObjects_1.SchemaItemType.Mixin || object.schemaItemType === ECObjects_1.SchemaItemType.RelationshipClass ||
522
+ object.schemaItemType === ECObjects_1.SchemaItemType.StructClass || object.schemaItemType === ECObjects_1.SchemaItemType.CustomAttributeClass;
523
+ }
524
+ /**
525
+ * @alpha
526
+ * A setter method used specifically for schema editing.
527
+ * @param modifier
528
+ */
529
+ setModifier(modifier) {
530
+ this._modifier = modifier;
531
+ }
532
+ }
533
+ exports.ECClass = ECClass;
534
+ /**
535
+ * A Typescript class representation of an ECStructClass.
536
+ * @beta
537
+ */
538
+ class StructClass extends ECClass {
539
+ constructor(schema, name, modifier) {
540
+ super(schema, name, modifier);
541
+ this.schemaItemType = ECObjects_1.SchemaItemType.StructClass;
542
+ }
543
+ }
544
+ exports.StructClass = StructClass;
545
+ /**
546
+ * @internal
547
+ * An abstract class used for schema editing.
548
+ */
549
+ class MutableStructClass extends StructClass {
550
+ }
551
+ exports.MutableStructClass = MutableStructClass;
552
+ /**
553
+ * Hackish approach that works like a "friend class" so we can access protected members without making them public.
554
+ * @internal
555
+ */
556
+ class MutableClass extends ECClass {
557
+ }
558
+ exports.MutableClass = MutableClass;
559
559
  //# sourceMappingURL=Class.js.map