Skip to content

Commit

Permalink
support encoding variant of null array
Browse files Browse the repository at this point in the history
  • Loading branch information
oroulet committed Dec 28, 2016
1 parent 727d420 commit 344b73e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 6 additions & 5 deletions opcua/ua/uatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,11 @@ def __str__(self):
def to_binary(self):
b = []
encoding = self.VariantType.value & 0b111111
if type(self.Value) in (list, tuple):
if self.is_array or type(self.Value) in (list, tuple):
self.is_array = True
encoding = uabin.set_bit(encoding, 7)
if self.Dimensions is not None:
encoding = uabin.set_bit(encoding, 6)
encoding = uabin.set_bit(encoding, 7)
b.append(uabin.Primitives.UInt8.pack(encoding))
b.append(uabin.pack_uatype_array(self.VariantType, flatten(self.Value)))
if self.Dimensions is not None:
Expand All @@ -842,8 +843,6 @@ def from_binary(data):
encoding = ord(data.read(1))
int_type = encoding & 0b00111111
vtype = datatype_to_varianttype(int_type)
if vtype == VariantType.Null:
return Variant(None, vtype, encoding)
if uabin.test_bit(encoding, 7):
value = uabin.unpack_uatype_array(vtype, data)
array = True
Expand Down Expand Up @@ -886,7 +885,9 @@ def flatten_and_get_shape(mylist):


def flatten(mylist):
if len(mylist) == 0:
if mylist is None:
return None
elif len(mylist) == 0:
return mylist
while isinstance(mylist[0], (list, tuple)):
mylist = [item for sublist in mylist for item in sublist]
Expand Down
13 changes: 13 additions & 0 deletions tests/tests_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ class TestUnit(unittest.TestCase):
Simple unit test that do not need to setup a server or a client
'''

def test_variant_array_none(self):
v = ua.Variant(None, varianttype=ua.VariantType.Int32, is_array=True)
data = v.to_binary()
v2 = ua.Variant.from_binary(ua.utils.Buffer(data))
self.assertEqual(v, v2)
self.assertTrue(v2.is_array)

v = ua.Variant(None, varianttype=ua.VariantType.Null, is_array=True)
data = v.to_binary()
v2 = ua.Variant.from_binary(ua.utils.Buffer(data))
self.assertEqual(v, v2)
self.assertTrue(v2.is_array)

def test_custom_structs(self):
xmlpath = "tests/example.bsd"
c = StructGenerator(xmlpath, "structures.py")
Expand Down

0 comments on commit 344b73e

Please sign in to comment.