10000 Merge pull request #91 from dahlia/abc-set-list · nirum-lang/nirum-python@0d00bb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d00bb8

Browse files
authored
Merge pull request #91 from dahlia/abc-set-list
Make serialize_meta() to properly reduce set/like-like types other than built-in set/list
2 parents ca4f214 + ae44034 commit 0d00bb8

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

CHANGES.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
Changelog
22
=========
33

4+
Version 0.4.2
5+
-------------
6+
7+
To be released.
8+
9+
- Fixed a serialization bug that other set-like (i.e. ``collections.Set``) types
10+
than Python built-in ``set`` hadn't been reduced to simpler forms so that they
11+
can be encoded to JSON.
12+
- Fixed a serialization bug that other list-like (i.e. ``collections.Sequence``)
13+
types than Python built-in ``list`` hadn't been reduced to simpler forms so
14+
that they can be encoded to JSON.
15+
16+
17+
418
Version 0.4.1
519
-------------
620

nirum/serialize.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import decimal
88
import uuid
99

10+
from six import string_types
11+
1012
__all__ = (
1113
'serialize_boxed_type', 'serialize_meta',
1214
'serialize_record_type', 'serialize_unboxed_type',
@@ -59,14 +61,18 @@ def serialize_union_type(data):
5961
def serialize_meta(data):
6062
if hasattr(data, '__nirum_serialize__'):
6163
d = data.__nirum_serialize__()
62-
elif type(data) in {str, float, bool, int}:
64+
elif isinstance(data, (string_types, bool, int, float)):
65+
# FIXME: str in py2 represents binary string as well as text string.
66+
# It should be refactored so that the function explicitly takes
67+
# an expected type as like deserialize_meta() does.
6368
d = data
6469
elif (isinstance(data, datetime.datetime) or
6570
isinstance(data, datetime.date)):
6671
d = data.isoformat()
6772
elif isinstance(data, decimal.Decimal) or isinstance(data, uuid.UUID):
6873
d = str(data)
69-
elif isinstance(data, set) or isinstance(data, list):
74+
elif (isinstance(data, collections.Set) or
75+
isinstance(data, collections.Sequence)):
7076
d = [serialize_meta(e) for e in data]
7177
elif isinstance(data, collections.Mapping):
7278
d = [

tests/serialize_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pytest import mark
66

77
from nirum._compat import utc
8+
from nirum.datastructures import List
89
from nirum.serialize import (serialize_unboxed_type, serialize_record_type,
910
serialize_meta, serialize_union_type)
1011
from .nirum_schema import import_nirum_fixture
@@ -94,13 +95,26 @@ def test_serialize_meta_set(d, expect):
9495
e in serialized
9596

9697

98+
def test_serialize_meta_list(fx_record_type, fx_unboxed_type, fx_offset):
99+
record = fx_record_type(fx_offset, fx_offset)
100+
record2 = fx_record_type(fx_unboxed_type(1.1), fx_unboxed_type(1.2))
101+
serialize_result = serialize_meta([record, record2])
102+
assert serialize_result == [
103+
{'_type': 'point', 'x': 1.2, 'top': 1.2},
104+
{'_type': 'point', 'x': 1.1, 'top': 1.2},
105+
]
106+
assert serialize_meta(List([record, record2])) == serialize_result
107+
108+
97109
def test_serialize_meta_set_of_record(fx_record_type, fx_unboxed_type,
98110
fx_offset):
99111
record = fx_record_type(fx_offset, fx_offset)
100112
record2 = fx_record_type(fx_unboxed_type(1.1), fx_unboxed_type(1.2))
101113
serialize_result = serialize_meta({record, record2})
102114
assert record.__nirum_serialize__() in serialize_result
103115
assert record2.__nirum_serialize__() in serialize_result
116+
assert (sorted(serialize_meta(frozenset([record, record2])), key=repr) ==
117+
sorted(serialize_result, key=repr))
104118

105119

106120
def test_serialize_meta_map(fx_point):

0 commit comments

Comments
 (0)
0