Merge pull request #13279 from zmstone/0617-json-decode-payload-for-map_to_redis_hset_args

fix(redis): json decode if arg for map_to_redis_hset_args is string
This commit is contained in:
zmstone 2024-06-19 14:55:21 +02:00 committed by GitHub
commit 02a6ee1ef4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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
%% - No escape sequence for keys and values
%% - 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 | 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) ->
try

View File

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