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.
This commit is contained in:
parent
a9ff2a7056
commit
e5c2b1a8c4
@ -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<std::string>& ignored_params,
|
||||
const MXS_MODULE_PARAM* basic_params,
|
||||
const MXS_MODULE_PARAM* module_params,
|
||||
json_t* output)
|
||||
{
|
||||
set<string> ignore_set;
|
||||
if (ignored_params)
|
||||
// Create a map of the config values to ease their extraction
|
||||
std::unordered_map<std::string, const MXS_CONFIG_PARAMETER*> 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<string, const MXS_CONFIG_PARAMETER*> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<std::string>& ignored_params,
|
||||
const MXS_MODULE_PARAM* basic_params,
|
||||
const MXS_MODULE_PARAM* module_params,
|
||||
json_t* output);
|
||||
|
||||
/**
|
||||
* @brief Convert object names to correct format
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user