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

Adding min/max_elements #89

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
12 changes: 12 additions & 0 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,12 @@ def must_conditions(self) -> Iterator[str]:
for must in ly_array_iter(pdata.musts):
yield c2str(must.arg.str)

def max_elements(self) -> int:
return self.cdata_leaflist.max if self.cdata_leaflist.max != 0 else None

def min_elements(self) -> int:
return self.cdata_leaflist.min

def __str__(self):
return "%s %s" % (self.name(), self.type().name())

Expand Down Expand Up @@ -1416,6 +1422,12 @@ def must_conditions(self) -> Iterator[str]:
for must in ly_array_iter(pdata.musts):
yield c2str(must.arg.str)

def max_elements(self) -> int:
return self.cdata_list.max if self.cdata_list.max != 0 else None

def min_elements(self) -> int:
return self.cdata_list.min

def __str__(self):
return "%s [%s]" % (self.name(), ", ".join(k.name() for k in self.keys()))

Expand Down
14 changes: 14 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,13 @@ def test_list_parent(self):
self.assertIsInstance(parent, SContainer)
self.assertEqual(parent.name(), "conf")

def test_list_min_max(self):
self.ctx.load_module("yolo-nodetypes")
self.list = next(self.ctx.find_path("/yolo-nodetypes:conf/list1"))
self.assertIsInstance(self.list, SList)
self.assertEqual(self.list.min_elements(), 2)
self.assertEqual(self.list.max_elements(), 10)


# -------------------------------------------------------------------------------------
class RpcTest(unittest.TestCase):
Expand Down Expand Up @@ -515,3 +522,10 @@ def test_leaflist_defaults(self):
leaflist = next(self.ctx.find_path("/yolo-nodetypes:conf/ratios"))
for d in leaflist.defaults():
self.assertIsInstance(d, float)

def test_leaf_list_min_max(self):
self.ctx.load_module("yolo-nodetypes")
node = next(self.ctx.find_path("/yolo-nodetypes:conf/leaf-list1"))
self.assertIsInstance(node, SLeafList)
self.assertEqual(node.min_elements(), 3)
self.assertEqual(node.max_elements(), 11)
22 changes: 22 additions & 0 deletions tests/yang/yolo/yolo-nodetypes.yang
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,27 @@ module yolo-nodetypes {
default 2.5;
default 2.6;
}

list list1 {
key leaf1;
unique "leaf2 leaf3";
min-elements 2;
max-elements 10;
leaf leaf1 {
type string;
}
leaf leaf2 {
type string;
}
leaf leaf3 {
type string;
}
}

leaf-list leaf-list1 {
type string;
min-elements 3;
max-elements 11;
}
}
}
Loading