From 3428ea055cd47d7f33e061c0d1ea8116af21b3d1 Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Tue, 28 Nov 2023 14:59:21 +0100 Subject: [PATCH] schema: return float for decimal64 in default function 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 Signed-off-by: Samuel Gauthier --- libyang/schema.py | 17 +++++++++------ tests/test_schema.py | 31 ++++++++++++++++++++++++++++ tests/yang/yolo/yolo-nodetypes.yang | 32 +++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 tests/yang/yolo/yolo-nodetypes.yang diff --git a/libyang/schema.py b/libyang/schema.py index 3aef4176..d85912d1 100644 --- a/libyang/schema.py +++ b/libyang/schema.py @@ -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) @@ -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]: @@ -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] @@ -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 diff --git a/tests/test_schema.py b/tests/test_schema.py index 770a476c..434618bf 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -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) diff --git a/tests/yang/yolo/yolo-nodetypes.yang b/tests/yang/yolo/yolo-nodetypes.yang new file mode 100644 index 00000000..3dd76832 --- /dev/null +++ b/tests/yang/yolo/yolo-nodetypes.yang @@ -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; + } + } +}