Skip to content

Commit 8d64c70

Browse files
stewegsamuel-gauthier
authored andcommitted
data: fixing broken DLeaf value and print_dict API
This patch fixes broken APIs, which were using no longer valid structs and performing validation step instead of just checking the realtype of data. Closes: #107 Signed-off-by: Stefan Gula <[email protected]> Signed-off-by: Samuel Gauthier <[email protected]>
1 parent 551f44b commit 8d64c70

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

cffi/cdefs.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,13 @@ struct lyd_value {
902902
...;
903903
};
904904

905+
struct lyd_value_union {
906+
struct lyd_value value;
907+
...;
908+
};
909+
905910
const char * lyd_get_value(const struct lyd_node *);
906911
struct lyd_node* lyd_child(const struct lyd_node *);
907-
LY_ERR lyd_value_validate(const struct ly_ctx *, const struct lysc_node *, const char *, size_t, const struct lyd_node *, const struct lysc_type **, const char **);
908912
LY_ERR lyd_find_path(const struct lyd_node *, const char *, ly_bool, struct lyd_node **);
909913
void lyd_free_siblings(struct lyd_node *);
910914
struct lyd_node* lyd_first_sibling(const struct lyd_node *);

libyang/data.py

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,38 +1119,30 @@ def cdata_leaf_value(cdata, context: "libyang.Context" = None) -> Any:
11191119
return None
11201120

11211121
val = c2str(val)
1122-
term_node = ffi.cast("struct lyd_node_term *", cdata)
1123-
val_type = ffi.new("const struct lysc_type **", ffi.NULL)
1124-
1125-
# get real value type
1126-
ctx = context.cdata if context else ffi.NULL
1127-
ret = lib.lyd_value_validate(
1128-
ctx,
1129-
term_node.schema,
1130-
str2c(val),
1131-
len(val),
1132-
ffi.NULL,
1133-
val_type,
1134-
ffi.NULL,
1135-
)
1136-
1137-
if ret in (lib.LY_SUCCESS, lib.LY_EINCOMPLETE):
1138-
val_type = val_type[0].basetype
1139-
if val_type in Type.STR_TYPES:
1140-
return val
1141-
if val_type in Type.NUM_TYPES:
1142-
return int(val)
1143-
if val_type == Type.BOOL:
1144-
return val == "true"
1145-
if val_type == Type.DEC64:
1146-
return float(val)
1147-
if val_type == Type.LEAFREF:
1148-
return DLeaf.cdata_leaf_value(cdata.value.leafref, context)
1149-
if val_type == Type.EMPTY:
1150-
return None
1122+
if cdata.schema == ffi.NULL:
1123+
# opaq node
11511124
return val
11521125

1153-
raise TypeError("value type validation error")
1126+
node_term = ffi.cast("struct lyd_node_term *", cdata)
1127+
1128+
# inspired from libyang lyd_value_validate
1129+
val_type = Type(context, node_term.value.realtype, None).base()
1130+
if val_type == Type.UNION:
1131+
val_type = Type(
1132+
context, node_term.value.subvalue.value.realtype, None
1133+
).base()
1134+
1135+
if val_type in Type.STR_TYPES:
1136+
return val
1137+
if val_type in Type.NUM_TYPES:
1138+
return int(val)
1139+
if val_type == Type.BOOL:
1140+
return val == "true"
1141+
if val_type == Type.DEC64:
1142+
return float(val)
1143+
if val_type == Type.EMPTY:
1144+
return None
1145+
return val
11541146

11551147

11561148
# -------------------------------------------------------------------------------------

tests/test_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,4 +1110,5 @@ def test_dnode_builtin_plugins_only(self):
11101110
module = self.ctx.load_module("yolo-nodetypes")
11111111
dnode = dict_to_dnode(MAIN, module, None, validate=False, store_only=True)
11121112
self.assertIsInstance(dnode, DLeaf)
1113+
self.assertEqual(dnode.value(), "test")
11131114
dnode.free()

0 commit comments

Comments
 (0)