refactor: call hocon_tconf:check_plain/4 directly for example conf check

the attemp to re-use emqx_hocon:check failed because
hocon_tconf:check_plain/3 and check_plain/4 behaves slightly different
for unknown root fields.

To keep the old logic unchanged, for example config checks, we call
check_plain/4 directly
This commit is contained in:
Zaiming (Stone) Shi 2023-06-26 23:08:11 +02:00
parent 92ed7d7639
commit 9b6ed09513
1 changed files with 16 additions and 15 deletions

View File

@ -47,8 +47,7 @@ check(SchemaModule, Conf) ->
check(SchemaModule, Conf, Opts) when is_map(Conf) -> check(SchemaModule, Conf, Opts) when is_map(Conf) ->
try try
RootNames = maps:keys(Conf), {ok, hocon_tconf:check_plain(SchemaModule, Conf, Opts)}
{ok, hocon_tconf:check_plain(SchemaModule, Conf, Opts, RootNames)}
catch catch
throw:Errors:Stacktrace -> throw:Errors:Stacktrace ->
compact_errors(Errors, Stacktrace) compact_errors(Errors, Stacktrace)
@ -144,19 +143,21 @@ load_and_check(SchemaModule, File) ->
try try
do_load_and_check(SchemaModule, File) do_load_and_check(SchemaModule, File)
catch catch
throw:Reason -> throw:Reason:Stacktrace ->
{error, Reason} compact_errors(Reason, Stacktrace)
end. end.
do_load_and_check(SchemaModule, File) -> do_load_and_check(SchemaModule, File) ->
Conf = case hocon:load(File, #{format => map}) of
case hocon:load(File, #{format => map}) of {ok, Conf} ->
{ok, Conf0} -> Opts = #{atom_key => false, required => false},
Conf0; %% here we check only the provided root keys
{error, {parse_error, Reason}} -> %% because the examples are all provided only with one (or maybe two) roots
throw(Reason); %% and some roots have required fields.
{error, Reason} -> RootKeys = maps:keys(Conf),
throw(Reason) {ok, hocon_tconf:check_plain(SchemaModule, Conf, Opts, RootKeys)};
end, {error, {parse_error, Reason}} ->
Opts = #{atom_key => false, required => false}, {error, Reason};
check(SchemaModule, Conf, Opts). {error, Reason} ->
{error, Reason}
end.