feat: add a 'this' for rule engine put env

now a dot is allowed in front of a var (path)
e.g. ${.clientid} is equivalent to ${clientid}

${.} however, means everything.
the parsed var is interally represented as $_THIS_
to make it easier to read/search.
This commit is contained in:
Zaiming (Stone) Shi 2023-01-14 10:45:20 +01:00
parent 4b6c95e3d2
commit 7073c62dd9
3 changed files with 12 additions and 4 deletions

View File

@ -17,6 +17,8 @@
-ifndef(EMQX_PLACEHOLDER_HRL).
-define(EMQX_PLACEHOLDER_HRL, true).
-define(PH_VAR_THIS, <<"$_THIS_">>).
-define(PH(Type), <<"${", Type/binary, "}">>).
%% action: publish/subscribe/all

View File

@ -39,6 +39,8 @@
sql_data/1
]).
-include_lib("emqx/include/emqx_placeholder.hrl").
-define(EX_PLACE_HOLDER, "(\\$\\{[a-zA-Z0-9\\._]+\\})").
-define(EX_PLACE_HOLDER_DOUBLE_QUOTE, "(\\$\\{[a-zA-Z0-9\\._]+\\}|\"\\$\\{[a-zA-Z0-9\\._]+\\}\")").
@ -233,9 +235,6 @@ proc_param_str(Tokens, Data, Quote) ->
proc_tmpl(Tokens, Data, #{return => rawlist, var_trans => Quote})
).
%% backward compatibility for hot upgrading from =< e4.2.1
get_phld_var(Fun, Data) when is_function(Fun) ->
Fun(Data);
get_phld_var(Phld, Data) ->
emqx_rule_maps:nested_get(Phld, Data).
@ -298,9 +297,12 @@ replace_with(Tmpl, RE, '$n') ->
Parts
),
Res.
parse_nested(<<".", R/binary>>) ->
%% ignroe the root .
parse_nested(R);
parse_nested(Attr) ->
case string:split(Attr, <<".">>, all) of
[<<>>] -> {var, ?PH_VAR_THIS};
[Attr] -> {var, Attr};
Nested -> {path, [{key, P} || P <- Nested]}
end.

View File

@ -26,9 +26,13 @@
unsafe_atom_key_map/1
]).
-include_lib("emqx/include/emqx_placeholder.hrl").
nested_get(Key, Data) ->
nested_get(Key, Data, undefined).
nested_get({var, ?PH_VAR_THIS}, Data, _Default) ->
Data;
nested_get({var, Key}, Data, Default) ->
general_map_get({key, Key}, Data, Data, Default);
nested_get({path, Path}, Data, Default) when is_list(Path) ->