laforge submitted this change.

View Change


Approvals: Jenkins Builder: Verified laforge: Looks good to me, approved
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(-)

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):

To view, visit change 35447. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I07e4feb3800b420d8be7aae8911f828f1da9dab8
Gerrit-Change-Number: 35447
Gerrit-PatchSet: 2
Gerrit-Owner: laforge <laforge@osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-MessageType: merged