diff --git a/apps/emqx/src/emqx_config.erl b/apps/emqx/src/emqx_config.erl index eaea5050b..bd63a06b9 100644 --- a/apps/emqx/src/emqx_config.erl +++ b/apps/emqx/src/emqx_config.erl @@ -73,12 +73,12 @@ %% @doc For the given path, get root value enclosed in a single-key map. -spec get_root(emqx_map_lib:config_key_path()) -> map(). 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. %% key is ensured to be binary. 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. %% The path should at least include root config name. @@ -233,15 +233,21 @@ to_plainmap(RichMap) -> bin(Bin) when is_binary(Bin) -> Bin; bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8). -do_get(PtKey, [RootName | KeyPath]) -> - RootV = persistent_term:get(PtKey(RootName), #{}), - emqx_map_lib:deep_get(KeyPath, RootV). +do_get(PtKey, KeyPath) -> + Ref = make_ref(), + 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) -> RootV = persistent_term:get(PtKey(RootName), #{}), emqx_map_lib:deep_get(KeyPath, RootV, Default). 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), persistent_term:put(PtKey(RootName), NewValue). diff --git a/apps/emqx/src/emqx_map_lib.erl b/apps/emqx/src/emqx_map_lib.erl index 03ea620f4..d720e771e 100644 --- a/apps/emqx/src/emqx_map_lib.erl +++ b/apps/emqx/src/emqx_map_lib.erl @@ -32,9 +32,11 @@ %%----------------------------------------------------------------- -spec deep_get(config_key_path(), map()) -> term(). deep_get(ConfKeyPath, Map) -> - case deep_find(ConfKeyPath, Map) of - {not_found, KeyPath, Data} -> error({not_found, KeyPath, Data}); - {ok, Data} -> Data + Ref = make_ref(), + Res = deep_get(ConfKeyPath, Map, Ref), + case Res =:= Ref of + true -> error({config_not_found, ConfKeyPath}); + false -> Res end. -spec deep_get(config_key_path(), map(), term()) -> term().