Skip to content

Commit 6cb2e0b

Browse files
committed
tree data UPDATE detect value type for any nodes
1 parent 7b28e3a commit 6cb2e0b

File tree

2 files changed

+76
-49
lines changed

2 files changed

+76
-49
lines changed

src/tree_data_common.c

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,55 +1022,6 @@ lyd_any_value_str(const struct lyd_node *any, char **value_str)
10221022
return ret;
10231023
}
10241024

1025-
LIBYANG_API_DEF LY_ERR
1026-
lyd_any_copy_value(struct lyd_node *trg, const void *value, LYD_ANYDATA_VALUETYPE value_type)
1027-
{
1028-
struct lyd_node_any *t;
1029-
struct lyd_node *node;
1030-
1031-
LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL);
1032-
LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
1033-
1034-
t = (struct lyd_node_any *)trg;
1035-
1036-
/* free trg */
1037-
switch (t->value_type) {
1038-
case LYD_ANYDATA_DATATREE:
1039-
lyd_free_siblings(t->child);
1040-
t->child = NULL;
1041-
break;
1042-
case LYD_ANYDATA_STRING:
1043-
case LYD_ANYDATA_XML:
1044-
case LYD_ANYDATA_JSON:
1045-
lydict_remove(LYD_CTX(trg), t->value);
1046-
t->value = NULL;
1047-
break;
1048-
}
1049-
1050-
if (!value) {
1051-
/* only free value in this case */
1052-
return LY_SUCCESS;
1053-
}
1054-
1055-
/* copy src */
1056-
t->value_type = value_type;
1057-
switch (value_type) {
1058-
case LYD_ANYDATA_DATATREE:
1059-
LY_CHECK_RET(lyd_dup_siblings(value, NULL, LYD_DUP_RECURSIVE, &t->child));
1060-
LY_LIST_FOR(t->child, node) {
1061-
node->parent = &t->node;
1062-
}
1063-
break;
1064-
case LYD_ANYDATA_STRING:
1065-
case LYD_ANYDATA_XML:
1066-
case LYD_ANYDATA_JSON:
1067-
LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value, 0, &t->value));
1068-
break;
1069-
}
1070-
1071-
return LY_SUCCESS;
1072-
}
1073-
10741025
LIBYANG_API_DEF const struct lysc_node *
10751026
lyd_node_schema(const struct lyd_node *node)
10761027
{

src/tree_data_new.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,82 @@ lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VA
369369
return rc;
370370
}
371371

372+
LIBYANG_API_DEF LY_ERR
373+
lyd_any_copy_value(struct lyd_node *trg, const void *value, LYD_ANYDATA_VALUETYPE value_type)
374+
{
375+
struct lyd_node_any *t;
376+
struct lyd_node *node;
377+
struct ly_in *in;
378+
ly_bool use_value = 0;
379+
LY_ERR r;
380+
381+
LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL);
382+
LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
383+
384+
t = (struct lyd_node_any *)trg;
385+
386+
/* free trg */
387+
switch (t->value_type) {
388+
case LYD_ANYDATA_DATATREE:
389+
lyd_free_siblings(t->child);
390+
t->child = NULL;
391+
break;
392+
case LYD_ANYDATA_STRING:
393+
case LYD_ANYDATA_XML:
394+
case LYD_ANYDATA_JSON:
395+
lydict_remove(LYD_CTX(trg), t->value);
396+
t->value = NULL;
397+
break;
398+
}
399+
400+
if (!value) {
401+
/* only free value in this case */
402+
return LY_SUCCESS;
403+
}
404+
405+
if (value_type == LYD_ANYDATA_STRING) {
406+
/* try to learn what the actual value is */
407+
lyd_create_any_string_valtype(value, &value_type);
408+
409+
if ((value_type == LYD_ANYDATA_XML) || (value_type == LYD_ANYDATA_JSON)) {
410+
/* create input */
411+
LY_CHECK_RET(ly_in_new_memory(value, &in));
412+
413+
/* try to parse as a data tree */
414+
r = lyd_create_any_datatree(LYD_CTX(trg), in, value_type, 0, &node);
415+
ly_in_free(in, 0);
416+
if (!r) {
417+
/* use the parsed data tree */
418+
use_value = 1;
419+
value = node;
420+
value_type = LYD_ANYDATA_DATATREE;
421+
}
422+
}
423+
}
424+
425+
/* copy src */
426+
t->value_type = value_type;
427+
switch (value_type) {
428+
case LYD_ANYDATA_DATATREE:
429+
if (use_value) {
430+
t->child = (void *)value;
431+
} else {
432+
LY_CHECK_RET(lyd_dup_siblings(value, NULL, LYD_DUP_RECURSIVE, &t->child));
433+
}
434+
LY_LIST_FOR(t->child, node) {
435+
node->parent = &t->node;
436+
}
437+
break;
438+
case LYD_ANYDATA_STRING:
439+
case LYD_ANYDATA_XML:
440+
case LYD_ANYDATA_JSON:
441+
LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value, 0, &t->value));
442+
break;
443+
}
444+
445+
return LY_SUCCESS;
446+
}
447+
372448
LY_ERR
373449
lyd_create_opaq(const struct ly_ctx *ctx, const char *name, uint32_t name_len, const char *prefix, uint32_t pref_len,
374450
const char *module_key, uint32_t module_key_len, const char *value, uint32_t value_len, ly_bool *dynamic,

0 commit comments

Comments
 (0)