Skip to content
Permalink
Browse files

Append status when list or get resources

  • Loading branch information...
terry-xiaoyu committed Jun 5, 2019
1 parent 92b8d60 commit e4574c9554d7e7d79a8ce55a6c9e4089ee00db79
Showing with 12 additions and 3 deletions.
  1. +2 −0 src/emqx_rule_engine_api.erl
  2. +7 −2 src/emqx_rule_utils.erl
  3. +2 −0 test/emqx_rule_engine_SUITE.erl
  4. +1 −1 test/emqx_rule_utils_tests.erl
@@ -389,9 +389,11 @@ record_to_map(#resource{id = Id,
type = Type,
config = Config,
description = Descr}) ->
{ok, Status} = emqx_rule_engine:get_resource_status(Id),
#{id => Id,
type => Type,
config => Config,
status => Status,
description => Descr
};

@@ -79,7 +79,7 @@ preproc_sql(Sql, ReplaceWith) ->
{match, PlaceHolders} ->
{repalce_with(Sql, ReplaceWith),
fun(Data) ->
[get_phld_var(Phld, Data)
[sql_data(get_phld_var(Phld, Data))
|| Phld <- [hd(PH) || PH <- PlaceHolders]]
end};
nomatch ->
@@ -105,7 +105,7 @@ repalce_with(Tmpl, '$n') ->

unsafe_atom_key(Key) when is_atom(Key) ->
Key;
unsafe_atom_key(Keys = [Key | SubKeys]) when is_binary(Key) ->
unsafe_atom_key(_Keys = [Key | SubKeys]) when is_binary(Key) ->
[binary_to_atom(Key, utf8) | SubKeys];
unsafe_atom_key(Key) when is_binary(Key) ->
binary_to_atom(Key, utf8).
@@ -158,6 +158,11 @@ bin(Bin) when is_binary(Bin) -> Bin;
bin(Num) when is_number(Num) -> number_to_binary(Num);
bin(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8).

sql_data(List) when is_list(List) -> List;
sql_data(Bin) when is_binary(Bin) -> Bin;
sql_data(Num) when is_number(Num) -> Num;
sql_data(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8).

number_to_binary(Int) when is_integer(Int) ->
integer_to_binary(Int);
number_to_binary(Float) when is_float(Float) ->
@@ -101,6 +101,7 @@ end_per_suite(_Config) ->

on_resource_create(_id, _) -> #{}.
on_resource_destroy(_id, _) -> ok.
on_get_resource_status(_id, _) -> #{}.

%%------------------------------------------------------------------------------
%% Group specific setup/teardown
@@ -160,6 +161,7 @@ init_per_testcase(_TestCase, Config) ->
params_spec = #{},
on_create = {?MODULE, on_resource_create},
on_destroy = {?MODULE, on_resource_destroy},
on_status = {?MODULE, on_get_resource_status},
title = #{en => <<"Built-In Resource Type (debug)">>},
description = #{en => <<"The built in resource type for debug purpose">>}}]),
%ct:pal("============ ~p", [ets:tab2list(emqx_resource_type)]),
@@ -44,4 +44,4 @@ preproc_sql2_test() ->
preproc_sql3_test() ->
{Tk, GetV} = emqx_rule_utils:preproc_sql(<<"v1: ${a}, v2: ${b}">>, '$n'),
?_assertEqual(<<"v1: $1, v2: $2">>, Tk),
?assertEqual([hello, <<"yes">>], GetV(#{a => hello, b => <<"yes">> })).
?assertEqual([<<"hello">>, <<"yes">>], GetV(#{a => hello, b => <<"yes">> })).

0 comments on commit e4574c9

Please sign in to comment.
You can’t perform that action at this time.