fix(emqx_config): handle default value correctly

This commit is contained in:
Zaiming Shi 2021-07-29 10:18:12 +02:00
parent 8d2f9d4e1c
commit 82df49d165
2 changed files with 17 additions and 9 deletions

View File

@ -73,12 +73,12 @@
%% @doc For the given path, get root value enclosed in a single-key map. %% @doc For the given path, get root value enclosed in a single-key map.
-spec get_root(emqx_map_lib:config_key_path()) -> map(). -spec get_root(emqx_map_lib:config_key_path()) -> map().
get_root([RootName | _]) -> get_root([RootName | _]) ->
#{RootName => do_get(?CONF, [RootName])}. #{RootName => do_get(?CONF, [RootName], #{})}.
%% @doc For the given path, get raw root value enclosed in a single-key map. %% @doc For the given path, get raw root value enclosed in a single-key map.
%% key is ensured to be binary. %% key is ensured to be binary.
get_root_raw([RootName | _]) -> get_root_raw([RootName | _]) ->
#{bin(RootName) => do_get(?RAW_CONF, [RootName])}. #{bin(RootName) => do_get(?RAW_CONF, [RootName], #{})}.
%% @doc Get a config value for the given path. %% @doc Get a config value for the given path.
%% The path should at least include root config name. %% The path should at least include root config name.
@ -233,15 +233,21 @@ to_plainmap(RichMap) ->
bin(Bin) when is_binary(Bin) -> Bin; bin(Bin) when is_binary(Bin) -> Bin;
bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8). bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8).
do_get(PtKey, [RootName | KeyPath]) -> do_get(PtKey, KeyPath) ->
RootV = persistent_term:get(PtKey(RootName), #{}), Ref = make_ref(),
emqx_map_lib:deep_get(KeyPath, RootV). Res = do_get(PtKey, KeyPath, Ref),
case Res =:= Ref of
true -> error({config_not_found, KeyPath});
false -> Res
end.
do_get(PtKey, [RootName], Default) ->
persistent_term:get(PtKey(RootName), Default);
do_get(PtKey, [RootName | KeyPath], Default) -> do_get(PtKey, [RootName | KeyPath], Default) ->
RootV = persistent_term:get(PtKey(RootName), #{}), RootV = persistent_term:get(PtKey(RootName), #{}),
emqx_map_lib:deep_get(KeyPath, RootV, Default). emqx_map_lib:deep_get(KeyPath, RootV, Default).
do_put(PtKey, [RootName | KeyPath], DeepValue) -> do_put(PtKey, [RootName | KeyPath], DeepValue) ->
OldValue = do_get(PtKey, [RootName]), OldValue = do_get(PtKey, [RootName], #{}),
NewValue = emqx_map_lib:deep_put(KeyPath, OldValue, DeepValue), NewValue = emqx_map_lib:deep_put(KeyPath, OldValue, DeepValue),
persistent_term:put(PtKey(RootName), NewValue). persistent_term:put(PtKey(RootName), NewValue).

View File

@ -32,9 +32,11 @@
%%----------------------------------------------------------------- %%-----------------------------------------------------------------
-spec deep_get(config_key_path(), map()) -> term(). -spec deep_get(config_key_path(), map()) -> term().
deep_get(ConfKeyPath, Map) -> deep_get(ConfKeyPath, Map) ->
case deep_find(ConfKeyPath, Map) of Ref = make_ref(),
{not_found, KeyPath, Data} -> error({not_found, KeyPath, Data}); Res = deep_get(ConfKeyPath, Map, Ref),
{ok, Data} -> Data case Res =:= Ref of
true -> error({config_not_found, ConfKeyPath});
false -> Res
end. end.
-spec deep_get(config_key_path(), map(), term()) -> term(). -spec deep_get(config_key_path(), map(), term()) -> term().