plugins types FEATURE enumeration LYB value support
diff --git a/src/plugins_types.h b/src/plugins_types.h
index 66b7a59..b5e8614 100644
--- a/src/plugins_types.h
+++ b/src/plugins_types.h
@@ -719,13 +719,18 @@
*/
/**
- * @brief Validate, canonize and store value of the YANG built-in enumeration type.
- * Implementation of the ::lyplg_type_store_clb.
+ * @brief Implementation of ::lyplg_type_store_clb for the built-in enumeration type.
*/
LY_ERR lyplg_type_store_enum(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, 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_print_clb for the built-in enumeration type.
+ */
+const void *lyplg_type_print_enum(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
+ void *prefix_data, ly_bool *dynamic, size_t *value_len);
+
/** @} pluginsTypesEnumeration */
/**
diff --git a/src/plugins_types/enumeration.c b/src/plugins_types/enumeration.c
index dadf51f..22422f4 100644
--- a/src/plugins_types/enumeration.c
+++ b/src/plugins_types/enumeration.c
@@ -12,8 +12,6 @@
* https://opensource.org/licenses/BSD-3-Clause
*/
-#define _GNU_SOURCE
-
#include "plugins_types.h"
#include <stdint.h>
@@ -27,56 +25,120 @@
#include "compat.h"
#include "plugins_internal.h" /* LY_TYPE_*_STR */
+/**
+ * @page howtoDataLYB LYB Binary Format
+ * @subsection howtoDataLYBTypesEnumeration enumeration (built-in)
+ *
+ * | Size (B) | Mandatory | Type | Meaning |
+ * | :------ | :-------: | :--: | :-----: |
+ * | 4 | yes | `int32 *` | assigned value of the enum |
+ */
+
API LY_ERR
lyplg_type_store_enum(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
- uint32_t options, LY_VALUE_FORMAT UNUSED(format), void *UNUSED(prefix_data), uint32_t hints,
+ uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
struct ly_err_item **err)
{
+ struct lysc_type_enum *type_enum = (struct lysc_type_enum *)type;
LY_ERR ret = LY_SUCCESS;
LY_ARRAY_COUNT_TYPE u;
- struct lysc_type_enum *type_enum = (struct lysc_type_enum *)type;
+ ly_bool found = 0;
- *err = NULL;
+ /* clear storage */
+ memset(storage, 0, sizeof *storage);
- /* check hints */
- ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
- LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
+ if (format == LY_VALUE_LYB) {
+ /* validation */
+ if (value_len != 4) {
+ ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB enumeration value size %zu (expected 4).",
+ value_len);
+ goto cleanup;
+ }
- /* find the matching enumeration value item */
- LY_ARRAY_FOR(type_enum->enums, u) {
- if (!ly_strncmp(type_enum->enums[u].name, value, value_len)) {
- /* we have a match */
- goto match;
+ /* find the matching enumeration value item */
+ LY_ARRAY_FOR(type_enum->enums, u) {
+ if (type_enum->enums[u].value == *(int32_t *)value) {
+ found = 1;
+ break;
+ }
+ }
+
+ if (!found) {
+ /* value not found */
+ ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid enumeration value % " PRIi32 ".",
+ *(int32_t *)value);
+ goto cleanup;
+ }
+ } else {
+ /* check hints */
+ ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
+ LY_CHECK_GOTO(ret, cleanup);
+
+ /* find the matching enumeration value item */
+ LY_ARRAY_FOR(type_enum->enums, u) {
+ if (!ly_strncmp(type_enum->enums[u].name, value, value_len)) {
+ found = 1;
+ break;
+ }
+ }
+
+ if (!found) {
+ /* enum not found */
+ ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid enumeration value \"%.*s\".", (int)value_len,
+ (char *)value);
+ goto cleanup;
}
}
- /* enum not found */
- ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid enumeration value \"%.*s\".", (int)value_len,
- (char *)value);
- goto cleanup;
-match:
- /* validation done */
-
- if (options & LYPLG_TYPE_STORE_DYNAMIC) {
- ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
- options &= ~LYPLG_TYPE_STORE_DYNAMIC;
- LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
- } else {
- ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
- LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
- }
+ /* init storage */
+ storage->_canonical = NULL;
storage->enum_item = &type_enum->enums[u];
storage->realtype = type;
-cleanup:
+ /* store canonical value */
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
- free((char *) value);
+ ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
+ options &= ~LYPLG_TYPE_STORE_DYNAMIC;
+ LY_CHECK_GOTO(ret, cleanup);
+ } else {
+ ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
+ LY_CHECK_GOTO(ret, cleanup);
}
+cleanup:
+ if (options & LYPLG_TYPE_STORE_DYNAMIC) {
+ free((void *)value);
+ }
+
+ if (ret) {
+ lyplg_type_free_simple(ctx, storage);
+ }
return ret;
}
+API 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)
+{
+ if (format == LY_VALUE_LYB) {
+ *dynamic = 0;
+ if (value_len) {
+ *value_len = 4;
+ }
+ return &value->enum_item->value;
+ }
+
+ /* use the cached canonical value */
+ if (dynamic) {
+ *dynamic = 0;
+ }
+ if (value_len) {
+ *value_len = strlen(value->_canonical);
+ }
+ return value->_canonical;
+}
+
/**
* @brief Plugin information for enumeration type implementation.
*
@@ -94,7 +156,7 @@
.plugin.store = lyplg_type_store_enum,
.plugin.validate = NULL,
.plugin.compare = lyplg_type_compare_simple,
- .plugin.print = lyplg_type_print_simple,
+ .plugin.print = lyplg_type_print_enum,
.plugin.hash = lyplg_type_hash_simple,
.plugin.duplicate = lyplg_type_dup_simple,
.plugin.free = lyplg_type_free_simple