Skip to content

Commit 0b394bb

Browse files
committed
add deserialization for the missing values (set-to-null) [#16]
1 parent e19d7a9 commit 0b394bb

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

bunq/sdk/json/converter.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class JsonAdapter(object):
2929
_PREFIX_KEY_PROTECTED = '_'
3030

3131
# Constants to fetch param types from the docstrings
32-
_PATTERN_PARAM_TYPES = ':type (_?{}): ([\w.]+)(?:\[([\w.]+)\])?'
32+
_TEMPLATE_PATTERN_PARAM_TYPES = ':type (_?{}): ([\w.]+)(?:\[([\w.]+)\])?'
33+
_PATTERN_PARAM_NAME_TYPED_ANY = ':type (\w+):'
3334
_SUBMATCH_INDEX_NAME = 1
3435
_SUBMATCH_INDEX_TYPE_MAIN = 2
3536
_SUBMATCH_INDEX_TYPE_SUB = 3
@@ -163,7 +164,9 @@ def _deserialize_dict(cls, cls_target, dict_):
163164
"""
164165

165166
instance = cls_target.__new__(cls_target, cls_target)
166-
instance.__dict__ = cls._deserialize_dict_attributes(cls_target, dict_)
167+
dict_deserialized = cls._deserialize_dict_attributes(cls_target, dict_)
168+
instance.__dict__ = cls._set_default_values(cls_target,
169+
dict_deserialized)
167170

168171
return instance
169172

@@ -192,18 +195,6 @@ def _deserialize_dict_attributes(cls, cls_context, dict_):
192195

193196
return dict_deserialized
194197

195-
@classmethod
196-
def _warn_key_unknown(cls, cls_context, key):
197-
"""
198-
:type cls_context: type
199-
:type key: str
200-
201-
:rtype: None
202-
"""
203-
204-
context_name = cls_context.__name__
205-
warnings.warn(cls._WARNING_KEY_UNKNOWN.format(key, context_name))
206-
207198
@classmethod
208199
def _deserialize_key(cls, key):
209200
"""
@@ -240,7 +231,7 @@ def _fetch_attribute_specs_from_doc(cls, cls_in, attribute_name):
240231
:rtype: ValueSpecs
241232
"""
242233

243-
pattern = cls._PATTERN_PARAM_TYPES.format(attribute_name)
234+
pattern = cls._TEMPLATE_PATTERN_PARAM_TYPES.format(attribute_name)
244235
match = re.search(pattern, cls_in.__doc__)
245236

246237
if match is not None:
@@ -366,6 +357,37 @@ def _deserialize_list(cls, type_item, list_):
366357

367358
return list_deserialized
368359

360+
@classmethod
361+
def _warn_key_unknown(cls, cls_context, key):
362+
"""
363+
:type cls_context: type
364+
:type key: str
365+
366+
:rtype: None
367+
"""
368+
369+
context_name = cls_context.__name__
370+
warnings.warn(cls._WARNING_KEY_UNKNOWN.format(key, context_name))
371+
372+
@classmethod
373+
def _set_default_values(cls, cls_context, dict_):
374+
"""
375+
:type cls_context: type
376+
:type dict_: dict
377+
378+
:rtype: dict
379+
"""
380+
381+
dict_with_default_values = dict_.copy()
382+
params = re.findall(cls._PATTERN_PARAM_NAME_TYPED_ANY,
383+
cls_context.__doc__)
384+
385+
for param in params:
386+
if param not in dict_with_default_values:
387+
dict_with_default_values[param] = None
388+
389+
return dict_with_default_values
390+
369391
@classmethod
370392
def can_serialize(cls):
371393
"""

0 commit comments

Comments
 (0)