Skip to content

Commit

Permalink
data: fixing broken DLeaf value and print_dict API
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
steweg committed Aug 3, 2024
1 parent 17cfe55 commit 1c6919b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
6 changes: 5 additions & 1 deletion cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -900,9 +900,13 @@ struct lyd_value {
...;
};

struct lyd_value_union {
struct lyd_value value;
...;
};

const char * lyd_get_value(const struct lyd_node *);
struct lyd_node* lyd_child(const struct lyd_node *);
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 **);
LY_ERR lyd_find_path(const struct lyd_node *, const char *, ly_bool, struct lyd_node **);
void lyd_free_siblings(struct lyd_node *);
struct lyd_node* lyd_first_sibling(const struct lyd_node *);
Expand Down
57 changes: 28 additions & 29 deletions libyang/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,38 +1119,37 @@ def cdata_leaf_value(cdata, context: "libyang.Context" = None) -> Any:
return None

val = c2str(val)
term_node = ffi.cast("struct lyd_node_term *", cdata)
val_type = ffi.new("const struct lysc_type **", ffi.NULL)

# get real value type
ctx = context.cdata if context else ffi.NULL
ret = lib.lyd_value_validate(
ctx,
term_node.schema,
str2c(val),
len(val),
ffi.NULL,
val_type,
ffi.NULL,
)

if ret in (lib.LY_SUCCESS, lib.LY_EINCOMPLETE):
val_type = val_type[0].basetype
if val_type in Type.STR_TYPES:
return val
if val_type in Type.NUM_TYPES:
return int(val)
if val_type == Type.BOOL:
return val == "true"
if val_type == Type.DEC64:
return float(val)
if val_type == Type.LEAFREF:
return DLeaf.cdata_leaf_value(cdata.value.leafref, context)
if val_type == Type.EMPTY:
return None
if cdata.schema == ffi.NULL:
# opaq node
return val

raise TypeError("value type validation error")
if cdata.schema.nodetype == SNode.LEAF:
snode = ffi.cast("struct lysc_node_leaf *", cdata.schema)
elif cdata.schema.nodetype == SNode.LEAFLIST:
snode = ffi.cast("struct lysc_node_leaflist *", cdata.schema)

# find the real type used
cdata = ffi.cast("struct lyd_node_term *", cdata)
curr_type = snode.type
while curr_type.basetype in (Type.LEAFREF, Type.UNION):
if curr_type.basetype == Type.LEAFREF:
curr_type = cdata.value.realtype
if curr_type.basetype == Type.UNION:
curr_type = cdata.value.subvalue.value.realtype

val_type = curr_type.basetype
if val_type in Type.STR_TYPES:
return val
if val_type in Type.NUM_TYPES:
return int(val)
if val_type == Type.BOOL:
return val == "true"
if val_type == Type.DEC64:
return float(val)
if val_type == Type.EMPTY:
return None
return val


# -------------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,4 +1110,5 @@ def test_dnode_builtin_plugins_only(self):
module = self.ctx.load_module("yolo-nodetypes")
dnode = dict_to_dnode(MAIN, module, None, validate=False, store_only=True)
self.assertIsInstance(dnode, DLeaf)
self.assertEqual(dnode.value(), "test")
dnode.free()

0 comments on commit 1c6919b

Please sign in to comment.