Skip to content

Commit

Permalink
refactor(AID): report proper valueType for value (#1149)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeintner authored Nov 6, 2023
1 parent 77d57d4 commit 282dff1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
32 changes: 27 additions & 5 deletions packages/td-tools/src/util/asset-interface-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,28 @@ export class AssetInterfaceDescriptionUtil {
return value;
}

private getSimpleValueTypeXsd(value: unknown): string {
// see https://www.w3.org/TR/xmlschema-2/#built-in-datatypes
if (typeof value === "boolean") {
return "xs:boolean";
} else if (typeof value === "number") {
const number = Number(value);
// TODO XSD can be even more fine-grained
if (Number.isInteger(number)) {
// int is ·derived· from long by setting the value of ·maxInclusive· to be 2147483647 and ·minInclusive· to be -2147483648
if (number <= 2147483647 && number >= -2147483648) {
return "xs:int";
} else {
return "xs:integer";
}
} else {
return "xs:double";
}
} else {
return "xs:string";
}
}

private getProtocolPrefixes(td: ThingDescription): string[] {
const protocols: string[] = [];

Expand Down Expand Up @@ -1159,7 +1181,7 @@ export class AssetInterfaceDescriptionUtil {
propertyValues.push({
idShort: "default",
semanticId: this.createSemanticId("https://www.w3.org/2019/wot/json-schema#default"),
valueType: "xs:string",
valueType: this.getSimpleValueTypeXsd(propertyValue.default),
value: propertyValue.default,
modelType: "Property",
});
Expand Down Expand Up @@ -1247,22 +1269,22 @@ export class AssetInterfaceDescriptionUtil {
propertyForm.push({
idShort: formTerm,
semanticId: this.createSemanticId(semanticId),
valueType: "xs:string",
valueType: this.getSimpleValueTypeXsd(formValue),
value: formValue.toString(),
modelType: "Property",
});
} else {
propertyForm.push({
idShort: formTerm,
valueType: "xs:string",
valueType: this.getSimpleValueTypeXsd(formValue),
value: formValue.toString(),
modelType: "Property",
});
}
}
}

// TODO terms that are not string-based, like op arrays?
// TODO terms that are not simple types like op arrays?
}

propertyValues.push({
idShort: "forms",
Expand Down
1 change: 1 addition & 0 deletions packages/td-tools/test/AssetInterfaceDescriptionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ class AssetInterfaceDescriptionUtilTest {
} else if (formEntry.idShort === "modbus_address") {
hasModbusAddress = true;
expect(formEntry.value).to.equal("1");
expect(formEntry.valueType).to.equal("xs:int");
}
}
expect(hasHref).to.equal(true);
Expand Down

0 comments on commit 282dff1

Please sign in to comment.