Skip to content

Commit

Permalink
Issue #722: Set DataValue.Value to Variant(nil) for no value
Browse files Browse the repository at this point in the history
This patch ensures that DataValue.Value contains a Variant(nil) if there
is no value.

Closes #722
  • Loading branch information
magiconair committed Dec 10, 2024
1 parent d7282d1 commit bada93d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ua/datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type DataValue struct {
func (d *DataValue) Decode(b []byte) (int, error) {
buf := NewBuffer(b)
d.EncodingMask = buf.ReadByte()
d.Value = new(Variant)
if d.Has(DataValueValue) {
d.Value = new(Variant)
buf.ReadStruct(d.Value)
}
if d.Has(DataValueStatusCode) {
Expand Down
45 changes: 43 additions & 2 deletions ua/datatypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import (

func TestDataValue(t *testing.T) {
cases := []CodecTestCase{
{
Name: "no value",
Struct: &DataValue{
EncodingMask: 0x00,
Value: MustVariant(nil),
},
Bytes: []byte{
// EncodingMask
0x00,
},
},
{
Name: "value only",
Struct: &DataValue{
Expand Down Expand Up @@ -45,14 +56,31 @@ func TestDataValue(t *testing.T) {
0x80, 0x3b, 0xe8, 0xb3, 0x92, 0x4e, 0xd4, 0x01,
},
},
{
Name: "source timestamp, server timestamp",
Struct: &DataValue{
EncodingMask: 0x0c,
Value: MustVariant(nil),
SourceTimestamp: time.Date(2018, time.September, 17, 14, 28, 29, 112000000, time.UTC),
ServerTimestamp: time.Date(2018, time.September, 17, 14, 28, 29, 112000000, time.UTC),
},
Bytes: []byte{
// EncodingMask
0x0c,
// SourceTimestamp
0x80, 0x3b, 0xe8, 0xb3, 0x92, 0x4e, 0xd4, 0x01,
// SeverTimestamp
0x80, 0x3b, 0xe8, 0xb3, 0x92, 0x4e, 0xd4, 0x01,
},
},
}
RunCodecTest(t, cases)
}

func TestDataValueArray(t *testing.T) {
cases := []CodecTestCase{
{
Name: "value only and value, source timestamp, server timestamp",
Name: "value only; value, source timestamp, server timestamp; source timestamp, server timestamp",
Struct: []*DataValue{
{
EncodingMask: 0x01,
Expand All @@ -64,10 +92,16 @@ func TestDataValueArray(t *testing.T) {
SourceTimestamp: time.Date(2018, time.September, 17, 14, 28, 29, 112000000, time.UTC),
ServerTimestamp: time.Date(2018, time.September, 17, 14, 28, 29, 112000000, time.UTC),
},
{
EncodingMask: 0x0c,
Value: MustVariant(nil),
SourceTimestamp: time.Date(2018, time.September, 17, 14, 28, 29, 112000000, time.UTC),
ServerTimestamp: time.Date(2018, time.September, 17, 14, 28, 29, 112000000, time.UTC),
},
},
Bytes: []byte{
// length
0x02, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00,

// EncodingMask
0x01,
Expand All @@ -84,6 +118,13 @@ func TestDataValueArray(t *testing.T) {
0x80, 0x3b, 0xe8, 0xb3, 0x92, 0x4e, 0xd4, 0x01,
// ServerTimestamp
0x80, 0x3b, 0xe8, 0xb3, 0x92, 0x4e, 0xd4, 0x01,

// EncodingMask
0x0c,
// SourceTimestamp
0x80, 0x3b, 0xe8, 0xb3, 0x92, 0x4e, 0xd4, 0x01,
// ServerTimestamp
0x80, 0x3b, 0xe8, 0xb3, 0x92, 0x4e, 0xd4, 0x01,
},
},
}
Expand Down

0 comments on commit bada93d

Please sign in to comment.