From e5c3199d6eb7ea6fe65f705cf7d189fbdc81a901 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Tue, 17 Aug 2021 19:30:43 +0800 Subject: [PATCH] fix(config): emqx:update_config/2,3 doesn't work on binary conf paths --- apps/emqx/src/emqx.erl | 10 +++++----- apps/emqx/src/emqx_config_handler.erl | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/apps/emqx/src/emqx.erl b/apps/emqx/src/emqx.erl index ce9036389..5df45bd15 100644 --- a/apps/emqx/src/emqx.erl +++ b/apps/emqx/src/emqx.erl @@ -193,30 +193,30 @@ run_fold_hook(HookPoint, Args, Acc) -> emqx_hooks:run_fold(HookPoint, Args, Acc). -spec update_config(emqx_map_lib:config_key_path(), emqx_config:update_request()) -> - {ok, emqx_config:config(), emqx_config:raw_config()} | {error, term()}. + {ok, emqx_config:update_result()} | {error, emqx_config:update_error()}. update_config(KeyPath, UpdateReq) -> update_config(KeyPath, UpdateReq, #{}). -spec update_config(emqx_map_lib:config_key_path(), emqx_config:update_request(), emqx_config:update_opts()) -> - {ok, emqx_config:config(), emqx_config:raw_config()} | {error, term()}. + {ok, emqx_config:update_result()} | {error, emqx_config:update_error()}. update_config([RootName | _] = KeyPath, UpdateReq, Opts) -> emqx_config_handler:update_config(emqx_config:get_schema_mod(RootName), KeyPath, {{update, UpdateReq}, Opts}). -spec remove_config(emqx_map_lib:config_key_path()) -> - {ok, emqx_config:config(), emqx_config:raw_config()} | {error, term()}. + {ok, emqx_config:update_result()} | {error, emqx_config:update_error()}. remove_config(KeyPath) -> remove_config(KeyPath, #{}). -spec remove_config(emqx_map_lib:config_key_path(), emqx_config:update_opts()) -> - ok | {error, term()}. + {ok, emqx_config:update_result()} | {error, emqx_config:update_error()}. remove_config([RootName | _] = KeyPath, Opts) -> emqx_config_handler:update_config(emqx_config:get_schema_mod(RootName), KeyPath, {remove, Opts}). -spec reset_config(emqx_map_lib:config_key_path(), emqx_config:update_opts()) -> - {ok, emqx_config:config(), emqx_config:raw_config()} | {error, term()}. + {ok, emqx_config:update_result()} | {error, emqx_config:update_error()}. reset_config([RootName | _] = KeyPath, Opts) -> case emqx_config:get_default_value(KeyPath) of {ok, Default} -> diff --git a/apps/emqx/src/emqx_config_handler.erl b/apps/emqx/src/emqx_config_handler.erl index ac21afaa1..2ee90cb04 100644 --- a/apps/emqx/src/emqx_config_handler.erl +++ b/apps/emqx/src/emqx_config_handler.erl @@ -38,6 +38,13 @@ -define(MOD, {mod}). +-define(ATOM_CONF_PATH(PATH, EXP, EXP_ON_FAIL), + try [safe_atom(Key) || Key <- PATH] of + AtomKeyPath -> EXP + catch + error:badarg -> EXP_ON_FAIL + end). + -type handler_name() :: module(). -type handlers() :: #{emqx_config:config_key() => handlers(), ?MOD => handler_name()}. @@ -62,7 +69,8 @@ start_link() -> -spec update_config(module(), emqx_config:config_key_path(), emqx_config:update_args()) -> {ok, emqx_config:update_result()} | {error, emqx_config:update_error()}. update_config(SchemaModule, ConfKeyPath, UpdateArgs) -> - gen_server:call(?MODULE, {change_config, SchemaModule, ConfKeyPath, UpdateArgs}). + ?ATOM_CONF_PATH(ConfKeyPath, gen_server:call(?MODULE, {change_config, SchemaModule, + AtomKeyPath, UpdateArgs}), {error, ConfKeyPath}). -spec add_handler(emqx_config:config_key_path(), handler_name()) -> ok. add_handler(ConfKeyPath, HandlerName) -> @@ -224,3 +232,10 @@ bin_path(ConfKeyPath) -> [bin(Key) || Key <- ConfKeyPath]. bin(A) when is_atom(A) -> atom_to_binary(A, utf8); bin(B) when is_binary(B) -> B. + +safe_atom(Bin) when is_binary(Bin) -> + binary_to_existing_atom(Bin, latin1); +safe_atom(Str) when is_list(Str) -> + list_to_existing_atom(Str); +safe_atom(Atom) when is_atom(Atom) -> + Atom. \ No newline at end of file