Skip to content

Commit 282dff1

Browse files
refactor(AID): report proper valueType for value (#1149)
1 parent 77d57d4 commit 282dff1

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

packages/td-tools/src/util/asset-interface-description.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,28 @@ export class AssetInterfaceDescriptionUtil {
264264
return value;
265265
}
266266

267+
private getSimpleValueTypeXsd(value: unknown): string {
268+
// see https://www.w3.org/TR/xmlschema-2/#built-in-datatypes
269+
if (typeof value === "boolean") {
270+
return "xs:boolean";
271+
} else if (typeof value === "number") {
272+
const number = Number(value);
273+
// TODO XSD can be even more fine-grained
274+
if (Number.isInteger(number)) {
275+
// int is ·derived· from long by setting the value of ·maxInclusive· to be 2147483647 and ·minInclusive· to be -2147483648
276+
if (number <= 2147483647 && number >= -2147483648) {
277+
return "xs:int";
278+
} else {
279+
return "xs:integer";
280+
}
281+
} else {
282+
return "xs:double";
283+
}
284+
} else {
285+
return "xs:string";
286+
}
287+
}
288+
267289
private getProtocolPrefixes(td: ThingDescription): string[] {
268290
const protocols: string[] = [];
269291

@@ -1159,7 +1181,7 @@ export class AssetInterfaceDescriptionUtil {
11591181
propertyValues.push({
11601182
idShort: "default",
11611183
semanticId: this.createSemanticId("https://www.w3.org/2019/wot/json-schema#default"),
1162-
valueType: "xs:string",
1184+
valueType: this.getSimpleValueTypeXsd(propertyValue.default),
11631185
value: propertyValue.default,
11641186
modelType: "Property",
11651187
});
@@ -1247,22 +1269,22 @@ export class AssetInterfaceDescriptionUtil {
12471269
propertyForm.push({
12481270
idShort: formTerm,
12491271
semanticId: this.createSemanticId(semanticId),
1250-
valueType: "xs:string",
1272+
valueType: this.getSimpleValueTypeXsd(formValue),
12511273
value: formValue.toString(),
12521274
modelType: "Property",
12531275
});
12541276
} else {
12551277
propertyForm.push({
12561278
idShort: formTerm,
1257-
valueType: "xs:string",
1279+
valueType: this.getSimpleValueTypeXsd(formValue),
12581280
value: formValue.toString(),
12591281
modelType: "Property",
12601282
});
12611283
}
12621284
}
1263-
}
12641285

1265-
// TODO terms that are not string-based, like op arrays?
1286+
// TODO terms that are not simple types like op arrays?
1287+
}
12661288

12671289
propertyValues.push({
12681290
idShort: "forms",

packages/td-tools/test/AssetInterfaceDescriptionTest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ class AssetInterfaceDescriptionUtilTest {
910910
} else if (formEntry.idShort === "modbus_address") {
911911
hasModbusAddress = true;
912912
expect(formEntry.value).to.equal("1");
913+
expect(formEntry.valueType).to.equal("xs:int");
913914
}
914915
}
915916
expect(hasHref).to.equal(true);

0 commit comments

Comments
 (0)