fix(placeholder): porting fix to support utf8 key in placeholder

This commit is contained in:
firest 2023-08-07 17:40:59 +08:00
parent 7b3843f6a3
commit 7567d211da
2 changed files with 56 additions and 2 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

@ -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)
).