Skip to content

Commit

Permalink
fix variant initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Feb 23, 2021
1 parent 5b63718 commit e3264db
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 30 deletions.
19 changes: 12 additions & 7 deletions packages/node-opcua-factory/source/factories_schema_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ export function initialize_field(field: StructuredTypeField, value: any): any {
}
}

const defaultValue = _t.computer_default_value ? _t.computer_default_value(field.defaultValue) : field.defaultValue;
if (value === undefined) {
return defaultValue;
}
if (defaultValue === null) {
if (value === null) {
return null;
if (value === undefined || value === null) {
const defaultValue = _t.computer_default_value ? _t.computer_default_value(field.defaultValue) : field.defaultValue;
if (value === undefined) {
if (_t.coerce) {
return _t.coerce(defaultValue);
}
return defaultValue;
}
if (defaultValue === null) {
if (value === null) {
return null;
}
}
}
if (_t.coerce) {
Expand Down
30 changes: 30 additions & 0 deletions packages/node-opcua-types/test/test_encode_decode_extObjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BinaryStream } from "node-opcua-binary-stream";
import { ExtensionObject } from "node-opcua-extension-object";
import { DataType } from "node-opcua-variant";
import { PubSubConfigurationDataType, PubSubConnectionDataType, UABinaryFileDataType } from "../dist";
import "should";

describe("Testing encoding/decoding of complex ExtensionObjects", () => {
it("should encode and decode a large extension object encodeExtensionObject/decodeExtensionObject", async () => {
const value = new PubSubConfigurationDataType({
connections: [new PubSubConnectionDataType({})]
});
const binaryFile = new UABinaryFileDataType({
body: { dataType: DataType.ExtensionObject, value }
});

const stream = new BinaryStream(binaryFile.binaryStoreSize());
binaryFile.encode(stream);

const reloaded = new UABinaryFileDataType();
stream.rewind();

reloaded.decode(stream);

const a = binaryFile.toJSON();
const b = reloaded.toJSON();

a.should.eql(b);
// console.log(a);
});
});
2 changes: 1 addition & 1 deletion packages/node-opcua-types/test/test_issue688.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { ReadValueId } = require("..");// node-opcua-types"

describe("Issue 688", ()=> {

it("should throw an exception when arguement of ReadValueId are invalid", () => {
it("should throw an exception when argument of ReadValueId are invalid", () => {
should(()=>{
const r = new ReadValueId({
nodeId: "someError" // << this invalid node id should cause a exception !
Expand Down
48 changes: 26 additions & 22 deletions packages/node-opcua-variant/source/variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface VariantOptions2 {
export class Variant extends BaseUAObject {
public static schema = schemaVariant;
public static coerce = _coerceVariant;

public static computer_default_value = () => new Variant({ dataType: DataType.Null });
public dataType: DataType;
public arrayType: VariantArrayType;
public value: any;
Expand Down Expand Up @@ -232,10 +232,14 @@ export const VARIANT_ARRAY_DIMENSIONS_MASK = 0x40;
*/
export const VARIANT_TYPE_MASK = 0x3f;

const nullVariant = new Variant({ dataType: DataType.Null });
/***
* @private
*/
export function encodeVariant(variant: Variant, stream: OutputBinaryStream): void {
export function encodeVariant(variant: Variant | undefined | null, stream: OutputBinaryStream): void {
if (!variant) {
variant = nullVariant;
}
let encodingByte = variant.dataType;

if (variant.arrayType === VariantArrayType.Array || variant.arrayType === VariantArrayType.Matrix) {
Expand Down Expand Up @@ -409,7 +413,7 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
// we do nothing here ....
throw new Error(
"Variant#constructor : when using UInt64 ou Int64" +
" arrayType must be specified , as automatic detection cannot be made"
" arrayType must be specified , as automatic detection cannot be made"
);
} else {
options.arrayType = VariantArrayType.Array;
Expand Down Expand Up @@ -438,11 +442,11 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
if (options.value.length !== calculate_product(options.dimensions)) {
throw new Error(
"Matrix Variant : invalid value size = options.value.length " +
options.value.length +
"!=" +
calculate_product(options.dimensions) +
" => " +
JSON.stringify(options.dimensions)
options.value.length +
"!=" +
calculate_product(options.dimensions) +
" => " +
JSON.stringify(options.dimensions)
);
}
}
Expand All @@ -457,14 +461,14 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
if (!isValidVariant(options.arrayType, options.dataType, options.value, null)) {
throw new Error(
"Invalid variant arrayType: " +
VariantArrayType[options.arrayType] +
" dataType: " +
DataType[options.dataType] +
" value:" +
options.value +
" (javascript type = " +
typeof options.value +
" )"
VariantArrayType[options.arrayType] +
" dataType: " +
DataType[options.dataType] +
" value:" +
options.value +
" (javascript type = " +
typeof options.value +
" )"
);
}
}
Expand Down Expand Up @@ -519,7 +523,7 @@ export type BufferedArray2 =

interface BufferedArrayConstructor {
BYTES_PER_ELEMENT: number;
new(buffer: any): any;
new (buffer: any): any;
}

function convertTo(dataType: DataType, arrayTypeConstructor: BufferedArrayConstructor | null, value: any) {
Expand Down Expand Up @@ -678,7 +682,7 @@ function _decodeVariantArrayDebug(stream: BinaryStream, decode: any, tracer: any
let i;
let element;
tracer.trace("start_array", "Variant", -1, cursorBefore, stream.length);
if (length === 0xFFFFFFFF) {
if (length === 0xffffffff) {
// empty array
tracer.trace("end_array", "Variant", stream.length);
return;
Expand Down Expand Up @@ -805,10 +809,10 @@ export function coerceVariantType(dataType: DataType, value: any): any {
function isValidScalarVariant(dataType: DataType, value: any): boolean {
assert(
value === null ||
DataType.Int64 === dataType ||
DataType.ByteString === dataType ||
DataType.UInt64 === dataType ||
!(value instanceof Array)
DataType.Int64 === dataType ||
DataType.ByteString === dataType ||
DataType.UInt64 === dataType ||
!(value instanceof Array)
);
assert(value === null || !(value instanceof Int32Array));
assert(value === null || !(value instanceof Uint32Array));
Expand Down

0 comments on commit e3264db

Please sign in to comment.