emqx_modules_schema.erl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2023 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. -module(emqx_modules_schema).
  17. -include_lib("hocon/include/hoconsc.hrl").
  18. -include_lib("typerefl/include/types.hrl").
  19. -behaviour(hocon_schema).
  20. -export([
  21. namespace/0,
  22. roots/0,
  23. fields/1,
  24. desc/1
  25. ]).
  26. namespace() -> modules.
  27. roots() ->
  28. [
  29. "delayed",
  30. "telemetry",
  31. array("rewrite", #{desc => "List of topic rewrite rules."}),
  32. array("topic_metrics", #{desc => "List of topics whose metrics are reported."})
  33. ].
  34. fields("telemetry") ->
  35. [{enable, ?HOCON(boolean(), #{default => true, desc => "Enable telemetry."})}];
  36. fields("delayed") ->
  37. [
  38. {enable, ?HOCON(boolean(), #{default => true, desc => ?DESC(enable)})},
  39. {max_delayed_messages,
  40. ?HOCON(integer(), #{desc => ?DESC(max_delayed_messages), default => 0})}
  41. ];
  42. fields("rewrite") ->
  43. [
  44. {action,
  45. ?HOCON(
  46. hoconsc:enum([subscribe, publish, all]),
  47. #{required => true, desc => ?DESC(tr_action), example => publish}
  48. )},
  49. {source_topic,
  50. ?HOCON(
  51. binary(),
  52. #{required => true, desc => ?DESC(tr_source_topic), example => "x/#"}
  53. )},
  54. {dest_topic,
  55. ?HOCON(
  56. binary(),
  57. #{required => true, desc => ?DESC(tr_dest_topic), example => "z/y/$1"}
  58. )},
  59. {re, fun regular_expression/1}
  60. ];
  61. fields("topic_metrics") ->
  62. [{topic, ?HOCON(binary(), #{desc => "Collect metrics for the topic."})}].
  63. desc("telemetry") ->
  64. "Settings for the telemetry module.";
  65. desc("delayed") ->
  66. "Settings for the delayed module.";
  67. desc("rewrite") ->
  68. ?DESC(rewrite);
  69. desc("topic_metrics") ->
  70. "";
  71. desc(_) ->
  72. undefined.
  73. regular_expression(type) -> binary();
  74. regular_expression(required) -> true;
  75. regular_expression(desc) -> ?DESC(tr_re);
  76. regular_expression(example) -> "^x/y/(.+)$";
  77. regular_expression(validator) -> fun is_re/1;
  78. regular_expression(_) -> undefined.
  79. is_re(Bin) ->
  80. case re:compile(Bin) of
  81. {ok, _} ->
  82. ok;
  83. {error, Reason} ->
  84. {error, #{
  85. regexp => Bin,
  86. compile_error => Reason
  87. }}
  88. end.
  89. array(Name, Meta) -> {Name, ?HOCON(?ARRAY(?R_REF(Name)), Meta)}.