Merge pull request #11399 from lafirest/fix/ph_utf8

fix(placeholder): porting fix to support utf8 key in placeholder
This commit is contained in:
lafirest 2023-08-10 09:41:39 +08:00 committed by GitHub
commit b2394cf92d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 3 deletions

View File

@ -48,9 +48,13 @@
-define(PH_VAR_THIS, '$this').
-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 this is non-greedy matching
%% e.g. if "${{abc}}" is given, the "${{abc}" should be matched, NOT "${{abc}}".
-define(EX_PLACE_HOLDER, "(\\$\\{[^}]+\\})").
-define(EX_PLACE_HOLDER_DOUBLE_QUOTE, "(\\$\\{[a-zA-Z0-9\\._]+\\}|\"\\$\\{[a-zA-Z0-9\\._]+\\}\")").
-define(EX_PLACE_HOLDER_DOUBLE_QUOTE, "(\\$\\{[^}]+\\}|\"\\$\\{[^}]+\\}\")").
%% Space and CRLF
-define(EX_WITHE_CHARS, "\\s").

View File

@ -2,7 +2,7 @@
{application, emqx_utils, [
{description, "Miscellaneous utilities for EMQX apps"},
% strict semver, bump manually!
{vsn, "5.0.5"},
{vsn, "5.0.6"},
{modules, [
emqx_utils,
emqx_utils_api,

View File

@ -206,3 +206,53 @@ t_preproc_tmpl_deep(_) ->
#{<<"${a}">> => [<<"1">>, "c", 2, 3.0, '${d}', {[<<"1.0">>], 0}]},
emqx_placeholder:proc_tmpl_deep(Tmpl1, 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_placeholder: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_placeholder:proc_tmpl(Tks, Selected)
).
t_proc_tmpl_arbitrary_var_name_double_quote(_) ->
Selected = #{
<<""/utf8>> => <<"1">>,
<<"中-1"/utf8>> => <<"1-1">>,
<<"-_+=<>,/?:;\"'\\[]|">> => 1,
<<"-_+=<>,">> => #{<<"/?:;\"'\\[]|">> => 2},
<<"!@#$%^&*()">> => 1.0,
<<"d">> => #{
<<"$ff">> => <<"oo">>,
<<"${f">> => <<"hi">>,
<<"${f}">> => <<"qq">>
}
},
Tks = emqx_placeholder:preproc_tmpl(
<<
"a:\"${中}\",a:\"${中-1}\",b:\"${-_+=<>,/?:;\"'\\[]|}\","
"b:\"${-_+=<>,./?:;\"'\\[]|}\",c:\"${!@#$%^&*()}\",d:\"${d.$ff}\",d1:\"${d.${f}\"}"/utf8
>>,
#{strip_double_quote => true}
),
ct:print("TKs:~p~n", [Tks]),
?assertEqual(
<<"a:1,a:1-1,b:1,b:2,c:1.0,d:oo,d1:hi}">>,
emqx_placeholder:proc_tmpl(Tks, Selected)
).

View File

@ -0,0 +1,8 @@
Improved the placeholder syntax of rule engine.
The parameters of actions support using placeholder syntax to
dynamically fill in the content of strings. The format of the
placeholder syntax is `${key}`.
Before this improvement, the `key` in `${key}` could only contain
letters, numbers, and underscores. Now the `key` supports any UTF8
characters.