emqx_modules_schema.erl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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", #{
  32. desc => "List of topic rewrite rules.",
  33. importance => ?IMPORTANCE_HIDDEN
  34. }),
  35. array("topic_metrics", #{
  36. desc => "List of topics whose metrics are reported.",
  37. importance => ?IMPORTANCE_HIDDEN
  38. })
  39. ].
  40. fields("telemetry") ->
  41. [{enable, ?HOCON(boolean(), #{default => true, desc => "Enable telemetry."})}];
  42. fields("delayed") ->
  43. [
  44. {enable, ?HOCON(boolean(), #{default => true, desc => ?DESC(enable)})},
  45. {max_delayed_messages,
  46. ?HOCON(integer(), #{desc => ?DESC(max_delayed_messages), default => 0})}
  47. ];
  48. fields("rewrite") ->
  49. [
  50. {action,
  51. ?HOCON(
  52. hoconsc:enum([subscribe, publish, all]),
  53. #{required => true, desc => ?DESC(tr_action), example => publish}
  54. )},
  55. {source_topic,
  56. ?HOCON(
  57. binary(),
  58. #{required => true, desc => ?DESC(tr_source_topic), example => "x/#"}
  59. )},
  60. {dest_topic,
  61. ?HOCON(
  62. binary(),
  63. #{required => true, desc => ?DESC(tr_dest_topic), example => "z/y/$1"}
  64. )},
  65. {re, fun regular_expression/1}
  66. ];
  67. fields("topic_metrics") ->
  68. [{topic, ?HOCON(binary(), #{desc => "Collect metrics for the topic."})}].
  69. desc("telemetry") ->
  70. "Settings for the telemetry module.";
  71. desc("delayed") ->
  72. "Settings for the delayed module.";
  73. desc("rewrite") ->
  74. ?DESC(rewrite);
  75. desc("topic_metrics") ->
  76. "";
  77. desc(_) ->
  78. undefined.
  79. regular_expression(type) -> binary();
  80. regular_expression(required) -> true;
  81. regular_expression(desc) -> ?DESC(tr_re);
  82. regular_expression(example) -> "^x/y/(.+)$";
  83. regular_expression(validator) -> fun is_re/1;
  84. regular_expression(_) -> undefined.
  85. is_re(Bin) ->
  86. case re:compile(Bin) of
  87. {ok, _} ->
  88. ok;
  89. {error, Reason} ->
  90. {error, #{
  91. regexp => Bin,
  92. compile_error => Reason
  93. }}
  94. end.
  95. array(Name, Meta) -> {Name, ?HOCON(?ARRAY(?R_REF(Name)), Meta)}.