emqx_modules_schema.erl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 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. array("rewrite", #{
  31. desc => "List of topic rewrite rules.",
  32. importance => ?IMPORTANCE_HIDDEN,
  33. validator => fun rewrite_validator/1,
  34. default => []
  35. }),
  36. array("topic_metrics", #{
  37. desc => "List of topics whose metrics are reported.",
  38. importance => ?IMPORTANCE_HIDDEN,
  39. default => []
  40. })
  41. ].
  42. rewrite_validator(Rules) ->
  43. case
  44. lists:foldl(
  45. fun
  46. (#{<<"action">> := subscribe}, Acc) ->
  47. Acc;
  48. (#{<<"dest_topic">> := DestTopic}, InvalidAcc) ->
  49. try
  50. true = emqx_topic:validate(name, DestTopic),
  51. InvalidAcc
  52. catch
  53. _:_ ->
  54. [DestTopic | InvalidAcc]
  55. end
  56. end,
  57. [],
  58. Rules
  59. )
  60. of
  61. [] ->
  62. ok;
  63. InvalidTopics ->
  64. {
  65. error,
  66. #{
  67. msg => "cannot_use_wildcard_for_destination_topic",
  68. invalid_topics => InvalidTopics
  69. }
  70. }
  71. end.
  72. fields("delayed") ->
  73. [
  74. {enable,
  75. ?HOCON(boolean(), #{
  76. default => true,
  77. importance => ?IMPORTANCE_NO_DOC,
  78. desc => ?DESC(enable)
  79. })},
  80. {max_delayed_messages,
  81. ?HOCON(integer(), #{desc => ?DESC(max_delayed_messages), default => 0})}
  82. ];
  83. fields("rewrite") ->
  84. [
  85. {action,
  86. ?HOCON(
  87. hoconsc:enum([subscribe, publish, all]),
  88. #{required => true, desc => ?DESC(tr_action), example => publish}
  89. )},
  90. {source_topic,
  91. ?HOCON(
  92. binary(),
  93. #{required => true, desc => ?DESC(tr_source_topic), example => "x/#"}
  94. )},
  95. {dest_topic,
  96. ?HOCON(
  97. binary(),
  98. #{required => true, desc => ?DESC(tr_dest_topic), example => "z/y/$1"}
  99. )},
  100. {re, fun regular_expression/1}
  101. ];
  102. fields("topic_metrics") ->
  103. [{topic, ?HOCON(binary(), #{desc => "Collect metrics for the topic."})}].
  104. desc("delayed") ->
  105. "Settings for the delayed module.";
  106. desc("rewrite") ->
  107. ?DESC(rewrite);
  108. desc("topic_metrics") ->
  109. "";
  110. desc(_) ->
  111. undefined.
  112. regular_expression(type) -> binary();
  113. regular_expression(required) -> true;
  114. regular_expression(desc) -> ?DESC(tr_re);
  115. regular_expression(example) -> "^x/y/(.+)$";
  116. regular_expression(validator) -> fun is_re/1;
  117. regular_expression(_) -> undefined.
  118. is_re(Bin) ->
  119. case re:compile(Bin) of
  120. {ok, _} ->
  121. ok;
  122. {error, Reason} ->
  123. {error, #{
  124. regexp => Bin,
  125. compile_error => Reason
  126. }}
  127. end.
  128. array(Name, Meta) -> {Name, ?HOCON(?ARRAY(?R_REF(Name)), Meta)}.