Skip to content

Commit

Permalink
schema: add default function to SChoice class
Browse files Browse the repository at this point in the history
This patches introduces a default function to SChoice class,
including unit test.

Fixes: #97
Signed-off-by: Stefan Gula <[email protected]>
Acked-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Jan 30, 2024
1 parent 1a069b9 commit 6e94f1b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,19 @@ struct lysc_when {

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

struct lysc_node_case {
struct lysc_node *child;
struct lysc_when **when;
...;
};

struct lysc_node_choice {
struct lysc_node_case *cases;
struct lysc_when **when;
struct lysc_node_case *dflt;
...;
};

#define LYD_DEFAULT ...
#define LYD_WHEN_TRUE ...
#define LYD_NEW ...
Expand Down
4 changes: 4 additions & 0 deletions libyang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
Must,
Pattern,
Revision,
SCase,
SChoice,
SContainer,
SLeaf,
SLeafList,
Expand Down Expand Up @@ -156,6 +158,8 @@
"RangeAdded",
"RangeRemoved",
"Revision",
"SCase",
"SChoice",
"SContainer",
"SLeaf",
"SLeafList",
Expand Down
11 changes: 11 additions & 0 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,12 @@ def children(
# -------------------------------------------------------------------------------------
@SNode.register(SNode.CHOICE)
class SChoice(SNode):
__slots__ = ("cdata_choice",)

def __init__(self, context: "libyang.Context", cdata):
super().__init__(context, cdata)
self.cdata_choice = ffi.cast("struct lysc_node_choice *", cdata)

def __iter__(self) -> Iterator[SNode]:
return self.children()

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

def default(self) -> Optional[SNode]:
if self.cdata_choice.dflt == ffi.NULL:
return None
return SNode.new(self.context, self.cdata_choice.dflt)


# -------------------------------------------------------------------------------------
@SNode.register(SNode.CASE)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
Must,
Pattern,
Revision,
SCase,
SChoice,
SContainer,
SLeaf,
SLeafList,
Expand Down Expand Up @@ -584,3 +586,20 @@ def test_leaf_list_min_max(self):
self.assertIsInstance(leaflist2, SLeafList)
self.assertEqual(leaflist2.min_elements(), 0)
self.assertEqual(leaflist2.max_elements(), None)


# -------------------------------------------------------------------------------------
class ChoiceTest(unittest.TestCase):
def setUp(self):
self.ctx = Context(YANG_DIR)
self.ctx.load_module("yolo-system")

def tearDown(self):
self.ctx.destroy()
self.ctx = None

def test_choice_default(self):
conf = next(self.ctx.find_path("/yolo-system:conf"))
choice = next(conf.children((SNode.CHOICE,), with_choice=True))
self.assertIsInstance(choice, SChoice)
self.assertIsInstance(choice.default(), SCase)
1 change: 1 addition & 0 deletions tests/yang/yolo/yolo-system.yang
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module yolo-system {
type boolean;
}
}
default red;
}

list url {
Expand Down

0 comments on commit 6e94f1b

Please sign in to comment.