Skip to content

Commit 1c6919b

Browse files
committed
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 Signed-off-by: Stefan Gula <[email protected]>
1 parent 17cfe55 commit 1c6919b

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

cffi/cdefs.h

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

903+
struct lyd_value_union {
904+
struct lyd_value value;
905+
...;
906+
};
907+
903908
const char * lyd_get_value(const struct lyd_node *);
904909
struct lyd_node* lyd_child(const struct lyd_node *);
905-
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 **);
906910
LY_ERR lyd_find_path(const struct lyd_node *, const char *, ly_bool, struct lyd_node **);
907911
void lyd_free_siblings(struct lyd_node *);
908912
struct lyd_node* lyd_first_sibling(const struct lyd_node *);

libyang/data.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,38 +1119,37 @@ 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-
)
11361122

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
1123+
if cdata.schema == ffi.NULL:
1124+
# opaq node
11511125
return val
11521126

1153-
raise TypeError("value type validation error")
1127+
if cdata.schema.nodetype == SNode.LEAF:
1128+
snode = ffi.cast("struct lysc_node_leaf *", cdata.schema)
1129+
elif cdata.schema.nodetype == SNode.LEAFLIST:
1130+
snode = ffi.cast("struct lysc_node_leaflist *", cdata.schema)
1131+
1132+
# find the real type used
1133+
cdata = ffi.cast("struct lyd_node_term *", cdata)
1134+
curr_type = snode.type
1135+
while curr_type.basetype in (Type.LEAFREF, Type.UNION):
1136+
if curr_type.basetype == Type.LEAFREF:
1137+
curr_type = cdata.value.realtype
1138+
if curr_type.basetype == Type.UNION:
1139+
curr_type = cdata.value.subvalue.value.realtype
1140+
1141+
val_type = curr_type.basetype
1142+
if val_type in Type.STR_TYPES:
1143+
return val
1144+
if val_type in Type.NUM_TYPES:
1145+
return int(val)
1146+
if val_type == Type.BOOL:
1147+
return val == "true"
1148+
if val_type == Type.DEC64:
1149+
return float(val)
1150+
if val_type == Type.EMPTY:
1151+
return None
1152+
return val
11541153

11551154

11561155
# -------------------------------------------------------------------------------------

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)