Skip to content

Commit e579191

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 <steweg@gmail.com>
1 parent 4e52cf8 commit e579191

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
@@ -775,9 +775,13 @@ struct lyd_value {
775775
...;
776776
};
777777

778+
struct lyd_value_union {
779+
struct lyd_value value;
780+
...;
781+
};
782+
778783
const char * lyd_get_value(const struct lyd_node *);
779784
struct lyd_node* lyd_child(const struct lyd_node *);
780-
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 **);
781785
LY_ERR lyd_find_path(const struct lyd_node *, const char *, ly_bool, struct lyd_node **);
782786
void lyd_free_siblings(struct lyd_node *);
783787
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
@@ -1078,38 +1078,37 @@ def cdata_leaf_value(cdata, context: "libyang.Context" = None) -> Any:
10781078
return None
10791079

10801080
val = c2str(val)
1081-
term_node = ffi.cast("struct lyd_node_term *", cdata)
1082-
val_type = ffi.new("const struct lysc_type **", ffi.NULL)
1083-
1084-
# get real value type
1085-
ctx = context.cdata if context else ffi.NULL
1086-
ret = lib.lyd_value_validate(
1087-
ctx,
1088-
term_node.schema,
1089-
str2c(val),
1090-
len(val),
1091-
ffi.NULL,
1092-
val_type,
1093-
ffi.NULL,
1094-
)
10951081

1096-
if ret in (lib.LY_SUCCESS, lib.LY_EINCOMPLETE):
1097-
val_type = val_type[0].basetype
1098-
if val_type in Type.STR_TYPES:
1099-
return val
1100-
if val_type in Type.NUM_TYPES:
1101-
return int(val)
1102-
if val_type == Type.BOOL:
1103-
return val == "true"
1104-
if val_type == Type.DEC64:
1105-
return float(val)
1106-
if val_type == Type.LEAFREF:
1107-
return DLeaf.cdata_leaf_value(cdata.value.leafref, context)
1108-
if val_type == Type.EMPTY:
1109-
return None
1082+
if cdata.schema == ffi.NULL:
1083+
# opaq node
11101084
return val
11111085

1112-
raise TypeError("value type validation error")
1086+
if cdata.schema.nodetype == SNode.LEAF:
1087+
snode = ffi.cast("struct lysc_node_leaf *", cdata.schema)
1088+
elif cdata.schema.nodetype == SNode.LEAFLIST:
1089+
snode = ffi.cast("struct lysc_node_leaflist *", cdata.schema)
1090+
1091+
# find the real type used
1092+
cdata = ffi.cast("struct lyd_node_term *", cdata)
1093+
curr_type = snode.type
1094+
while curr_type.basetype in (Type.LEAFREF, Type.UNION):
1095+
if curr_type.basetype == Type.LEAFREF:
1096+
curr_type = cdata.value.realtype
1097+
if curr_type.basetype == Type.UNION:
1098+
curr_type = cdata.value.subvalue.value.realtype
1099+
1100+
val_type = curr_type.basetype
1101+
if val_type in Type.STR_TYPES:
1102+
return val
1103+
if val_type in Type.NUM_TYPES:
1104+
return int(val)
1105+
if val_type == Type.BOOL:
1106+
return val == "true"
1107+
if val_type == Type.DEC64:
1108+
return float(val)
1109+
if val_type == Type.EMPTY:
1110+
return None
1111+
return val
11131112

11141113

11151114
# -------------------------------------------------------------------------------------

tests/test_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,4 +1054,5 @@ def test_dnode_builtin_plugins_only(self):
10541054
module = self.ctx.load_module("yolo-nodetypes")
10551055
dnode = dict_to_dnode(MAIN, module, None, validate=False, store_only=True)
10561056
self.assertIsInstance(dnode, DLeaf)
1057+
self.assertEqual(dnode.value(), "test")
10571058
dnode.free()

0 commit comments

Comments
 (0)