Skip to content

Commit 4c79f6d

Browse files
author
Diego van Haaster
committed
Refactor: Simplify type resolution with alias support and remove field callbacks
- Add alias-based type lookups in findEnumType, findStructuredType, findCallable, findEntitySet, findSingleton - Remove optionsForType/modelForType/collectionForType/enumForType/structuredForType callbacks from ODataModelField, using api directly instead - Simplify isTypeOf in ODataSchemaElement to use type() directly - Fix template typo: *ngFor -> *
1 parent 44d396c commit 4c79f6d

4 files changed

Lines changed: 27 additions & 41 deletions

File tree

docs/api/template-playground/template-playground.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ interface CompoDocConfig {
9494
<div class="template-file-list">
9595
<h3>Templates</h3>
9696
<ul class="file-list">
97-
<li *ngFor="let template of templates; trackBy: trackByName"
97+
<li *="let template of templates; trackBy: trackByName"
9898
[class.active]="selectedFile === template"
9999
(click)="selectFile(template)">
100100
<i class="file-icon ion-document-text"></i>

projects/angular-odata/src/lib/api.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ export class ODataApi {
283283
bodyQueryOptions?: QueryOption[];
284284
},
285285
): Observable<any> {
286-
let req = ODataRequest.factory(this, method, resource, {
286+
const req = ODataRequest.factory(this, method, resource, {
287287
body: options.body,
288288
etag: options.etag,
289289
context: options.context,
@@ -440,6 +440,7 @@ export class ODataApi {
440440
<ODataEnumType<T>[]>[],
441441
);
442442
let enumType = enumTypes.find((e) => e.type() === value);
443+
enumType = enumType ?? enumTypes.find((e) => e.type({alias: true}) === value);
443444
enumType = enumType ?? enumTypes.find((e) => e.name === value);
444445
this.memo.enumTypes.set(value, enumType);
445446
return enumType;
@@ -456,6 +457,7 @@ export class ODataApi {
456457
<ODataStructuredType<T>[]>[],
457458
);
458459
let structuredType = structuredTypes.find((e) => e.type() === value);
460+
structuredType = structuredType ?? structuredTypes.find((e) => e.type({alias: true}) === value);
459461
structuredType = structuredType ?? structuredTypes.find((e) => e.name === value);
460462
this.memo.structuredTypes.set(value, structuredType);
461463
return structuredType;
@@ -490,6 +492,21 @@ export class ODataApi {
490492
bindingStructuredType.isSubtypeOf(callableBindingStructuredType)))
491493
);
492494
});
495+
callable = callables.find((c) => {
496+
const isCallableType = c.type({alias: true}) == value;
497+
const callableBindingType = c.binding()?.type;
498+
const callableBindingStructuredType =
499+
callableBindingType !== undefined
500+
? this.findStructuredType(callableBindingType)
501+
: undefined;
502+
503+
return (
504+
isCallableType &&
505+
(!bindingStructuredType ||
506+
(callableBindingStructuredType &&
507+
bindingStructuredType.isSubtypeOf(callableBindingStructuredType)))
508+
);
509+
});
493510
callable =
494511
callable ??
495512
callables.find((c) => {
@@ -523,6 +540,7 @@ export class ODataApi {
523540
<ODataEntitySet[]>[],
524541
);
525542
let entitySet = entitySets.find((e) => e.type() === value);
543+
entitySet = entitySet ?? entitySets.find((e) => e.type({alias: true}) === value);
526544
entitySet = entitySet ?? entitySets.find((e) => e.name === value);
527545
this.memo.entitySets.set(value, entitySet);
528546
return entitySet;
@@ -539,6 +557,7 @@ export class ODataApi {
539557
<ODataSingleton[]>[],
540558
);
541559
let singleton = singletons.find((e) => e.type() === value);
560+
singleton = singleton ?? singletons.find((e) => e.type({alias: true}) === value);
542561
singleton = singleton ?? singletons.find((e) => e.name === value);
543562
this.memo.singletons.set(value, singleton);
544563
return singleton;

projects/angular-odata/src/lib/models/options.ts

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,6 @@ export class ODataModelField<F> {
311311
field: string;
312312
parser: ODataStructuredTypeFieldParser<F>;
313313
options: ODataModelOptions<any>;
314-
optionsForType?: (type: string) => ODataModelOptions<any> | undefined;
315-
modelForType?: (t: string) => typeof ODataModel<any> | undefined;
316-
collectionForType?: (t: string) => typeof ODataCollection<any, ODataModel<any>> | undefined;
317-
enumForType?: (t: string) => ODataEnumType<F> | undefined;
318-
structuredForType?: (t: string) => ODataStructuredType<F> | undefined;
319314
default?: any;
320315
required: boolean;
321316
concurrency: boolean;
@@ -361,27 +356,12 @@ export class ODataModelField<F> {
361356
}
362357

363358
configure({
364-
optionsForType,
365-
modelForType,
366-
collectionForType,
367-
enumForType,
368-
structuredForType,
369359
concurrency,
370360
options,
371361
}: {
372-
optionsForType: (type: string) => ODataModelOptions<any> | undefined;
373-
modelForType: (t: string) => typeof ODataModel<any> | undefined;
374-
collectionForType: (t: string) => typeof ODataCollection<any, any> | undefined;
375-
enumForType: (t: string) => ODataEnumType<any> | undefined;
376-
structuredForType: (t: string) => ODataStructuredType<any> | undefined;
377362
concurrency: boolean;
378363
options: ParserOptions;
379364
}) {
380-
this.optionsForType = optionsForType;
381-
this.modelForType = modelForType;
382-
this.collectionForType = collectionForType;
383-
this.enumForType = enumForType;
384-
this.structuredForType = structuredForType;
385365
this.parserOptions = options;
386366
if (concurrency) this.concurrency = concurrency;
387367
if (this.default !== undefined) this.default = this.deserialize(this.default, options);
@@ -404,7 +384,7 @@ export class ODataModelField<F> {
404384
}
405385

406386
structuredType() {
407-
const structuredType = this.structuredForType ? this.structuredForType(this.type) : undefined;
387+
const structuredType = this.options.api.findStructuredType(this.type);
408388
//Throw error if not found
409389
if (!structuredType) throw new Error(`Could not find structured type for ${this.parser.type}`);
410390
return structuredType;
@@ -419,7 +399,7 @@ export class ODataModelField<F> {
419399
}
420400

421401
enumType() {
422-
const enumType = this.enumForType ? this.enumForType(this.type) : undefined;
402+
const enumType = this.options.api.findEnumType(this.type);
423403
//Throw error if not found
424404
if (!enumType) throw new Error(`Could not find enum type for ${this.parser.type}`);
425405
return enumType;
@@ -479,8 +459,7 @@ export class ODataModelField<F> {
479459
}
480460

481461
defaults(): any {
482-
const meta =
483-
this.optionsForType && this.isStructuredType() ? this.optionsForType(this.type) : undefined;
462+
const meta = this.isStructuredType() ? this.options.api.optionsForType(this.type) : undefined;
484463
return meta !== undefined ? meta.defaults() : this.default;
485464
}
486465

@@ -534,7 +513,7 @@ export class ODataModelField<F> {
534513
}): ODataModel<F> {
535514
// Model
536515
const annots = this.annotationsFactory(parent.annots()) as ODataEntityAnnotations<F>;
537-
let Model = this.modelForType ? this.modelForType(this.type) : undefined;
516+
let Model = this.options.api.modelForType(this.type);
538517
if (Model === undefined) throw Error(`No Model type for ${this.name}`);
539518
if (value !== undefined) {
540519
annots.update(value);
@@ -567,7 +546,7 @@ export class ODataModelField<F> {
567546
}): ODataCollection<F, ODataModel<F>> {
568547
// Collection Factory
569548
const annots = this.annotationsFactory(parent.annots()) as ODataEntitiesAnnotations<F>;
570-
const Collection = this.collectionForType ? this.collectionForType(this.type) : undefined;
549+
const Collection = this.options.api.collectionForType(this.type);
571550
if (Collection === undefined) throw Error(`No Collection type for ${this.name}`);
572551
return Collection.factory((value || []) as Partial<F>[] | { [name: string]: any }[], {
573552
annots: annots,
@@ -870,11 +849,6 @@ export class ODataModelOptions<T> {
870849
this._fields.forEach((field) => {
871850
const concurrency = concurrencyFields.indexOf(field.field) !== -1;
872851
field.configure({
873-
optionsForType: (t: string) => this.api.optionsForType(t),
874-
modelForType: (t: string) => this.api.modelForType(t),
875-
collectionForType: (t: string) => this.api.collectionForType(t),
876-
enumForType: (t: string) => this.api.findEnumType(t),
877-
structuredForType: (t: string) => this.api.findStructuredType(t),
878852
concurrency,
879853
options,
880854
});
@@ -951,11 +925,6 @@ export class ODataModelOptions<T> {
951925
parser: structuredFieldParser,
952926
});
953927
modelField.configure({
954-
optionsForType: (t: string) => this.api.optionsForType(t),
955-
modelForType: (t: string) => this.api.modelForType(t),
956-
collectionForType: (t: string) => this.api.collectionForType(t),
957-
enumForType: (t: string) => this.api.findEnumType(t),
958-
structuredForType: (t: string) => this.api.findStructuredType(t),
959928
options: this.api.options,
960929
concurrency: false,
961930
});

projects/angular-odata/src/lib/schema/element.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ export class ODataSchemaElement extends ODataAnnotatable {
4545
* @returns True if the callable is type of the given type
4646
*/
4747
isTypeOf(element: ODataSchemaElement): boolean {
48-
const names = [`${this.schema.namespace}.${this.name}`];
49-
if (this.schema.alias) names.push(`${this.schema.alias}.${this.name}`);
50-
return names.includes(element.type());
48+
return this.type() === element.type();
5149
}
5250

5351
/**

0 commit comments

Comments
 (0)