Skip to content

Commit

Permalink
Merge pull request #6 from admire93/bugfix/deserializer-21
Browse files Browse the repository at this point in the history
Deserialize recursively to deserialize boxed type
  • Loading branch information
kanghyojun authored Jul 12, 2016
2 parents 60cad1c + e57904a commit bc1fb46
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
4 changes: 4 additions & 0 deletions nirum/deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def deserialize_meta(cls, data):


def deserialize_boxed_type(cls, value):
deserializer = getattr(cls.__nirum_boxed_type__,
'__nirum_deserialize__', None)
if deserializer:
value = deserializer(value)
return cls(value=value)


Expand Down
13 changes: 1 addition & 12 deletions nirum/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@


def validate_boxed_type(boxed, type_hint) -> bool:
actual_boxed_val_type = type(boxed)
while True:
try:
actual_boxed_val_type = actual_boxed_val_type.__nirum_boxed_type__
except AttributeError:
break
while True:
try:
type_hint = type_hint.__nirum_boxed_type__
except AttributeError:
break
if actual_boxed_val_type != type_hint:
if not isinstance(boxed, type_hint):
raise TypeError('{0} expected, found: {1}'.format(type_hint,
type(boxed)))
return boxed
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def __repr__(self) -> str:

class C:

__nirum_boxed_type__ = A
__nirum_boxed_type__ = B

def __init__(self, value: B) -> None:
validate_boxed_type(value, B)
Expand Down
8 changes: 8 additions & 0 deletions tests/deserialize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ def test_deserialize_meta_boxed(fx_boxed_type, fx_record_type, fx_point):
meta = deserialize_meta(fx_boxed_type, v)
boxed = fx_boxed_type(v)
assert meta == boxed


def test_deserialize_multiple_boxed_type(fx_layered_boxed_types):
A, B, C = fx_layered_boxed_types
assert B.__nirum_deserialize__('lorem') == B(A('lorem'))
assert C.__nirum_deserialize__('x') == C(B(A('x')))
with raises(TypeError):
B.__nirum_deserialize__(1)
6 changes: 6 additions & 0 deletions tests/serialize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ def test_serialize_union_type(fx_point, fx_offset, fx_circle_type,
'lower_right': serialize_record_type(fx_point),
}
assert serialize_union_type(rectangle) == s


def test_multiple_boxed_type(fx_layered_boxed_types):
A, B, _ = fx_layered_boxed_types
assert B(A('hello')).value.value == 'hello'
assert B(A('lorem')).__nirum_serialize__() == 'lorem'
13 changes: 12 additions & 1 deletion tests/validate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ def test_validate_union_type(fx_rectangle, fx_rectangle_type, fx_point):


def test_validate_layered_boxed_types(fx_layered_boxed_types):
assert validate_boxed_type('test', fx_layered_boxed_types[1])
A, B, C = fx_layered_boxed_types
assert validate_boxed_type('test', str)
assert validate_boxed_type(A('test'), A)
assert validate_boxed_type(B(A('test')), B)
with raises(TypeError):
assert validate_boxed_type('test', A)

with raises(TypeError):
assert validate_boxed_type('test', B)

with raises(TypeError):
assert validate_boxed_type(A('test'), B)

0 comments on commit bc1fb46

Please sign in to comment.