From 9b6ed09513795dae08515dbcf27b3e6be0a038cf Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Mon, 26 Jun 2023 23:08:11 +0200 Subject: [PATCH] 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 --- apps/emqx/src/emqx_hocon.erl | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/apps/emqx/src/emqx_hocon.erl b/apps/emqx/src/emqx_hocon.erl index 62573f201..4423ad29d 100644 --- a/apps/emqx/src/emqx_hocon.erl +++ b/apps/emqx/src/emqx_hocon.erl @@ -47,8 +47,7 @@ check(SchemaModule, Conf) -> check(SchemaModule, Conf, Opts) when is_map(Conf) -> try - RootNames = maps:keys(Conf), - {ok, hocon_tconf:check_plain(SchemaModule, Conf, Opts, RootNames)} + {ok, hocon_tconf:check_plain(SchemaModule, Conf, Opts)} catch throw:Errors:Stacktrace -> compact_errors(Errors, Stacktrace) @@ -144,19 +143,21 @@ load_and_check(SchemaModule, File) -> try do_load_and_check(SchemaModule, File) catch - throw:Reason -> - {error, Reason} + throw:Reason:Stacktrace -> + compact_errors(Reason, Stacktrace) end. do_load_and_check(SchemaModule, File) -> - Conf = - case hocon:load(File, #{format => map}) of - {ok, Conf0} -> - Conf0; - {error, {parse_error, Reason}} -> - throw(Reason); - {error, Reason} -> - throw(Reason) - end, - Opts = #{atom_key => false, required => false}, - check(SchemaModule, Conf, Opts). + case hocon:load(File, #{format => map}) of + {ok, Conf} -> + Opts = #{atom_key => false, required => false}, + %% here we check only the provided root keys + %% because the examples are all provided only with one (or maybe two) roots + %% and some roots have required fields. + RootKeys = maps:keys(Conf), + {ok, hocon_tconf:check_plain(SchemaModule, Conf, Opts, RootKeys)}; + {error, {parse_error, Reason}} -> + {error, Reason}; + {error, Reason} -> + {error, Reason} + end.