Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 00d50f0

Browse files
1 parent 32ba560 commit 00d50f0

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/rabbit_data_coercion.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ to_binary(Val) when is_atom(Val) -> atom_to_binary(Val, utf8);
2525
to_binary(Val) when is_integer(Val) -> integer_to_binary(Val);
2626
to_binary(Val) -> Val.
2727

28-
-spec to_list(Val :: integer() | list() | binary() | atom()) -> list().
28+
-spec to_list(Val :: integer() | list() | binary() | atom() | map()) -> list().
2929
to_list(Val) when is_list(Val) -> Val;
30+
to_list(Val) when is_map(Val) -> maps:to_list(Val);
3031
to_list(Val) when is_atom(Val) -> atom_to_list(Val);
3132
to_list(Val) when is_binary(Val) -> binary_to_list(Val);
3233
to_list(Val) when is_integer(Val) -> integer_to_list(Val).

src/rabbit_misc.erl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,15 @@ is_process_alive(Pid) ->
881881
lists:member(Node, [node() | nodes(connected)]) andalso
882882
rpc:call(Node, erlang, is_process_alive, [Pid]) =:= true.
883883

884-
-spec pget(term(), list()) -> term().
884+
-spec pget(term(), list() | map()) -> term().
885+
pget(K, M) when is_map(M) ->
886+
case maps:find(K, M) of
887+
{ok, V} ->
888+
V;
889+
_ ->
890+
undefined
891+
end;
892+
885893
pget(K, P) ->
886894
case lists:keyfind(K, 1, P) of
887895
{K, V} ->
@@ -890,7 +898,15 @@ pget(K, P) ->
890898
undefined
891899
end.
892900

893-
-spec pget(term(), list(), term()) -> term().
901+
-spec pget(term(), list() | map(), term()) -> term().
902+
pget(K, M, D) when is_map(M) ->
903+
case maps:find(K, M) of
904+
{ok, V} ->
905+
V;
906+
_ ->
907+
D
908+
end;
909+
894910
pget(K, P, D) ->
895911
case lists:keyfind(K, 1, P) of
896912
{K, V} ->
@@ -899,7 +915,13 @@ pget(K, P, D) ->
899915
D
900916
end.
901917

902-
-spec pget_or_die(term(), list()) -> term() | no_return().
918+
-spec pget_or_die(term(), list() | map()) -> term() | no_return().
919+
pget_or_die(K, M) when is_map(M) ->
920+
case maps:find(K, M) of
921+
error -> exit({error, key_missing, K});
922+
{ok, V} -> V
923+
end;
924+
903925
pget_or_die(K, P) ->
904926
case proplists:get_value(K, P) of
905927
undefined -> exit({error, key_missing, K});

test/unit_SUITE.erl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ groups() ->
3535
[
3636
{parallel_tests, [parallel], [
3737
data_coercion_to_proplist,
38+
data_coercion_to_list,
3839
data_coercion_to_map,
40+
pget,
3941
encrypt_decrypt,
4042
encrypt_decrypt_term,
4143
version_equivalence,
@@ -292,6 +294,17 @@ data_coercion_to_proplist(_Config) ->
292294
?assertEqual([{a, 1}], rabbit_data_coercion:to_proplist([{a, 1}])),
293295
?assertEqual([{a, 1}], rabbit_data_coercion:to_proplist(#{a => 1})).
294296

297+
data_coercion_to_list(_Config) ->
298+
?assertEqual([{a, 1}], rabbit_data_coercion:to_list([{a, 1}])),
299+
?assertEqual([{a, 1}], rabbit_data_coercion:to_list(#{a => 1})).
300+
301+
pget(_Config) ->
302+
?assertEqual(1, rabbit_misc:pget(a, [{a, 1}])),
303+
?assertEqual(undefined, rabbit_misc:pget(b, [{a, 1}])),
304+
305+
?assertEqual(1, rabbit_misc:pget(a, #{a => 1})),
306+
?assertEqual(undefined, rabbit_misc:pget(b, #{a => 1})).
307+
295308
pid_decompose_compose(_Config) ->
296309
Pid = self(),
297310
{Node, Cre, Id, Ser} = rabbit_misc:decompose_pid(Pid),

0 commit comments

Comments
 (0)