| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- %%--------------------------------------------------------------------
- %% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
- %%
- %% Licensed under the Apache License, Version 2.0 (the "License");
- %% you may not use this file except in compliance with the License.
- %% You may obtain a copy of the License at
- %%
- %% http://www.apache.org/licenses/LICENSE-2.0
- %%
- %% Unless required by applicable law or agreed to in writing, software
- %% distributed under the License is distributed on an "AS IS" BASIS,
- %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- %% See the License for the specific language governing permissions and
- %% limitations under the License.
- %%--------------------------------------------------------------------
- -module(emqx_modules_schema).
- -include_lib("hocon/include/hoconsc.hrl").
- -include_lib("typerefl/include/types.hrl").
- -behaviour(hocon_schema).
- -export([
- namespace/0,
- roots/0,
- fields/1,
- desc/1
- ]).
- namespace() -> modules.
- roots() ->
- [
- "delayed",
- "telemetry",
- array("rewrite", #{desc => "List of topic rewrite rules."}),
- array("topic_metrics", #{desc => "List of topics whose metrics are reported."})
- ].
- fields("telemetry") ->
- [{enable, hoconsc:mk(boolean(), #{default => false, desc => "Enable telemetry."})}];
- fields("delayed") ->
- [
- {enable, hoconsc:mk(boolean(), #{default => false, desc => ?DESC(enable)})},
- {max_delayed_messages, sc(integer(), #{desc => ?DESC(max_delayed_messages)})}
- ];
- fields("rewrite") ->
- [
- {action,
- sc(
- hoconsc:enum([subscribe, publish, all]),
- #{required => true, desc => ?DESC(tr_action), example => publish}
- )},
- {source_topic,
- sc(
- binary(),
- #{required => true, desc => ?DESC(tr_source_topic), example => "x/#"}
- )},
- {dest_topic,
- sc(
- binary(),
- #{required => true, desc => ?DESC(tr_dest_topic), example => "z/y/$1"}
- )},
- {re, fun regular_expression/1}
- ];
- fields("topic_metrics") ->
- [{topic, sc(binary(), #{desc => "Collect metrics for the topic."})}].
- desc("telemetry") ->
- "Settings for the telemetry module.";
- desc("delayed") ->
- "Settings for the delayed module.";
- desc("rewrite") ->
- ?DESC(rewrite);
- desc("topic_metrics") ->
- "";
- desc(_) ->
- undefined.
- regular_expression(type) -> binary();
- regular_expression(required) -> true;
- regular_expression(desc) -> ?DESC(tr_re);
- regular_expression(example) -> "^x/y/(.+)$";
- regular_expression(validator) -> fun is_re/1;
- regular_expression(_) -> undefined.
- is_re(Bin) ->
- case re:compile(Bin) of
- {ok, _} -> ok;
- {error, Reason} -> {error, {Bin, Reason}}
- end.
- array(Name, Meta) -> {Name, hoconsc:mk(hoconsc:array(hoconsc:ref(?MODULE, Name)), Meta)}.
- sc(Type, Meta) -> hoconsc:mk(Type, Meta).
|