feat: improve the syntax of ${} in payload templates

This commit is contained in:
Shawn 2023-04-21 16:49:28 +08:00
parent daa2088b94
commit 8db1aae642
3 changed files with 24 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{application, emqx_rule_engine, {application, emqx_rule_engine,
[{description, "EMQX Rule Engine"}, [{description, "EMQX Rule Engine"},
{vsn, "4.4.17"}, % strict semver, bump manually! {vsn, "4.4.18"}, % strict semver, bump manually!
{modules, []}, {modules, []},
{registered, [emqx_rule_engine_sup, emqx_rule_registry, emqx_rule_engine_jwt_sup]}, {registered, [emqx_rule_engine_sup, emqx_rule_registry, emqx_rule_engine_jwt_sup]},
{applications, [kernel,stdlib,rulesql,getopt,jose]}, {applications, [kernel,stdlib,rulesql,getopt,jose]},

View File

@ -71,7 +71,10 @@
[ float/1 [ float/1
]}). ]}).
-define(EX_PLACE_HOLDER, "(\\$\\{[a-zA-Z0-9\\._]+\\})"). %% To match any pattern starts with '$' and followed by '{', and closed by a '}' char:
%% e.g. for string "a${abc}bb", "${abc}" will be matched.
%% Note that if "${{abc}}" is given, the "${{abc}" should be matched, NOT "${{abc}}".
-define(EX_PLACE_HOLDER, "(\\$\\{.*?\\})").
-define(EX_WITHE_CHARS, "\\s"). %% Space and CRLF -define(EX_WITHE_CHARS, "\\s"). %% Space and CRLF
-define(FLOAT_PRECISION, 17). -define(FLOAT_PRECISION, 17).

View File

@ -76,6 +76,25 @@ t_proc_tmpl(_) ->
Selected = #{a => <<"1">>, b => 1, c => 1.0, d => #{d1 => <<"hi">>}}, Selected = #{a => <<"1">>, b => 1, c => 1.0, d => #{d1 => <<"hi">>}},
Tks = emqx_rule_utils:preproc_tmpl(<<"a:${a},b:${b},c:${c},d:${d}">>), Tks = emqx_rule_utils:preproc_tmpl(<<"a:${a},b:${b},c:${c},d:${d}">>),
?assertEqual(<<"a:1,b:1,c:1.0,d:{\"d1\":\"hi\"}">>, ?assertEqual(<<"a:1,b:1,c:1.0,d:{\"d1\":\"hi\"}">>,
emqx_rule_utils:proc_tmpl(Tks, Selected)),
Tks1 = emqx_rule_utils:preproc_tmpl(<<"a:${a},b:${b},c:${c},d:${d.d1}">>),
?assertEqual(<<"a:1,b:1,c:1.0,d:hi">>,
emqx_rule_utils:proc_tmpl(Tks1, Selected)).
t_proc_tmpl_arbitrary_var_name(_) ->
Selected = #{<<""/utf8>> => <<"1">>,
<<"中-1"/utf8>> => <<"1-1">>,
<<"-_+=<>,/?:;\"'\\[]|">> => 1,
<<"-_+=<>,">> => #{<<"/?:;\"'\\[]|">> => 2},
<<"!@#$%^&*()">> => 1.0,
<<"d">> => #{
<<"$ff">> => <<"oo">>,
<<"${f">> => <<"hi">>,
<<"${f}">> => <<"qq">>
}},
Tks = emqx_rule_utils:preproc_tmpl(
<<"a:${中},a:${中-1},b:${-_+=<>,/?:;\"'\\[]|},b:${-_+=<>,./?:;\"'\\[]|},c:${!@#$%^&*()},d:${d.$ff},d1:${d.${f}}"/utf8>>),
?assertEqual(<<"a:1,a:1-1,b:1,b:2,c:1.0,d:oo,d1:hi}">>,
emqx_rule_utils:proc_tmpl(Tks, Selected)). emqx_rule_utils:proc_tmpl(Tks, Selected)).
t_proc_tmpl1(_) -> t_proc_tmpl1(_) ->