mirror of
https://github.com/capnproto/pycapnp.git
synced 2025-03-04 00:14:45 +01:00
Add Union on top level union messages
Closes https://github.com/capnproto/pycapnp/issues/247
This commit is contained in:
parent
581332f9c9
commit
0c904443bd
3 changed files with 46 additions and 0 deletions
|
@ -3175,6 +3175,13 @@ class _StructModule(object):
|
|||
for union_field in field_schema.fields:
|
||||
setattr(sub_module, union_field.name, union_field.discriminantValue)
|
||||
setattr(self, name, sub_module)
|
||||
if schema.union_fields and not schema.non_union_fields:
|
||||
sub_module = _StructModuleWhich()
|
||||
for union_field in schema.node.struct.fields:
|
||||
name = union_field.name
|
||||
name = name[0].upper() + name[1:]
|
||||
setattr(sub_module, name, union_field.discriminantValue)
|
||||
setattr(self, 'Union', sub_module)
|
||||
|
||||
def read(self, file, traversal_limit_in_words=None, nesting_limit=None):
|
||||
"""Returns a Reader for the unpacked object read from file.
|
||||
|
|
|
@ -50,3 +50,21 @@ struct TestAllTypes {
|
|||
enumList @32 : List(TestEnum);
|
||||
interfaceList @33 : List(Void); # TODO
|
||||
}
|
||||
|
||||
struct UnionAllTypes {
|
||||
union {
|
||||
unionStructField1 @0 : TestAllTypes;
|
||||
unionStructField2 @1 : TestAllTypes;
|
||||
}
|
||||
}
|
||||
|
||||
struct GroupedUnionAllTypes {
|
||||
union {
|
||||
g1 :group {
|
||||
unionStructField1 @0 : TestAllTypes;
|
||||
}
|
||||
g2 :group {
|
||||
unionStructField2 @1 : TestAllTypes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -189,9 +189,30 @@ def test_set_dict(all_types):
|
|||
def test_set_dict_union(addressbook):
|
||||
person = addressbook.Person.new_message(**{'employment': {'employer': {'name': 'foo'}}})
|
||||
|
||||
assert person.employment.which == addressbook.Person.Employment.employer
|
||||
|
||||
assert person.employment.employer.name == 'foo'
|
||||
|
||||
|
||||
def test_union_enum(all_types):
|
||||
assert all_types.UnionAllTypes.Union.UnionStructField1 == 0
|
||||
assert all_types.UnionAllTypes.Union.UnionStructField2 == 1
|
||||
|
||||
msg = all_types.UnionAllTypes.new_message(**{'unionStructField1': {'textField': "foo"}})
|
||||
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField1
|
||||
msg = all_types.UnionAllTypes.new_message(**{'unionStructField2': {'textField': "foo"}})
|
||||
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField2
|
||||
|
||||
assert all_types.GroupedUnionAllTypes.Union.G1 == 0
|
||||
assert all_types.GroupedUnionAllTypes.Union.G2 == 1
|
||||
|
||||
msg = all_types.GroupedUnionAllTypes.new_message(**{'g1': {'unionStructField1': {'textField': "foo"}}})
|
||||
assert msg.which == all_types.GroupedUnionAllTypes.Union.G1
|
||||
|
||||
msg = all_types.GroupedUnionAllTypes.new_message(**{'g2': {'unionStructField2': {'textField': "foo"}}})
|
||||
assert msg.which == all_types.GroupedUnionAllTypes.Union.G2
|
||||
|
||||
|
||||
def isstr(s):
|
||||
return isinstance(s, str)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue