emqx_modules_schema.erl 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, ?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, _} -> ok;
  82. {error, Reason} -> {error, {Bin, Reason}}
  83. end.
  84. array(Name, Meta) -> {Name, ?HOCON(?ARRAY(?R_REF(Name)), Meta)}.