Skip to content

Commit 4147016

Browse files
stewegrjarry
authored andcommitted
schema: adds ability to use with_choice option in children calls
This patch adds ability to use with_choice within RPC and Notification Signed-off-by: Stefan Gula <[email protected]>
1 parent 39afe5e commit 4147016

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

libyang/schema.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,8 +1679,12 @@ class SRpcInOut(SNode):
16791679
def __iter__(self) -> Iterator[SNode]:
16801680
return self.children()
16811681

1682-
def children(self, types: Optional[Tuple[int, ...]] = None) -> Iterator[SNode]:
1683-
return iter_children(self.context, self.cdata, types=types)
1682+
def children(
1683+
self, types: Optional[Tuple[int, ...]] = None, with_choice: bool = False
1684+
) -> Iterator[SNode]:
1685+
return iter_children(
1686+
self.context, self.cdata, types=types, with_choice=with_choice
1687+
)
16841688

16851689

16861690
# -------------------------------------------------------------------------------------
@@ -1710,11 +1714,17 @@ def output(self) -> Optional[SRpcInOut]:
17101714
def __iter__(self) -> Iterator[SNode]:
17111715
return self.children()
17121716

1713-
def children(self, types: Optional[Tuple[int, ...]] = None) -> Iterator[SNode]:
1714-
yield from iter_children(self.context, self.cdata, types=types)
1717+
def children(
1718+
self, types: Optional[Tuple[int, ...]] = None, with_choice: bool = False
1719+
) -> Iterator[SNode]:
1720+
yield from iter_children(
1721+
self.context, self.cdata, types=types, with_choice=with_choice
1722+
)
17151723
# With libyang2, you can get only input or output
17161724
# To keep behavior, we iter 2 times witt output options
1717-
yield from iter_children(self.context, self.cdata, types=types, output=True)
1725+
yield from iter_children(
1726+
self.context, self.cdata, types=types, output=True, with_choice=with_choice
1727+
)
17181728

17191729

17201730
# -------------------------------------------------------------------------------------
@@ -1723,8 +1733,12 @@ class SNotif(SNode):
17231733
def __iter__(self) -> Iterator[SNode]:
17241734
return self.children()
17251735

1726-
def children(self, types: Optional[Tuple[int, ...]] = None) -> Iterator[SNode]:
1727-
return iter_children(self.context, self.cdata, types=types)
1736+
def children(
1737+
self, types: Optional[Tuple[int, ...]] = None, with_choice: bool = False
1738+
) -> Iterator[SNode]:
1739+
return iter_children(
1740+
self.context, self.cdata, types=types, with_choice=with_choice
1741+
)
17281742

17291743

17301744
# -------------------------------------------------------------------------------------

tests/test_schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ def test_rpc_attrs(self):
491491
self.assertEqual(self.rpc.nodetype(), SNode.RPC)
492492
self.assertEqual(self.rpc.keyword(), "rpc")
493493
self.assertEqual(self.rpc.schema_path(), "/yolo-system:format-disk")
494+
choice = next(self.rpc.input().children((SNode.CHOICE,), with_choice=True))
495+
self.assertIsInstance(choice, SChoice)
494496

495497
def test_rpc_extensions(self):
496498
ext = list(self.rpc.extensions())

tests/yang/yolo/yolo-system.yang

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,14 @@ module yolo-system {
191191
ext:human-name "Disk";
192192
type types:str;
193193
}
194-
anyxml html-info;
194+
choice xml-or-json {
195+
case xml {
196+
anyxml html-info;
197+
}
198+
case json {
199+
anydata json-info;
200+
}
201+
}
195202
}
196203
output {
197204
leaf duration {

0 commit comments

Comments
 (0)