Skip to content

Commit

Permalink
patch binary type to string in all container types
Browse files Browse the repository at this point in the history
  • Loading branch information
Guo Zi-Xing committed Nov 10, 2022
1 parent 43d0efb commit 638fa24
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 170 deletions.
19 changes: 19 additions & 0 deletions tests/container.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,22 @@ struct MixItem {
1: optional list<map<string, string>> list_map,
2: optional map<string, list<string>> map_list,
}

struct BinListStruct {
1: optional list<ListItem> list_items,
}

struct BinListItem {
1: optional list<binary> list_binary,
2: optional list<list<binary>> list_list_binary,
}

struct BinMapItem {
1: optional map<binary, binary> map_binary,
2: optional map<binary, map<binary, binary>> map_map_binary,
}

struct BinMixItem {
1: optional list<map<binary, binary>> list_map,
2: optional map<binary, list<binary>> map_list,
}
2 changes: 1 addition & 1 deletion tests/test_all_protocols_binary_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def test_complex_map():
spec=(TType.STRING, TType.BINARY))
b2.flush()

assert b1.getvalue() != b2.getvalue()
assert b1.getvalue() == b2.getvalue()


type_map = {
Expand Down
62 changes: 62 additions & 0 deletions tests/test_protocol_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from thriftpy2.thrift import TType, TPayload
from thriftpy2.utils import hexlify
from thriftpy2.protocol import binary as proto
from thriftpy2 import load
from thriftpy2.utils import serialize


class TItem(TPayload):
Expand Down Expand Up @@ -160,3 +162,63 @@ def test_write_huge_struct():
b = BytesIO()
item = TItem(id=12345, phones=["1234567890"] * 100000)
proto.TBinaryProtocol(b).write_struct(item)


def test_string_binary_equivalency():
from thriftpy2.protocol.binary import TBinaryProtocolFactory
from thriftpy2.protocol.cybin import TCyBinaryProtocolFactory
string_binary_equivalency(TBinaryProtocolFactory)
string_binary_equivalency(TCyBinaryProtocolFactory)


def string_binary_equivalency(proto_factory):
container = load("./container.thrift")
l_item = container.ListItem()
l_item.list_string = ['foo', 'bar']
l_item.list_list_string = [['foo', 'bar']]

bl_item = container.BinListItem()
bl_item.list_binary = ['foo', 'bar']
bl_item.list_list_binary = [['foo', 'bar']]

assert serialize(l_item, proto_factory=proto_factory()) == serialize(
l_item, proto_factory=proto_factory())

m_item = container.MapItem()
m_item.map_string = {'foo': 'bar'}
m_item.map_map_string = {'foo': {'hello': 'world'}}

bm_item = container.BinMapItem()
bm_item.map_binary = {'foo': 'bar'}
bm_item.map_map_binary = {'foo': {'hello': 'world'}}

assert serialize(m_item, proto_factory=proto_factory()) == serialize(
bm_item, proto_factory=proto_factory())

x_item = container.MixItem()
x_item.list_map = [{'foo': 'bar'}]
x_item.map_list = {'foo': ['hello', 'world']}

bx_item = container.BinMixItem()
bx_item.list_map = [{'foo': 'bar'}]
bx_item.map_list = {'foo': ['hello', 'world']}

assert serialize(x_item, proto_factory=proto_factory()) == serialize(
bx_item, proto_factory=proto_factory())

l_item = container.ListItem()
l_item.list_string = ['foo', 'bar'] * 100
l_item.list_list_string = [['foo', 'bar']] * 100

l_struct = container.ListStruct()
l_struct.list_items = [l_item] * 100

bl_item = container.BinListItem()
bl_item.list_binary = ['foo', 'bar'] * 100
bl_item.list_list_binary = [['foo', 'bar']] * 100

bl_struct = container.BinListStruct()
bl_struct.list_items = [l_item] * 100

assert serialize(l_struct, proto_factory=proto_factory()) == serialize(
bl_struct, proto_factory=proto_factory())
Loading

0 comments on commit 638fa24

Please sign in to comment.