diff --git a/libyang/schema.py b/libyang/schema.py index 6b0c803b..c7dab125 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_type = self.cdata_leaflist.dflts[i].realtype - if val_type == Type.BOOL: - ret = val == "true" - elif val_type in Type.NUM_TYPES: - ret = int(val) - yield ret + val = c2str(val) + val_type = Type(self.context, self.cdata_leaflist.dflts[i].realtype, None) + if val_type.base() == Type.BOOL: + yield val == "true" + elif val_type.base() in Type.NUM_TYPES: + 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 f493ba14..7672eaec 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; + } + } +}