From c179c72087afb971a173804dc678988101e19e0d Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Mon, 31 May 2021 19:31:53 +0800 Subject: [PATCH] Allow enum to be used with .init --- capnp/lib/capnp.pyx | 2 ++ test/test_struct.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 4c0183d..42f96c0 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -1393,6 +1393,8 @@ cdef class _DynamicStructBuilder: :Raises: :exc:`KjException` if the field isn't in this struct """ + if isinstance(field, _StructModuleWhich): + field = field.name[0].lower() + field.name[1:] if size is None: return to_python_builder(self.thisptr.init(field), self._parent) else: diff --git a/test/test_struct.py b/test/test_struct.py index b1bff8b..8dca0fd 100644 --- a/test/test_struct.py +++ b/test/test_struct.py @@ -217,6 +217,9 @@ def test_union_enum(all_types): msg = all_types.GroupedUnionAllTypes.new_message(**{'g2': {'unionStructField2': {'textField': "foo"}}}) assert msg.which == all_types.GroupedUnionAllTypes.Union.G2 + msg = all_types.UnionAllTypes.new_message() + msg.unionStructField2 = msg.init(all_types.UnionAllTypes.Union.UnionStructField2) + def isstr(s): return isinstance(s, str)