Skip to content

Commit

Permalink
Merge pull request #725 from lukesmolo/thing-model
Browse files Browse the repository at this point in the history
Thing model
  • Loading branch information
danielpeintner authored Mar 30, 2022
2 parents 4c2a716 + 238f4f0 commit 96fe7c4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 25 deletions.
22 changes: 19 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/td-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"is-absolute-url": "3.0.3",
"json-placeholder-replacer": "^1.0.35",
"url-toolkit": "2.1.6",
"wot-thing-description-types": "^1.1.0-09-February-2022",
"wot-thing-model-types": "^1.1.0-3-February-2022",
Expand Down
26 changes: 4 additions & 22 deletions packages/td-tools/src/thing-model-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Ajv, { ValidateFunction, ErrorObject } from "ajv";
import * as http from "http";
import * as https from "https";
import * as fs from "fs";
import { JsonPlaceholderReplacer } from "json-placeholder-replacer";
import { LinkElement } from "wot-thing-description-types";
import { DataSchema, ExposedThingInit } from "wot-typescript-definitions";
import { ThingModel } from "wot-thing-model-types";
Expand Down Expand Up @@ -552,28 +553,9 @@ export class ThingModelHelpers {
}

private fillPlaceholder(data: Record<string, unknown>, map: Record<string, unknown>): ThingModel {
let dataString = JSON.stringify(data);
for (const key in map) {
const value = map[key];
let word = `{{${key}}}`;
const instances = (dataString.match(new RegExp(word, "g")) || []).length;
for (let i = 0; i < instances; i++) {
word = `{{${key}}}`;
const re = `"(${word})"`;
const match = dataString.match(re);
if (match === null) {
// word is included in another string/number/element. Keep that type
dataString = dataString.replace(word, value as string);
} else {
// keep the new value type
if (typeof value !== "string") {
word = `"{{${key}}}"`;
}
dataString = dataString.replace(word, value as string);
}
}
}
return JSON.parse(dataString);
const placeHolderReplacer = new JsonPlaceholderReplacer();
placeHolderReplacer.addVariableMap(map);
return placeHolderReplacer.replace(data) as ThingModel;
}

private checkPlaceholderMap(model: ThingModel, map: Record<string, unknown>): { valid: boolean; errors: string } {
Expand Down
68 changes: 68 additions & 0 deletions packages/td-tools/test/ThingModelHelperTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,74 @@ class ThingModelHelperTest {
expect(partialTd).to.be.deep.equal(finalJSON);
}

@test async "should correctly fill placeholders with composed types"() {
const thing = {
"@context": ["http://www.w3.org/ns/td"],
"@type": "tm:ThingModel",
arrayField: "{{ARRAY}}",
title: "Thermostate No. 4",
versionInfo: "{{VERSION_INFO}}",
} as unknown as ThingModel;
const map = {
ARRAY: ["random", "random1", "random2"],
VERSION_INFO: { instance: "xyz", model: "ABC" },
};
const finalJSON = {
"@context": ["http://www.w3.org/ns/td"],
"@type": "Thing",
title: "Thermostate No. 4",
arrayField: ["random", "random1", "random2"],
versionInfo: { instance: "xyz", model: "ABC" },
links: [
{
href: "./ThermostateNo.4.tm.jsonld",
rel: "type",
type: "application/tm+json",
},
],
};
const options: CompositionOptions = {
map,
selfComposition: false,
};
const [partialTd] = await this.thingModelHelpers.getPartialTDs(thing, options);
expect(partialTd).to.be.deep.equal(finalJSON);
}

@test async "should correctly fill placeholders with composed types in strings"() {
const thing = {
"@context": ["http://www.w3.org/ns/td"],
"@type": "tm:ThingModel",
data: "data: {{ARRAY}}",
title: "Thermostate No. 4",
versionInfo: "version: {{VERSION_INFO}}",
} as unknown as ThingModel;
const map = {
ARRAY: [1, 2, 3],
VERSION_INFO: { instance: "xyz", model: "ABC" },
};
const finalJSON = {
"@context": ["http://www.w3.org/ns/td"],
"@type": "Thing",
title: "Thermostate No. 4",
data: "data: [1,2,3]",
versionInfo: 'version: {"instance":"xyz","model":"ABC"}',
links: [
{
href: "./ThermostateNo.4.tm.jsonld",
rel: "type",
type: "application/tm+json",
},
],
};
const options: CompositionOptions = {
map,
selfComposition: false,
};
const [partialTd] = await this.thingModelHelpers.getPartialTDs(thing, options);
expect(partialTd).to.be.deep.equal(finalJSON);
}

@test async "should reject fill placeholders because of missing fields in map"() {
const thing = {
"@context": ["http://www.w3.org/ns/td"],
Expand Down

0 comments on commit 96fe7c4

Please sign in to comment.