emqx_modules_schema.erl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2021 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("typerefl/include/types.hrl").
  18. -behaviour(hocon_schema).
  19. -export([ namespace/0
  20. , roots/0
  21. , fields/1]).
  22. namespace() -> modules.
  23. roots() ->
  24. ["delayed",
  25. "telemetry",
  26. "event_message",
  27. array("rewrite"),
  28. array("topic_metrics")].
  29. fields("telemetry") ->
  30. [ {enable, hoconsc:mk(boolean(), #{default => false})}
  31. ];
  32. fields("delayed") ->
  33. [ {enable, hoconsc:mk(boolean(), #{default => false})}
  34. , {max_delayed_messages, sc(integer(), #{})}
  35. ];
  36. fields("rewrite") ->
  37. [ {action, sc(hoconsc:enum([subscribe, publish, all]), #{desc => "Action", example => publish})}
  38. , {source_topic, sc(binary(), #{desc => "Origin Topic", example => "x/#"})}
  39. , {dest_topic, sc(binary(), #{desc => "Destination Topic", example => "z/y/$1"})}
  40. , {re, fun regular_expression/1 }
  41. ];
  42. fields("event_message") ->
  43. [ {"$event/client_connected", sc(boolean(), #{default => false})}
  44. , {"$event/client_disconnected", sc(boolean(), #{default => false})}
  45. , {"$event/client_subscribed", sc(boolean(), #{default => false})}
  46. , {"$event/client_unsubscribed", sc(boolean(), #{default => false})}
  47. , {"$event/message_delivered", sc(boolean(), #{default => false})}
  48. , {"$event/message_acked", sc(boolean(), #{default => false})}
  49. , {"$event/message_dropped", sc(boolean(), #{default => false})}
  50. ];
  51. fields("topic_metrics") ->
  52. [{topic, sc(binary(), #{})}].
  53. regular_expression(type) -> binary();
  54. regular_expression(desc) -> "Regular expressions";
  55. regular_expression(example) -> "^x/y/(.+)$";
  56. regular_expression(validator) -> fun is_re/1;
  57. regular_expression(_) -> undefined.
  58. is_re(Bin) ->
  59. case re:compile(Bin) of
  60. {ok, _} -> ok;
  61. {error, Reason} -> {error, {Bin, Reason}}
  62. end.
  63. array(Name) -> {Name, hoconsc:array(hoconsc:ref(?MODULE, Name))}.
  64. sc(Type, Meta) -> hoconsc:mk(Type, Meta).