From 9fa2de29d9efe7445d848b0aac457a982b27839a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Thu, 5 Jan 2017 12:18:32 +0200 Subject: [PATCH] Return enum values as integers instead of strings If the enums are converted to the acutual enum values before they are returned, this removes the need for the modules to process the enum strings to enum values. This allows modules to use enumerations with minimal effort. --- include/maxscale/config.h | 5 +++-- include/maxscale/modinfo.h | 9 ++++++++- server/core/config.c | 19 +++++++++++++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/maxscale/config.h b/include/maxscale/config.h index f39190e77..e35c54aac 100644 --- a/include/maxscale/config.h +++ b/include/maxscale/config.h @@ -267,12 +267,13 @@ const char* config_get_string(const CONFIG_PARAMETER *params, const char *key); * * @param params List of configuration parameters * @param key Parameter name + * @param values All possible enumeration values * - * @return The raw string value + * @return The enumeration value converted to an int * * TODO: Allow multiple enumeration values */ -const char* config_get_enum(const CONFIG_PARAMETER *params, const char *key); +int config_get_enum(const CONFIG_PARAMETER *params, const char *key, const MXS_ENUM_VALUE *values); char* config_clean_string_list(const char* str); CONFIG_PARAMETER* config_clone_param(const CONFIG_PARAMETER* param); diff --git a/include/maxscale/modinfo.h b/include/maxscale/modinfo.h index b8a5ada92..e70fd3b60 100644 --- a/include/maxscale/modinfo.h +++ b/include/maxscale/modinfo.h @@ -93,6 +93,13 @@ enum mxs_module_param_options MXS_MODULE_OPT_PATH_F_OK = (1 << 4) /**< PATH: Path must exist */ }; +/** String to enum value mappings */ +typedef struct mxs_enum_value +{ + const char *name; /**< Name of the enum value */ + int enum_value; /**< The integer value of the enum */ +}MXS_ENUM_VALUE; + /** Module parameter declaration */ typedef struct mxs_module_param { @@ -100,7 +107,7 @@ typedef struct mxs_module_param enum mxs_module_param_type type; /**< Type of the parameter */ const char *default_value; /**< Default value for the parameter, NULL for no default value */ uint64_t options; /**< Parameter options */ - const char **accepted_values; /**< Only for enum values */ + const MXS_ENUM_VALUE *accepted_values; /**< Only for enum values */ } MXS_MODULE_PARAM; /** diff --git a/server/core/config.c b/server/core/config.c index aaaf0a40e..bf0b2e53c 100644 --- a/server/core/config.c +++ b/server/core/config.c @@ -1071,11 +1071,22 @@ const char* config_get_string(const CONFIG_PARAMETER *params, const char *key) return value; } -const char* config_get_enum(const CONFIG_PARAMETER *params, const char *key) +int config_get_enum(const CONFIG_PARAMETER *params, const char *key, const MXS_ENUM_VALUE *enum_values) { const char *value = config_get_value_string(params, key); + ss_dassert(*value); - return value; + + for (int i = 0; enum_values[i].name; i++) + { + if (strcmp(enum_values[i].name, key) == 0) + { + return enum_values[i].enum_value; + } + } + + ss_dassert(false); + return -1; } CONFIG_PARAMETER* config_clone_param(const CONFIG_PARAMETER* param) @@ -3449,9 +3460,9 @@ bool config_param_is_valid(const char *module, const char *type, const char *key case MXS_MODULE_PARAM_ENUM: if (mod->parameters[i].accepted_values) { - for (int j = 0; mod->parameters[i].accepted_values[j]; j++) + for (int j = 0; mod->parameters[i].accepted_values[j].name; j++) { - if (strcmp(mod->parameters[i].accepted_values[j], value) == 0) + if (strcmp(mod->parameters[i].accepted_values[j].name, value) == 0) { valid = true; break;