plugins types FEATURE sort callback in enumeration
diff --git a/src/plugins_types.h b/src/plugins_types.h
index a6851e8..c7069c2 100644
--- a/src/plugins_types.h
+++ b/src/plugins_types.h
@@ -877,6 +877,12 @@
const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err);
/**
+ * @brief Implementation of ::lyplg_type_sort_clb for the built-in enumeration type.
+ */
+LIBYANG_API_DEF int lyplg_type_sort_enum(const struct ly_ctx *ctx, const struct lyd_value *val1,
+ const struct lyd_value *val2);
+
+/**
* @brief Implementation of ::lyplg_type_print_clb for the built-in enumeration type.
*/
LIBYANG_API_DECL const void *lyplg_type_print_enum(const struct ly_ctx *ctx, const struct lyd_value *value,
diff --git a/src/plugins_types/enumeration.c b/src/plugins_types/enumeration.c
index 2e7543c..462b7e3 100644
--- a/src/plugins_types/enumeration.c
+++ b/src/plugins_types/enumeration.c
@@ -134,6 +134,19 @@
return ret;
}
+LIBYANG_API_DEF int
+lyplg_type_sort_enum(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
+ const struct lyd_value *val2)
+{
+ if (val1->enum_item->value > val2->enum_item->value) {
+ return -1;
+ } else if (val1->enum_item->value < val2->enum_item->value) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
LIBYANG_API_DEF const void *
lyplg_type_print_enum(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
@@ -192,7 +205,7 @@
.plugin.store = lyplg_type_store_enum,
.plugin.validate = NULL,
.plugin.compare = lyplg_type_compare_simple,
- .plugin.sort = NULL,
+ .plugin.sort = lyplg_type_sort_enum,
.plugin.print = lyplg_type_print_enum,
.plugin.duplicate = lyplg_type_dup_simple,
.plugin.free = lyplg_type_free_simple,
diff --git a/tests/utests/types/enumeration.c b/tests/utests/types/enumeration.c
index 88c6a69..04bbae2 100644
--- a/tests/utests/types/enumeration.c
+++ b/tests/utests/types/enumeration.c
@@ -88,6 +88,34 @@
}
static void
+test_plugin_sort(void **state)
+{
+ const char *v1, *v2;
+ const char *schema;
+ struct lys_module *mod;
+ struct lyd_value val1 = {0}, val2 = {0};
+ struct lyplg_type *type = lyplg_type_plugin_find("", NULL, ly_data_type2str[LY_TYPE_ENUM]);
+ struct lysc_type *lysc_type;
+ struct ly_err_item *err = NULL;
+
+ schema = MODULE_CREATE_YANG("sort", "leaf l1 {type enumeration {enum white; enum yellow; enum black;}}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
+ lysc_type = ((struct lysc_node_leaf *)mod->compiled->data)->type;
+
+ v1 = "white";
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, v1, strlen(v1),
+ 0, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL, &val1, NULL, &err));
+ v2 = "black";
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, v2, strlen(v2),
+ 0, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL, &val2, NULL, &err));
+ assert_int_equal(1, type->sort(UTEST_LYCTX, &val1, &val2));
+ assert_int_equal(0, type->sort(UTEST_LYCTX, &val1, &val1));
+ assert_int_equal(-1, type->sort(UTEST_LYCTX, &val2, &val1));
+ type->free(UTEST_LYCTX, &val1);
+ type->free(UTEST_LYCTX, &val2);
+}
+
+static void
test_plugin_lyb(void **state)
{
const char *schema;
@@ -103,6 +131,8 @@
{
const struct CMUnitTest tests[] = {
UTEST(test_data_xml),
+ UTEST(test_data_xml),
+ UTEST(test_plugin_sort),
UTEST(test_plugin_lyb),
};