refactor: call new hocon apis
This commit is contained in:
parent
8c114db168
commit
252d7e85d9
|
@ -155,7 +155,7 @@ do_check_config(Type, Config, Module) ->
|
||||||
fun(C) ->
|
fun(C) ->
|
||||||
Key = list_to_binary(?EMQX_AUTHENTICATION_CONFIG_ROOT_NAME),
|
Key = list_to_binary(?EMQX_AUTHENTICATION_CONFIG_ROOT_NAME),
|
||||||
AtomKey = list_to_atom(?EMQX_AUTHENTICATION_CONFIG_ROOT_NAME),
|
AtomKey = list_to_atom(?EMQX_AUTHENTICATION_CONFIG_ROOT_NAME),
|
||||||
R = hocon_schema:check_plain(Module, #{Key => C},
|
R = hocon_tconf:check_plain(Module, #{Key => C},
|
||||||
#{atom_key => true}),
|
#{atom_key => true}),
|
||||||
maps:get(AtomKey, R)
|
maps:get(AtomKey, R)
|
||||||
end
|
end
|
||||||
|
|
|
@ -236,7 +236,7 @@ get_raw(KeyPath, Default) -> do_get(?RAW_CONF, KeyPath, Default).
|
||||||
put_raw(Config) ->
|
put_raw(Config) ->
|
||||||
maps:fold(fun(RootName, RootV, _) ->
|
maps:fold(fun(RootName, RootV, _) ->
|
||||||
?MODULE:put_raw([RootName], RootV)
|
?MODULE:put_raw([RootName], RootV)
|
||||||
end, ok, hocon_schema:get_value([], Config)).
|
end, ok, hocon_maps:ensure_plain(Config)).
|
||||||
|
|
||||||
-spec put_raw(emqx_map_lib:config_key_path(), term()) -> ok.
|
-spec put_raw(emqx_map_lib:config_key_path(), term()) -> ok.
|
||||||
put_raw(KeyPath, Config) -> do_put(?RAW_CONF, KeyPath, Config).
|
put_raw(KeyPath, Config) -> do_put(?RAW_CONF, KeyPath, Config).
|
||||||
|
@ -299,7 +299,7 @@ merge_envs(SchemaMod, RawConf) ->
|
||||||
format => map,
|
format => map,
|
||||||
apply_override_envs => true
|
apply_override_envs => true
|
||||||
},
|
},
|
||||||
hocon_schema:merge_env_overrides(SchemaMod, RawConf, all, Opts).
|
hocon_tconf:merge_env_overrides(SchemaMod, RawConf, all, Opts).
|
||||||
|
|
||||||
-spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf}
|
-spec check_config(module(), raw_config()) -> {AppEnvs, CheckedConf}
|
||||||
when AppEnvs :: app_envs(), CheckedConf :: config().
|
when AppEnvs :: app_envs(), CheckedConf :: config().
|
||||||
|
@ -313,7 +313,7 @@ check_config(SchemaMod, RawConf, Opts0) ->
|
||||||
},
|
},
|
||||||
Opts = maps:merge(Opts0, Opts1),
|
Opts = maps:merge(Opts0, Opts1),
|
||||||
{AppEnvs, CheckedConf} =
|
{AppEnvs, CheckedConf} =
|
||||||
hocon_schema:map_translate(SchemaMod, RawConf, Opts),
|
hocon_tconf:map_translate(SchemaMod, RawConf, Opts),
|
||||||
{AppEnvs, emqx_map_lib:unsafe_atom_key_map(CheckedConf)}.
|
{AppEnvs, emqx_map_lib:unsafe_atom_key_map(CheckedConf)}.
|
||||||
|
|
||||||
-spec fill_defaults(raw_config()) -> map().
|
-spec fill_defaults(raw_config()) -> map().
|
||||||
|
@ -330,7 +330,7 @@ fill_defaults(RawConf) ->
|
||||||
|
|
||||||
-spec fill_defaults(module(), raw_config()) -> map().
|
-spec fill_defaults(module(), raw_config()) -> map().
|
||||||
fill_defaults(SchemaMod, RawConf) ->
|
fill_defaults(SchemaMod, RawConf) ->
|
||||||
hocon_schema:check_plain(SchemaMod, RawConf,
|
hocon_tconf:check_plain(SchemaMod, RawConf,
|
||||||
#{nullable => true, only_fill_defaults => true},
|
#{nullable => true, only_fill_defaults => true},
|
||||||
root_names_from_conf(RawConf)).
|
root_names_from_conf(RawConf)).
|
||||||
|
|
||||||
|
|
|
@ -1107,7 +1107,7 @@ base_listener() ->
|
||||||
%% utils
|
%% utils
|
||||||
-spec(conf_get(string() | [string()], hocon:config()) -> term()).
|
-spec(conf_get(string() | [string()], hocon:config()) -> term()).
|
||||||
conf_get(Key, Conf) ->
|
conf_get(Key, Conf) ->
|
||||||
V = hocon_schema:get_value(Key, Conf),
|
V = hocon_maps:get(Key, Conf),
|
||||||
case is_binary(V) of
|
case is_binary(V) of
|
||||||
true ->
|
true ->
|
||||||
binary_to_list(V);
|
binary_to_list(V);
|
||||||
|
@ -1116,7 +1116,7 @@ conf_get(Key, Conf) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
conf_get(Key, Conf, Default) ->
|
conf_get(Key, Conf, Default) ->
|
||||||
V = hocon_schema:get_value(Key, Conf, Default),
|
V = hocon_maps:get(Key, Conf, Default),
|
||||||
case is_binary(V) of
|
case is_binary(V) of
|
||||||
true ->
|
true ->
|
||||||
binary_to_list(V);
|
binary_to_list(V);
|
||||||
|
|
|
@ -59,7 +59,7 @@ enable(_) -> undefined.
|
||||||
|
|
||||||
check_config(C) ->
|
check_config(C) ->
|
||||||
#{config := R} =
|
#{config := R} =
|
||||||
hocon_schema:check_plain(?MODULE, #{<<"config">> => C},
|
hocon_tconf:check_plain(?MODULE, #{<<"config">> => C},
|
||||||
#{atom_key => true}),
|
#{atom_key => true}),
|
||||||
R.
|
R.
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ validate(Schema, Data0) ->
|
||||||
, keyfile => <<"keyfile">>
|
, keyfile => <<"keyfile">>
|
||||||
},
|
},
|
||||||
#{ssl_opts := Checked} =
|
#{ssl_opts := Checked} =
|
||||||
hocon_schema:check_plain(Sc, #{<<"ssl_opts">> => Data},
|
hocon_tconf:check_plain(Sc, #{<<"ssl_opts">> => Data},
|
||||||
#{atom_key => true}),
|
#{atom_key => true}),
|
||||||
Checked.
|
Checked.
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ ciperhs_schema_test() ->
|
||||||
Sc = emqx_schema:ciphers_schema(undefined),
|
Sc = emqx_schema:ciphers_schema(undefined),
|
||||||
WSc = #{roots => [{ciphers, Sc}]},
|
WSc = #{roots => [{ciphers, Sc}]},
|
||||||
?assertThrow({_, [{validation_error, _}]},
|
?assertThrow({_, [{validation_error, _}]},
|
||||||
hocon_schema:check_plain(WSc, #{<<"ciphers">> => <<"foo,bar">>})).
|
hocon_tconf:check_plain(WSc, #{<<"ciphers">> => <<"foo,bar">>})).
|
||||||
|
|
||||||
bad_tls_version_test() ->
|
bad_tls_version_test() ->
|
||||||
Sc = emqx_schema:server_ssl_opts_schema(#{}, false),
|
Sc = emqx_schema:server_ssl_opts_schema(#{}, false),
|
||||||
|
|
|
@ -59,7 +59,7 @@ do_check_config(#{<<"mechanism">> := Mec} = Config, Opts) ->
|
||||||
false ->
|
false ->
|
||||||
throw({unknown_handler, Key});
|
throw({unknown_handler, Key});
|
||||||
{_, ProviderModule} ->
|
{_, ProviderModule} ->
|
||||||
hocon_schema:check_plain(ProviderModule, #{?CONF_NS_BINARY => Config},
|
hocon_tconf:check_plain(ProviderModule, #{?CONF_NS_BINARY => Config},
|
||||||
Opts#{atom_key => true})
|
Opts#{atom_key => true})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
|
@ -332,4 +332,4 @@ to_bin(L) when is_list(L) ->
|
||||||
list_to_binary(L).
|
list_to_binary(L).
|
||||||
|
|
||||||
get_conf_val(Name, Conf) ->
|
get_conf_val(Name, Conf) ->
|
||||||
hocon_schema:get_value(?CONF_NS ++ "." ++ Name, Conf).
|
hocon_maps:get(?CONF_NS ++ "." ++ Name, Conf).
|
||||||
|
|
|
@ -62,7 +62,7 @@ t_check_schema(_Config) ->
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
hocon_schema:check_plain(emqx_authn_mnesia, ?CONF(ConfigOk)),
|
hocon_tconf:check_plain(emqx_authn_mnesia, ?CONF(ConfigOk)),
|
||||||
|
|
||||||
ConfigNotOk = #{
|
ConfigNotOk = #{
|
||||||
<<"mechanism">> => <<"password-based">>,
|
<<"mechanism">> => <<"password-based">>,
|
||||||
|
@ -76,7 +76,7 @@ t_check_schema(_Config) ->
|
||||||
?assertException(
|
?assertException(
|
||||||
throw,
|
throw,
|
||||||
{emqx_authn_mnesia, _},
|
{emqx_authn_mnesia, _},
|
||||||
hocon_schema:check_plain(emqx_authn_mnesia, ?CONF(ConfigNotOk))).
|
hocon_tconf:check_plain(emqx_authn_mnesia, ?CONF(ConfigNotOk))).
|
||||||
|
|
||||||
t_create(_) ->
|
t_create(_) ->
|
||||||
Config0 = config(),
|
Config0 = config(),
|
||||||
|
|
|
@ -320,7 +320,7 @@ do_authorize(Client, PubSub, Topic,
|
||||||
check_sources(RawSources) ->
|
check_sources(RawSources) ->
|
||||||
Schema = #{roots => emqx_authz_schema:fields("authorization"), fields => #{}},
|
Schema = #{roots => emqx_authz_schema:fields("authorization"), fields => #{}},
|
||||||
Conf = #{<<"sources">> => RawSources},
|
Conf = #{<<"sources">> => RawSources},
|
||||||
#{sources := Sources} = hocon_schema:check_plain(Schema, Conf, #{atom_key => true}),
|
#{sources := Sources} = hocon_tconf:check_plain(Schema, Conf, #{atom_key => true}),
|
||||||
Sources.
|
Sources.
|
||||||
|
|
||||||
take(Type) -> take(Type, lookup()).
|
take(Type) -> take(Type, lookup()).
|
||||||
|
|
|
@ -415,7 +415,7 @@ get_raw_sources() ->
|
||||||
Schema = #{roots => emqx_authz_schema:fields("authorization"), fields => #{}},
|
Schema = #{roots => emqx_authz_schema:fields("authorization"), fields => #{}},
|
||||||
Conf = #{<<"sources">> => RawSources},
|
Conf = #{<<"sources">> => RawSources},
|
||||||
Options = #{only_fill_defaults => true},
|
Options = #{only_fill_defaults => true},
|
||||||
#{<<"sources">> := Sources} = hocon_schema:check_plain(Schema, Conf, Options),
|
#{<<"sources">> := Sources} = hocon_tconf:check_plain(Schema, Conf, Options),
|
||||||
merge_default_headers(Sources).
|
merge_default_headers(Sources).
|
||||||
|
|
||||||
merge_default_headers(Sources) ->
|
merge_default_headers(Sources) ->
|
||||||
|
|
|
@ -211,9 +211,9 @@ check_ssl_opts(Conf)
|
||||||
when Conf =:= #{} ->
|
when Conf =:= #{} ->
|
||||||
true;
|
true;
|
||||||
check_ssl_opts(Conf) ->
|
check_ssl_opts(Conf) ->
|
||||||
case emqx_authz_http:parse_url(hocon_schema:get_value("config.url", Conf)) of
|
case emqx_authz_http:parse_url(hocon_maps:get("config.url", Conf)) of
|
||||||
#{scheme := https} ->
|
#{scheme := https} ->
|
||||||
case hocon_schema:get_value("config.ssl.enable", Conf) of
|
case hocon_maps:get("config.ssl.enable", Conf) of
|
||||||
true -> ok;
|
true -> ok;
|
||||||
false -> false
|
false -> false
|
||||||
end;
|
end;
|
||||||
|
@ -225,8 +225,8 @@ check_headers(Conf)
|
||||||
when Conf =:= #{} ->
|
when Conf =:= #{} ->
|
||||||
true;
|
true;
|
||||||
check_headers(Conf) ->
|
check_headers(Conf) ->
|
||||||
Method = to_bin(hocon_schema:get_value("config.method", Conf)),
|
Method = to_bin(hocon_maps:get("config.method", Conf)),
|
||||||
Headers = hocon_schema:get_value("config.headers", Conf),
|
Headers = hocon_maps:get("config.headers", Conf),
|
||||||
Method =:= <<"post">> orelse (not lists:member(<<"content-type">>, Headers)).
|
Method =:= <<"post">> orelse (not lists:member(<<"content-type">>, Headers)).
|
||||||
|
|
||||||
union_array(Item) when is_list(Item) ->
|
union_array(Item) when is_list(Item) ->
|
||||||
|
|
|
@ -757,10 +757,10 @@ sort_log_levels(Levels) ->
|
||||||
%% utils
|
%% utils
|
||||||
-spec(conf_get(string() | [string()], hocon:config()) -> term()).
|
-spec(conf_get(string() | [string()], hocon:config()) -> term()).
|
||||||
conf_get(Key, Conf) ->
|
conf_get(Key, Conf) ->
|
||||||
ensure_list(hocon_schema:get_value(Key, Conf)).
|
ensure_list(hocon_maps:get(Key, Conf)).
|
||||||
|
|
||||||
conf_get(Key, Conf, Default) ->
|
conf_get(Key, Conf, Default) ->
|
||||||
ensure_list(hocon_schema:get_value(Key, Conf, Default)).
|
ensure_list(hocon_maps:get(Key, Conf, Default)).
|
||||||
|
|
||||||
filter(Opts) ->
|
filter(Opts) ->
|
||||||
[{K, V} || {K, V} <- Opts, V =/= undefined].
|
[{K, V} || {K, V} <- Opts, V =/= undefined].
|
||||||
|
|
|
@ -313,8 +313,8 @@ check_ssl_opts(Conf) ->
|
||||||
check_ssl_opts("base_url", Conf).
|
check_ssl_opts("base_url", Conf).
|
||||||
|
|
||||||
check_ssl_opts(URLFrom, Conf) ->
|
check_ssl_opts(URLFrom, Conf) ->
|
||||||
#{scheme := Scheme} = hocon_schema:get_value(URLFrom, Conf),
|
#{scheme := Scheme} = hocon_maps:get(URLFrom, Conf),
|
||||||
SSL= hocon_schema:get_value("ssl", Conf),
|
SSL= hocon_maps:get("ssl", Conf),
|
||||||
case {Scheme, maps:get(enable, SSL, false)} of
|
case {Scheme, maps:get(enable, SSL, false)} of
|
||||||
{http, false} -> true;
|
{http, false} -> true;
|
||||||
{https, true} -> true;
|
{https, true} -> true;
|
||||||
|
|
|
@ -147,10 +147,10 @@ translate_req(Request, #{module := Module, path := Path, method := Method}, Chec
|
||||||
end.
|
end.
|
||||||
|
|
||||||
check_and_translate(Schema, Map, Opts) ->
|
check_and_translate(Schema, Map, Opts) ->
|
||||||
hocon_schema:check_plain(Schema, Map, Opts).
|
hocon_tconf:check_plain(Schema, Map, Opts).
|
||||||
|
|
||||||
check_only(Schema, Map, Opts) ->
|
check_only(Schema, Map, Opts) ->
|
||||||
_ = hocon_schema:check_plain(Schema, Map, Opts),
|
_ = hocon_tconf:check_plain(Schema, Map, Opts),
|
||||||
Map.
|
Map.
|
||||||
|
|
||||||
support_check_schema(#{check_schema := true, translate_body := true}) ->
|
support_check_schema(#{check_schema := true, translate_body := true}) ->
|
||||||
|
@ -199,12 +199,12 @@ check_parameter([{Name, Type} | Spec], Bindings, QueryStr, Module, BindingsAcc,
|
||||||
case hocon_schema:field_schema(Type, in) of
|
case hocon_schema:field_schema(Type, in) of
|
||||||
path ->
|
path ->
|
||||||
Option = #{atom_key => true},
|
Option = #{atom_key => true},
|
||||||
NewBindings = hocon_schema:check_plain(Schema, Bindings, Option),
|
NewBindings = hocon_tconf:check_plain(Schema, Bindings, Option),
|
||||||
NewBindingsAcc = maps:merge(BindingsAcc, NewBindings),
|
NewBindingsAcc = maps:merge(BindingsAcc, NewBindings),
|
||||||
check_parameter(Spec, Bindings, QueryStr, Module, NewBindingsAcc, QueryStrAcc);
|
check_parameter(Spec, Bindings, QueryStr, Module, NewBindingsAcc, QueryStrAcc);
|
||||||
query ->
|
query ->
|
||||||
Option = #{},
|
Option = #{},
|
||||||
NewQueryStr = hocon_schema:check_plain(Schema, QueryStr, Option),
|
NewQueryStr = hocon_tconf:check_plain(Schema, QueryStr, Option),
|
||||||
NewQueryStrAcc = maps:merge(QueryStrAcc, NewQueryStr),
|
NewQueryStrAcc = maps:merge(QueryStrAcc, NewQueryStr),
|
||||||
check_parameter(Spec, Bindings, QueryStr, Module,BindingsAcc, NewQueryStrAcc)
|
check_parameter(Spec, Bindings, QueryStr, Module,BindingsAcc, NewQueryStrAcc)
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -322,7 +322,7 @@ do_read_plugin({file, InfoFile}) ->
|
||||||
[_, NameVsn | _] = lists:reverse(filename:split(InfoFile)),
|
[_, NameVsn | _] = lists:reverse(filename:split(InfoFile)),
|
||||||
case hocon:load(InfoFile, #{format => richmap}) of
|
case hocon:load(InfoFile, #{format => richmap}) of
|
||||||
{ok, RichMap} ->
|
{ok, RichMap} ->
|
||||||
Info = check_plugin(hocon_util:richmap_to_map(RichMap), NameVsn, InfoFile),
|
Info = check_plugin(hocon_maps:ensure_plain(RichMap), NameVsn, InfoFile),
|
||||||
maps:merge(Info, plugin_status(NameVsn));
|
maps:merge(Info, plugin_status(NameVsn));
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
throw(#{error => "bad_info_file",
|
throw(#{error => "bad_info_file",
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-define(SAFE_CALL(_EXP_),
|
-define(SAFE_CALL(_EXP_),
|
||||||
?SAFE_CALL(_EXP_, _ = do_nothing)).
|
?SAFE_CALL(_EXP_, ok)).
|
||||||
|
|
||||||
-define(SAFE_CALL(_EXP_, _EXP_ON_FAIL_),
|
-define(SAFE_CALL(_EXP_, _EXP_ON_FAIL_),
|
||||||
fun() ->
|
fun() ->
|
||||||
|
|
|
@ -297,14 +297,14 @@ call_jsonify(Mod, Config) ->
|
||||||
check_config(ResourceType, RawConfig) when is_binary(RawConfig) ->
|
check_config(ResourceType, RawConfig) when is_binary(RawConfig) ->
|
||||||
case hocon:binary(RawConfig, #{format => richmap}) of
|
case hocon:binary(RawConfig, #{format => richmap}) of
|
||||||
{ok, MapConfig} ->
|
{ok, MapConfig} ->
|
||||||
case ?SAFE_CALL(hocon_schema:check(ResourceType, MapConfig, ?HOCON_CHECK_OPTS)) of
|
case ?SAFE_CALL(hocon_tconf:check(ResourceType, MapConfig, ?HOCON_CHECK_OPTS)) of
|
||||||
{error, Reason} -> {error, Reason};
|
{error, Reason} -> {error, Reason};
|
||||||
Config -> {ok, hocon_schema:richmap_to_map(Config)}
|
Config -> {ok, hocon_maps:ensure_plain(Config)}
|
||||||
end;
|
end;
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end;
|
end;
|
||||||
check_config(ResourceType, RawConfigTerm) ->
|
check_config(ResourceType, RawConfigTerm) ->
|
||||||
case ?SAFE_CALL(hocon_schema:check_plain(ResourceType, RawConfigTerm, ?HOCON_CHECK_OPTS)) of
|
case ?SAFE_CALL(hocon_tconf:check_plain(ResourceType, RawConfigTerm, ?HOCON_CHECK_OPTS)) of
|
||||||
{error, Reason} -> {error, Reason};
|
{error, Reason} -> {error, Reason};
|
||||||
Config -> {ok, Config}
|
Config -> {ok, Config}
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
-spec check_params(map(), tag()) -> {ok, map()} | {error, term()}.
|
-spec check_params(map(), tag()) -> {ok, map()} | {error, term()}.
|
||||||
check_params(Params, Tag) ->
|
check_params(Params, Tag) ->
|
||||||
BTag = atom_to_binary(Tag),
|
BTag = atom_to_binary(Tag),
|
||||||
try hocon_schema:check_plain(?MODULE, #{BTag => Params},
|
try hocon_tconf:check_plain(?MODULE, #{BTag => Params},
|
||||||
#{atom_key => true, nullable => true}, [BTag]) of
|
#{atom_key => true, nullable => true}, [BTag]) of
|
||||||
#{Tag := Checked} -> {ok, Checked}
|
#{Tag := Checked} -> {ok, Checked}
|
||||||
catch
|
catch
|
||||||
|
|
Loading…
Reference in New Issue