emqx_modules_schema.erl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-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. -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, hoconsc:mk(boolean(), #{default => false, desc => "Enable telemetry."})}];
  36. fields("delayed") ->
  37. [
  38. {enable, hoconsc:mk(boolean(), #{default => false, desc => ?DESC(enable)})},
  39. {max_delayed_messages, sc(integer(), #{desc => ?DESC(max_delayed_messages)})}
  40. ];
  41. fields("rewrite") ->
  42. [
  43. {action,
  44. sc(
  45. hoconsc:enum([subscribe, publish, all]),
  46. #{required => true, desc => ?DESC(tr_action), example => publish}
  47. )},
  48. {source_topic,
  49. sc(
  50. binary(),
  51. #{required => true, desc => ?DESC(tr_source_topic), example => "x/#"}
  52. )},
  53. {dest_topic,
  54. sc(
  55. binary(),
  56. #{required => true, desc => ?DESC(tr_dest_topic), example => "z/y/$1"}
  57. )},
  58. {re, fun regular_expression/1}
  59. ];
  60. fields("topic_metrics") ->
  61. [{topic, sc(binary(), #{desc => "Collect metrics for the topic."})}].
  62. desc("telemetry") ->
  63. "Settings for the telemetry module.";
  64. desc("delayed") ->
  65. "Settings for the delayed module.";
  66. desc("rewrite") ->
  67. ?DESC(rewrite);
  68. desc("topic_metrics") ->
  69. "";
  70. desc(_) ->
  71. undefined.
  72. regular_expression(type) -> binary();
  73. regular_expression(required) -> true;
  74. regular_expression(desc) -> ?DESC(tr_re);
  75. regular_expression(example) -> "^x/y/(.+)$";
  76. regular_expression(validator) -> fun is_re/1;
  77. regular_expression(_) -> undefined.
  78. is_re(Bin) ->
  79. case re:compile(Bin) of
  80. {ok, _} -> ok;
  81. {error, Reason} -> {error, {Bin, Reason}}
  82. end.
  83. array(Name, Meta) -> {Name, hoconsc:mk(hoconsc:array(hoconsc:ref(?MODULE, Name)), Meta)}.
  84. sc(Type, Meta) -> hoconsc:mk(Type, Meta).