Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema: add default function to SChoice class #97

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -77,6 +77,8 @@
Must,
Pattern,
Revision,
SCase,
SChoice,
SContainer,
SLeaf,
SLeafList,
Expand Down Expand Up @@ -155,6 +157,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
Loading