Skip to content

Commit

Permalink
Fixed/Enhanced union_types function
Browse files Browse the repository at this point in the history
This patch fixes union_types output in case of using
typedefs which uses union within with another typedefs/unions
  • Loading branch information
steweg committed Dec 7, 2023
1 parent 5b3f698 commit cb412b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,16 +555,27 @@ def typedef(self) -> "Typedef":
return import_module.get_typedef(type_name)
return None

def union_types(self) -> Iterator["Type"]:
def union_types(self, with_typedefs: bool = False) -> Iterator["Type"]:
if self.cdata.basetype != self.UNION:
return

typedef = self.typedef()
t = ffi.cast("struct lysc_type_union *", self.cdata)
if self.cdata_parsed and self.cdata_parsed.types != ffi.NULL:
for union_type, union_type_parsed in zip(
ly_array_iter(t.types), ly_array_iter(self.cdata_parsed.types)
):
yield Type(self.context, union_type, union_type_parsed)
elif (
with_typedefs
and typedef
and typedef.cdata
and typedef.cdata.type.types != ffi.NULL
):
for union_type, union_type_parsed in zip(
ly_array_iter(t.types), ly_array_iter(typedef.cdata.type.types)
):
yield Type(self.context, union_type, union_type_parsed)
else:
for union_type in ly_array_iter(t.types):
yield Type(self.context, union_type, None)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ def test_leaf_type_union(self):
self.assertEqual(t.name(), "types:number")
self.assertEqual(t.base(), Type.UNION)
types = set(u.name() for u in t.union_types())
types2 = set(u.name() for u in t.union_types(with_typedefs=True))
self.assertEqual(types, set(["int16", "int32", "uint16", "uint32"]))
self.assertEqual(types2, set(["signed", "unsigned"]))
for u in t.union_types():
ext = u.get_extension(
"type-desc", prefix="omg-extensions", arg_value=f"<{u.name()}>"
Expand Down

0 comments on commit cb412b0

Please sign in to comment.