emqx_modules_schema.erl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. 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, ?HOCON(boolean(), #{default => true, desc => ?DESC(enable)})},
  75. {max_delayed_messages,
  76. ?HOCON(integer(), #{desc => ?DESC(max_delayed_messages), default => 0})}
  77. ];
  78. fields("rewrite") ->
  79. [
  80. {action,
  81. ?HOCON(
  82. hoconsc:enum([subscribe, publish, all]),
  83. #{required => true, desc => ?DESC(tr_action), example => publish}
  84. )},
  85. {source_topic,
  86. ?HOCON(
  87. binary(),
  88. #{required => true, desc => ?DESC(tr_source_topic), example => "x/#"}
  89. )},
  90. {dest_topic,
  91. ?HOCON(
  92. binary(),
  93. #{required => true, desc => ?DESC(tr_dest_topic), example => "z/y/$1"}
  94. )},
  95. {re, fun regular_expression/1}
  96. ];
  97. fields("topic_metrics") ->
  98. [{topic, ?HOCON(binary(), #{desc => "Collect metrics for the topic."})}].
  99. desc("delayed") ->
  100. "Settings for the delayed module.";
  101. desc("rewrite") ->
  102. ?DESC(rewrite);
  103. desc("topic_metrics") ->
  104. "";
  105. desc(_) ->
  106. undefined.
  107. regular_expression(type) -> binary();
  108. regular_expression(required) -> true;
  109. regular_expression(desc) -> ?DESC(tr_re);
  110. regular_expression(example) -> "^x/y/(.+)$";
  111. regular_expression(validator) -> fun is_re/1;
  112. regular_expression(_) -> undefined.
  113. is_re(Bin) ->
  114. case re:compile(Bin) of
  115. {ok, _} ->
  116. ok;
  117. {error, Reason} ->
  118. {error, #{
  119. regexp => Bin,
  120. compile_error => Reason
  121. }}
  122. end.
  123. array(Name, Meta) -> {Name, ?HOCON(?ARRAY(?R_REF(Name)), Meta)}.