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.

Closes: #107
Signed-off-by: Stefan Gula <[email protected]>
Signed-off-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Sep 27, 2024
1 parent 551f44b commit 8d64c70
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
6 changes: 5 additions & 1 deletion cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,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
52 changes: 22 additions & 30 deletions libyang/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,38 +1119,30 @@ 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")
node_term = ffi.cast("struct lyd_node_term *", cdata)

# inspired from libyang lyd_value_validate
val_type = Type(context, node_term.value.realtype, None).base()
if val_type == Type.UNION:
val_type = Type(
context, node_term.value.subvalue.value.realtype, None
).base()

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 8d64c70

Please sign in to comment.