fix(redis): json decode if arg for map_to_redis_hset_args is string

This commit is contained in:
zmstone 2024-06-17 20:03:32 +02:00
parent b64f0c0ca7
commit 9479c8d33b
2 changed files with 16 additions and 1 deletions

View File

@ -825,8 +825,21 @@ join_to_string(Sep, List) -> emqx_variform_bif:join_to_string(Sep, List).
%% - String values are always quoted %% - String values are always quoted
%% - No escape sequence for keys and values %% - No escape sequence for keys and values
%% - Float point values are formatted with fixed (6) decimal point compact-formatting %% - Float point values are formatted with fixed (6) decimal point compact-formatting
map_to_redis_hset_args(Payload) when erlang:is_binary(Payload) ->
try
Map = json_decode(Payload),
map_to_redis_hset_args(Map)
catch
_:_ ->
%% Discard invalid JSON
[map_to_redis_hset_args]
end;
map_to_redis_hset_args(Map) when erlang:is_map(Map) -> map_to_redis_hset_args(Map) when erlang:is_map(Map) ->
[map_to_redis_hset_args | maps:fold(fun redis_hset_acc/3, [], Map)]. Fields = maps:fold(fun redis_hset_acc/3, [], Map),
%% Fields can be [], the final template may have other fields for concatenation
[map_to_redis_hset_args | Fields];
map_to_redis_hset_args(_Other) ->
[map_to_redis_hset_args].
redis_hset_acc(K, V, IoData) -> redis_hset_acc(K, V, IoData) ->
try try

View File

@ -1395,6 +1395,8 @@ t_map_to_redis_hset_args(_Config) ->
true true
end end
), ),
?assertEqual([], Do(<<"not json">>)),
?assertEqual([], Do([<<"not map">>, <<"not json either">>])),
ok. ok.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------