Skip to content

Commit 0f42bd7

Browse files
committed
tree data UPDATE function with hints for canonizing values
Needed for default values.
1 parent 048dea4 commit 0f42bd7

File tree

8 files changed

+59
-23
lines changed

8 files changed

+59
-23
lines changed

src/path.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,8 @@ ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_
716716

717717
/* do not store the canonical value, only validate */
718718
LOG_LOCSET(key, NULL);
719-
r = lyd_value_validate2(ctx_node->module->ctx, key, val, val_len, format, prefix_data, NULL, top_ext,
720-
NULL, &p->value);
719+
r = lyd_value_validate3(ctx_node->module->ctx, key, val, val_len, format, prefix_data, LYD_HINT_DATA,
720+
NULL, top_ext, NULL, &p->value);
721721
LOG_LOCBACK(1, 0);
722722
LY_CHECK_ERR_GOTO(r && (r != LY_EINCOMPLETE), rc = r, cleanup);
723723

@@ -775,8 +775,8 @@ ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_
775775

776776
/* do not store the value, only validate */
777777
LOG_LOCSET(ctx_node, NULL);
778-
r = lyd_value_validate2(ctx_node->module->ctx, ctx_node, val, val_len, format, prefix_data, NULL, top_ext, NULL,
779-
&p->value);
778+
r = lyd_value_validate3(ctx_node->module->ctx, ctx_node, val, val_len, format, prefix_data, LYD_HINT_DATA, NULL,
779+
top_ext, NULL, &p->value);
780780
LOG_LOCBACK(1, 0);
781781
LY_CHECK_ERR_GOTO(r && (r != LY_EINCOMPLETE), rc = r, cleanup);
782782

src/tree_data.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,30 @@ LIBYANG_API_DECL void lyd_free_attr_siblings(const struct ly_ctx *ctx, struct ly
18681868
LIBYANG_API_DECL LY_ERR lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value,
18691869
uint32_t value_len, const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical);
18701870

1871+
/**
1872+
* @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
1873+
*
1874+
* The given node is not modified in any way - it is just checked if the @p value can be set to the node.
1875+
*
1876+
* @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
1877+
* @param[in] schema Schema node of the @p value.
1878+
* @param[in] value String value to be checked, it is expected to be in JSON format.
1879+
* @param[in] value_len Length of the given @p value (mandatory).
1880+
* @param[in] hints Value hints, bitmap of @ref lydvalhints. Use ::LYD_HINT_DATA for generic data value and
1881+
* ::LYD_HINT_SCHEMA for a schema value (default value, for example).
1882+
* @param[in] ctx_node Optional data tree context node for the value (leafref target, instance-identifier).
1883+
* If not set and is required for the validation to complete, ::LY_EINCOMPLETE is be returned.
1884+
* @param[out] realtype Optional real type of @p value.
1885+
* @param[out] canonical Optional canonical value of @p value in the dictionary, needs to be freed using ::lydict_remove().
1886+
* @return LY_SUCCESS on success
1887+
* @return LY_EINCOMPLETE in case the @p ctx_node is not provided and it was needed to finish the validation
1888+
* (e.g. due to require-instance).
1889+
* @return LY_ERR value if an error occurred.
1890+
*/
1891+
LIBYANG_API_DECL LY_ERR lyd_value_validate2(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value,
1892+
uint32_t value_len, uint32_t hints, const struct lyd_node *ctx_node, const struct lysc_type **realtype,
1893+
const char **canonical);
1894+
18711895
/**
18721896
* @brief Compare the node's value with the given string value. The string value is first validated according to
18731897
* the (current) node's type.

src/tree_data_common.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,23 @@ lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, con
625625
{
626626
LY_CHECK_ARG_RET(ctx, schema, !value_len || value, LY_EINVAL);
627627

628-
return lyd_value_validate2(ctx, schema, value, value_len, LY_VALUE_JSON, NULL, ctx_node, NULL, realtype, canonical);
628+
return lyd_value_validate3(ctx, schema, value, value_len, LY_VALUE_JSON, NULL, LYD_HINT_DATA, ctx_node, NULL,
629+
realtype, canonical);
630+
}
631+
632+
LIBYANG_API_DEF LY_ERR
633+
lyd_value_validate2(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, uint32_t value_len,
634+
uint32_t hints, const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
635+
{
636+
LY_CHECK_ARG_RET(ctx, schema, !value_len || value, LY_EINVAL);
637+
638+
return lyd_value_validate3(ctx, schema, value, value_len, LY_VALUE_JSON, NULL, hints, ctx_node, NULL,
639+
realtype, canonical);
629640
}
630641

631642
LY_ERR
632-
lyd_value_validate2(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
633-
LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node,
643+
lyd_value_validate3(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
644+
LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lyd_node *ctx_node,
634645
const struct lysc_ext_instance *top_ext, const struct lysc_type **realtype, const char **canonical)
635646
{
636647
LY_ERR rc;
@@ -652,8 +663,7 @@ lyd_value_validate2(const struct ly_ctx *ctx, const struct lysc_node *schema, co
652663
type_plg = LYSC_GET_TYPE_PLG(type->plugin_ref);
653664

654665
/* store */
655-
rc = type_plg->store(ctx, type, value, value_len * 8, 0, format, prefix_data, LYD_HINT_DATA, schema, top_ext, &val,
656-
NULL, &err);
666+
rc = type_plg->store(ctx, type, value, value_len * 8, 0, format, prefix_data, hints, schema, top_ext, &val, NULL, &err);
657667
if (!rc || (rc == LY_EINCOMPLETE)) {
658668
stored = 1;
659669
}

src/tree_data_internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ LY_ERR ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node,
647647
* @param[in] value_len Length of the given @p value (mandatory).
648648
* @param[in] format Value prefix format.
649649
* @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
650+
* @param[in] hints Value hints, bitmap of LYD_VALHINT_* values.
650651
* @param[in] ctx_node Optional data tree context node for the value (leafref target, instance-identifier).
651652
* If not set and is required for the validation to complete, ::LY_EINCOMPLETE is be returned.
652653
* @param[in] top_ext Extension instance containing the definition of the data being validated.
@@ -657,8 +658,8 @@ LY_ERR ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node,
657658
* (e.g. due to require-instance).
658659
* @return LY_ERR value if an error occurred.
659660
*/
660-
LY_ERR lyd_value_validate2(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
661-
LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node,
661+
LY_ERR lyd_value_validate3(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
662+
LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lyd_node *ctx_node,
662663
const struct lysc_ext_instance *top_ext, const struct lysc_type **realtype, const char **canonical);
663664

664665
/**

src/tree_data_new.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,8 +1579,8 @@ lyd_new_path_check_find_lypath(struct ly_path *path, const struct lysc_ext_insta
15791579
}
15801580
if (!r) {
15811581
/* validate the value and store the canonical value */
1582-
r = lyd_value_validate2(NULL, schema, value, LYPLG_BITS2BYTES(value_size_bits), format, NULL, NULL, ext,
1583-
NULL, &canon);
1582+
r = lyd_value_validate3(NULL, schema, value, LYPLG_BITS2BYTES(value_size_bits), format, NULL,
1583+
LYD_HINT_DATA, NULL, ext, NULL, &canon);
15841584
LY_CHECK_RET(r && r != LY_EINCOMPLETE, r);
15851585

15861586
/* store the new predicate so that it is used when searching for this instance */

src/tree_schema_common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,8 +2861,8 @@ lysc_value_cmp(const struct lysc_node *schema, const struct lyd_node *ctx_node,
28612861
int cmp;
28622862
const char *canon;
28632863

2864-
r = lyd_value_validate2(schema->module->ctx, schema, val->str, strlen(val->str), LY_VALUE_SCHEMA_RESOLVED,
2865-
val->prefixes, ctx_node, NULL, NULL, &canon);
2864+
r = lyd_value_validate3(schema->module->ctx, schema, val->str, strlen(val->str), LY_VALUE_SCHEMA_RESOLVED,
2865+
val->prefixes, LYD_HINT_SCHEMA, ctx_node, NULL, NULL, &canon);
28662866
if (r && (r != LY_EINCOMPLETE)) {
28672867
return -1;
28682868
}

src/validation.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,9 +1336,9 @@ lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *c
13361336
val1 = lyd_get_value(diter);
13371337
} else if (slist->uniques[u][v]->dflt.str) {
13381338
/* use canonical default value */
1339-
lyd_value_validate2(ctx, first->schema, slist->uniques[u][v]->dflt.str,
1339+
lyd_value_validate3(ctx, first->schema, slist->uniques[u][v]->dflt.str,
13401340
strlen(slist->uniques[u][v]->dflt.str), LY_VALUE_SCHEMA_RESOLVED,
1341-
slist->uniques[u][v]->dflt.prefixes, NULL, NULL, NULL, &canon2);
1341+
slist->uniques[u][v]->dflt.prefixes, LYD_HINT_SCHEMA, NULL, NULL, NULL, &canon2);
13421342
val1 = canon1;
13431343
}
13441344

@@ -1350,9 +1350,9 @@ lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *c
13501350
val2 = lyd_get_value(diter);
13511351
} else if (slist->uniques[u][v]->dflt.str) {
13521352
/* use canonical default value */
1353-
lyd_value_validate2(ctx, first->schema, slist->uniques[u][v]->dflt.str,
1353+
lyd_value_validate3(ctx, first->schema, slist->uniques[u][v]->dflt.str,
13541354
strlen(slist->uniques[u][v]->dflt.str), LY_VALUE_SCHEMA_RESOLVED,
1355-
slist->uniques[u][v]->dflt.prefixes, NULL, NULL, NULL, &canon2);
1355+
slist->uniques[u][v]->dflt.prefixes, LYD_HINT_SCHEMA, NULL, NULL, NULL, &canon2);
13561356
val2 = canon2;
13571357
}
13581358

@@ -1480,8 +1480,9 @@ lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode,
14801480
val = lyd_get_value(diter);
14811481
} else if (uniques[u][v]->dflt.str) {
14821482
/* use canonical default value */
1483-
ret = lyd_value_validate2(ctx, snode, uniques[u][v]->dflt.str, strlen(uniques[u][v]->dflt.str),
1484-
LY_VALUE_SCHEMA_RESOLVED, uniques[u][v]->dflt.prefixes, NULL, NULL, NULL, &val);
1483+
ret = lyd_value_validate3(ctx, snode, uniques[u][v]->dflt.str, strlen(uniques[u][v]->dflt.str),
1484+
LY_VALUE_SCHEMA_RESOLVED, uniques[u][v]->dflt.prefixes, LYD_HINT_SCHEMA, NULL, NULL,
1485+
NULL, &val);
14851486
LY_CHECK_GOTO(ret, cleanup);
14861487
}
14871488
if (!val) {

src/xpath.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,8 +1709,8 @@ set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
17091709
}
17101710

17111711
/* print canonized string, ignore errors, the value may not satisfy schema constraints */
1712-
r = lyd_value_validate2(NULL, xp_node->node->schema, set->val.str, strlen(set->val.str), set->format,
1713-
set->prefix_data, NULL, set->ext, NULL, &canon);
1712+
r = lyd_value_validate3(NULL, xp_node->node->schema, set->val.str, strlen(set->val.str), set->format,
1713+
set->prefix_data, LYD_HINT_DATA, NULL, set->ext, NULL, &canon);
17141714
if (r && (r != LY_EINCOMPLETE)) {
17151715
/* invalid value, fine */
17161716
return LY_SUCCESS;

0 commit comments

Comments
 (0)