plugins types FEATURE sort callback in binary
diff --git a/src/plugins_types.h b/src/plugins_types.h
index 1b92cb7..d0027e0 100644
--- a/src/plugins_types.h
+++ b/src/plugins_types.h
@@ -547,14 +547,17 @@
const struct lyd_value *val2);
/**
- * @brief Unused callback for sorting values.
+ * @brief Callback for sorting values.
+ *
+ * It can be assumed that the same context (dictionary) was used for storing both values and the realtype
+ * member of both the values is the same.
*
* @param[in] ctx libyang context.
* @param[in] val1 First value to compare.
* @param[in] val2 Second value to compare.
- * @return -1 if val1 < val2,
- * @return 0 if val1 == val2,
- * @return 1 if val1 > val2.
+ * @return Negative number if val1 < val2,
+ * @return Zero if val1 == val2,
+ * @return Positive number if val1 > val2.
*/
typedef int (*lyplg_type_sort_clb)(const struct ly_ctx *ctx, const struct lyd_value *val1,
const struct lyd_value *val2);
@@ -617,7 +620,7 @@
lyplg_type_store_clb store; /**< store and canonize the value in the type-specific way */
lyplg_type_validate_clb validate; /**< optional, validate the value in the type-specific way in data */
lyplg_type_compare_clb compare; /**< comparison callback to compare 2 values of the same type */
- lyplg_type_sort_clb sort; /**< unused comparison callback for sorting values */
+ lyplg_type_sort_clb sort; /**< comparison callback for sorting values */
lyplg_type_print_clb print; /**< printer callback to get string representing the value */
lyplg_type_dup_clb duplicate; /**< data duplication callback */
lyplg_type_free_clb free; /**< optional function to free the type-spceific way stored value */
@@ -695,6 +698,12 @@
const struct lyd_value *val2);
/**
+ * @brief Implementation of ::lyplg_type_sort_clb for the built-in binary type.
+ */
+LIBYANG_API_DECL int lyplg_type_sort_binary(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 binary type.
*/
LIBYANG_API_DECL const void *lyplg_type_print_binary(const struct ly_ctx *ctx, const struct lyd_value *value,
diff --git a/src/plugins_types/binary.c b/src/plugins_types/binary.c
index 9edc811..a801144 100644
--- a/src/plugins_types/binary.c
+++ b/src/plugins_types/binary.c
@@ -349,6 +349,26 @@
return LY_SUCCESS;
}
+LIBYANG_API_DEF int
+lyplg_type_sort_binary(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
+{
+ struct lyd_value_binary *v1, *v2;
+ int cmp;
+
+ LYD_VALUE_GET(val1, v1);
+ LYD_VALUE_GET(val2, v2);
+
+ if (v1->size < v2->size) {
+ return -1;
+ } else if (v1->size > v2->size) {
+ return 1;
+ }
+
+ cmp = memcmp(v1->data, v2->data, v1->size);
+
+ return cmp;
+}
+
LIBYANG_API_DEF const void *
lyplg_type_print_binary(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
@@ -452,7 +472,7 @@
.plugin.store = lyplg_type_store_binary,
.plugin.validate = NULL,
.plugin.compare = lyplg_type_compare_binary,
- .plugin.sort = NULL,
+ .plugin.sort = lyplg_type_sort_binary,
.plugin.print = lyplg_type_print_binary,
.plugin.duplicate = lyplg_type_dup_binary,
.plugin.free = lyplg_type_free_binary,
diff --git a/tests/utests/types/binary.c b/tests/utests/types/binary.c
index 4f3ea66..94a1b99 100644
--- a/tests/utests/types/binary.c
+++ b/tests/utests/types/binary.c
@@ -283,6 +283,48 @@
}
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_BINARY]);
+ struct lysc_type *lysc_type;
+ struct ly_err_item *err = NULL;
+
+ /* create schema. Prepare common used variables */
+ schema = MODULE_CREATE_YANG("a", "leaf-list ll {type binary;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
+ lysc_type = ((struct lysc_node_leaflist *)mod->compiled->data)->type;
+
+ /* v1 < v2, v2 > v1, v1 == v1 */
+ v1 = "YWhveQ=="; /* ahoy */
+ 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 = "YWhveg=="; /* ahoz */
+ 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);
+
+ /* v2 is shorter */
+ v1 = "YWhveQ=="; /* ahoj */
+ 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 = "YWhv"; /* aho */
+ 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(-1, type->sort(UTEST_LYCTX, &val2, &val1));
+ type->free(UTEST_LYCTX, &val1);
+ type->free(UTEST_LYCTX, &val2);
+}
+
+static void
test_data_lyb(void **state)
{
const char *schema;
@@ -300,6 +342,7 @@
UTEST(test_plugin_store),
UTEST(test_plugin_print),
UTEST(test_plugin_duplicate),
+ UTEST(test_plugin_sort),
UTEST(test_data_lyb),
};