Skip to content

Commit

Permalink
Configure Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomvh committed Jun 4, 2024
1 parent 08918d3 commit dfe7b37
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 37 deletions.
42 changes: 25 additions & 17 deletions projects/angular-odata/src/lib/models/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '../resources';
import {
ODataEntitySet,
ODataEnumType,
ODataStructuredType,
ODataStructuredTypeFieldParser,
} from '../schema';
Expand Down Expand Up @@ -317,8 +318,10 @@ export class ODataModelField<F> {
parser: ODataStructuredTypeFieldParser<F>;
options: ODataModelOptions<any>;
meta?: ODataModelOptions<any>;
model?: typeof ODataModel<any>;
collection?: typeof ODataCollection<any, any>;
modelForType?: (t: string) => typeof ODataModel<any> | undefined;
collectionForType?: (t: string) => typeof ODataCollection<any, any> | undefined;
enumForType?: (t: string) => ODataEnumType<F> | undefined;
structuredForType?: (t: string) => ODataStructuredType<F> | undefined;
default?: any;
required: boolean;
concurrency: boolean;
Expand Down Expand Up @@ -367,18 +370,24 @@ export class ODataModelField<F> {
optionsForType,
modelForType,
collectionForType,
enumForType,
structuredForType,
concurrency,
options,
}: {
optionsForType: (type: string) => ODataModelOptions<any> | undefined;
modelForType: (t: string) => typeof ODataModel<any> | undefined
collectionForType: (t: string) => typeof ODataCollection<any, any> | undefined,
enumForType: (t: string) => ODataEnumType<any> | undefined
structuredForType: (t: string) => ODataStructuredType<any> | undefined,
concurrency: boolean;
options: ParserOptions;
}) {
this.meta = optionsForType(this.parser.type);
this.model = modelForType(this.parser.type);
this.collection = collectionForType(this.parser.type);
this.meta = optionsForType(this.type);
this.modelForType = modelForType;
this.collectionForType = collectionForType;
this.enumForType = enumForType;
this.structuredForType = structuredForType;
this.parserOptions = options;
if (concurrency) this.concurrency = concurrency;
if (this.default !== undefined)
Expand All @@ -400,31 +409,26 @@ export class ODataModelField<F> {
isStructuredType() {
return this.parser.isStructuredType();
}
/*
structured() {
const structuredType = this.api.findStructuredTypeForType<F>(
this.parser.type
);

structuredType() {
const structuredType = this.structuredForType ? this.structuredForType(this.type) : undefined;
//Throw error if not found
if (!structuredType)
throw new Error(`Could not find structured type for ${this.parser.type}`);
return structuredType;
}
*/

isEnumType() {
return this.parser.isEnumType();
}

/*
enum() {
const enumType = this.api.findEnumTypeForType<F>(this.parser.type);
enumType() {
const enumType = this.enumForType ? this.enumForType(this.type) : undefined;
//Throw error if not found
if (!enumType)
throw new Error(`Could not find enum type for ${this.parser.type}`);
return enumType;
}
*/

validate(
value: any,
Expand Down Expand Up @@ -559,7 +563,7 @@ export class ODataModelField<F> {
const annots = this.annotationsFactory(
parent.annots()
) as ODataEntityAnnotations<F>;
let Model = this.model;
let Model = this.modelForType ? this.modelForType(this.type) : undefined;
if (Model === undefined) throw Error(`No Model type for ${this.name}`);
if (value !== undefined) {
annots.update(value);
Expand Down Expand Up @@ -594,7 +598,7 @@ export class ODataModelField<F> {
const annots = this.annotationsFactory(
parent.annots()
) as ODataEntitiesAnnotations<F>;
const Collection = this.collection;
const Collection = this.collectionForType ? this.collectionForType(this.type) : undefined;
if (Collection === undefined)
throw Error(`No Collection type for ${this.name}`);
return new Collection(
Expand Down Expand Up @@ -870,6 +874,8 @@ export class ODataModelOptions<T> {
optionsForType: (t: string) => this.api.optionsForType(t),
modelForType: (t: string) => this.api.modelForType(t),
collectionForType: (t: string) => this.api.collectionForType(t),
enumForType: (t: string) => this.api.findEnumTypeForType(t),
structuredForType: (t: string) => this.api.findStructuredTypeForType(t),
concurrency,
options,
});
Expand Down Expand Up @@ -950,6 +956,8 @@ export class ODataModelOptions<T> {
optionsForType: (t: string) => this.api.optionsForType(t),
modelForType: (t: string) => this.api.modelForType(t),
collectionForType: (t: string) => this.api.collectionForType(t),
enumForType: (t: string) => this.api.findEnumTypeForType(t),
structuredForType: (t: string) => this.api.findStructuredTypeForType(t),
options: this.api.options,
concurrency: false,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ export class Type<T> implements Renderable {
parser =
parser instanceof ODataStructuredTypeFieldParser &&
parser.isStructuredType()
? parser.structured()
? parser.structuredType()
: parser;
return parser?.findChildParser((p: any) => p.isTypeOf(this.type));
}
Expand Down
4 changes: 2 additions & 2 deletions projects/angular-odata/src/lib/schema/parsers/callable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class ODataParameterParser<T> {
return this.parser instanceof ODataEnumTypeParser;
}

enum() {
enumType() {
if (!this.isEnumType()) throw new Error('Field are not EnumType');
return this.parser as ODataEnumTypeParser<T>;
}
Expand All @@ -68,7 +68,7 @@ export class ODataParameterParser<T> {
return this.parser instanceof ODataStructuredTypeParser;
}

structured() {
structuredType() {
if (!this.isStructuredType())
throw new Error('Field are not StrucuturedType');
return this.parser as ODataStructuredTypeParser<T>;
Expand Down
32 changes: 15 additions & 17 deletions projects/angular-odata/src/lib/schema/parsers/structured-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ export class ODataReferential {

export class ODataStructuredTypeFieldParser<T>
extends ODataAnnotatable
implements FieldParser<T>
{
implements FieldParser<T> {
name: string;
private structuredType: ODataStructuredTypeParser<any>;
private structured: ODataStructuredTypeParser<any>;
type: string | EdmType;
private parser: Parser<T>;
collection: boolean;
Expand All @@ -82,12 +81,12 @@ export class ODataStructuredTypeFieldParser<T>

constructor(
name: string,
structuredType: ODataStructuredTypeParser<any>,
structured: ODataStructuredTypeParser<any>,
field: StructuredTypeFieldConfig
) {
super(field);
this.name = name;
this.structuredType = structuredType;
this.structured = structured;
this.type = field.type;
this.parser = NONE_PARSER;
this.referentials = (field.referentials || []).map(
Expand Down Expand Up @@ -128,13 +127,13 @@ export class ODataStructuredTypeFieldParser<T>
(this.navigation && value !== undefined)
) {
errors =
this.structured().validate(value, { method, navigation }) ||
this.structuredType().validate(value, { method, navigation }) ||
({} as { [name: string]: any[] });
} else if (
this.isEnumType() &&
(typeof value === 'string' || typeof value === 'number')
) {
errors = this.enum().validate(value, { method, navigation });
errors = this.enumType().validate(value, { method, navigation });
} else {
// IsEdmType
const computed = this.annotatedValue<boolean>(COMPUTED);
Expand Down Expand Up @@ -252,8 +251,8 @@ export class ODataStructuredTypeFieldParser<T>
toJsonSchema(options: JsonSchemaOptions<T> = {}) {
let schema: any =
this.parser instanceof ODataStructuredTypeFieldParser ||
this.parser instanceof ODataStructuredTypeParser ||
this.parser instanceof ODataEnumTypeParser
this.parser instanceof ODataStructuredTypeParser ||
this.parser instanceof ODataEnumTypeParser
? this.parser.toJsonSchema(options)
: ({ title: this.name, type: JsonSchemaType.object } as any);

Expand All @@ -268,7 +267,7 @@ export class ODataStructuredTypeFieldParser<T>
case EdmType.Binary:
schema.type = JsonSchemaType.string;
schema.contentEncoding = 'base64';
break;
break;
case EdmType.Date:
schema.type = JsonSchemaType.string;
schema.format = 'date';
Expand Down Expand Up @@ -317,7 +316,7 @@ export class ODataStructuredTypeFieldParser<T>

isKey() {
return (
this.structuredType
this.structured
.keys({ include_parents: true })
?.find((k) => k.name === this.name) !== undefined
);
Expand All @@ -335,7 +334,7 @@ export class ODataStructuredTypeFieldParser<T>
return this.parser instanceof ODataEnumTypeParser;
}

enum() {
enumType() {
if (!this.isEnumType()) throw new Error('Field are not EnumType');
return this.parser as ODataEnumTypeParser<T>;
}
Expand All @@ -344,7 +343,7 @@ export class ODataStructuredTypeFieldParser<T>
return this.parser instanceof ODataStructuredTypeParser;
}

structured() {
structuredType() {
if (!this.isStructuredType())
throw new Error('Field are not StrucuturedType');
return this.parser as ODataStructuredTypeParser<T>;
Expand All @@ -363,8 +362,7 @@ export class ODataStructuredTypeFieldParser<T>

export class ODataStructuredTypeParser<T>
extends ODataAnnotatable
implements Parser<T>
{
implements Parser<T> {
name: string;
namespace: string;
open: boolean;
Expand Down Expand Up @@ -649,7 +647,7 @@ export class ODataStructuredTypeParser<T>
if (field !== undefined) {
v = Types.isPlainObject(v) ? v[field.name] : v;
structured = field.isStructuredType()
? field.structured()
? field.structuredType()
: undefined;
}
}
Expand All @@ -671,7 +669,7 @@ export class ODataStructuredTypeParser<T>
return {
...fields.reduce((acc, f) => {
let value: any = f.isStructuredType()
? f.structured().defaults()
? f.structuredType().defaults()
: f.default;
return Types.isEmpty(value) ? acc : { ...acc, [f.name]: value };
}, {}),
Expand Down
4 changes: 4 additions & 0 deletions projects/angular-odata/src/lib/schema/structured-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class ODataStructuredType<T> extends ODataSchemaElement {
});
if (this.model !== undefined) {
this.model.meta = this.api.optionsForType<T>(this.type(), {config: this.model.options, structuredType: this})!;
if (this.model.meta !== undefined) {
// Configure
this.model.meta.configure({ options });
}
if (this.collection !== undefined) {
this.collection.model = this.model;
}
Expand Down

0 comments on commit dfe7b37

Please sign in to comment.