Allow enum to be used with .init

This commit is contained in:
John Vandenberg
2021-05-31 19:31:53 +08:00
parent 1795cac230
commit c179c72087
2 changed files with 5 additions and 0 deletions

View File

@@ -1393,6 +1393,8 @@ cdef class _DynamicStructBuilder:
:Raises: :exc:`KjException` if the field isn't in this struct :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: if size is None:
return to_python_builder(self.thisptr.init(field), self._parent) return to_python_builder(self.thisptr.init(field), self._parent)
else: else:

View File

@@ -217,6 +217,9 @@ def test_union_enum(all_types):
msg = all_types.GroupedUnionAllTypes.new_message(**{'g2': {'unionStructField2': {'textField': "foo"}}}) msg = all_types.GroupedUnionAllTypes.new_message(**{'g2': {'unionStructField2': {'textField': "foo"}}})
assert msg.which == all_types.GroupedUnionAllTypes.Union.G2 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): def isstr(s):
return isinstance(s, str) return isinstance(s, str)