Skip to content

Commit

Permalink
schema: add require_instance function for leafrefs
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Jan 27, 2024
1 parent 06d486d commit f26b56a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ def all_patterns(self) -> Iterator[Tuple[str, bool]]:
else:
yield from self.patterns()

def require_instance(self) -> Optional[bool]:
if self.cdata.basetype != self.LEAFREF:
return None
t = ffi.cast("struct lysc_type_leafref *", self.cdata)
return bool(t.require_instance)

def module(self) -> Module:
if not self.cdata_parsed:
return None
Expand Down
7 changes: 7 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,13 @@ def test_leaf_type_fraction_digits(self):
self.assertIsInstance(t, Type)
self.assertEqual(next(t.all_fraction_digits(), None), 2)

def test_leaf_type_require_instance(self):
leaf = next(self.ctx.find_path("/yolo-system:conf/hostname-ref"))
self.assertIsInstance(leaf, SLeaf)
t = leaf.type()
self.assertIsInstance(t, Type)
self.assertFalse(t.require_instance())


# -------------------------------------------------------------------------------------
class LeafTest(unittest.TestCase):
Expand Down
1 change: 1 addition & 0 deletions tests/yang/yolo/yolo-system.yang
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module yolo-system {
leaf hostname-ref {
type leafref {
path "../hostname";
require-instance false;
}
}

Expand Down

0 comments on commit f26b56a

Please sign in to comment.