Skip to content
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: 12 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 @@ -1416,6 +1416,17 @@ def must_conditions(self) -> Iterator[str]:
for must in ly_array_iter(pdata.musts):
yield c2str(must.arg.str)

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
11 changes: 11 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ def test_list_parent(self):
self.assertIsInstance(parent, SContainer)
self.assertEqual(parent.name(), "conf")

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 = list(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
14 changes: 14 additions & 0 deletions tests/yang/yolo/yolo-nodetypes.yang
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,19 @@ module yolo-nodetypes {
default 2.5;
default 2.6;
}

list list1 {
key leaf1;
unique "leaf2 leaf3";
leaf leaf1 {
type string;
}
leaf leaf2 {
type string;
}
leaf leaf3 {
type string;
}
}
}
}