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.
This commit is contained in:
Markus Mäkelä 2017-01-05 12:18:32 +02:00
parent 03391748ee
commit 9fa2de29d9
3 changed files with 26 additions and 7 deletions

View File

@ -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);

View File

@ -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;
/**

View File

@ -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;