Skip to content

Commit bfe8a0c

Browse files
samuel-gauthierrjarry
authored andcommitted
schema: add parsed schema for union types
When available, let's add the parsed schema for union types, they will be more complete, adding names, extensions, ranges, units, patterns, etc. Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com> Acked-by: Robin Jarry <robin@jarry.cc>
1 parent 93676bc commit bfe8a0c

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

libyang/schema.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,16 @@ def leafref_path(self) -> Optional["str"]:
479479
def union_types(self) -> Iterator["Type"]:
480480
if self.cdata.basetype != self.UNION:
481481
return
482+
482483
t = ffi.cast("struct lysc_type_union *", self.cdata)
483-
for union_type in ly_array_iter(t.types):
484-
yield Type(self.context, union_type, None)
484+
if self.cdata_parsed and self.cdata_parsed.types != ffi.NULL:
485+
for union_type, union_type_parsed in zip(
486+
ly_array_iter(t.types), ly_array_iter(self.cdata_parsed.types)
487+
):
488+
yield Type(self.context, union_type, union_type_parsed)
489+
else:
490+
for union_type in ly_array_iter(t.types):
491+
yield Type(self.context, union_type, None)
485492

486493
def enums(self) -> Iterator[Enum]:
487494
if self.cdata.basetype != self.ENUM:

tests/test_schema.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ def test_leaf_type_union(self):
406406
self.assertEqual(t.base(), Type.UNION)
407407
types = set(u.name() for u in t.union_types())
408408
self.assertEqual(types, set(["int16", "int32", "uint16", "uint32"]))
409+
for u in t.union_types():
410+
ext = u.get_extension(
411+
"type-desc", prefix="omg-extensions", arg_value=f"<{u.name()}>"
412+
)
413+
self.assertIsInstance(ext, Extension)
414+
self.assertEqual(len(list(u.extensions())), 2)
409415
bases = set(t.basenames())
410416
self.assertEqual(bases, set(["int16", "int32", "uint16", "uint32"]))
411417

tests/yang/wtf/wtf-types.yang

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module wtf-types {
22
namespace "urn:yang:wtf:types";
33
prefix t;
44

5+
import omg-extensions { prefix ext; }
6+
57
typedef str {
68
type string;
79
}
@@ -12,17 +14,32 @@ module wtf-types {
1214
}
1315
}
1416

17+
extension signed;
18+
extension unsigned;
19+
1520
typedef unsigned {
1621
type union {
17-
type uint16;
18-
type uint32;
22+
type uint16 {
23+
t:unsigned;
24+
ext:type-desc "<uint16>";
25+
}
26+
type uint32 {
27+
ext:type-desc "<uint32>";
28+
t:unsigned;
29+
}
1930
}
2031
}
2132

2233
typedef signed {
2334
type union {
24-
type int16;
25-
type int32;
35+
type int16 {
36+
ext:type-desc "<int16>";
37+
t:signed;
38+
}
39+
type int32 {
40+
ext:type-desc "<int32>";
41+
t:signed;
42+
}
2643
}
2744
}
2845

0 commit comments

Comments
 (0)