tlv: Fix from_dict() symmetry

the to_dict() method generates a {class_name: value} dictionary,
for both the nested and non-nested case.  However, before this patch,
the from_dict() method expects a plain list of child IE dicts
in the nested case.  This is illogical.

Let's make sure from_dict always expectes a {class_name: value} dict
for both nested and non-nested situations.

Change-Id: I07e4feb3800b420d8be7aae8911f828f1da9dab8
This commit is contained in:
Harald Welte
2023-12-27 15:52:11 +01:00
committed by laforge
parent a5eb924f9e
commit a9b21bdb1f

View File

@@ -157,13 +157,13 @@ class IE(Transcodable, metaclass=TlvMeta):
def from_dict(self, decoded: dict):
"""Set the IE internal decoded representation to data from the argument.
If this is a nested IE, the child IE instance list is re-created."""
expected_key_name = camel_to_snake(type(self).__name__)
if not expected_key_name in decoded:
raise ValueError("Dict %s doesn't contain expected key %s" % (decoded, expected_key_name))
if self.nested_collection:
self.children = self.nested_collection.from_dict(decoded)
self.children = self.nested_collection.from_dict(decoded[expected_key_name])
else:
self.children = []
expected_key_name = camel_to_snake(type(self).__name__)
if not expected_key_name in decoded:
raise ValueError("Dict %s doesn't contain expected key %s" % (decoded, expected_key_name))
self.decoded = decoded[expected_key_name]
def is_constructed(self):