/
ECObjects.ts
452 lines (412 loc) · 16.9 KB
/
ECObjects.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Metadata
*/
import { ECStringConstants } from "./Constants";
import { ECObjectsError, ECObjectsStatus } from "./Exception";
export { PropertyType } from "./PropertyTypes";
// NEEDSWORK: This shim is currently required to use async iterators. See https://github.com/Microsoft/TypeScript/issues/14151
if (!(Symbol as any).asyncIterator) {
(Symbol as any).asyncIterator = Symbol.for("Symbol.asyncIterator");
}
/** @beta */
export enum ECClassModifier {
None,
Abstract,
Sealed,
}
/**
* An enumeration that has all the schema item type names as values
* @beta */
export enum SchemaItemType {
EntityClass = "EntityClass",
Mixin = "Mixin",
StructClass = "StructClass",
CustomAttributeClass = "CustomAttributeClass",
RelationshipClass = "RelationshipClass",
Enumeration = "Enumeration",
KindOfQuantity = "KindOfQuantity",
PropertyCategory = "PropertyCategory",
Unit = "Unit",
InvertedUnit = "InvertedUnit",
Constant = "Constant",
Phenomenon = "Phenomenon",
UnitSystem = "UnitSystem",
Format = "Format",
}
/**
* Primitive data types for ECProperties.
* @beta
*/
export enum PrimitiveType {
Uninitialized = 0x00,
Binary = 0x101,
Boolean = 0x201,
DateTime = 0x301,
Double = 0x401,
Integer = 0x501,
Long = 0x601,
Point2d = 0x701,
Point3d = 0x801,
String = 0x901,
IGeometry = 0xa01,
}
/**
* Defines the valid CustomAttribute container types.
* @beta
*/
export enum CustomAttributeContainerType {
Schema = (0x0001 << 0),
EntityClass = (0x0001 << 1),
CustomAttributeClass = (0x0001 << 2),
StructClass = (0x0001 << 3),
RelationshipClass = (0x0001 << 4),
AnyClass = EntityClass | CustomAttributeClass | StructClass | RelationshipClass,
PrimitiveProperty = (0x0001 << 5),
StructProperty = (0x0001 << 6),
PrimitiveArrayProperty = (0x0001 << 7),
StructArrayProperty = (0x0001 << 8),
NavigationProperty = (0x0001 << 9),
AnyProperty = PrimitiveProperty | StructProperty | PrimitiveArrayProperty | StructArrayProperty | NavigationProperty,
SourceRelationshipConstraint = (0x0001 << 10),
TargetRelationshipConstraint = (0x0001 << 11),
AnyRelationshipConstraint = SourceRelationshipConstraint | TargetRelationshipConstraint,
Any = Schema | AnyClass | AnyProperty | AnyRelationshipConstraint,
}
/**
* Defines what sort of match should be used when locating a schema.
* @beta
*/
export enum SchemaMatchType {
// Find exact VersionRead, VersionWrite, VersionMinor match as well as Data
Identical,
// Find exact VersionRead, VersionWrite, VersionMinor match.
Exact,
// Find latest version with matching VersionRead and VersionWrite
LatestWriteCompatible,
// Find latest version.
Latest,
// Find latest version with matching VersionRead
LatestReadCompatible,
}
/**
* Identifer for an ECRelationshipConstraint. Used to determine the side of the relationship the constraint is representing.
* @beta
*/
export enum RelationshipEnd {
Source = 0,
Target = 1,
}
/** @beta */
export enum StrengthType {
Referencing,
Holding,
Embedding,
}
/** @beta */
export enum StrengthDirection {
Forward = 1,
Backward = 2,
}
// Helper methods for going to/from string for the above Enumerations.
/**
* Parses the provided string into an ECClassModifier if the string is a valid modifier.
* @param modifier The modifier string to parse.
* @beta
*/
export function parseClassModifier(modifier: string): ECClassModifier | undefined {
const lowerModifier = modifier.toLowerCase();
switch (lowerModifier) {
case "abstract": return ECClassModifier.Abstract;
case "none": return ECClassModifier.None;
case "sealed": return ECClassModifier.Sealed;
}
return undefined;
}
/**
* @return A string representing the provided ECClassModifier. If the modifier is not valid, an empty string is returned.
* @beta
*/
export function classModifierToString(modifier: ECClassModifier): string {
switch (modifier) {
case ECClassModifier.Abstract: return "Abstract";
case ECClassModifier.None: return "None";
case ECClassModifier.Sealed: return "Sealed";
default: throw new ECObjectsError(ECObjectsStatus.InvalidModifier, "An invalid ECClassModifier has been provided.");
}
}
/**
* Tries to parse the given string as one of the 8 schema item types.
* @param type The schema item type string to parse.
* @returns A valid SchemaItemType if successfully parsed. Otherwise, undefined if the provided string is not a valid SchemaItemType.
* @beta
*/
export function parseSchemaItemType(type: string): SchemaItemType | undefined {
switch (type.toLowerCase()) {
case "entityclass": return SchemaItemType.EntityClass;
case "mixin": return SchemaItemType.Mixin;
case "structclass": return SchemaItemType.StructClass;
case "customattributeclass": return SchemaItemType.CustomAttributeClass;
case "relationshipclass": return SchemaItemType.RelationshipClass;
case "enumeration": return SchemaItemType.Enumeration;
case "kindofquantity": return SchemaItemType.KindOfQuantity;
case "propertycategory": return SchemaItemType.PropertyCategory;
case "unit": return SchemaItemType.Unit;
case "invertedunit": return SchemaItemType.InvertedUnit;
case "constant": return SchemaItemType.Constant;
case "phenomenon": return SchemaItemType.Phenomenon;
case "unitsystem": return SchemaItemType.UnitSystem;
case "format": return SchemaItemType.Format;
}
return undefined;
}
/**
* Converts a valid SchemaItemType to a display string.
* @param value The SchemaItemType to stringify.
* @return A string representing the provided SchemaItemType. If the type is not valid, an empty string is returned.
* @beta
* @deprecated in 4.6.0 SchemaItemType is a string enum so just use it directly
*/
export function schemaItemTypeToString(value: SchemaItemType): string {
return value; // TODO: Remove
}
/** @internal */
export function schemaItemTypeToXmlString(value: SchemaItemType): string {
switch (value) {
case SchemaItemType.EntityClass: return "ECEntityClass";
case SchemaItemType.Mixin: return "ECEntityClass";
case SchemaItemType.StructClass: return "ECStructClass";
case SchemaItemType.CustomAttributeClass: return "ECCustomAttributeClass";
case SchemaItemType.RelationshipClass: return "ECRelationshipClass";
case SchemaItemType.Enumeration: return "ECEnumeration";
case SchemaItemType.KindOfQuantity: return "KindOfQuantity";
case SchemaItemType.PropertyCategory: return "PropertyCategory";
case SchemaItemType.Unit: return "Unit";
case SchemaItemType.InvertedUnit: return "InvertedUnit";
case SchemaItemType.Constant: return "Constant";
case SchemaItemType.Phenomenon: return "Phenomenon";
case SchemaItemType.UnitSystem: return "UnitSystem";
case SchemaItemType.Format: return "Format";
default: throw new ECObjectsError(ECObjectsStatus.InvalidSchemaItemType, "An invalid SchemaItemType has been provided.");
}
}
/**
* Tries to parse the given string as one of the 10 primitive types.
* @param type The primitive type string to parse.
* @returns A valid PrimitiveType if successfully parsed, or undefined if the provided string is not a valid PrimitiveType.
* @beta
*/
export function parsePrimitiveType(type: string): PrimitiveType | undefined {
switch (type.toLowerCase()) {
case "binary": return PrimitiveType.Binary;
case "boolean": case "bool": return PrimitiveType.Boolean;
case "datetime": return PrimitiveType.DateTime;
case "double": return PrimitiveType.Double;
case "int": return PrimitiveType.Integer;
case "long": return PrimitiveType.Long;
case "point2d": return PrimitiveType.Point2d;
case "point3d": return PrimitiveType.Point3d;
case "string": return PrimitiveType.String;
case "bentley.geometry.common.igeometry": return PrimitiveType.IGeometry;
}
return undefined;
}
/** @beta */
export function primitiveTypeToString(type: PrimitiveType): string {
switch (type) {
case PrimitiveType.Binary: return "binary";
case PrimitiveType.Boolean: return "boolean";
case PrimitiveType.DateTime: return "dateTime";
case PrimitiveType.Double: return "double";
case PrimitiveType.Integer: return "int";
case PrimitiveType.IGeometry: return "Bentley.Geometry.Common.IGeometry";
case PrimitiveType.Long: return "long";
case PrimitiveType.Point2d: return "point2d";
case PrimitiveType.Point3d: return "point3d";
case PrimitiveType.String: return "string";
default: throw new ECObjectsError(ECObjectsStatus.InvalidPrimitiveType, "An invalid PrimitiveType has been provided.");
}
}
/**
* Parses the given string into the appropriate CustomAttributeContainerType if the string is valid.
* @param type The container type string to parse.
* @beta
*/
export function parseCustomAttributeContainerType(type: string): CustomAttributeContainerType | undefined {
const typeTokens = type.split(/[|,;]+/);
let containerType = 0;
typeTokens.forEach((typeToken) => {
typeToken = typeToken.trim();
if (typeToken.length === 0)
return;
typeToken = typeToken.toLowerCase();
switch (typeToken) {
case "schema":
containerType = containerType | CustomAttributeContainerType.Schema;
break;
case "entityclass":
containerType = containerType | CustomAttributeContainerType.EntityClass;
break;
case "customattributeclass":
containerType = containerType | CustomAttributeContainerType.CustomAttributeClass;
break;
case "structclass":
containerType = containerType | CustomAttributeContainerType.StructClass;
break;
case "relationshipclass":
containerType = containerType | CustomAttributeContainerType.RelationshipClass;
break;
case "anyclass":
containerType = containerType | CustomAttributeContainerType.AnyClass;
break;
case "primitiveproperty":
containerType = containerType | CustomAttributeContainerType.PrimitiveProperty;
break;
case "structproperty":
containerType = containerType | CustomAttributeContainerType.StructProperty;
break;
case "arrayproperty":
containerType = containerType | CustomAttributeContainerType.PrimitiveArrayProperty;
break;
case "structarrayproperty":
containerType = containerType | CustomAttributeContainerType.StructArrayProperty;
break;
case "navigationproperty":
containerType = containerType | CustomAttributeContainerType.NavigationProperty;
break;
case "anyproperty":
containerType = containerType | CustomAttributeContainerType.AnyProperty;
break;
case "sourcerelationshipconstraint":
containerType = containerType | CustomAttributeContainerType.SourceRelationshipConstraint;
break;
case "targetrelationshipconstraint":
containerType = containerType | CustomAttributeContainerType.TargetRelationshipConstraint;
break;
case "anyrelationshipconstraint":
containerType = containerType | CustomAttributeContainerType.AnyRelationshipConstraint;
break;
case "any":
containerType = containerType | CustomAttributeContainerType.Any;
break;
default:
throw new ECObjectsError(ECObjectsStatus.InvalidContainerType, `${typeToken} is not a valid CustomAttributeContainerType value.`);
}
});
return containerType as CustomAttributeContainerType;
}
/**
* Creates a string representing a valid CustomAttributeContainerType.
* @param value The CustomAttributeContainerType to stringify.
* @return A string representing the provided CustomAttributeContainerType. If the type is not valid, an empty string is returned.
* @beta
*/
export function containerTypeToString(type: CustomAttributeContainerType): string {
const testContainerTypeValue = (compareType: CustomAttributeContainerType, otherType: CustomAttributeContainerType) => {
return (compareType === (compareType & otherType));
};
if (testContainerTypeValue(CustomAttributeContainerType.Any, type))
return ECStringConstants.CONTAINERTYPE_ANY;
let containerType = "";
const setOrAppend = (val: string) => {
if (containerType.length === 0)
containerType = val;
else
containerType = `${containerType}, ${val}`;
};
if (testContainerTypeValue(CustomAttributeContainerType.Schema, type))
setOrAppend("Schema");
if (testContainerTypeValue(CustomAttributeContainerType.AnyClass, type))
setOrAppend("AnyClass");
else {
if (testContainerTypeValue(CustomAttributeContainerType.EntityClass, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_ENTITYCLASS);
if (testContainerTypeValue(CustomAttributeContainerType.CustomAttributeClass, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_CUSTOMATTRIBUTECLASS);
if (testContainerTypeValue(CustomAttributeContainerType.StructClass, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_STRUCTCLASS);
if (testContainerTypeValue(CustomAttributeContainerType.RelationshipClass, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_RELATIONSHIPCLASS);
}
if (testContainerTypeValue(CustomAttributeContainerType.AnyProperty, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_ANYPROPERTY);
else {
if (testContainerTypeValue(CustomAttributeContainerType.PrimitiveProperty, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_PRIMITIVEPROPERTY);
if (testContainerTypeValue(CustomAttributeContainerType.StructProperty, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_STRUCTPROPERTY);
if (testContainerTypeValue(CustomAttributeContainerType.PrimitiveArrayProperty, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_PRIMITIVEARRAYPROPERTY);
if (testContainerTypeValue(CustomAttributeContainerType.StructArrayProperty, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_STRUCTARRAYPROPERTY);
if (testContainerTypeValue(CustomAttributeContainerType.NavigationProperty, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_NAVIGATIONPROPERTY);
}
if (testContainerTypeValue(CustomAttributeContainerType.AnyRelationshipConstraint, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_ANYRELATIONSHIPCONSTRAINT);
else {
if (testContainerTypeValue(CustomAttributeContainerType.SourceRelationshipConstraint, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_SOURCERELATIONSHIPCONSTRAINT);
if (testContainerTypeValue(CustomAttributeContainerType.TargetRelationshipConstraint, type))
setOrAppend(ECStringConstants.CONTAINERTYPE_TARGETRELATIONSHIPCONSTRAINT);
}
return containerType;
}
/** @beta */
export function parseRelationshipEnd(end: string): RelationshipEnd | undefined {
switch (end.toLowerCase()) {
case "source": return RelationshipEnd.Source;
case "target": return RelationshipEnd.Target;
}
return undefined;
}
/** @beta */
export function relationshipEndToString(end: RelationshipEnd): string {
switch (end) {
case RelationshipEnd.Source: return ECStringConstants.RELATIONSHIP_END_SOURCE;
case RelationshipEnd.Target: return ECStringConstants.RELATIONSHIP_END_TARGET;
default: throw new ECObjectsError(ECObjectsStatus.InvalidRelationshipEnd, `An invalid RelationshipEnd has been provided.`);
}
}
/**
* Takes a string representing a StrengthType, will parse it and return the corresponding StrengthType.
* @throws ECObjectsStatus.InvalidStrength if the provided string that is not valid
* @param strength
* @beta
*/
export function parseStrength(strength: string): StrengthType | undefined {
switch (strength.toLowerCase()) {
case "referencing": return StrengthType.Referencing;
case "holding": return StrengthType.Holding;
case "embedding": return StrengthType.Embedding;
}
return undefined;
}
/** @beta */
export function strengthToString(strength: StrengthType): string {
switch (strength) {
case StrengthType.Embedding: return "Embedding";
case StrengthType.Holding: return "Holding";
case StrengthType.Referencing: return "Referencing";
default: throw new ECObjectsError(ECObjectsStatus.InvalidStrength, `An invalid Strength has been provided.`);
}
}
/** @beta */
export function parseStrengthDirection(direction: string): StrengthDirection | undefined {
switch (direction.toLowerCase()) {
case "forward": return StrengthDirection.Forward;
case "backward": return StrengthDirection.Backward;
}
return undefined;
}
/** @beta */
export function strengthDirectionToString(direction: StrengthDirection): string {
switch (direction) {
case StrengthDirection.Forward: return "Forward";
case StrengthDirection.Backward: return "Backward";
default: throw new ECObjectsError(ECObjectsStatus.InvalidStrengthDirection, `An invalid StrengthDirection has been provided.`);
}
}