refactor(paginate): use exported fun call `{M,F}`
This commit is contained in:
parent
3683ba13ee
commit
4a07d5e1f3
|
@ -41,7 +41,10 @@
|
||||||
, list_users/2
|
, list_users/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
-export([format_user_info/1]).
|
||||||
|
|
||||||
-define(TAB, ?MODULE).
|
-define(TAB, ?MODULE).
|
||||||
|
-define(FORMAT_FUN, {?MODULE, format_user_info}).
|
||||||
|
|
||||||
-export([mnesia/1]).
|
-export([mnesia/1]).
|
||||||
|
|
||||||
|
@ -195,7 +198,7 @@ lookup_user(UserID, #{user_group := UserGroup}) ->
|
||||||
|
|
||||||
list_users(PageParams, #{user_group := UserGroup}) ->
|
list_users(PageParams, #{user_group := UserGroup}) ->
|
||||||
MatchSpec = [{{user_info, {UserGroup, '_'}, '_', '_', '_', '_'}, [], ['$_']}],
|
MatchSpec = [{{user_info, {UserGroup, '_'}, '_', '_', '_', '_'}, [], ['$_']}],
|
||||||
{ok, emqx_mgmt_api:paginate(?TAB, MatchSpec, PageParams, fun format_user_info/1)}.
|
{ok, emqx_mgmt_api:paginate(?TAB, MatchSpec, PageParams, ?FORMAT_FUN)}.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
|
|
|
@ -42,6 +42,8 @@
|
||||||
, list_users/2
|
, list_users/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
-export([format_user_info/1]).
|
||||||
|
|
||||||
-type user_id_type() :: clientid | username.
|
-type user_id_type() :: clientid | username.
|
||||||
|
|
||||||
-type user_group() :: {binary(), binary()}.
|
-type user_group() :: {binary(), binary()}.
|
||||||
|
@ -61,6 +63,7 @@
|
||||||
-boot_mnesia({mnesia, [boot]}).
|
-boot_mnesia({mnesia, [boot]}).
|
||||||
|
|
||||||
-define(TAB, ?MODULE).
|
-define(TAB, ?MODULE).
|
||||||
|
-define(FORMAT_FUN, {?MODULE, format_user_info}).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Mnesia bootstrap
|
%% Mnesia bootstrap
|
||||||
|
@ -244,7 +247,7 @@ lookup_user(UserID, #{user_group := UserGroup}) ->
|
||||||
|
|
||||||
list_users(PageParams, #{user_group := UserGroup}) ->
|
list_users(PageParams, #{user_group := UserGroup}) ->
|
||||||
MatchSpec = [{{user_info, {UserGroup, '_'}, '_', '_', '_'}, [], ['$_']}],
|
MatchSpec = [{{user_info, {UserGroup, '_'}, '_', '_', '_'}, [], ['$_']}],
|
||||||
{ok, emqx_mgmt_api:paginate(?TAB, MatchSpec, PageParams, fun format_user_info/1)}.
|
{ok, emqx_mgmt_api:paginate(?TAB, MatchSpec, PageParams, ?FORMAT_FUN)}.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
|
|
|
@ -66,6 +66,9 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}).
|
}).
|
||||||
|
-define(FORMAT_USERNAME_FUN, {?MODULE, format_by_username}).
|
||||||
|
-define(FORMAT_CLIENTID_FUN, {?MODULE, format_by_clientid}).
|
||||||
|
|
||||||
|
|
||||||
-export([ api_spec/0
|
-export([ api_spec/0
|
||||||
, purge/2
|
, purge/2
|
||||||
|
@ -76,6 +79,9 @@
|
||||||
, all/2
|
, all/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
-export([ format_by_username/1
|
||||||
|
, format_by_clientid/1]).
|
||||||
|
|
||||||
api_spec() ->
|
api_spec() ->
|
||||||
{[ purge_api()
|
{[ purge_api()
|
||||||
, users_api()
|
, users_api()
|
||||||
|
@ -507,15 +513,7 @@ users(get, #{query_string := PageParams}) ->
|
||||||
fun({?ACL_TABLE, {?ACL_TABLE_USERNAME, Username}, Rules}) ->
|
fun({?ACL_TABLE, {?ACL_TABLE_USERNAME, Username}, Rules}) ->
|
||||||
[{username, Username}, {rules, Rules}]
|
[{username, Username}, {rules, Rules}]
|
||||||
end),
|
end),
|
||||||
Format = fun ([{username, Username}, {rules, Rules}]) ->
|
{200, emqx_mgmt_api:paginate(?ACL_TABLE, MatchSpec, PageParams, ?FORMAT_USERNAME_FUN)};
|
||||||
#{username => Username,
|
|
||||||
rules => [ #{topic => Topic,
|
|
||||||
action => Action,
|
|
||||||
permission => Permission
|
|
||||||
} || {Permission, Action, Topic} <- Rules]
|
|
||||||
}
|
|
||||||
end,
|
|
||||||
{200, emqx_mgmt_api:paginate(?ACL_TABLE, MatchSpec, PageParams, Format)};
|
|
||||||
users(post, #{body := Body}) when is_list(Body) ->
|
users(post, #{body := Body}) when is_list(Body) ->
|
||||||
lists:foreach(fun(#{<<"username">> := Username, <<"rules">> := Rules}) ->
|
lists:foreach(fun(#{<<"username">> := Username, <<"rules">> := Rules}) ->
|
||||||
mria:dirty_write(#emqx_acl{
|
mria:dirty_write(#emqx_acl{
|
||||||
|
@ -530,15 +528,7 @@ clients(get, #{query_string := PageParams}) ->
|
||||||
fun({?ACL_TABLE, {?ACL_TABLE_CLIENTID, Clientid}, Rules}) ->
|
fun({?ACL_TABLE, {?ACL_TABLE_CLIENTID, Clientid}, Rules}) ->
|
||||||
[{clientid, Clientid}, {rules, Rules}]
|
[{clientid, Clientid}, {rules, Rules}]
|
||||||
end),
|
end),
|
||||||
Format = fun ([{clientid, Clientid}, {rules, Rules}]) ->
|
{200, emqx_mgmt_api:paginate(?ACL_TABLE, MatchSpec, PageParams, ?FORMAT_CLIENTID_FUN)};
|
||||||
#{clientid => Clientid,
|
|
||||||
rules => [ #{topic => Topic,
|
|
||||||
action => Action,
|
|
||||||
permission => Permission
|
|
||||||
} || {Permission, Action, Topic} <- Rules]
|
|
||||||
}
|
|
||||||
end,
|
|
||||||
{200, emqx_mgmt_api:paginate(?ACL_TABLE, MatchSpec, PageParams, Format)};
|
|
||||||
clients(post, #{body := Body}) when is_list(Body) ->
|
clients(post, #{body := Body}) when is_list(Body) ->
|
||||||
lists:foreach(fun(#{<<"clientid">> := Clientid, <<"rules">> := Rules}) ->
|
lists:foreach(fun(#{<<"clientid">> := Clientid, <<"rules">> := Rules}) ->
|
||||||
mria:dirty_write(#emqx_acl{
|
mria:dirty_write(#emqx_acl{
|
||||||
|
@ -635,6 +625,20 @@ format_rules(Rules) when is_list(Rules) ->
|
||||||
AccIn ++ [{ atom(Permission), atom(Action), Topic }]
|
AccIn ++ [{ atom(Permission), atom(Action), Topic }]
|
||||||
end, [], Rules).
|
end, [], Rules).
|
||||||
|
|
||||||
|
format_by_username([{username, Username}, {rules, Rules}]) ->
|
||||||
|
#{username => Username,
|
||||||
|
rules => [ #{topic => Topic,
|
||||||
|
action => Action,
|
||||||
|
permission => Permission
|
||||||
|
} || {Permission, Action, Topic} <- Rules]
|
||||||
|
}.
|
||||||
|
format_by_clientid([{clientid, Clientid}, {rules, Rules}]) ->
|
||||||
|
#{clientid => Clientid,
|
||||||
|
rules => [ #{topic => Topic,
|
||||||
|
action => Action,
|
||||||
|
permission => Permission
|
||||||
|
} || {Permission, Action, Topic} <- Rules]
|
||||||
|
}.
|
||||||
atom(B) when is_binary(B) ->
|
atom(B) when is_binary(B) ->
|
||||||
try binary_to_existing_atom(B, utf8)
|
try binary_to_existing_atom(B, utf8)
|
||||||
catch
|
catch
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
|
|
||||||
-export([do_query/6]).
|
-export([do_query/6]).
|
||||||
|
|
||||||
paginate(Tables, Params, RowFun) ->
|
paginate(Tables, Params, {Module, FormatFun}) ->
|
||||||
Qh = query_handle(Tables),
|
Qh = query_handle(Tables),
|
||||||
Count = count(Tables),
|
Count = count(Tables),
|
||||||
Page = b2i(page(Params)),
|
Page = b2i(page(Params)),
|
||||||
|
@ -47,9 +47,9 @@ paginate(Tables, Params, RowFun) ->
|
||||||
Rows = qlc:next_answers(Cursor, Limit),
|
Rows = qlc:next_answers(Cursor, Limit),
|
||||||
qlc:delete_cursor(Cursor),
|
qlc:delete_cursor(Cursor),
|
||||||
#{meta => #{page => Page, limit => Limit, count => Count},
|
#{meta => #{page => Page, limit => Limit, count => Count},
|
||||||
data => [RowFun(Row) || Row <- Rows]}.
|
data => [Module:FormatFun(Row) || Row <- Rows]}.
|
||||||
|
|
||||||
paginate(Tables, MatchSpec, Params, RowFun) ->
|
paginate(Tables, MatchSpec, Params, {Module, FormatFun}) ->
|
||||||
Qh = query_handle(Tables, MatchSpec),
|
Qh = query_handle(Tables, MatchSpec),
|
||||||
Count = count(Tables, MatchSpec),
|
Count = count(Tables, MatchSpec),
|
||||||
Page = b2i(page(Params)),
|
Page = b2i(page(Params)),
|
||||||
|
@ -64,7 +64,7 @@ paginate(Tables, MatchSpec, Params, RowFun) ->
|
||||||
Rows = qlc:next_answers(Cursor, Limit),
|
Rows = qlc:next_answers(Cursor, Limit),
|
||||||
qlc:delete_cursor(Cursor),
|
qlc:delete_cursor(Cursor),
|
||||||
#{meta => #{page => Page, limit => Limit, count => Count},
|
#{meta => #{page => Page, limit => Limit, count => Count},
|
||||||
data => [RowFun(Row) || Row <- Rows]}.
|
data => [Module:FormatFun(Row) || Row <- Rows]}.
|
||||||
|
|
||||||
query_handle(Table) when is_atom(Table) ->
|
query_handle(Table) when is_atom(Table) ->
|
||||||
qlc:q([R || R <- ets:table(Table)]);
|
qlc:q([R || R <- ets:table(Table)]);
|
||||||
|
|
|
@ -39,6 +39,8 @@
|
||||||
-export([format/1]).
|
-export([format/1]).
|
||||||
|
|
||||||
-define(TAB, emqx_banned).
|
-define(TAB, emqx_banned).
|
||||||
|
-define(FORMAT_FUN, {?MODULE, format}).
|
||||||
|
|
||||||
|
|
||||||
api_spec() ->
|
api_spec() ->
|
||||||
{[banned_api(), delete_banned_api()], []}.
|
{[banned_api(), delete_banned_api()], []}.
|
||||||
|
@ -98,7 +100,7 @@ delete_banned_api() ->
|
||||||
{Path, MetaData, delete_banned}.
|
{Path, MetaData, delete_banned}.
|
||||||
|
|
||||||
banned(get, #{query_string := Params}) ->
|
banned(get, #{query_string := Params}) ->
|
||||||
Response = emqx_mgmt_api:paginate(?TAB, Params, fun format/1),
|
Response = emqx_mgmt_api:paginate(?TAB, Params, ?FORMAT_FUN),
|
||||||
{200, Response};
|
{200, Response};
|
||||||
banned(post, #{body := Body}) ->
|
banned(post, #{body := Body}) ->
|
||||||
_ = emqx_banned:create(emqx_banned:parse(Body)),
|
_ = emqx_banned:create(emqx_banned:parse(Body)),
|
||||||
|
|
|
@ -49,6 +49,8 @@
|
||||||
, delete_delayed_message/1
|
, delete_delayed_message/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
-export([format_delayed/1]).
|
||||||
|
|
||||||
-record(delayed_message, {key, delayed, msg}).
|
-record(delayed_message, {key, delayed, msg}).
|
||||||
|
|
||||||
%% sync ms with record change
|
%% sync ms with record change
|
||||||
|
@ -58,6 +60,7 @@
|
||||||
-define(TAB, ?MODULE).
|
-define(TAB, ?MODULE).
|
||||||
-define(SERVER, ?MODULE).
|
-define(SERVER, ?MODULE).
|
||||||
-define(MAX_INTERVAL, 4294967).
|
-define(MAX_INTERVAL, 4294967).
|
||||||
|
-define(FORMAT_FUN, {?MODULE, format_delayed}).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Mnesia bootstrap
|
%% Mnesia bootstrap
|
||||||
|
@ -124,7 +127,7 @@ set_max_delayed_messages(Max) ->
|
||||||
gen_server:call(?SERVER, {set_max_delayed_messages, Max}).
|
gen_server:call(?SERVER, {set_max_delayed_messages, Max}).
|
||||||
|
|
||||||
list(Params) ->
|
list(Params) ->
|
||||||
emqx_mgmt_api:paginate(?TAB, Params, fun format_delayed/1).
|
emqx_mgmt_api:paginate(?TAB, Params, ?FORMAT_FUN).
|
||||||
|
|
||||||
format_delayed(Delayed) ->
|
format_delayed(Delayed) ->
|
||||||
format_delayed(Delayed, false).
|
format_delayed(Delayed, false).
|
||||||
|
|
Loading…
Reference in New Issue