Skip to content

Commit 6e94f1b

Browse files
stewegsamuel-gauthier
authored andcommitted
schema: add default function to SChoice class
This patches introduces a default function to SChoice class, including unit test. Fixes: #97 Signed-off-by: Stefan Gula <steweg@gmail.com> Acked-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 1a069b9 commit 6e94f1b

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

cffi/cdefs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,19 @@ struct lysc_when {
10231023

10241024
struct lysc_when** lysc_node_when(const struct lysc_node *);
10251025

1026+
struct lysc_node_case {
1027+
struct lysc_node *child;
1028+
struct lysc_when **when;
1029+
...;
1030+
};
1031+
1032+
struct lysc_node_choice {
1033+
struct lysc_node_case *cases;
1034+
struct lysc_when **when;
1035+
struct lysc_node_case *dflt;
1036+
...;
1037+
};
1038+
10261039
#define LYD_DEFAULT ...
10271040
#define LYD_WHEN_TRUE ...
10281041
#define LYD_NEW ...

libyang/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
Must,
7979
Pattern,
8080
Revision,
81+
SCase,
82+
SChoice,
8183
SContainer,
8284
SLeaf,
8385
SLeafList,
@@ -156,6 +158,8 @@
156158
"RangeAdded",
157159
"RangeRemoved",
158160
"Revision",
161+
"SCase",
162+
"SChoice",
159163
"SContainer",
160164
"SLeaf",
161165
"SLeafList",

libyang/schema.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,12 @@ def children(
14161416
# -------------------------------------------------------------------------------------
14171417
@SNode.register(SNode.CHOICE)
14181418
class SChoice(SNode):
1419+
__slots__ = ("cdata_choice",)
1420+
1421+
def __init__(self, context: "libyang.Context", cdata):
1422+
super().__init__(context, cdata)
1423+
self.cdata_choice = ffi.cast("struct lysc_node_choice *", cdata)
1424+
14191425
def __iter__(self) -> Iterator[SNode]:
14201426
return self.children()
14211427

@@ -1424,6 +1430,11 @@ def children(
14241430
) -> Iterator[SNode]:
14251431
return iter_children(self.context, self.cdata, types=types, with_case=with_case)
14261432

1433+
def default(self) -> Optional[SNode]:
1434+
if self.cdata_choice.dflt == ffi.NULL:
1435+
return None
1436+
return SNode.new(self.context, self.cdata_choice.dflt)
1437+
14271438

14281439
# -------------------------------------------------------------------------------------
14291440
@SNode.register(SNode.CASE)

tests/test_schema.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
Must,
1616
Pattern,
1717
Revision,
18+
SCase,
19+
SChoice,
1820
SContainer,
1921
SLeaf,
2022
SLeafList,
@@ -584,3 +586,20 @@ def test_leaf_list_min_max(self):
584586
self.assertIsInstance(leaflist2, SLeafList)
585587
self.assertEqual(leaflist2.min_elements(), 0)
586588
self.assertEqual(leaflist2.max_elements(), None)
589+
590+
591+
# -------------------------------------------------------------------------------------
592+
class ChoiceTest(unittest.TestCase):
593+
def setUp(self):
594+
self.ctx = Context(YANG_DIR)
595+
self.ctx.load_module("yolo-system")
596+
597+
def tearDown(self):
598+
self.ctx.destroy()
599+
self.ctx = None
600+
601+
def test_choice_default(self):
602+
conf = next(self.ctx.find_path("/yolo-system:conf"))
603+
choice = next(conf.children((SNode.CHOICE,), with_choice=True))
604+
self.assertIsInstance(choice, SChoice)
605+
self.assertIsInstance(choice.default(), SCase)

tests/yang/yolo/yolo-system.yang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module yolo-system {
7272
type boolean;
7373
}
7474
}
75+
default red;
7576
}
7677

7778
list url {

0 commit comments

Comments
 (0)