refactor(emqx_config): use dynamic callback to upgrade raw config

This commit is contained in:
Zaiming (Stone) Shi 2023-10-30 10:38:09 +01:00 committed by Ivan Dyachkov
parent 9dc3a169b3
commit 0656b6be3c
2 changed files with 19 additions and 9 deletions

View File

@ -325,22 +325,32 @@ init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) ->
ok = save_schema_mod_and_names(SchemaMod), ok = save_schema_mod_and_names(SchemaMod),
HasDeprecatedFile = has_deprecated_file(), HasDeprecatedFile = has_deprecated_file(),
RawConf0 = load_config_files(HasDeprecatedFile, Conf), RawConf0 = load_config_files(HasDeprecatedFile, Conf),
warning_deprecated_root_key(RawConf0), RawConf1 = upgrade_raw_conf(SchemaMod, RawConf0),
RawConf1 = warning_deprecated_root_key(RawConf1),
RawConf2 =
case HasDeprecatedFile of case HasDeprecatedFile of
true -> true ->
overlay_v0(SchemaMod, RawConf0); overlay_v0(SchemaMod, RawConf1);
false -> false ->
overlay_v1(SchemaMod, RawConf0) overlay_v1(SchemaMod, RawConf1)
end, end,
RawConf = fill_defaults_for_all_roots(SchemaMod, RawConf1), RawConf3 = fill_defaults_for_all_roots(SchemaMod, RawConf2),
%% check configs against the schema %% check configs against the schema
{AppEnvs, CheckedConf} = check_config(SchemaMod, RawConf, #{}), {AppEnvs, CheckedConf} = check_config(SchemaMod, RawConf3, #{}),
save_to_app_env(AppEnvs), save_to_app_env(AppEnvs),
ok = save_to_config_map(CheckedConf, RawConf), ok = save_to_config_map(CheckedConf, RawConf3),
maybe_init_default_zone(), maybe_init_default_zone(),
ok. ok.
upgrade_raw_conf(SchemaMod, RawConf) ->
case erlang:function_exported(SchemaMod, upgrade_raw_conf, 1) of
true ->
%% TODO make it a schema module behaviour in hocon_schema
apply(SchemaMod, upgrade_raw_conf, [RawConf]);
false ->
RawConf
end.
%% Merge environment variable overrides on top, then merge with overrides. %% Merge environment variable overrides on top, then merge with overrides.
overlay_v0(SchemaMod, RawConf) when is_map(RawConf) -> overlay_v0(SchemaMod, RawConf) when is_map(RawConf) ->
RawConfWithEnvs = merge_envs(SchemaMod, RawConf), RawConfWithEnvs = merge_envs(SchemaMod, RawConf),

View File

@ -19,7 +19,7 @@
-include("logger.hrl"). -include("logger.hrl").
-include("emqx_schema.hrl"). -include("emqx_schema.hrl").
-include_lib("hocon/include/hoconsc.hrl"). -include_lib("hocon/include/hocon_types.hrl").
-behaviour(gen_server). -behaviour(gen_server).
@ -736,7 +736,7 @@ remove_empty_leaf(KeyPath, Handlers) ->
end. end.
assert_callback_function(Mod) -> assert_callback_function(Mod) ->
_ = Mod:module_info(), _ = apply(Mod, module_info, []),
case case
erlang:function_exported(Mod, pre_config_update, 3) orelse erlang:function_exported(Mod, pre_config_update, 3) orelse
erlang:function_exported(Mod, post_config_update, 5) erlang:function_exported(Mod, post_config_update, 5)