emqx_hocon.erl 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. %% @doc HOCON schema help module
  17. -module(emqx_hocon).
  18. -export([
  19. format_path/1,
  20. check/2,
  21. format_error/1,
  22. format_error/2
  23. ]).
  24. %% @doc Format hocon config field path to dot-separated string in iolist format.
  25. -spec format_path([atom() | string() | binary()]) -> iolist().
  26. format_path([]) -> "";
  27. format_path([Name]) -> iol(Name);
  28. format_path([Name | Rest]) -> [iol(Name), "." | format_path(Rest)].
  29. %% @doc Plain check the input config.
  30. %% The input can either be `richmap' or plain `map'.
  31. %% Always return plain map with atom keys.
  32. -spec check(module(), hocon:config() | iodata()) ->
  33. {ok, hocon:config()} | {error, any()}.
  34. check(SchemaModule, Conf) when is_map(Conf) ->
  35. %% TODO: remove required
  36. %% fields should state required or not in their schema
  37. Opts = #{atom_key => true, required => false},
  38. try
  39. {ok, hocon_tconf:check_plain(SchemaModule, Conf, Opts)}
  40. catch
  41. throw:Reason ->
  42. {error, Reason}
  43. end;
  44. check(SchemaModule, HoconText) ->
  45. case hocon:binary(HoconText, #{format => map}) of
  46. {ok, MapConfig} ->
  47. check(SchemaModule, MapConfig);
  48. {error, Reason} ->
  49. {error, Reason}
  50. end.
  51. %% @doc Check if the error error term is a hocon check error.
  52. %% Return {true, FirstError}, otherwise false.
  53. %% NOTE: Hocon tries to be comprehensive, so it returns all found errors
  54. -spec format_error(term()) -> {ok, binary()} | false.
  55. format_error(X) ->
  56. format_error(X, #{}).
  57. format_error({_Schema, [#{kind := K} = First | Rest] = All}, Opts) when
  58. K =:= validation_error orelse K =:= translation_error
  59. ->
  60. Update =
  61. case maps:get(no_stacktrace, Opts, false) of
  62. true ->
  63. fun no_stacktrace/1;
  64. false ->
  65. fun(X) -> X end
  66. end,
  67. case Rest of
  68. [] ->
  69. {ok, emqx_logger_jsonfmt:best_effort_json(Update(First), [])};
  70. _ ->
  71. {ok, emqx_logger_jsonfmt:best_effort_json(lists:map(Update, All), [])}
  72. end;
  73. format_error(_Other, _) ->
  74. false.
  75. %% Ensure iolist()
  76. iol(B) when is_binary(B) -> B;
  77. iol(A) when is_atom(A) -> atom_to_binary(A, utf8);
  78. iol(L) when is_list(L) -> L.
  79. no_stacktrace(Map) ->
  80. maps:without([stacktrace], Map).