Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema: Fix of SLeafList defaults API #119

Closed
wants to merge 1 commit into from
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: 5 additions & 8 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,18 +1364,15 @@ def type(self) -> Type:
def defaults(self) -> Iterator[Union[None, bool, int, str, float]]:
if self.cdata_leaflist.dflts == ffi.NULL:
return
arr_length = ffi.cast("uint64_t *", self.cdata_leaflist.dflts)[-1]
for i in range(arr_length):
val = lib.lyd_value_get_canonical(
self.context.cdata, self.cdata_leaflist.dflts[i]
)
for dflt in ly_array_iter(self.cdata_leaflist.dflts):
val = lib.lyd_value_get_canonical(self.context.cdata, dflt)
if not val:
yield None
val = c2str(val)
val_type = Type(self.context, self.cdata_leaflist.dflts[i].realtype, None)
if val_type == Type.BOOL:
val_type = Type(self.context, dflt.realtype, None)
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 @@ -575,6 +575,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 @@ -44,6 +44,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
Loading