Skip to content

Commit 3428ea0

Browse files
stewegsamuel-gauthier
authored andcommitted
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 <steweg@gmail.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 3b4d509 commit 3428ea0

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

libyang/schema.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ def __init__(self, context: "libyang.Context", cdata):
12091209
self.cdata_leaf = ffi.cast("struct lysc_node_leaf *", cdata)
12101210
self.cdata_leaf_parsed = ffi.cast("struct lysp_node_leaf *", self.cdata_parsed)
12111211

1212-
def default(self) -> Union[None, bool, int, str]:
1212+
def default(self) -> Union[None, bool, int, str, float]:
12131213
if not self.cdata_leaf.dflt:
12141214
return None
12151215
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]:
12211221
return val == "true"
12221222
if val_type.base() in Type.NUM_TYPES:
12231223
return int(val)
1224+
if val_type.base() == Type.DEC64:
1225+
return float(val)
12241226
return val
12251227

12261228
def units(self) -> Optional[str]:
@@ -1268,7 +1270,7 @@ def type(self) -> Type:
12681270
self.context, self.cdata_leaflist.type, self.cdata_leaflist_parsed.type
12691271
)
12701272

1271-
def defaults(self) -> Iterator[str]:
1273+
def defaults(self) -> Iterator[Union[None, bool, int, str, float]]:
12721274
if self.cdata_leaflist.dflts == ffi.NULL:
12731275
return
12741276
arr_length = ffi.cast("uint64_t *", self.cdata_leaflist.dflts)[-1]
@@ -1278,13 +1280,16 @@ def defaults(self) -> Iterator[str]:
12781280
)
12791281
if not val:
12801282
yield None
1281-
ret = c2str(val)
1283+
val = c2str(val)
12821284
val_type = Type(self.context, self.cdata_leaflist.dflts[i].realtype, None)
12831285
if val_type == Type.BOOL:
1284-
ret = val == "true"
1286+
yield val == "true"
12851287
elif val_type in Type.NUM_TYPES:
1286-
ret = int(val)
1287-
yield ret
1288+
yield int(val)
1289+
elif val_type.base() == Type.DEC64:
1290+
yield float(val)
1291+
else:
1292+
yield val
12881293

12891294
def must_conditions(self) -> Iterator[str]:
12901295
pdata = self.cdata_leaflist_parsed

tests/test_schema.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,34 @@ def test_leaf_parent(self):
474474
def test_iter_tree(self):
475475
leaf = next(self.ctx.find_path("/yolo-system:conf"))
476476
self.assertEqual(len(list(leaf.iter_tree(full=True))), 23)
477+
478+
479+
# -------------------------------------------------------------------------------------
480+
class LeafTest(unittest.TestCase):
481+
def setUp(self):
482+
self.ctx = Context(YANG_DIR)
483+
self.ctx.load_module("yolo-nodetypes")
484+
485+
def tearDown(self):
486+
self.ctx.destroy()
487+
self.ctx = None
488+
489+
def test_leaf_default(self):
490+
leaf = next(self.ctx.find_path("/yolo-nodetypes:conf/percentage"))
491+
self.assertIsInstance(leaf.default(), float)
492+
493+
494+
# -------------------------------------------------------------------------------------
495+
class LeafListTest(unittest.TestCase):
496+
def setUp(self):
497+
self.ctx = Context(YANG_DIR)
498+
self.ctx.load_module("yolo-nodetypes")
499+
500+
def tearDown(self):
501+
self.ctx.destroy()
502+
self.ctx = None
503+
504+
def test_leaflist_defaults(self):
505+
leaflist = next(self.ctx.find_path("/yolo-nodetypes:conf/ratios"))
506+
for d in leaflist.defaults():
507+
self.assertIsInstance(d, float)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module yolo-nodetypes {
2+
yang-version 1.1;
3+
namespace "urn:yang:yolo:nodetypes";
4+
prefix sys;
5+
6+
description
7+
"YOLO Nodetypes.";
8+
9+
revision 2024-01-25 {
10+
description
11+
"Initial version.";
12+
}
13+
14+
container conf {
15+
description
16+
"Configuration.";
17+
leaf percentage {
18+
type decimal64 {
19+
fraction-digits 2;
20+
}
21+
default 10.2;
22+
}
23+
24+
leaf-list ratios {
25+
type decimal64 {
26+
fraction-digits 2;
27+
}
28+
default 2.5;
29+
default 2.6;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)