From b7dd6cf0edc4d9df4d9a8ba2124758984e0829b9 Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Fri, 26 Jan 2024 09:29:00 +0100 Subject: [PATCH] Adding uniques This patch add ability get list of SList unique definitions. --- libyang/schema.py | 13 ++++++++++++- tests/test_schema.py | 11 +++++++++++ tests/yang/yolo/yolo-nodetypes.yang | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/libyang/schema.py b/libyang/schema.py index bd315596..455e5fc3 100644 --- a/libyang/schema.py +++ b/libyang/schema.py @@ -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 @@ -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())) diff --git a/tests/test_schema.py b/tests/test_schema.py index 1287012a..fe49cfbc 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -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): diff --git a/tests/yang/yolo/yolo-nodetypes.yang b/tests/yang/yolo/yolo-nodetypes.yang index 3dd76832..b9b200b8 100644 --- a/tests/yang/yolo/yolo-nodetypes.yang +++ b/tests/yang/yolo/yolo-nodetypes.yang @@ -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; + } + } } }