Преглед изворни кода

chore: add convenience function for creating action schemas

Kjell Winblad пре 2 година
родитељ
комит
9feba802e9
1 измењених фајлова са 23 додато и 1 уклоњено
  1. 23 1
      apps/emqx_bridge/src/schema/emqx_bridge_v2_schema.erl

+ 23 - 1
apps/emqx_bridge/src/schema/emqx_bridge_v2_schema.erl

@@ -40,6 +40,8 @@
 
 
 -export([types/0, types_sc/0]).
 -export([types/0, types_sc/0]).
 
 
+-export([make_action_schema/1]).
+
 -export_type([action_type/0]).
 -export_type([action_type/0]).
 
 
 %% Should we explicitly list them here so dialyzer may be more helpful?
 %% Should we explicitly list them here so dialyzer may be more helpful?
@@ -116,7 +118,9 @@ roots() ->
     end.
     end.
 
 
 fields(actions) ->
 fields(actions) ->
-    registered_schema_fields().
+    registered_schema_fields();
+fields(resource_opts) ->
+    emqx_resource_schema:create_opts(_Overrides = []).
 
 
 registered_schema_fields() ->
 registered_schema_fields() ->
     [
     [
@@ -150,6 +154,24 @@ examples(Method) ->
     SchemaModules = [Mod || {_, Mod} <- emqx_action_info:registered_schema_modules()],
     SchemaModules = [Mod || {_, Mod} <- emqx_action_info:registered_schema_modules()],
     lists:foldl(Fun, #{}, SchemaModules).
     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).
 -ifdef(TEST).
 -include_lib("hocon/include/hocon_types.hrl").
 -include_lib("hocon/include/hocon_types.hrl").
 schema_homogeneous_test() ->
 schema_homogeneous_test() ->