Skip to content

Commit

Permalink
schema: fix of SLeafList defaults API
Browse files Browse the repository at this point in the history
This patch fixes defaults API of SLeafList. The returned values are now
correctly converted to python types.

Closes: #119
Signed-off-by: Stefan Gula <[email protected]>
Signed-off-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Aug 2, 2024
1 parent e1cefcf commit 32165ef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1420,9 +1420,9 @@ def defaults(self) -> Iterator[Union[None, bool, int, str, float]]:
yield None
val = c2str(val)
val_type = Type(self.context, dflt.realtype, None)
if val_type == Type.BOOL:
if val_type.base() == Type.BOOL:
yield val == "true"
elif val_type in Type.NUM_TYPES:
elif val_type.base() in Type.NUM_TYPES:
yield int(val)
elif val_type.base() == Type.DEC64:
yield float(val)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,12 @@ def test_leaflist_defaults(self):
leaflist = next(self.ctx.find_path("/yolo-nodetypes:conf/ratios"))
for d in leaflist.defaults():
self.assertIsInstance(d, float)
leaflist = next(self.ctx.find_path("/yolo-nodetypes:conf/bools"))
for d in leaflist.defaults():
self.assertIsInstance(d, bool)
leaflist = next(self.ctx.find_path("/yolo-nodetypes:conf/integers"))
for d in leaflist.defaults():
self.assertIsInstance(d, int)

def test_leaf_list_min_max(self):
leaflist1 = next(self.ctx.find_path("/yolo-nodetypes:conf/leaf-list1"))
Expand Down
11 changes: 11 additions & 0 deletions tests/yang/yolo/yolo-nodetypes.yang
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ module yolo-nodetypes {
default 2.6;
}

leaf-list bools {
type boolean;
default true;
}

leaf-list integers {
type uint32;
default 10;
default 20;
}

list list1 {
key leaf1;
unique "leaf2 leaf3";
Expand Down

0 comments on commit 32165ef

Please sign in to comment.