Просмотр исходного кода

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
Zaiming (Stone) Shi 2 лет назад
Родитель
Сommit
9b6ed09513
1 измененных файлов с 16 добавлено и 15 удалено
  1. 16 15
      apps/emqx/src/emqx_hocon.erl

+ 16 - 15
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.