From c7f40190b90f7eb908d8589923359804d36f52d4 Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Sat, 11 Nov 2023 23:48:04 +0100 Subject: [PATCH] Fixed/Enhanced union_types function This patch fixes union_types output in case of using typedefs, which uses union within another typedefs/unions --- libyang/schema.py | 13 ++++++++++++- tests/test_schema.py | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/libyang/schema.py b/libyang/schema.py index 6b0c803b..bae659c3 100644 --- a/libyang/schema.py +++ b/libyang/schema.py @@ -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) diff --git a/tests/test_schema.py b/tests/test_schema.py index f493ba14..372ae3a2 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -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()}>"