fix(rule_engine): nested_get value from binary was supported in 4.1
This commit is contained in:
parent
86d20b060c
commit
e15c0c325e
|
@ -30,15 +30,8 @@ nested_get(Key, Data) ->
|
|||
|
||||
nested_get({var, Key}, Data, Default) ->
|
||||
general_map_get({key, Key}, Data, Data, Default);
|
||||
nested_get({path, Path}, Data, Default) when is_map(Data) orelse is_list(Data),
|
||||
is_list(Path) ->
|
||||
do_nested_get(Path, Data, Data, Default);
|
||||
nested_get(Key, Data, Default) when not is_map(Data) ->
|
||||
try emqx_json:decode(Data, [return_maps]) of
|
||||
Json -> nested_get(Key, Json, Default)
|
||||
catch
|
||||
_:_ -> Default
|
||||
end.
|
||||
nested_get({path, Path}, Data, Default) when is_list(Path) ->
|
||||
do_nested_get(Path, Data, Data, Default).
|
||||
|
||||
do_nested_get([Key|More], Data, OrgData, Default) ->
|
||||
case general_map_get(Key, Data, OrgData, undefined) of
|
||||
|
@ -81,6 +74,12 @@ general_map_put(Key, Val, Map, OrgData) ->
|
|||
(_) -> do_put(Key, Val, Map, OrgData)
|
||||
end).
|
||||
|
||||
general_find(KeyOrIndex, Data, OrgData, Handler) when is_binary(Data) ->
|
||||
try emqx_json:decode(Data, [return_maps]) of
|
||||
Json -> general_find(KeyOrIndex, Json, OrgData, Handler)
|
||||
catch
|
||||
_:_ -> Handler(not_found)
|
||||
end;
|
||||
general_find({key, Key}, Map, _OrgData, Handler) when is_map(Map) ->
|
||||
case maps:find(Key, Map) of
|
||||
{ok, Val} -> Handler({found, {{key, Key}, Val}});
|
||||
|
|
|
@ -125,7 +125,8 @@ groups() ->
|
|||
[t_events
|
||||
]},
|
||||
{bugs, [],
|
||||
[t_sqlparse_payload_as
|
||||
[t_sqlparse_payload_as,
|
||||
t_sqlparse_nested_get
|
||||
]},
|
||||
{multi_actions, [],
|
||||
[t_sqlselect_multi_actoins_1,
|
||||
|
@ -2008,6 +2009,17 @@ t_sqlparse_payload_as(_Config) ->
|
|||
}
|
||||
}, Res02).
|
||||
|
||||
t_sqlparse_nested_get(_Config) ->
|
||||
Sql = "select payload as p, p.a.b as c "
|
||||
"from \"t/#\" ",
|
||||
?assertMatch({ok,#{<<"c">> := 0}},
|
||||
emqx_rule_sqltester:test(
|
||||
#{<<"rawsql">> => Sql,
|
||||
<<"ctx">> => #{
|
||||
<<"topic">> => <<"t/1">>,
|
||||
<<"payload">> => <<"{\"a\": {\"b\": 0}}">>
|
||||
}})).
|
||||
|
||||
%%------------------------------------------------------------------------------
|
||||
%% Internal helpers
|
||||
%%------------------------------------------------------------------------------
|
||||
|
|
|
@ -101,6 +101,11 @@ t_nested_get_map(_) ->
|
|||
?assertEqual(v1, nested_get(?path([<<"p">>,<<"x">>]), #{p => #{x => v1}})),
|
||||
?assertEqual(c, nested_get(?path([a,b,c]), #{a => #{b => #{c => c}}})).
|
||||
|
||||
t_nested_get_map_1(_) ->
|
||||
?assertEqual(1, nested_get(?path([a]), <<"{\"a\": 1}">>)),
|
||||
?assertEqual(<<"{\"b\": 1}">>, nested_get(?path([a]), #{a => <<"{\"b\": 1}">>})),
|
||||
?assertEqual(1, nested_get(?path([a,b]), #{a => <<"{\"b\": 1}">>})).
|
||||
|
||||
t_nested_get_index(_) ->
|
||||
%% single index get
|
||||
?assertEqual(1, nested_get(?path([{ic,1}]), [1,2,3])),
|
||||
|
|
Loading…
Reference in New Issue