Skip to content

Commit c6caf46

Browse files
stewegsamuel-gauthier
authored andcommitted
schema: add a with_typedefs option to union_types
This patch adds a new with_typedefs option to the union_types function that will return the typedefs instead of the resolved types when available. A unit test is added as well. Fixes: #83 Signed-off-by: Stefan Gula <steweg@gmail.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 6502d47 commit c6caf46

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

libyang/schema.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,16 +555,27 @@ def typedef(self) -> "Typedef":
555555
return import_module.get_typedef(type_name)
556556
return None
557557

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

562+
typedef = self.typedef()
562563
t = ffi.cast("struct lysc_type_union *", self.cdata)
563564
if self.cdata_parsed and self.cdata_parsed.types != ffi.NULL:
564565
for union_type, union_type_parsed in zip(
565566
ly_array_iter(t.types), ly_array_iter(self.cdata_parsed.types)
566567
):
567568
yield Type(self.context, union_type, union_type_parsed)
569+
elif (
570+
with_typedefs
571+
and typedef
572+
and typedef.cdata
573+
and typedef.cdata.type.types != ffi.NULL
574+
):
575+
for union_type, union_type_parsed in zip(
576+
ly_array_iter(t.types), ly_array_iter(typedef.cdata.type.types)
577+
):
578+
yield Type(self.context, union_type, union_type_parsed)
568579
else:
569580
for union_type in ly_array_iter(t.types):
570581
yield Type(self.context, union_type, None)

tests/test_schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ def test_leaf_type_union(self):
420420
self.assertEqual(t.name(), "types:number")
421421
self.assertEqual(t.base(), Type.UNION)
422422
types = set(u.name() for u in t.union_types())
423+
types2 = set(u.name() for u in t.union_types(with_typedefs=True))
423424
self.assertEqual(types, set(["int16", "int32", "uint16", "uint32"]))
425+
self.assertEqual(types2, set(["signed", "unsigned"]))
424426
for u in t.union_types():
425427
ext = u.get_extension(
426428
"type-desc", prefix="omg-extensions", arg_value=f"<{u.name()}>"

0 commit comments

Comments
 (0)