feat(emqx_resource): delete API generation from parse transfrom
This commit is contained in:
parent
f1552f4f4f
commit
4f451ee862
|
@ -47,23 +47,20 @@ trans(Mod, Forms) ->
|
|||
|
||||
forms(Mod, [F0 | Fs0]) ->
|
||||
case form(Mod, F0) of
|
||||
{CurrForm, AppendedForms} ->
|
||||
CurrForm ++ forms(Mod, Fs0) ++ AppendedForms;
|
||||
{AHeadForms, CurrForm, AppendedForms} ->
|
||||
AHeadForms ++ CurrForm ++ forms(Mod, Fs0) ++ AppendedForms
|
||||
{CurrForms, AppendedForms} ->
|
||||
CurrForms ++ forms(Mod, Fs0) ++ AppendedForms;
|
||||
{CurrForms, FollowerForms, AppendedForms} ->
|
||||
CurrForms ++ FollowerForms ++ forms(Mod, Fs0) ++ AppendedForms
|
||||
end;
|
||||
forms(_, []) -> [].
|
||||
|
||||
form(Mod, Form) ->
|
||||
case Form of
|
||||
?Q("-emqx_resource_api_path('@Path').") ->
|
||||
{fix_spec_attrs() ++ fix_api_attrs(Mod, erl_syntax:concrete(Path))
|
||||
++ fix_api_exports(),
|
||||
[],
|
||||
fix_spec_funcs(Mod) ++ fix_api_funcs(Mod)};
|
||||
?Q("-module('@_').") ->
|
||||
{[Form], fix_spec_attrs(), fix_spec_funcs(Mod)};
|
||||
_ ->
|
||||
%io:format("---other form: ~p~n", [Form]),
|
||||
{[], [Form], []}
|
||||
{[Form], [], []}
|
||||
end.
|
||||
|
||||
fix_spec_attrs() ->
|
||||
|
@ -81,40 +78,3 @@ fix_spec_funcs(_Mod) ->
|
|||
"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.
|
||||
|
|
Loading…
Reference in New Issue