From e5c2b1a8c49f961e5b2a99d1d9118c702b032b46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Thu, 16 Aug 2018 11:43:50 +0300 Subject: [PATCH] Create JSON serialization from object parameters The services, monitors and filters now construct the JSON format parameters from the configuration parameters. This reduces the need for the amount of explicit operations and makes adding new parameters easier. --- server/core/config.cc | 55 ++++++++++++++++------------------ server/core/filter.cc | 3 +- server/core/internal/config.hh | 9 ++++-- server/core/monitor.cc | 8 ++--- server/core/service.cc | 30 ++----------------- 5 files changed, 38 insertions(+), 67 deletions(-) diff --git a/server/core/config.cc b/server/core/config.cc index 930166135..3fee38462 100644 --- a/server/core/config.cc +++ b/server/core/config.cc @@ -3390,43 +3390,38 @@ static json_t* param_value_to_json(const MXS_CONFIG_PARAMETER* param, const MXS_ return rval; } -void config_add_module_params_json(const MXS_CONFIG_PARAMETER* parameters, const MXS_MODULE_PARAM* param_info, - const MXS_MODULE_PARAM* ignored_params, json_t* output) +void config_add_module_params_json(const MXS_CONFIG_PARAMETER* parameters, + const std::unordered_set& ignored_params, + const MXS_MODULE_PARAM* basic_params, + const MXS_MODULE_PARAM* module_params, + json_t* output) { - set ignore_set; - if (ignored_params) + // Create a map of the config values to ease their extraction + std::unordered_map params; + + for (const MXS_CONFIG_PARAMETER* p = parameters; p; p = p->next) { - for (int i = 0; ignored_params[i].name; i++) - { - ignore_set.insert(ignored_params[i].name); - } + params[p->name] = p; } - // The parameter values are added to the json output in the order they are in 'param_info'. - // Add the config values to a map and loop through 'param_info'. - // 'parameters' is a linked list while the others are null-terminated arrays. - std::map param_values_map; - for (const MXS_CONFIG_PARAMETER* param = parameters; param; param = param->next) + for (auto param_info : {basic_params, module_params}) { - ss_dassert(param_values_map.count(param->name) == 0); - param_values_map[param->name] = param; - } - - for (int i = 0; param_info[i].name; i++) - { - if (ignore_set.count(param_info[i].name) == 0) + for (int i = 0; param_info[i].name; i++) { - auto item = param_values_map.find(param_info[i].name); - if (item != param_values_map.end()) + if (ignored_params.count(param_info[i].name) == 0 && + !json_object_get(output, param_info[i].name)) { - json_object_set_new(output, param_info[i].name, - param_value_to_json(item->second, ¶m_info[i])); - } - else - { - // The parameter was not set in config and does not have a default value. - // Print a null value. - json_object_set_new(output, param_info[i].name, json_null()); + if (auto item = params[param_info[i].name]) + { + json_object_set_new(output, param_info[i].name, + param_value_to_json(item, ¶m_info[i])); + } + else + { + // The parameter was not set in config and does not have a default value. + // Print a null value. + json_object_set_new(output, param_info[i].name, json_null()); + } } } } diff --git a/server/core/filter.cc b/server/core/filter.cc index a8b903b5c..7552265c7 100644 --- a/server/core/filter.cc +++ b/server/core/filter.cc @@ -401,7 +401,8 @@ json_t* filter_parameters_to_json(const SFilterDef& filter) /** Add custom module parameters */ const MXS_MODULE* mod = get_module(filter->module.c_str(), MODULE_FILTER); - config_add_module_params_json(filter->parameters, mod->parameters, config_filter_params, rval); + config_add_module_params_json(filter->parameters, {CN_TYPE, CN_MODULE}, + config_filter_params, mod->parameters, rval); return rval; } diff --git a/server/core/internal/config.hh b/server/core/internal/config.hh index 4a50638bd..d941bc1f7 100644 --- a/server/core/internal/config.hh +++ b/server/core/internal/config.hh @@ -169,11 +169,14 @@ bool config_have_required_ssl_params(CONFIG_CONTEXT *obj); * * @param parameters List of configuration parameter values * @param param_info Configuration parameter type information - * @param ignored_params List of parameters which should not be added to the output + * @param ignored_params Set of parameters which should not be added to the output * @param output Output JSON object where the parameters are added */ -void config_add_module_params_json(const MXS_CONFIG_PARAMETER* parameters, const MXS_MODULE_PARAM* param_info, - const MXS_MODULE_PARAM* ignored_params, json_t* output); +void config_add_module_params_json(const MXS_CONFIG_PARAMETER* parameters, + const std::unordered_set& ignored_params, + const MXS_MODULE_PARAM* basic_params, + const MXS_MODULE_PARAM* module_params, + json_t* output); /** * @brief Convert object names to correct format diff --git a/server/core/monitor.cc b/server/core/monitor.cc index b5dff04c6..8a252b13a 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -1743,13 +1743,9 @@ static const char* monitor_state_to_string(monitor_state_t state) json_t* monitor_parameters_to_json(const MXS_MONITOR* monitor) { json_t* rval = json_object(); - // The 'monitor->parameters' contain some items which are printed specially and should be ignored here. - static const MXS_MODULE_PARAM already_printed[] = {{CN_TYPE}, {CN_MODULE}, {CN_SERVERS}}; - config_add_module_params_json(monitor->parameters, config_monitor_params, already_printed, rval); - /** Add module-specific parameters */ const MXS_MODULE* mod = get_module(monitor->module_name, MODULE_MONITOR); - config_add_module_params_json(monitor->parameters, mod->parameters, NULL, rval); - + config_add_module_params_json(monitor->parameters, {CN_TYPE, CN_MODULE, CN_SERVERS}, + config_monitor_params, mod->parameters, rval); return rval; } diff --git a/server/core/service.cc b/server/core/service.cc index 3ebf1321b..e3b39b29d 100644 --- a/server/core/service.cc +++ b/server/core/service.cc @@ -1929,34 +1929,10 @@ json_t* service_parameters_to_json(const SERVICE* service) { json_t* rval = json_object(); - string options{config_get_string(service->svc_config_param, "router_options")}; - - json_object_set_new(rval, CN_ROUTER_OPTIONS, json_string(options.c_str())); - json_object_set_new(rval, CN_USER, json_string(service->user)); - json_object_set_new(rval, CN_PASSWORD, json_string(service->password)); - - json_object_set_new(rval, CN_ENABLE_ROOT_USER, json_boolean(service->enable_root)); - json_object_set_new(rval, CN_MAX_RETRY_INTERVAL, json_integer(service->max_retry_interval)); - json_object_set_new(rval, CN_MAX_CONNECTIONS, json_integer(service->max_connections)); - json_object_set_new(rval, CN_CONNECTION_TIMEOUT, json_integer(service->conn_idle_timeout)); - - json_object_set_new(rval, CN_AUTH_ALL_SERVERS, json_boolean(service->users_from_all)); - json_object_set_new(rval, CN_STRIP_DB_ESC, json_boolean(service->strip_db_esc)); - json_object_set_new(rval, CN_LOCALHOST_MATCH_WILDCARD_HOST, - json_boolean(service->localhost_match_wildcard_host)); - json_object_set_new(rval, CN_VERSION_STRING, json_string(service->version_string)); - - if (*service->weightby) - { - json_object_set_new(rval, CN_WEIGHTBY, json_string(service->weightby)); - } - - json_object_set_new(rval, CN_LOG_AUTH_WARNINGS, json_boolean(service->log_auth_warnings)); - json_object_set_new(rval, CN_RETRY_ON_FAILURE, json_boolean(service->retry_start)); - - /** Add custom module parameters */ const MXS_MODULE* mod = get_module(service->routerModule, MODULE_ROUTER); - config_add_module_params_json(service->svc_config_param, mod->parameters, config_service_params, rval); + config_add_module_params_json(service->svc_config_param, + {CN_TYPE, CN_ROUTER, CN_SERVERS, CN_FILTERS}, + config_service_params, mod->parameters, rval); return rval; }