feat(emqx_resource): delete API generation from parse transfrom

This commit is contained in:
Shawn 2021-06-05 15:30:21 +08:00
parent f1552f4f4f
commit 4f451ee862
1 changed files with 7 additions and 47 deletions

View File

@ -47,23 +47,20 @@ trans(Mod, Forms) ->
forms(Mod, [F0 | Fs0]) -> forms(Mod, [F0 | Fs0]) ->
case form(Mod, F0) of case form(Mod, F0) of
{CurrForm, AppendedForms} -> {CurrForms, AppendedForms} ->
CurrForm ++ forms(Mod, Fs0) ++ AppendedForms; CurrForms ++ forms(Mod, Fs0) ++ AppendedForms;
{AHeadForms, CurrForm, AppendedForms} -> {CurrForms, FollowerForms, AppendedForms} ->
AHeadForms ++ CurrForm ++ forms(Mod, Fs0) ++ AppendedForms CurrForms ++ FollowerForms ++ forms(Mod, Fs0) ++ AppendedForms
end; end;
forms(_, []) -> []. forms(_, []) -> [].
form(Mod, Form) -> form(Mod, Form) ->
case Form of case Form of
?Q("-emqx_resource_api_path('@Path').") -> ?Q("-module('@_').") ->
{fix_spec_attrs() ++ fix_api_attrs(Mod, erl_syntax:concrete(Path)) {[Form], fix_spec_attrs(), fix_spec_funcs(Mod)};
++ fix_api_exports(),
[],
fix_spec_funcs(Mod) ++ fix_api_funcs(Mod)};
_ -> _ ->
%io:format("---other form: ~p~n", [Form]), %io:format("---other form: ~p~n", [Form]),
{[], [Form], []} {[Form], [], []}
end. end.
fix_spec_attrs() -> fix_spec_attrs() ->
@ -81,40 +78,3 @@ fix_spec_funcs(_Mod) ->
"fields(\"schema\") -> schema()." "fields(\"schema\") -> schema()."
) )
]. ].
fix_api_attrs(Mod, Path) ->
BaseName = atom_to_list(Mod),
[erl_syntax:revert(
erl_syntax:attribute(?Q("rest_api"), [
erl_syntax:abstract(#{
name => list_to_atom(Act ++ "_" ++ BaseName),
method => Method,
path => mk_path(Path, WithId),
func => Func,
descr => Act ++ " the " ++ BaseName})]))
|| {Act, Method, WithId, Func} <- [
{"list", 'GET', noid, api_get_all},
{"get", 'GET', id, api_get},
{"update", 'PUT', id, api_put},
{"delete", 'DELETE', id, api_delete}]].
fix_api_exports() ->
[?Q("-export([api_get_all/2, api_get/2, api_put/2, api_delete/2]).")].
fix_api_funcs(Mod) ->
[erl_syntax:revert(?Q(
"api_get_all(Binding, Params) ->
emqx_resource_api:get_all('@Mod@', Binding, Params).")),
erl_syntax:revert(?Q(
"api_get(Binding, Params) ->
emqx_resource_api:get('@Mod@', Binding, Params).")),
erl_syntax:revert(?Q(
"api_put(Binding, Params) ->
emqx_resource_api:put('@Mod@', Binding, Params).")),
erl_syntax:revert(?Q(
"api_delete(Binding, Params) ->
emqx_resource_api:delete('@Mod@', Binding, Params)."))
].
mk_path(Path, id) -> string:trim(Path, trailing, "/") ++ "/:bin:id";
mk_path(Path, noid) -> Path.