Skip to content

Commit 4b10649

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 45b6811 commit 4b10649

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

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

11101110
val = c2str(val)
1111-
term_node = ffi.cast("struct lyd_node_term *", cdata)
1112-
val_type = ffi.new("const struct lysc_type **", ffi.NULL)
1113-
1114-
# get real value type
1115-
ctx = context.cdata if context else ffi.NULL
1116-
ret = lib.lyd_value_validate(
1117-
ctx,
1118-
term_node.schema,
1119-
str2c(val),
1120-
len(val),
1121-
ffi.NULL,
1122-
val_type,
1123-
ffi.NULL,
1124-
)
11251111

1126-
if ret in (lib.LY_SUCCESS, lib.LY_EINCOMPLETE):
1127-
val_type = val_type[0].basetype
1128-
if val_type in Type.STR_TYPES:
1129-
return val
1130-
if val_type in Type.NUM_TYPES:
1131-
return int(val)
1132-
if val_type == Type.BOOL:
1133-
return val == "true"
1134-
if val_type == Type.DEC64:
1135-
return float(val)
1136-
if val_type == Type.LEAFREF:
1137-
return DLeaf.cdata_leaf_value(cdata.value.leafref, context)
1138-
if val_type == Type.EMPTY:
1139-
return None
1112+
if cdata.schema == ffi.NULL:
1113+
# opaq node
11401114
return val
11411115

1142-
raise TypeError("value type validation error")
1116+
if cdata.schema.nodetype == SNode.LEAF:
1117+
snode = ffi.cast("struct lysc_node_leaf *", cdata.schema)
1118+
elif cdata.schema.nodetype == SNode.LEAFLIST:
1119+
snode = ffi.cast("struct lysc_node_leaflist *", cdata.schema)
1120+
1121+
# find the real type used
1122+
cdata = ffi.cast("struct lyd_node_term *", cdata)
1123+
curr_type = snode.type
1124+
while curr_type.basetype in (Type.LEAFREF, Type.UNION):
1125+
if curr_type.basetype == Type.LEAFREF:
1126+
curr_type = cdata.value.realtype
1127+
if curr_type.basetype == Type.UNION:
1128+
curr_type = cdata.value.subvalue.value.realtype
1129+
1130+
val_type = curr_type.basetype
1131+
if val_type in Type.STR_TYPES:
1132+
return val
1133+
if val_type in Type.NUM_TYPES:
1134+
return int(val)
1135+
if val_type == Type.BOOL:
1136+
return val == "true"
1137+
if val_type == Type.DEC64:
1138+
return float(val)
1139+
if val_type == Type.EMPTY:
1140+
return None
1141+
return val
11431142

11441143

11451144
# -------------------------------------------------------------------------------------

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)