Skip to content

Commit

Permalink
schema: return float for decimal64 in default function
Browse files Browse the repository at this point in the history
This patch changes the output for leaf/leaf-list nodes using decimal64
type, and adds new unit test for this purpose.

The leaf-list defaults function is reworked to be similar to the leaf
default function. A new yang schema is added for clarity, and to avoid
modifying all the tests using yolo-system.yang.

Fixes: #80
Signed-off-by: Stefan Gula <[email protected]>
Signed-off-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Jan 25, 2024
1 parent 3b4d509 commit 3428ea0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
17 changes: 11 additions & 6 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ def __init__(self, context: "libyang.Context", cdata):
self.cdata_leaf = ffi.cast("struct lysc_node_leaf *", cdata)
self.cdata_leaf_parsed = ffi.cast("struct lysp_node_leaf *", self.cdata_parsed)

def default(self) -> Union[None, bool, int, str]:
def default(self) -> Union[None, bool, int, str, float]:
if not self.cdata_leaf.dflt:
return None
val = lib.lyd_value_get_canonical(self.context.cdata, self.cdata_leaf.dflt)
Expand All @@ -1221,6 +1221,8 @@ def default(self) -> Union[None, bool, int, str]:
return val == "true"
if val_type.base() in Type.NUM_TYPES:
return int(val)
if val_type.base() == Type.DEC64:
return float(val)
return val

def units(self) -> Optional[str]:
Expand Down Expand Up @@ -1268,7 +1270,7 @@ def type(self) -> Type:
self.context, self.cdata_leaflist.type, self.cdata_leaflist_parsed.type
)

def defaults(self) -> Iterator[str]:
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]
Expand All @@ -1278,13 +1280,16 @@ def defaults(self) -> Iterator[str]:
)
if not val:
yield None
ret = c2str(val)
val = c2str(val)
val_type = Type(self.context, self.cdata_leaflist.dflts[i].realtype, None)
if val_type == Type.BOOL:
ret = val == "true"
yield val == "true"
elif val_type in Type.NUM_TYPES:
ret = int(val)
yield ret
yield int(val)
elif val_type.base() == Type.DEC64:
yield float(val)
else:
yield val

def must_conditions(self) -> Iterator[str]:
pdata = self.cdata_leaflist_parsed
Expand Down
31 changes: 31 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,34 @@ def test_leaf_parent(self):
def test_iter_tree(self):
leaf = next(self.ctx.find_path("/yolo-system:conf"))
self.assertEqual(len(list(leaf.iter_tree(full=True))), 23)


# -------------------------------------------------------------------------------------
class LeafTest(unittest.TestCase):
def setUp(self):
self.ctx = Context(YANG_DIR)
self.ctx.load_module("yolo-nodetypes")

def tearDown(self):
self.ctx.destroy()
self.ctx = None

def test_leaf_default(self):
leaf = next(self.ctx.find_path("/yolo-nodetypes:conf/percentage"))
self.assertIsInstance(leaf.default(), float)


# -------------------------------------------------------------------------------------
class LeafListTest(unittest.TestCase):
def setUp(self):
self.ctx = Context(YANG_DIR)
self.ctx.load_module("yolo-nodetypes")

def tearDown(self):
self.ctx.destroy()
self.ctx = None

def test_leaflist_defaults(self):
leaflist = next(self.ctx.find_path("/yolo-nodetypes:conf/ratios"))
for d in leaflist.defaults():
self.assertIsInstance(d, float)
32 changes: 32 additions & 0 deletions tests/yang/yolo/yolo-nodetypes.yang
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module yolo-nodetypes {
yang-version 1.1;
namespace "urn:yang:yolo:nodetypes";
prefix sys;

description
"YOLO Nodetypes.";

revision 2024-01-25 {
description
"Initial version.";
}

container conf {
description
"Configuration.";
leaf percentage {
type decimal64 {
fraction-digits 2;
}
default 10.2;
}

leaf-list ratios {
type decimal64 {
fraction-digits 2;
}
default 2.5;
default 2.6;
}
}
}

0 comments on commit 3428ea0

Please sign in to comment.