Skip to content

Commit

Permalink
new metadata class
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Weirich <matthias.weirich@selectcode.de>
  • Loading branch information
vavido committed Jan 11, 2021
1 parent ca69fe8 commit b898da1
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions javascript/lib/api/src/model/things.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ export class Thing extends EntityWithId {

public static readonly NAMESPACE_SEPARATION_REGEX = /([^:]*):(.*)/;

public constructor(private readonly _thingId: string,
public constructor(
private readonly _thingId: string,
private readonly _policyId?: string,
private readonly _attributes?: object,
private readonly _features?: Features,
private readonly __revision?: number,
private readonly __modified?: string,
private readonly __metadata?: Metadata,
private readonly _acl?: Acl) {
super();
}
Expand All @@ -41,9 +43,15 @@ export class Thing extends EntityWithId {
return o;
}
// @ts-ignore
return new Thing(o['thingId'], o['policyId'], o['attributes'],
return new Thing(o['thingId'],
o['policyId'],
o['attributes'],
// @ts-ignore
Features.fromObject(o['features']), o['_revision'], o['_modified'], Metadata.fromObject(o['_metadata']), Acl.fromObject(o['acl']));
Features.fromObject(o['features']),
o['_revision'],
o['_modified'],
Metadata.fromObject(o['_metadata']),
Acl.fromObject(o['acl']));
}

public static empty(): Thing {
Expand Down Expand Up @@ -93,7 +101,7 @@ export class Thing extends EntityWithId {
return this.__revision;
}

get _metadata(): Features | undefined {
get _metadata(): Metadata | undefined {
return this.__metadata;
}

Expand Down Expand Up @@ -144,6 +152,43 @@ export class Features extends IndexedEntityModel<Feature> {

}


export class Metadata extends EntityModel {

public constructor(
private readonly _attributes?: Record<string, any>,
private readonly _features?: Features) {
super();
}


get attributes(): Record<string, any> | undefined {
return this._attributes;
}

get features(): Features | undefined {
return this._features;
}

public static fromObject(o: any): Metadata {
if (o === undefined) {
return o;
}
return new Metadata(Features.fromObject(o.features), o.attributes);
}

toObject(): Object | undefined {
const features = this.features ? Features.toObject(this.features) : undefined;

return EntityModel.buildObject(new Map<string, any>([
['features', features],
['attributes', this.attributes]
]));
}

}


/**
* Representation of a Feature
*/
Expand Down

0 comments on commit b898da1

Please sign in to comment.