sink.utils.counter: validate parameters using param-validation
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 8 Oct 2019 19:21:55 +0000 (15:21 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 17 Oct 2019 16:13:26 +0000 (12:13 -0400)
This patch makes sink.utils.counter validate its parameters using the
param-validation utility, instead of doing it by hand.

Change-Id: I0355eef4f1d7ecdfe4b943ff5f71af429b9419fa
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2154
CI-Build: Francis Deslauriers <francis.deslauriers@efficios.com>

src/plugins/utils/Makefile.am
src/plugins/utils/counter/counter.c

index 6f91de9f0f6ae4a8a6762c30bae242e07042c269..3b525afe614ae609a078bca6a60d34647774f4c4 100644 (file)
@@ -17,5 +17,6 @@ if !ENABLE_BUILT_IN_PLUGINS
 babeltrace_plugin_utils_la_LIBADD += \
        $(top_builddir)/src/lib/libbabeltrace2.la \
        $(top_builddir)/src/common/libbabeltrace2-common.la \
-       $(top_builddir)/src/logging/libbabeltrace2-logging.la
+       $(top_builddir)/src/logging/libbabeltrace2-logging.la \
+       $(top_builddir)/src/plugins/common/param-validation/libbabeltrace2-param-validation.la
 endif
index 1c0ed44e90e98292f8e11e4b2badd028b07dd28d..82d1b907e45b2d49ea43bf92e23d1a44ebb35cca 100644 (file)
@@ -31,6 +31,7 @@
 #include "common/assert.h"
 #include <inttypes.h>
 #include <stdint.h>
+#include "plugins/common/param-validation/param-validation.h"
 
 #include "counter.h"
 
@@ -135,6 +136,12 @@ void counter_finalize(bt_self_component_sink *comp)
        g_free(counter);
 }
 
+struct bt_param_validation_map_value_entry_descr counter_params[] = {
+       { "step", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_UNSIGNED_INTEGER } },
+       { "hide-zero", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
+       BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
+};
+
 BT_HIDDEN
 bt_component_class_initialize_method_status counter_init(
                bt_self_component_sink *component,
@@ -142,12 +149,13 @@ bt_component_class_initialize_method_status counter_init(
                const bt_value *params,
                __attribute__((unused)) void *init_method_data)
 {
-       bt_component_class_initialize_method_status status =
-               BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
+       bt_component_class_initialize_method_status status;
        bt_self_component_add_port_status add_port_status;
        struct counter *counter = g_new0(struct counter, 1);
        const bt_value *step = NULL;
        const bt_value *hide_zero = NULL;
+       enum bt_param_validation_status validation_status;
+       gchar *validate_error = NULL;
 
        if (!counter) {
                status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
@@ -160,56 +168,47 @@ bt_component_class_initialize_method_status counter_init(
                bt_self_component_as_component(counter->self_comp));
        add_port_status = bt_self_component_sink_add_input_port(component,
                "in", NULL, NULL);
-       switch (add_port_status) {
-       case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
-               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
+       if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
+               status = (int) add_port_status;
                goto error;
-       case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
+       }
+
+       validation_status = bt_param_validation_validate(params,
+               counter_params, &validate_error);
+       if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
                status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto error;
-       default:
-               break;
+       } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
+               BT_COMP_LOGE_APPEND_CAUSE(counter->self_comp,
+                       "%s", validate_error);
+               goto error;
        }
 
        counter->last_printed_total = -1ULL;
        counter->step = 10000;
        step = bt_value_map_borrow_entry_value_const(params, "step");
        if (step) {
-               if (!bt_value_is_unsigned_integer(step)) {
-                       BT_COMP_LOGE("`step` parameter: expecting an unsigned integer value: "
-                               "type=%s", bt_common_value_type_string(
-                                       bt_value_get_type(step)));
-                       goto error;
-               }
-
                counter->step = bt_value_integer_unsigned_get(step);
        }
 
        hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
        if (hide_zero) {
-               if (!bt_value_is_bool(hide_zero)) {
-                       BT_COMP_LOGE("`hide-zero` parameter: expecting a boolean value: "
-                               "type=%s", bt_common_value_type_string(
-                                       bt_value_get_type(hide_zero)));
-                       goto error;
-               }
-
                counter->hide_zero = (bool) bt_value_bool_get(hide_zero);
        }
 
        bt_self_component_set_data(
                bt_self_component_sink_as_self_component(component),
                counter);
+
+       status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
        goto end;
 
 error:
        destroy_private_counter_data(counter);
 
-       if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
-               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
-       }
-
 end:
+       g_free(validate_error);
        return status;
 }
 
This page took 0.025736 seconds and 4 git commands to generate.