From 12b8297745f672e3090eb73b34356f729b58de61 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Wed, 1 Sep 2021 10:35:05 +0800 Subject: [PATCH] fix(config): emqx_config:fill_defaults/1,2 not working --- apps/emqx/src/emqx_config.erl | 23 ++----------- apps/emqx/test/emqx_config_SUITE.erl | 50 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 apps/emqx/test/emqx_config_SUITE.erl diff --git a/apps/emqx/src/emqx_config.erl b/apps/emqx/src/emqx_config.erl index ddedef024..317aba401 100644 --- a/apps/emqx/src/emqx_config.erl +++ b/apps/emqx/src/emqx_config.erl @@ -261,7 +261,7 @@ init_load(SchemaMod, RawRichConf) when is_map(RawRichConf) -> normalize_conf(hocon_schema:richmap_to_map(RawRichConf))). normalize_conf(Conf) -> - maps:with(get_root_names(bin), Conf). + maps:with(get_root_names(), Conf). -spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf} when AppEnvs :: app_envs(), CheckedConf :: config(). @@ -277,7 +277,7 @@ check_config(SchemaMod, RawConf) -> -spec fill_defaults(raw_config()) -> map(). fill_defaults(RawConf) -> - RootNames = get_root_names(bin), + RootNames = get_root_names(), maps:fold(fun(Key, Conf, Acc) -> SubMap = #{Key => Conf}, WithDefaults = case lists:member(Key, RootNames) of @@ -320,9 +320,6 @@ get_schema_mod(RootName) -> get_root_names() -> maps:get(names, persistent_term:get(?PERSIS_SCHEMA_MODS, #{names => []})). -get_root_names(bin) -> - maps:keys(get_schema_mod()). - -spec save_configs(app_envs(), config(), raw_config(), raw_config()) -> ok | {error, term()}. save_configs(_AppEnvs, Conf, RawConf, OverrideConf) -> %% We may need also support hot config update for the apps that use application envs. @@ -412,14 +409,7 @@ do_deep_put(?RAW_CONF, KeyPath, Map, Value) -> root_names_from_conf(RawConf) -> Keys = maps:keys(RawConf), - StrNames = [str(K) || K <- Keys], - AtomNames = lists:foldl(fun(K, Acc) -> - try [atom(K) | Acc] - catch error:badarg -> Acc - end - end, [], Keys), - PossibleNames = StrNames ++ AtomNames, - [Name || Name <- get_root_names(), lists:member(Name, PossibleNames)]. + [Name || Name <- get_root_names(), lists:member(Name, Keys)]. atom(Bin) when is_binary(Bin) -> binary_to_existing_atom(Bin, latin1); @@ -428,13 +418,6 @@ atom(Str) when is_list(Str) -> atom(Atom) when is_atom(Atom) -> Atom. -str(Bin) when is_binary(Bin) -> - binary_to_list(Bin); -str(Str) when is_list(Str) -> - Str; -str(Atom) when is_atom(Atom) -> - atom_to_list(Atom). - bin(Bin) when is_binary(Bin) -> Bin; bin(Str) when is_list(Str) -> list_to_binary(Str); bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8). diff --git a/apps/emqx/test/emqx_config_SUITE.erl b/apps/emqx/test/emqx_config_SUITE.erl new file mode 100644 index 000000000..50d575c0e --- /dev/null +++ b/apps/emqx/test/emqx_config_SUITE.erl @@ -0,0 +1,50 @@ +%%-------------------------------------------------------------------- +%% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. All Rights Reserved. +%% +%% Licensed under the Apache License, Version 2.0 (the "License"); +%% you may not use this file except in compliance with the License. +%% You may obtain a copy of the License at +%% +%% http://www.apache.org/licenses/LICENSE-2.0 +%% +%% Unless required by applicable law or agreed to in writing, software +%% distributed under the License is distributed on an "AS IS" BASIS, +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +%% See the License for the specific language governing permissions and +%% limitations under the License. +%%-------------------------------------------------------------------- + +-module(emqx_config_SUITE). + +-compile(export_all). +-compile(nowarn_export_all). +-include_lib("eunit/include/eunit.hrl"). + +all() -> emqx_ct:all(?MODULE). + +init_per_suite(Config) -> + emqx_ct_helpers:boot_modules(all), + emqx_ct_helpers:start_apps([]), + Config. + +end_per_suite(_Config) -> + emqx_ct_helpers:stop_apps([]). + +t_fill_default_values(_) -> + Conf = #{ + <<"broker">> => #{ + <<"perf">> => #{}, + <<"route_batch_clean">> => false} + }, + ?assertMatch(#{<<"broker">> := + #{<<"enable_session_registry">> := true, + <<"perf">> := + #{<<"route_lock_type">> := key, + <<"trie_compaction">> := true}, + <<"route_batch_clean">> := false, + <<"session_locking_strategy">> := quorum, + <<"shared_dispatch_ack_enabled">> := false, + <<"shared_subscription_strategy">> := round_robin, + <<"sys_heartbeat_interval">> := "30s", + <<"sys_msg_interval">> := "1m"}}, + emqx_config:fill_defaults(Conf)).