laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/35447?usp=email )
Change subject: tlv: Fix from_dict() symmetry ......................................................................
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 --- M pySim/tlv.py 1 file changed, 21 insertions(+), 4 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/47/35447/1
diff --git a/pySim/tlv.py b/pySim/tlv.py index 6ea7dd1..4ac5ebf 100644 --- a/pySim/tlv.py +++ b/pySim/tlv.py @@ -157,13 +157,13 @@ 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):