Просмотр исходного кода

feat(emqx_resource): remove the schema sugar from parse transformed code

Shawn 4 лет назад
Родитель
Сommit
65dc2f2be8

+ 2 - 1
apps/emqx_connector/src/emqx_connector_mysql.erl

@@ -18,7 +18,7 @@
 -include_lib("typerefl/include/types.hrl").
 -include_lib("emqx_resource/include/emqx_resource_behaviour.hrl").
 
--export([ on_jsonify/1
+-export([ schema/0
         ]).
 
 %% callbacks of behaviour emqx_resource
@@ -26,6 +26,7 @@
         , on_stop/2
         , on_query/4
         , on_health_check/2
+        , on_jsonify/1
         ]).
 
 -export([connect/1]).

+ 1 - 1
apps/emqx_resource/src/emqx_resource.erl

@@ -284,7 +284,7 @@ check_config(ResourceType, RawConfigTerm) ->
 
 -spec do_check_config(resource_type(), map()) -> {ok, resource_config()} | {error, term()}.
 do_check_config(ResourceType, MapConfig) ->
-    case ?SAFE_CALL(hocon_schema:check(ResourceType, MapConfig)) of
+    case ?SAFE_CALL(emqx_resource_schema:check(ResourceType, MapConfig)) of
         {error, Reason} -> {error, Reason};
         Config -> {ok, maps:get(<<"config">>, hocon_schema:richmap_to_map(Config))}
     end.

+ 38 - 0
apps/emqx_resource/src/emqx_resource_schema.erl

@@ -0,0 +1,38 @@
+%%--------------------------------------------------------------------
+%% Copyright (c) 2020-2021 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_resource_schema).
+
+-export([check/2]).
+
+-export([structs/0, fields/1]).
+
+-behaviour(hocon_schema).
+
+check(SchemaMod, Conf) ->
+    _ = erlang:erase(res_schema_mod),
+    erlang:put(res_schema_mod, SchemaMod),
+    hocon_schema:check(?MODULE, Conf).
+
+structs() -> ["config"].
+
+fields("config") ->
+    [fun(type) -> "schema";
+        (_) -> undefined
+     end];
+fields("schema") ->
+    SchemaMod = erlang:get(res_schema_mod),
+    SchemaMod:schema().

+ 0 - 9
apps/emqx_resource/src/emqx_resource_transform.erl

@@ -65,16 +65,7 @@ form(Mod, Form) ->
 
 fix_spec_attrs() ->
     [ ?Q("-export([emqx_resource_schema/0]).")
-    , ?Q("-export([structs/0, fields/1]).")
-    , ?Q("-behaviour(hocon_schema).")
     ].
 fix_spec_funcs(_Mod) ->
     [ ?Q("emqx_resource_schema() -> <<\"demo_swagger_schema\">>.")
-    , ?Q("structs() -> [\"config\"].")
-    , ?Q("fields(\"config\") -> "
-           "[fun (type) -> \"schema\"; "
-           "    (_) -> undefined "
-           " end];"
-         "fields(\"schema\") -> schema()."
-        )
     ].