Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -420,7 +420,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