Przeglądaj źródła

fix(config): emqx:update_config/2,3 doesn't work on binary conf paths

Shawn 4 lat temu
rodzic
commit
e5c3199d6e
2 zmienionych plików z 21 dodań i 6 usunięć
  1. 5 5
      apps/emqx/src/emqx.erl
  2. 16 1
      apps/emqx/src/emqx_config_handler.erl

+ 5 - 5
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} ->

+ 16 - 1
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.