Add Union on top level union messages
Closes https://github.com/capnproto/pycapnp/issues/247
This commit is contained in:
@@ -3175,6 +3175,13 @@ class _StructModule(object):
|
|||||||
for union_field in field_schema.fields:
|
for union_field in field_schema.fields:
|
||||||
setattr(sub_module, union_field.name, union_field.discriminantValue)
|
setattr(sub_module, union_field.name, union_field.discriminantValue)
|
||||||
setattr(self, name, sub_module)
|
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):
|
def read(self, file, traversal_limit_in_words=None, nesting_limit=None):
|
||||||
"""Returns a Reader for the unpacked object read from file.
|
"""Returns a Reader for the unpacked object read from file.
|
||||||
|
|||||||
@@ -50,3 +50,21 @@ struct TestAllTypes {
|
|||||||
enumList @32 : List(TestEnum);
|
enumList @32 : List(TestEnum);
|
||||||
interfaceList @33 : List(Void); # TODO
|
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):
|
def test_set_dict_union(addressbook):
|
||||||
person = addressbook.Person.new_message(**{'employment': {'employer': {'name': 'foo'}}})
|
person = addressbook.Person.new_message(**{'employment': {'employer': {'name': 'foo'}}})
|
||||||
|
|
||||||
|
assert person.employment.which == addressbook.Person.Employment.employer
|
||||||
|
|
||||||
assert person.employment.employer.name == 'foo'
|
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):
|
def isstr(s):
|
||||||
return isinstance(s, str)
|
return isinstance(s, str)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user