tree schema FEATURE use LY_ENOT for false feature state
diff --git a/src/plugins_types.c b/src/plugins_types.c
index 290c9fa..38a3747 100644
--- a/src/plugins_types.c
+++ b/src/plugins_types.c
@@ -920,7 +920,7 @@
/* check that the bit is not disabled */
LY_ARRAY_FOR(type_bits->bits[u].iffeatures, v) {
- if (!lysc_iffeature_value(&type_bits->bits[u].iffeatures[v])) {
+ if (lysc_iffeature_value(&type_bits->bits[u].iffeatures[v]) == LY_ENOT) {
asprintf(&errmsg, "Bit \"%s\" is disabled by its %" LY_PRI_ARRAY_SIZE_TYPE ". if-feature condition.",
type_bits->bits[u].name, v + 1);
goto error;
@@ -1076,7 +1076,7 @@
/* check that the enumeration value is not disabled */
LY_ARRAY_FOR(type_enum->enums[u].iffeatures, v) {
- if (!lysc_iffeature_value(&type_enum->enums[u].iffeatures[v])) {
+ if (lysc_iffeature_value(&type_enum->enums[u].iffeatures[v]) == LY_ENOT) {
asprintf(&errmsg, "Enumeration \"%s\" is disabled by its %" LY_PRI_ARRAY_SIZE_TYPE ". if-feature condition.",
type_enum->enums[u].name, v + 1);
goto error;
diff --git a/src/tree_schema.c b/src/tree_schema.c
index f0c386e..958c342 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -405,11 +405,11 @@
return lysc_path_until(node, NULL, pathtype, buffer, buflen);
}
-API int
+API LY_ERR
lysc_feature_value(const struct lysc_feature *feature)
{
- LY_CHECK_ARG_RET(NULL, feature, -1);
- return feature->flags & LYS_FENABLED ? 1 : 0;
+ LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
+ return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
}
uint8_t
@@ -425,11 +425,11 @@
return result >> 2 * (pos % 4);
}
-static int
+static LY_ERR
lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
{
uint8_t op;
- int a, b;
+ LY_ERR a, b;
op = lysc_iff_getop(iff->expr, *index_e);
(*index_e)++;
@@ -440,22 +440,30 @@
return lysc_feature_value(iff->features[(*index_f)++]);
case LYS_IFF_NOT:
/* invert result */
- return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
+ return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
case LYS_IFF_AND:
case LYS_IFF_OR:
a = lysc_iffeature_value_(iff, index_e, index_f);
b = lysc_iffeature_value_(iff, index_e, index_f);
if (op == LYS_IFF_AND) {
- return a && b;
+ if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
+ return LY_SUCCESS;
+ } else {
+ return LY_ENOT;
+ }
} else { /* LYS_IFF_OR */
- return a || b;
+ if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
+ return LY_SUCCESS;
+ } else {
+ return LY_ENOT;
+ }
}
}
return 0;
}
-API int
+API LY_ERR
lysc_iffeature_value(const struct lysc_iffeature *iff)
{
int index_e = 0, index_f = 0;
@@ -533,7 +541,7 @@
if (value) { /* enable */
/* check referenced features if they are enabled */
LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
- if (!lysc_iffeature_value(iff)) {
+ if (lysc_iffeature_value(iff) == LY_ENOT) {
if (all) {
++disabled_count;
goto next;
@@ -612,7 +620,7 @@
}
/* check the feature's if-features which could change by the previous change of our feature */
LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
- if (!lysc_iffeature_value(iff)) {
+ if (lysc_iffeature_value(iff) == LY_ENOT) {
/* the feature must be disabled now */
(*df)->flags &= ~LYS_FENABLED;
/* add the feature into the list of changed features */
@@ -680,7 +688,7 @@
if (node->iffeatures) {
/* check local if-features */
LY_ARRAY_FOR(node->iffeatures, u) {
- if (!lysc_iffeature_value(&node->iffeatures[u])) {
+ if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
return node;
}
}
diff --git a/src/tree_schema.h b/src/tree_schema.h
index 7764c64..9917b5d 100644
--- a/src/tree_schema.h
+++ b/src/tree_schema.h
@@ -1723,19 +1723,21 @@
* @brief Get how the if-feature statement currently evaluates.
*
* @param[in] iff Compiled if-feature statement to evaluate.
- * @return If the statement evaluates to true, 1 is returned. 0 is returned when the statement evaluates to false.
+ * @return LY_SUCCESS if the statement evaluates to true,
+ * @return LY_ENOT if it evaluates to false,
+ * @return LY_ERR on error.
*/
-int lysc_iffeature_value(const struct lysc_iffeature *iff);
+LY_ERR lysc_iffeature_value(const struct lysc_iffeature *iff);
/**
* @brief Get the current status of the provided feature.
*
* @param[in] feature Compiled feature statement to examine.
- * @return 1 if feature is enabled,
- * @return 0 if feature is disabled,
- * @return -1 in case of error (invalid argument)
+ * @return LY_SUCCESS if feature is enabled,
+ * @return LY_ENOT if feature is disabled,
+ * @return LY_ERR in case of error (invalid argument)
*/
-int lysc_feature_value(const struct lysc_feature *feature);
+LY_ERR lysc_feature_value(const struct lysc_feature *feature);
/**
* @brief Get all the schema nodes (atoms) that are required for \p xpath to be evaluated.
diff --git a/tests/utests/schema/test_schema_stmts.c b/tests/utests/schema/test_schema_stmts.c
index 35ce06a..eab21c9 100644
--- a/tests/utests/schema/test_schema_stmts.c
+++ b/tests/utests/schema/test_schema_stmts.c
@@ -211,56 +211,56 @@
/* all features are disabled by default */
LY_ARRAY_FOR(mod->compiled->features, struct lysc_feature, f) {
- assert_int_equal(0, lysc_feature_value(f));
+ assert_int_equal(LY_ENOT, lysc_feature_value(f));
}
/* enable f1 */
assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f1"));
f1 = &mod->compiled->features[0];
- assert_int_equal(1, lysc_feature_value(f1));
+ assert_int_equal(LY_SUCCESS, lysc_feature_value(f1));
/* enable orfeature */
f = &mod->compiled->features[3];
- assert_int_equal(0, lysc_feature_value(f));
+ assert_int_equal(LY_ENOT, lysc_feature_value(f));
assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "orfeature"));
- assert_int_equal(1, lysc_feature_value(f));
+ assert_int_equal(LY_SUCCESS, lysc_feature_value(f));
/* enable andfeature - no possible since f2 is disabled */
f = &mod->compiled->features[4];
- assert_int_equal(0, lysc_feature_value(f));
+ assert_int_equal(LY_ENOT, lysc_feature_value(f));
assert_int_equal(LY_EDENIED, lys_feature_enable(mod, "andfeature"));
logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s).");
- assert_int_equal(0, lysc_feature_value(f));
+ assert_int_equal(LY_ENOT, lysc_feature_value(f));
/* first enable f2, so f5 can be enabled then */
assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f2"));
assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "andfeature"));
- assert_int_equal(1, lysc_feature_value(f));
+ assert_int_equal(LY_SUCCESS, lysc_feature_value(f));
/* f1 is enabled, so f6 cannot be enabled */
f = &mod->compiled->features[5];
- assert_int_equal(0, lysc_feature_value(f));
+ assert_int_equal(LY_ENOT, lysc_feature_value(f));
assert_int_equal(LY_EDENIED, lys_feature_enable(mod, "f6"));
logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s).");
- assert_int_equal(0, lysc_feature_value(f));
+ assert_int_equal(LY_ENOT, lysc_feature_value(f));
/* so disable f1 - andfeature will became also disabled */
- assert_int_equal(1, lysc_feature_value(f1));
+ assert_int_equal(LY_SUCCESS, lysc_feature_value(f1));
assert_int_equal(LY_SUCCESS, lys_feature_disable(mod, "f1"));
- assert_int_equal(0, lysc_feature_value(f1));
- assert_int_equal(0, lysc_feature_value(&mod->compiled->features[4]));
+ assert_int_equal(LY_ENOT, lysc_feature_value(f1));
+ assert_int_equal(LY_ENOT, lysc_feature_value(&mod->compiled->features[4]));
/* while orfeature is stille enabled */
- assert_int_equal(1, lysc_feature_value(&mod->compiled->features[3]));
+ assert_int_equal(LY_SUCCESS, lysc_feature_value(&mod->compiled->features[3]));
/* and finally f6 can be enabled */
assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f6"));
- assert_int_equal(1, lysc_feature_value(&mod->compiled->features[5]));
+ assert_int_equal(LY_SUCCESS, lysc_feature_value(&mod->compiled->features[5]));
/* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */
- assert_int_equal(1, lysc_iffeature_value(&mod->compiled->features[6].iffeatures[0]));
+ assert_int_equal(LY_SUCCESS, lysc_iffeature_value(&mod->compiled->features[6].iffeatures[0]));
/* long evaluation of f8 to need to reallocate internal stack for operators */
- assert_int_equal(1, lysc_iffeature_value(&mod->compiled->features[7].iffeatures[0]));
+ assert_int_equal(LY_SUCCESS, lysc_iffeature_value(&mod->compiled->features[7].iffeatures[0]));
/* double negation of disabled f1 -> disabled */
- assert_int_equal(0, lysc_iffeature_value(&mod->compiled->features[8].iffeatures[0]));
+ assert_int_equal(LY_ENOT, lysc_iffeature_value(&mod->compiled->features[8].iffeatures[0]));
/* disable all features */
assert_int_equal(LY_SUCCESS, lys_feature_disable(mod, "*"));
diff --git a/tests/utests/schema/test_tree_schema_compile.c b/tests/utests/schema/test_tree_schema_compile.c
index 81d13bb..b82b814 100644
--- a/tests/utests/schema/test_tree_schema_compile.c
+++ b/tests/utests/schema/test_tree_schema_compile.c
@@ -2227,14 +2227,14 @@
assert_string_equal("f", child->iffeatures[0].features[0]->name);
assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f"));
logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it.");
- assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0]));
+ assert_int_equal(LY_ENOT, lysc_iffeature_value(&child->iffeatures[0]));
/* make the imported module implemented and enable the feature */
assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL));
assert_int_equal(LY_SUCCESS, lys_set_implemented(mod));
assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f"));
assert_string_equal("f", child->iffeatures[0].features[0]->name);
- assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0]));
+ assert_int_equal(LY_SUCCESS, lysc_iffeature_value(&child->iffeatures[0]));
ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule bsub {belongs-to b {prefix b;} grouping grp {leaf b {when 1; type string;} leaf c {type string;}}}");
assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include bsub;uses grp {when 2;}}", LYS_IN_YANG));