Skip to content

Commit 95012d9

Browse files
stewegsamuel-gauthier
authored andcommitted
schema: add uniques function for SList
This patch add ability get a list of SList unique definitions. Fixes: CESNET#88 Signed-off-by: Stefan Gula <steweg@gmail.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 3e3af68 commit 95012d9

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

libyang/schema.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: MIT
44

55
from contextlib import suppress
6-
from typing import IO, Any, Dict, Iterator, Optional, Tuple, Union
6+
from typing import IO, Any, Dict, Iterator, List, Optional, Tuple, Union
77

88
from _libyang import ffi, lib
99
from .util import IOType, LibyangError, c2str, init_output, ly_array_iter, str2c
@@ -1416,6 +1416,13 @@ def must_conditions(self) -> Iterator[str]:
14161416
for must in ly_array_iter(pdata.musts):
14171417
yield c2str(must.arg.str)
14181418

1419+
def uniques(self) -> Iterator[List[SNode]]:
1420+
for unique in ly_array_iter(self.cdata_list.uniques):
1421+
nodes = []
1422+
for node in ly_array_iter(unique):
1423+
nodes.append(SNode.new(self.context, node))
1424+
yield nodes
1425+
14191426
def __str__(self):
14201427
return "%s [%s]" % (self.name(), ", ".join(k.name() for k in self.keys()))
14211428

tests/test_schema.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,22 @@ def test_list_parent(self):
328328
self.assertIsInstance(parent, SContainer)
329329
self.assertEqual(parent.name(), "conf")
330330

331+
def test_list_uniques(self):
332+
self.ctx.load_module("yolo-nodetypes")
333+
list1 = next(self.ctx.find_path("/yolo-nodetypes:conf/list1"))
334+
self.assertIsInstance(list1, SList)
335+
uniques = list(list1.uniques())
336+
self.assertEqual(len(uniques), 1)
337+
elements = [u.name() for u in uniques[0]]
338+
self.assertEqual(len(elements), 2)
339+
self.assertTrue("leaf2" in elements)
340+
self.assertTrue("leaf3" in elements)
341+
342+
list2 = next(self.ctx.find_path("/yolo-nodetypes:conf/list2"))
343+
self.assertIsInstance(list2, SList)
344+
uniques = list(list2.uniques())
345+
self.assertEqual(len(uniques), 0)
346+
331347

332348
# -------------------------------------------------------------------------------------
333349
class RpcTest(unittest.TestCase):

tests/yang/yolo/yolo-nodetypes.yang

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,26 @@ module yolo-nodetypes {
2828
default 2.5;
2929
default 2.6;
3030
}
31+
32+
list list1 {
33+
key leaf1;
34+
unique "leaf2 leaf3";
35+
leaf leaf1 {
36+
type string;
37+
}
38+
leaf leaf2 {
39+
type string;
40+
}
41+
leaf leaf3 {
42+
type string;
43+
}
44+
}
45+
46+
list list2 {
47+
key leaf1;
48+
leaf leaf1 {
49+
type string;
50+
}
51+
}
3152
}
3253
}

0 commit comments

Comments
 (0)