Skip to content

Commit

Permalink
use -1 as default EnumValue when not specified in XML
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Oct 2, 2023
1 parent 9f65a03 commit 9cc09bc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 48 deletions.
19 changes: 10 additions & 9 deletions packages/node-opcua-address-space/source/loader/load_nodeset2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { promisify, types } from "util";
import chalk from "chalk";

import { coerceByte , coerceBoolean, coerceInt32} from "node-opcua-basic-types";
import { coerceByte, coerceBoolean, coerceInt32 } from "node-opcua-basic-types";
import {
AddReferenceTypeOptions,
BaseNode,
Expand Down Expand Up @@ -231,8 +231,8 @@ function makeNodeSetParserEngine(addressSpace: IAddressSpace, options: NodeSetLo
if (!namespace) {
throw new Error(
"cannot find namespace for " +
namespaceUri +
"\nplease make sure to initialize your address space with the corresponding nodeset files"
namespaceUri +
"\nplease make sure to initialize your address space with the corresponding nodeset files"
);
}
found_namespace_in_uri[namespaceUri] = namespace;
Expand Down Expand Up @@ -315,7 +315,7 @@ function makeNodeSetParserEngine(addressSpace: IAddressSpace, options: NodeSetLo
}
} else {
namespace = addressSpace1.registerNamespace(model.modelUri);
namespace.setRequiredModels(model.requiredModels)
namespace.setRequiredModels(model.requiredModels);
}

namespace.version = model.version;
Expand Down Expand Up @@ -470,7 +470,7 @@ function makeNodeSetParserEngine(addressSpace: IAddressSpace, options: NodeSetLo

this.obj = {};
this.obj.nodeClass = NodeClass.ReferenceType;
this.obj.isAbstract =coerceBoolean(attrs.IsAbstract);
this.obj.isAbstract = coerceBoolean(attrs.IsAbstract);
this.obj.nodeId = convertToNodeId(attrs.NodeId) || null;
this.obj.browseName = convertQualifiedName(attrs.BrowseName);
},
Expand Down Expand Up @@ -1329,7 +1329,6 @@ function makeNodeSetParserEngine(addressSpace: IAddressSpace, options: NodeSetLo
const v = node as UAVariable;
assert(v.getBasicDataType() === DataType.ExtensionObject, "expecting an extension object");
v.bindExtensionObject(variant.value, { createMissingProp: false });

} else if (node.nodeClass === NodeClass.VariableType) {
const v = node as UAVariableType;
(v as any) /*fix me*/.value.value = variant.value;
Expand Down Expand Up @@ -1536,7 +1535,6 @@ function makeNodeSetParserEngine(addressSpace: IAddressSpace, options: NodeSetLo
}
};


interface Model {
modelUri: string;
version: string;
Expand Down Expand Up @@ -1691,7 +1689,7 @@ function makeNodeSetParserEngine(addressSpace: IAddressSpace, options: NodeSetLo
doDebug &&
debugLog(
chalk.bgGreenBright("Performing post loading tasks -------------------------------------------") +
chalk.green("DONE")
chalk.green("DONE")
);

async function performPostLoadingTasks(tasks: Task[]): Promise<void> {
Expand Down Expand Up @@ -1768,7 +1766,10 @@ function makeNodeSetParserEngine(addressSpace: IAddressSpace, options: NodeSetLo

export class NodeSetLoader {
_s: NodeSet2ParserEngine;
constructor(addressSpace: IAddressSpace, private options?: NodeSetLoaderOptions) {
constructor(
addressSpace: IAddressSpace,
private options?: NodeSetLoaderOptions
) {
this._s = makeNodeSetParserEngine(addressSpace, options || {});
}

Expand Down
7 changes: 5 additions & 2 deletions packages/node-opcua-address-space/src/ua_data_type_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import * as tools from "./tool_isSubtypeOf";
import { get_subtypeOf } from "./tool_isSubtypeOf";
import { get_subtypeOfObj } from "./tool_isSubtypeOf";
import { BaseNode_getCache } from "./base_node_private";
import { coerceInt32, coerceInt64toInt32 } from "node-opcua-basic-types";
import { Int64, coerceInt32, coerceInt64, coerceInt64toInt32 } from "node-opcua-basic-types";

export interface UADataTypeImpl {
_extensionObjectConstructor: ExtensionObjectConstructorFuncWithSchema;
Expand Down Expand Up @@ -396,12 +396,15 @@ export function DataType_toString(this: UADataTypeImpl, options: ToStringOption)
dataTypeDefinition_toString.call(this, options);
}


const defaultEnumValue: Int64 = coerceInt64(-1);

function makeEnumDefinition(definitionFields: EnumFieldOptions[]) {
return new EnumDefinition({
fields: definitionFields.map((x) => ({
description: x.description,
name: x.name,
value: x.value
value: x.value === undefined ? defaultEnumValue : coerceInt64(x.value)
}))
});
}
Expand Down
32 changes: 14 additions & 18 deletions packages/node-opcua-xml2json/source/definition_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,39 @@ import { ReaderStateParserLike, XmlAttributes } from "./xml2json";
// (IsOptionSet)
//
//
type UAString = string |null;
type UAString = string | null;
type NodeIdLike = string | null;
type Int32 = number;
type UInt32 = number;
type UABoolean = boolean;
type LocalizedTextLike = string | null;

export interface StructureFieldOptions {
name?: UAString ;
description?: (LocalizedTextLike | null);
dataType?: (NodeIdLike | null);
valueRank?: Int32 ;
arrayDimensions?: UInt32 [] | null;
maxStringLength?: UInt32 ;
isOptional?: UABoolean ;
name?: UAString;
description?: LocalizedTextLike | null;
dataType?: NodeIdLike | null;
valueRank?: Int32;
arrayDimensions?: UInt32[] | null;
maxStringLength?: UInt32;
isOptional?: UABoolean;
}
interface AA {
parent: {
definitionFields: StructureFieldOptions[];
nFields: number;
definitionName: string;

},
};
array: StructureFieldOptions[];
isUnion: boolean;
}
interface FieldParser {
description?: (LocalizedTextLike | null);
description?: LocalizedTextLike | null;
parent: AA;
attrs: Record<string, string>;
}
export const _definitionParser: ReaderStateParserLike = {
init(this: AA, name: string, attrs: XmlAttributes) {
assert(!this.parent.
nFields || this.parent.definitionFields.length === 0);
assert(!this.parent.nFields || this.parent.definitionFields.length === 0);
this.parent.definitionFields = [];
this.parent.definitionName = attrs.SymbolicName || attrs.Name;
this.array = this.parent.definitionFields;
Expand Down Expand Up @@ -86,9 +84,6 @@ export const _definitionParser: ReaderStateParserLike = {
}
if (this.attrs.Value !== undefined) {
obj.value = parseInt(this.attrs.Value, 10);
} else {
// when not specified value = -1 , on enumeration at least
obj.value = -1;
}
if (this.attrs.ValueRank !== undefined) {
obj.valueRank = parseInt(this.attrs.ValueRank, 10);
Expand All @@ -100,8 +95,9 @@ export const _definitionParser: ReaderStateParserLike = {
obj.arrayDimensions = this.attrs.ArrayDimensions.split(",").map((e: string) => parseInt(e, 10));
}

obj.isOptional = this.attrs.IsOptional === "true" ? true : false;

if (this.attrs.IsOptional !== undefined) {
obj.isOptional = this.attrs.IsOptional === "true" ? true : false;
}
if (this.attrs.SymbolicName !== undefined) {
obj.symbolicName = this.attrs.SymbolicName;
}
Expand Down
23 changes: 4 additions & 19 deletions packages/node-opcua-xml2json/test/test_definition_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,30 @@ describe("Definition Parser", () => {
{
dataType: "Int32",
name: "Id",
isOptional: false,
allowSubtype: false,
valueRank: -1
valueRank: -1,
},
{
dataType: "Double",
name: "HighValue",
isOptional: false,
allowSubtype: false,
valueRank: -1
},
{
dataType: "Double",
name: "LowValue",
isOptional: false,
allowSubtype: false,
valueRank: -1
},
{
dataType: "LocalizedText",
name: "Comments",
isOptional: false,
allowSubtype: false,
valueRank: -1
},
{
dataType: "EUInformation",
name: "EngineeringUnits",
isOptional: false,
allowSubtype: false,
valueRank: -1
}
Expand All @@ -68,7 +63,7 @@ describe("Definition Parser", () => {
<Field DataType="String" Name="IsoGrade"/>
<Field DataType="Int16" Name="RMin"/>
<Field SymbolicName="Decode_" DataType="Int16" Name="Decode"/>
<Field DataType="Int16" Name="PrintGain"/>
<Field DataType="Int16" Name="PrintGain" IsOptional="true"/>
</Definition>`;
const parser = new Xml2Json(definitionReaderStateParser);
const a = parser.parseStringSync(xmlDoc);
Expand All @@ -80,30 +75,27 @@ describe("Definition Parser", () => {
{
name: "IsoGrade",
dataType: "String",
isOptional: false,
allowSubtype: false,
valueRank: -1
},
{
name: "RMin",
dataType: "Int16",
isOptional: false,
allowSubtype: false,
valueRank: -1
},
{
name: "Decode",
dataType: "Int16",
symbolicName: "Decode_",
isOptional: false,
allowSubtype: false,
valueRank: -1
},
{
name: "PrintGain",
dataType: "Int16",
isOptional: false,
allowSubtype: false,
isOptional: true,
valueRank: -1
}
]
Expand Down Expand Up @@ -172,24 +164,22 @@ describe("Definition Parser", () => {
{
dataType: "ResultIdDataType",
name: "ResultId",
isOptional: false,
allowSubtype: false,
valueRank: -1,
description:
"System-wide unique identifier, which is assigned by the system. This ID can be used for fetching exactly this result using the pertinent result management methods and it is identical to the ResultId of the ResultReadyEventType."
},
{
dataType: "Boolean",
isOptional: true,
allowSubtype: false,
isOptional: true,
valueRank: -1,
name: "HasTransferableDataOnFile",
description: "Indicates that additional data for this result can be retrieved by temporary file transfer."
},
{
dataType: "Boolean",
name: "IsPartial",
isOptional: false,
allowSubtype: false,
valueRank: -1,
description: "Indicates whether the result is the partial result of a total result."
Expand All @@ -205,7 +195,6 @@ describe("Definition Parser", () => {
{
dataType: "ResultStateDataType",
name: "ResultState",
isOptional: false,
allowSubtype: false,
valueRank: -1,
description:
Expand Down Expand Up @@ -241,7 +230,6 @@ describe("Definition Parser", () => {
{
dataType: "RecipeIdInternalDataType",
name: "InternalRecipeId",
isOptional: false,
allowSubtype: false,
valueRank: -1,
description:
Expand All @@ -267,7 +255,6 @@ describe("Definition Parser", () => {
{
dataType: "ConfigurationIdDataType",
name: "InternalConfigurationId",
isOptional: false,
allowSubtype: false,
valueRank: -1,
description:
Expand All @@ -276,7 +263,6 @@ describe("Definition Parser", () => {
{
dataType: "JobIdDataType",
name: "JobId",
isOptional: false,
allowSubtype: false,
valueRank: -1,
description:
Expand All @@ -285,7 +271,6 @@ describe("Definition Parser", () => {
{
dataType: "UtcTime",
name: "CreationTime",
isOptional: false,
allowSubtype: false,
valueRank: -1,
description: "CreationTime indicates the time when the result was created."
Expand Down

0 comments on commit 9cc09bc

Please sign in to comment.