Skip to content

Commit f26b56a

Browse files
stewegsamuel-gauthier
authored andcommitted
schema: add require_instance function for leafrefs
This patch introduces require_instance function, to allow user to get information whether the leafref requires valid instace prior being instanciated. Fixes: #93 Signed-off-by: Stefan Gula <steweg@gmail.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 06d486d commit f26b56a

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

libyang/schema.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,12 @@ def all_patterns(self) -> Iterator[Tuple[str, bool]]:
681681
else:
682682
yield from self.patterns()
683683

684+
def require_instance(self) -> Optional[bool]:
685+
if self.cdata.basetype != self.LEAFREF:
686+
return None
687+
t = ffi.cast("struct lysc_type_leafref *", self.cdata)
688+
return bool(t.require_instance)
689+
684690
def module(self) -> Module:
685691
if not self.cdata_parsed:
686692
return None

tests/test_schema.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,13 @@ def test_leaf_type_fraction_digits(self):
512512
self.assertIsInstance(t, Type)
513513
self.assertEqual(next(t.all_fraction_digits(), None), 2)
514514

515+
def test_leaf_type_require_instance(self):
516+
leaf = next(self.ctx.find_path("/yolo-system:conf/hostname-ref"))
517+
self.assertIsInstance(leaf, SLeaf)
518+
t = leaf.type()
519+
self.assertIsInstance(t, Type)
520+
self.assertFalse(t.require_instance())
521+
515522

516523
# -------------------------------------------------------------------------------------
517524
class LeafTest(unittest.TestCase):

tests/yang/yolo/yolo-system.yang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module yolo-system {
4343
leaf hostname-ref {
4444
type leafref {
4545
path "../hostname";
46+
require-instance false;
4647
}
4748
}
4849

0 commit comments

Comments
 (0)