chore: add convenience function for creating action schemas

This commit is contained in:
Kjell Winblad 2023-11-17 17:53:30 +01:00 committed by Thales Macedo Garitezi
parent bdbadaa2ca
commit 9feba802e9
1 changed files with 23 additions and 1 deletions

View File

@ -40,6 +40,8 @@
-export([types/0, types_sc/0]).
-export([make_action_schema/1]).
-export_type([action_type/0]).
%% Should we explicitly list them here so dialyzer may be more helpful?
@ -116,7 +118,9 @@ roots() ->
end.
fields(actions) ->
registered_schema_fields().
registered_schema_fields();
fields(resource_opts) ->
emqx_resource_schema:create_opts(_Overrides = []).
registered_schema_fields() ->
[
@ -150,6 +154,24 @@ examples(Method) ->
SchemaModules = [Mod || {_, Mod} <- emqx_action_info:registered_schema_modules()],
lists:foldl(Fun, #{}, SchemaModules).
%%======================================================================================
%% Helper functions for making HOCON Schema
%%======================================================================================
make_action_schema(ActionParametersRef) ->
[
{enable, mk(boolean(), #{desc => ?DESC("config_enable"), default => true})},
{connector,
mk(binary(), #{
desc => ?DESC(emqx_connector_schema, "connector_field"), required => true
})},
{description, emqx_schema:description_schema()},
{local_topic, mk(binary(), #{required => false, desc => ?DESC(mqtt_topic)})},
{parameters, ActionParametersRef},
{resource_opts,
mk(ref(?MODULE, resource_opts), #{default => #{}, desc => ?DESC(resource_opts)})}
].
-ifdef(TEST).
-include_lib("hocon/include/hocon_types.hrl").
schema_homogeneous_test() ->