Skip to content

Commit

Permalink
refactor: take over changes from eclipse-thingweb/node-wot#1210
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner committed Jul 12, 2024
1 parent 6d81600 commit 612d7db
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions node/thing-model/src/tm-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,17 @@ export class ThingModelHelpers {
modelInput.imports = [];
for (const affType of affordanceTypes) {
const affRefs = ThingModelHelpers.getThingModelRef(data[affType] as DataSchema);
if (Object.keys(affRefs).length > 0) {
for (const aff in affRefs) {
const affUri = affRefs[aff] as string;
const refObj = this.parseTmRef(affUri);
if (refObj.uri == null) {
throw new Error(`Missing remote path in ${affUri}`);
}
let source = await this.fetchModel(refObj.uri);
[source] = await this._getPartialTDs(source);
delete (data[affType] as DataSchema)[aff]["tm:ref"];
const importedAffordance = this.getRefAffordance(refObj, source) ?? {};
refObj.name = aff; // update the name of the affordance
modelInput.imports.push({ affordance: importedAffordance, ...refObj });
for (const [aff, affUri] of Object.entries(affRefs)) {
const refObj = this.parseTmRef(affUri);
if (refObj.uri == null) {
throw new Error(`Missing remote path in ${affUri}`);
}
let source = await this.fetchModel(refObj.uri);
[source] = await this._getPartialTDs(source);
delete (data[affType] as DataSchema)[aff]["tm:ref"];
const importedAffordance = this.getRefAffordance(refObj, source) ?? {};
refObj.name = aff; // update the name of the affordance
modelInput.imports.push({ affordance: importedAffordance, ...refObj });
}
}
const tmLinks = ThingModelHelpers.getThingModelLinks(data, "tm:submodel");
Expand Down Expand Up @@ -386,8 +383,7 @@ export class ThingModelHelpers {
if ("submodel" in modelObject) {
const submodelObj = modelObject.submodel;

for (const key in submodelObj) {
const sub = submodelObj[key];
for (const [key, sub] of Object.entries(submodelObj ?? {})) {
if (options.selfComposition === true) {
if (!data.links) {
throw new Error(
Expand All @@ -405,11 +401,9 @@ export class ThingModelHelpers {
const [subPartialTD] = await this._getPartialTDs(sub, options);
const affordanceTypes = ["properties", "actions", "events"];
for (const affType of affordanceTypes) {
for (const affKey in subPartialTD[affType] as DataSchema) {
for (const affKey of Object.keys((subPartialTD[affType] ?? {}) as DataSchema)) {
data[affType] ??= {} as DataSchema;
const newAffKey = `${instanceName}_${affKey}`;
if (!(affType in data)) {
data[affType] = {} as DataSchema;
}
(data[affType] as DataSchema)[newAffKey] = (subPartialTD[affType] as DataSchema)[
affKey
] as DataSchema;
Expand Down Expand Up @@ -456,15 +450,15 @@ export class ThingModelHelpers {
return tmpThingModels;
}

private static getThingModelRef(data: Record<string, unknown>): Record<string, unknown> {
const refs = {} as Record<string, unknown>;
private static getThingModelRef(data: Record<string, unknown>): Record<string, string> {
const refs = {} as Record<string, string>;
if (data == null) {
return refs;
}
for (const key in data) {
for (const key1 in data[key] as Record<string, unknown>) {
if (key1 === "tm:ref") {
refs[key] = (data[key] as Record<string, unknown>)["tm:ref"] as string;
for (const [key, value] of Object.entries(data)) {
for (const valueKey of Object.keys(value as Record<string, unknown>)) {
if (valueKey === "tm:ref") {
refs[key] = (value as Record<string, unknown>)["tm:ref"] as string;
}
}
}
Expand Down

0 comments on commit 612d7db

Please sign in to comment.