/
Property.ts
727 lines (607 loc) · 26 KB
/
Property.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*---------------------------------------------------------------------------------------------
* 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 { DelayedPromiseWithProps } from "../DelayedPromise";
import {
ArrayPropertyProps, EnumerationPropertyProps, NavigationPropertyProps, PrimitiveArrayPropertyProps, PrimitiveOrEnumPropertyBaseProps,
PrimitivePropertyProps, PropertyProps, StructPropertyProps,
} from "../Deserialization/JsonProps";
import { XmlSerializationUtils } from "../Deserialization/XmlSerializationUtils";
import { parsePrimitiveType, PrimitiveType, primitiveTypeToString, StrengthDirection, strengthDirectionToString } from "../ECObjects";
import { ECObjectsError, ECObjectsStatus } from "../Exception";
import { AnyClass, LazyLoadedEnumeration, LazyLoadedKindOfQuantity, LazyLoadedPropertyCategory, LazyLoadedRelationshipClass } from "../Interfaces";
import { PropertyType, propertyTypeToString, PropertyTypeUtils } from "../PropertyTypes";
import { SchemaItemKey } from "../SchemaKey";
import { ECName } from "../ECName";
import { ECClass, StructClass } from "./Class";
import { CustomAttribute, CustomAttributeContainerProps, CustomAttributeSet, serializeCustomAttributes } from "./CustomAttribute";
import { Enumeration } from "./Enumeration";
import { KindOfQuantity } from "./KindOfQuantity";
import { PropertyCategory } from "./PropertyCategory";
import { RelationshipClass } from "./RelationshipClass";
import { Schema } from "./Schema";
/**
* A common abstract class for all ECProperty types.
* @beta
*/
export abstract class Property implements CustomAttributeContainerProps {
protected _name: ECName;
protected _type: PropertyType;
protected _class: AnyClass; // TODO: class seems to be unused?
protected _description?: string;
protected _label?: string;
protected _isReadOnly?: boolean;
protected _priority?: number;
protected _category?: LazyLoadedPropertyCategory;
protected _kindOfQuantity?: LazyLoadedKindOfQuantity;
private _customAttributes?: Map<string, CustomAttribute>;
constructor(ecClass: ECClass, name: string, type: PropertyType) {
this._class = ecClass as AnyClass;
this._name = new ECName(name);
this._type = type;
}
public isArray(): this is AnyArrayProperty { return PropertyTypeUtils.isArray(this._type); }
public isPrimitive(): this is AnyPrimitiveProperty { return PropertyTypeUtils.isPrimitive(this._type); }
public isStruct(): this is AnyStructProperty { return PropertyTypeUtils.isStruct(this._type); }
public isEnumeration(): this is AnyEnumerationProperty { return PropertyTypeUtils.isEnumeration(this._type); }
public isNavigation(): this is NavigationProperty { return PropertyTypeUtils.isNavigation(this._type); }
public get name() { return this._name.name; }
public get class() { return this._class; }
public get label() { return this._label; }
public get description() { return this._description; }
public get isReadOnly() { return this._isReadOnly || false; }
public get priority() { return this._priority || 0; }
public get category(): LazyLoadedPropertyCategory | undefined { return this._category; }
public get kindOfQuantity(): LazyLoadedKindOfQuantity | undefined { return this._kindOfQuantity; }
public get propertyType() { return this._type; }
public get customAttributes(): CustomAttributeSet | undefined { return this._customAttributes; }
/** Returns the name in the format 'ClassName.PropertyName'. */
public get fullName(): string { return `${this._class.name}.${this.name}`; }
/** Returns the schema of the class holding the property. */
public get schema(): Schema { return this._class.schema; }
public getCategorySync(): PropertyCategory | undefined {
if (!this._category)
return undefined;
return this.class.schema.lookupItemSync(this._category);
}
public getKindOfQuantitySync(): KindOfQuantity | undefined {
if (!this._kindOfQuantity)
return undefined;
return this.class.schema.lookupItemSync(this._kindOfQuantity);
}
/**
* Save this Property's properties to an object for serializing to JSON.
*/
public toJSON(): PropertyProps {
const schemaJson: any = {};
schemaJson.name = this.name;
schemaJson.type = propertyTypeToString(this._type);
if (this.description !== undefined)
schemaJson.description = this.description;
if (this.label !== undefined)
schemaJson.label = this.label;
if (this._isReadOnly !== undefined)
schemaJson.isReadOnly = this._isReadOnly;
if (this.category !== undefined)
schemaJson.category = this.category.fullName; // needs to be fully qualified name
if (this._priority !== undefined)
schemaJson.priority = this._priority;
if (this.kindOfQuantity !== undefined)
schemaJson.kindOfQuantity = this.kindOfQuantity.fullName;
const customAttributes = serializeCustomAttributes(this.customAttributes);
if (customAttributes !== undefined)
schemaJson.customAttributes = customAttributes;
return schemaJson;
}
/** @internal */
public async toXml(schemaXml: Document): Promise<Element> {
const propType = `EC${propertyTypeToString(this._type)}`.replace("Primitive", "");
const itemElement = schemaXml.createElement(propType);
itemElement.setAttribute("propertyName", this.name);
if (undefined !== this.description)
itemElement.setAttribute("description", this.description);
if (undefined !== this.label)
itemElement.setAttribute("displayLabel", this.label);
if (undefined !== this.isReadOnly)
itemElement.setAttribute("readOnly", String(this.isReadOnly));
if (undefined !== this.category) {
const category = await this.category;
const categoryName = XmlSerializationUtils.createXmlTypedName(this.schema, category.schema, category.name);
itemElement.setAttribute("category", categoryName);
}
if (undefined !== this.priority)
itemElement.setAttribute("priority", this.priority.toString());
if (undefined !== this.kindOfQuantity) {
const kindOfQuantity = await this.kindOfQuantity;
const kindOfQuantityName = XmlSerializationUtils.createXmlTypedName(this.schema, kindOfQuantity.schema, kindOfQuantity.name);
itemElement.setAttribute("kindOfQuantity", kindOfQuantityName);
}
if (this._customAttributes) {
const caContainerElement = schemaXml.createElement("ECCustomAttributes");
for (const [name, attribute] of this._customAttributes) {
const caElement = await XmlSerializationUtils.writeCustomAttribute(name, attribute, schemaXml, this.schema);
caContainerElement.appendChild(caElement);
}
itemElement.appendChild(caContainerElement);
}
return itemElement;
}
public fromJSONSync(propertyProps: PropertyProps) {
if (undefined !== propertyProps.label) {
this._label = propertyProps.label;
}
if (undefined !== propertyProps.description) {
this._description = propertyProps.description;
}
if (undefined !== propertyProps.priority) {
this._priority = propertyProps.priority;
}
if (undefined !== propertyProps.isReadOnly) {
this._isReadOnly = propertyProps.isReadOnly;
}
if (undefined !== propertyProps.category) {
const propertyCategorySchemaItemKey = this.class.schema.getSchemaItemKey(propertyProps.category);
if (!propertyCategorySchemaItemKey)
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `The Property ${this.name} has a 'category' ("${propertyProps.category}") that cannot be found.`);
this._category = new DelayedPromiseWithProps<SchemaItemKey, PropertyCategory>(propertyCategorySchemaItemKey,
async () => {
const category = await this.class.schema.lookupItem<PropertyCategory>(propertyCategorySchemaItemKey);
if (undefined === category)
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `The Property ${this.name} has a 'category' ("${propertyProps.category}") that cannot be found.`);
return category;
});
}
if (undefined !== propertyProps.kindOfQuantity) {
const koqSchemaItemKey = this.class.schema.getSchemaItemKey(propertyProps.kindOfQuantity);
if (!koqSchemaItemKey)
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `The Property ${this.name} has a 'kindOfQuantity' ("${propertyProps.kindOfQuantity}") that cannot be found.`);
this._kindOfQuantity = new DelayedPromiseWithProps<SchemaItemKey, KindOfQuantity>(koqSchemaItemKey,
async () => {
const koq = await this.class.schema.lookupItem<KindOfQuantity>(koqSchemaItemKey);
if (undefined === koq)
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `The Property ${this.name} has a 'kindOfQuantity' ("${propertyProps.kindOfQuantity}") that cannot be found.`);
return koq;
});
}
}
public async fromJSON(propertyProps: PropertyProps) {
this.fromJSONSync(propertyProps);
}
protected addCustomAttribute(customAttribute: CustomAttribute) {
if (!this._customAttributes)
this._customAttributes = new Map<string, CustomAttribute>();
this._customAttributes.set(customAttribute.className, customAttribute);
}
protected setName(name: ECName) {
this._name = name;
}
/**
* @internal Used in schema editing.
*/
protected setDescription(description: string) {
this._description = description;
}
/**
* @internal Used in schema editing.
*/
protected setLabel(label: string) {
this._label = label;
}
/**
* @internal Used in schema editing.
*/
protected setIsReadOnly(isReadOnly: boolean) {
this._isReadOnly = isReadOnly;
}
/**
* @internal Used in schema editing.
*/
protected setPriority(priority: number) {
this._priority = priority;
}
/**
* @internal Used in schema editing.
*/
protected setCategory(category: LazyLoadedPropertyCategory) {
this._category = category;
}
/**
* Retrieve all custom attributes in the current property and its base
* This is the async version of getCustomAttributesSync()
*/
public async getCustomAttributes(): Promise<CustomAttributeSet> {
return this.getCustomAttributesSync();
}
/**
* Retrieve all custom attributes in the current property and its base.
*/
public getCustomAttributesSync(): CustomAttributeSet {
let customAttributes: Map<string, CustomAttribute> | undefined = this._customAttributes;
if (undefined === customAttributes) {
customAttributes = new Map<string, CustomAttribute>();
}
const baseProperty = this.class.getInheritedPropertySync(this.name);
let baseCustomAttributes;
if (undefined !== baseProperty)
baseCustomAttributes = baseProperty.getCustomAttributesSync();
if (undefined !== baseCustomAttributes) {
customAttributes = new Map<string, CustomAttribute>([...baseCustomAttributes, ...customAttributes]);
}
return customAttributes;
}
/**
* @internal
*/
public static isProperty(object: any): object is AnyProperty {
const property = object as Property;
return property !== undefined && property.class !== undefined && property.name !== undefined
&& property.propertyType !== undefined;
}
}
/** @beta */
export abstract class PrimitiveOrEnumPropertyBase extends Property {
protected _extendedTypeName?: string;
protected _minLength?: number;
protected _maxLength?: number;
protected _minValue?: number;
protected _maxValue?: number;
public get extendedTypeName() { return this._extendedTypeName; }
public get minLength() { return this._minLength; }
public get maxLength() { return this._maxLength; }
public get minValue() { return this._minValue; }
public get maxValue() { return this._maxValue; }
constructor(ecClass: ECClass, name: string, type: PropertyType) {
super(ecClass, name, type);
}
/**
* Save this PrimitiveOrEnumPropertyBase's properties to an object for serializing to JSON.
*/
public override toJSON(): PrimitiveOrEnumPropertyBaseProps {
const schemaJson = super.toJSON() as any;
if (this.extendedTypeName !== undefined)
schemaJson.extendedTypeName = this.extendedTypeName;
if (this._minLength !== undefined)
schemaJson.minLength = this.minLength;
if (this._maxLength !== undefined)
schemaJson.maxLength = this.maxLength;
if (this._minValue !== undefined)
schemaJson.minValue = this.minValue;
if (this._maxValue !== undefined)
schemaJson.maxValue = this.maxValue;
return schemaJson;
}
/** @internal */
public override async toXml(schemaXml: Document): Promise<Element> {
const itemElement = await super.toXml(schemaXml);
if (undefined !== this.extendedTypeName)
itemElement.setAttribute("extendedTypeName", this.extendedTypeName);
if (undefined !== this.minValue)
itemElement.setAttribute("minimumValue", this.minValue.toString());
if (undefined !== this.maxValue)
itemElement.setAttribute("maximumValue", this.maxValue.toString());
if (undefined !== this.minLength)
itemElement.setAttribute("minimumLength", this.minLength.toString());
if (undefined !== this.maxLength)
itemElement.setAttribute("maximumLength", this.maxLength.toString());
return itemElement;
}
public override fromJSONSync(propertyBaseProps: PrimitiveOrEnumPropertyBaseProps) {
super.fromJSONSync(propertyBaseProps);
if (undefined !== propertyBaseProps.minLength) {
this._minLength = propertyBaseProps.minLength;
}
if (undefined !== propertyBaseProps.maxLength) {
this._maxLength = propertyBaseProps.maxLength;
}
if (undefined !== propertyBaseProps.minValue) {
this._minValue = propertyBaseProps.minValue;
}
if (undefined !== propertyBaseProps.maxValue) {
this._maxValue = propertyBaseProps.maxValue;
}
if (undefined !== propertyBaseProps.extendedTypeName) {
this._extendedTypeName = propertyBaseProps.extendedTypeName;
}
}
/**
* @internal Used in schema editing.
*/
protected setExtendedTypeName(extendedTypeName: string) {
this._extendedTypeName = extendedTypeName;
}
/**
* @internal Used in schema editing.
*/
protected setMinLength(minLength: number) {
this._minLength = minLength;
}
/**
* @internal Used in schema editing.
*/
protected setMaxLength(maxLength: number) {
this._maxLength = maxLength;
}
/**
* @internal Used in schema editing.
*/
protected setMinValue(minValue: number) {
this._minValue = minValue;
}
/**
* @internal Used in schema editing.
*/
protected setMaxValue(maxValue: number) {
this._maxValue = maxValue;
}
public override async fromJSON(propertyBaseProps: PrimitiveOrEnumPropertyBaseProps) {
this.fromJSONSync(propertyBaseProps);
}
}
/** @beta */
export class PrimitiveProperty extends PrimitiveOrEnumPropertyBase {
public get primitiveType(): PrimitiveType { return PropertyTypeUtils.getPrimitiveType(this._type); }
constructor(ecClass: ECClass, name: string, primitiveType: PrimitiveType = PrimitiveType.Integer) {
super(ecClass, name, PropertyTypeUtils.fromPrimitiveType(primitiveType));
}
public override fromJSONSync(primitivePropertyProps: PrimitivePropertyProps) {
super.fromJSONSync(primitivePropertyProps);
if (undefined !== primitivePropertyProps.typeName) {
if (this.primitiveType !== parsePrimitiveType(primitivePropertyProps.typeName))
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, ``);
}
}
public override async fromJSON(primitivePropertyProps: PrimitivePropertyProps) {
this.fromJSONSync(primitivePropertyProps);
}
/**
* Save this PrimitiveProperty's properties to an object for serializing to JSON.
*/
public override toJSON(): PrimitivePropertyProps {
const schemaJson = super.toJSON() as any;
schemaJson.typeName = primitiveTypeToString(this.primitiveType);
return schemaJson;
}
/** @internal */
public override async toXml(schemaXml: Document): Promise<Element> {
const itemElement = await super.toXml(schemaXml);
itemElement.setAttribute("typeName", primitiveTypeToString(this.primitiveType));
return itemElement;
}
}
/** @beta */
export class EnumerationProperty extends PrimitiveOrEnumPropertyBase {
protected _enumeration?: LazyLoadedEnumeration;
public get enumeration(): LazyLoadedEnumeration | undefined { return this._enumeration; }
/**
* Save this EnumerationProperty's properties to an object for serializing to JSON.
*/
public override toJSON(): EnumerationPropertyProps {
const schemaJson = super.toJSON() as any;
schemaJson.typeName = this.enumeration!.fullName;
return schemaJson;
}
constructor(ecClass: ECClass, name: string, type: LazyLoadedEnumeration) {
// TODO: Should we allow specifying the backing type?
super(ecClass, name, PropertyType.Integer_Enumeration);
this._enumeration = type;
}
public override fromJSONSync(enumerationPropertyProps: EnumerationPropertyProps) {
super.fromJSONSync(enumerationPropertyProps);
if (undefined !== enumerationPropertyProps.typeName) {
if (!(this.enumeration!.fullName).match(enumerationPropertyProps.typeName)) // need to match {schema}.{version}.{itemName} on typeName
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, ``);
const enumSchemaItemKey = this.class.schema.getSchemaItemKey(this.enumeration!.fullName);
if (!enumSchemaItemKey)
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `Unable to locate the enumeration ${enumerationPropertyProps.typeName}.`);
this._enumeration = new DelayedPromiseWithProps<SchemaItemKey, Enumeration>(enumSchemaItemKey,
async () => {
const enumeration = await this.class.schema.lookupItem<Enumeration>(enumSchemaItemKey);
if (undefined === enumeration)
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, `Unable to locate the enumeration ${enumerationPropertyProps.typeName}.`);
return enumeration;
});
}
}
/** @internal */
public override async toXml(schemaXml: Document): Promise<Element> {
const itemElement = await super.toXml(schemaXml);
const enumeration = await this.enumeration;
const enumerationName = XmlSerializationUtils.createXmlTypedName(this.schema, enumeration!.schema, enumeration!.name);
itemElement.setAttribute("typeName", enumerationName);
return itemElement;
}
public override async fromJSON(enumerationPropertyProps: EnumerationPropertyProps) {
this.fromJSONSync(enumerationPropertyProps);
}
}
/** @beta */
export class StructProperty extends Property {
protected _structClass: StructClass;
public get structClass(): StructClass { return this._structClass; }
constructor(ecClass: ECClass, name: string, type: StructClass) {
super(ecClass, name, PropertyType.Struct);
this._structClass = type;
}
/**
* Save this StructProperty's properties to an object for serializing to JSON.
*/
public override toJSON(): StructPropertyProps {
const schemaJson = super.toJSON() as any;
schemaJson.typeName = this.structClass.fullName;
return schemaJson;
}
/** @internal */
public override async toXml(schemaXml: Document): Promise<Element> {
const itemElement = await super.toXml(schemaXml);
const structClassName = XmlSerializationUtils.createXmlTypedName(this.schema, this.structClass.schema, this.structClass.name);
itemElement.setAttribute("typeName", structClassName);
return itemElement;
}
public override fromJSONSync(structPropertyProps: StructPropertyProps) {
super.fromJSONSync(structPropertyProps);
if (undefined !== structPropertyProps.typeName) {
if (!this.structClass.key.matchesFullName(structPropertyProps.typeName))
throw new ECObjectsError(ECObjectsStatus.InvalidECJson, ``);
}
}
public override async fromJSON(structPropertyProps: StructPropertyProps) {
this.fromJSONSync(structPropertyProps);
}
}
/** @beta */
export class NavigationProperty extends Property {
protected _relationshipClass: LazyLoadedRelationshipClass;
protected _direction: StrengthDirection;
public get relationshipClass(): LazyLoadedRelationshipClass { return this._relationshipClass; }
public getRelationshipClassSync(): RelationshipClass | undefined {
if (!this._relationshipClass) // eslint-disable-line @typescript-eslint/no-misused-promises
return undefined;
return this.class.schema.lookupItemSync(this._relationshipClass);
}
public get direction() { return this._direction; }
/**
* Save this NavigationProperty's properties to an object for serializing to JSON.
*/
public override toJSON(): NavigationPropertyProps {
const schemaJson = super.toJSON() as any;
schemaJson.relationshipName = this.relationshipClass.fullName;
schemaJson.direction = strengthDirectionToString(this.direction);
return schemaJson;
}
/** @internal */
public override async toXml(schemaXml: Document): Promise<Element> {
const itemElement = await super.toXml(schemaXml);
const relationshipClass = await this.relationshipClass;
const relationshipClassName = XmlSerializationUtils.createXmlTypedName(this.schema, relationshipClass.schema, relationshipClass.name);
itemElement.setAttribute("relationshipName", relationshipClassName);
itemElement.setAttribute("direction", strengthDirectionToString(this.direction));
return itemElement;
}
constructor(ecClass: ECClass, name: string, relationship: LazyLoadedRelationshipClass, direction?: StrengthDirection) {
super(ecClass, name, PropertyType.Navigation);
this._relationshipClass = relationship;
this._direction = (direction !== undefined) ? direction : StrengthDirection.Forward;
}
}
type Constructor<T> = new (...args: any[]) => T;
// TODO: Consolidate all of the INT32_MAX variables.
const INT32_MAX = 2147483647;
/** @beta */
export abstract class ArrayProperty extends Property {
protected _minOccurs: number = 0;
protected _maxOccurs?: number = INT32_MAX;
public get minOccurs() { return this._minOccurs; }
public get maxOccurs() { return this._maxOccurs; }
/**
* @internal Used in schema editing.
*/
protected setMinOccurs(minOccurs: number) {
this._minOccurs = minOccurs;
}
/**
* @internal Used in schema editing.
*/
protected setMaxOccurs(maxOccurs: number) {
this._maxOccurs = maxOccurs;
}
}
// eslint-disable-next-line @typescript-eslint/naming-convention
const ArrayPropertyMixin = <T extends Constructor<Property>>(Base: T) => {
return class extends Base {
protected _minOccurs: number = 0;
protected _maxOccurs: number = INT32_MAX;
public get minOccurs() { return this._minOccurs; }
public get maxOccurs() { return this._maxOccurs; }
constructor(...args: any[]) {
super(...args);
this._type = PropertyTypeUtils.asArray(this._type);
}
public override fromJSONSync(arrayPropertyProps: PrimitiveArrayPropertyProps) {
super.fromJSONSync(arrayPropertyProps);
if (undefined !== arrayPropertyProps.minOccurs) {
this._minOccurs = arrayPropertyProps.minOccurs;
}
if (undefined !== arrayPropertyProps.maxOccurs) {
this._maxOccurs = arrayPropertyProps.maxOccurs;
}
}
public override async fromJSON(arrayPropertyProps: PrimitiveArrayPropertyProps) {
this.fromJSONSync(arrayPropertyProps);
}
/**
* Save this ArrayProperty's properties to an object for serializing to JSON.
*/
public override toJSON(): ArrayPropertyProps {
const schemaJson = super.toJSON() as any;
schemaJson.minOccurs = this.minOccurs;
if (this.maxOccurs !== undefined)
schemaJson.maxOccurs = this.maxOccurs;
return schemaJson;
}
/** @internal */
public override async toXml(schemaXml: Document): Promise<Element> {
const itemElement = await super.toXml(schemaXml);
itemElement.setAttribute("minOccurs", this.minOccurs.toString());
if (this.maxOccurs)
itemElement.setAttribute("maxOccurs", this.maxOccurs.toString());
return itemElement;
}
/**
* @internal Used in schema editing.
*/
protected setMinOccurs(minOccurs: number) {
this._minOccurs = minOccurs;
}
/**
* @internal Used in schema editing.
*/
protected setMaxOccurs(maxOccurs: number) {
this._maxOccurs = maxOccurs;
}
} as Constructor<Property> as typeof Base & Constructor<ArrayProperty>;
};
/** @beta */
export class PrimitiveArrayProperty extends ArrayPropertyMixin(PrimitiveProperty) {
constructor(ecClass: ECClass, name: string, primitiveType: PrimitiveType = PrimitiveType.Integer) {
super(ecClass, name, primitiveType);
}
/**
* Save this PrimitiveArrayProperty's properties to an object for serializing to JSON.
*/
public override toJSON(): PrimitiveArrayPropertyProps {
return super.toJSON();
}
}
/** @beta */
export class EnumerationArrayProperty extends ArrayPropertyMixin(EnumerationProperty) {
constructor(ecClass: ECClass, name: string, type: LazyLoadedEnumeration) {
super(ecClass, name, type);
}
}
/** @beta */
export class StructArrayProperty extends ArrayPropertyMixin(StructProperty) {
constructor(ecClass: ECClass, name: string, type: StructClass) {
super(ecClass, name, type);
}
}
/** @beta */
export type AnyArrayProperty = PrimitiveArrayProperty | EnumerationArrayProperty | StructArrayProperty;
/** @beta */
export type AnyPrimitiveProperty = PrimitiveProperty | PrimitiveArrayProperty;
/** @beta */
export type AnyEnumerationProperty = EnumerationProperty | EnumerationArrayProperty;
/** @beta */
export type AnyStructProperty = StructProperty | StructArrayProperty;
/** @beta */
export type AnyProperty = AnyPrimitiveProperty | AnyEnumerationProperty | AnyStructProperty | NavigationProperty;
/**
* Hackish approach that works like a "friend class" so we can access protected members without making them public.
* @internal
*/
export abstract class MutableProperty extends Property {
public abstract override addCustomAttribute(customAttribute: CustomAttribute): void;
}