Skip to content

Commit

Permalink
Adding min/max_elements and uniques
Browse files Browse the repository at this point in the history
This patch add ability to get minumum and maximum number of elements.
It also adds ability to get list of SList unique definitions.
  • Loading branch information
steweg committed Jan 25, 2024
1 parent 097412c commit 62bef51
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
25 changes: 24 additions & 1 deletion libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT

from contextlib import suppress
from typing import IO, Any, Dict, Iterator, Optional, Tuple, Union
from typing import IO, Any, Dict, Iterator, List, Optional, Tuple, Union

from _libyang import ffi, lib
from .util import IOType, LibyangError, c2str, init_output, ly_array_iter, str2c
Expand Down Expand Up @@ -1293,6 +1293,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 @@ -1384,6 +1390,23 @@ 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 uniques(self) -> Iterator[List[SNode]]:
if self.cdata_list == ffi.NULL:
return
if self.cdata_list.uniques == ffi.NULL:
return
for unique in ly_array_iter(self.cdata_list.uniques):
nodes = []
for node in ly_array_iter(unique):
nodes.append(SNode.new(self.context, node))
yield nodes

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

Expand Down
36 changes: 36 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,24 @@ 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)

def test_list_uniques(self):
self.ctx.load_module("yolo-nodetypes")
self.list = next(self.ctx.find_path("/yolo-nodetypes:conf/list1"))
self.assertIsInstance(self.list, SList)
uniques = [u for u in self.list.uniques()]
self.assertEqual(len(uniques), 1)
elements = [u.name() for u in uniques[0]]
self.assertEqual(len(elements), 2)
self.assertTrue("leaf2" in elements)
self.assertTrue("leaf3" in elements)


# -------------------------------------------------------------------------------------
class RpcTest(unittest.TestCase):
Expand Down Expand Up @@ -474,3 +492,21 @@ def test_leaf_parent(self):
def test_iter_tree(self):
leaf = next(self.ctx.find_path("/yolo-system:conf"))
self.assertEqual(len(list(leaf.iter_tree(full=True))), 23)


# -------------------------------------------------------------------------------------
class LeafListTest(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_leaf_list_min_max(self):
self.ctx.load_module("yolo-nodetypes")
self.list = next(self.ctx.find_path("/yolo-nodetypes:conf/leaf-list1"))
self.assertIsInstance(self.list, SLeafList)
self.assertEqual(self.list.min_elements(), 3)
self.assertEqual(self.list.max_elements(), 11)
54 changes: 54 additions & 0 deletions tests/yang/yolo/yolo-nodetypes.yang
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module yolo-nodetypes {
yang-version 1.1;
namespace "urn:yang:yolo:nodetypes";
prefix sys;

description
"YOLO Nodetypes.";

revision 2024-01-25 {
description
"Initial version.";
}

container conf {
description
"Configuration.";
leaf percentage {
type decimal64 {
fraction-digits 2;
}
default 10.2;
}

leaf-list ratios {
type decimal64 {
fraction-digits 2;
}
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;
}
}
}

0 comments on commit 62bef51

Please sign in to comment.