Skip to content

Commit a59fdea

Browse files
committed
Allow for extra unknown fields
Apply the following PR feedback: > The data returned is strongly validated; there can't be an extra unknown field. > I would suggest ignoring field names that are not known as this allows for better > extensibility. On the other hand, there should be a check that all required fields > are found.
1 parent 8a3f3c6 commit a59fdea

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

deps/rabbitmq_amqp_client/src/rabbitmq_amqp_client.erl

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,8 @@ purge_or_delete_queue(LinkPair, QueueName, PathSuffix) ->
321321
{ok, Resp} ->
322322
case is_success(Resp) of
323323
true ->
324-
#'v1_0.amqp_value'{content = Content} = amqp10_msg:body(Resp),
325-
{map, [
326-
{{utf8, <<"message_count">>}, {ulong, Count}}
327-
]
328-
} = Content,
324+
#'v1_0.amqp_value'{content = {map, KVList}} = amqp10_msg:body(Resp),
325+
#{{utf8, <<"message_count">>} := {ulong, Count}} = maps:from_list(KVList),
329326
{ok, #{message_count => Count}};
330327
false ->
331328
{error, Resp}
@@ -411,34 +408,45 @@ request(#link_pair{session = Session,
411408
{ok, queue_info()}.
412409
get_queue_info(Response) ->
413410
#'v1_0.amqp_value'{content = {map, KVList}} = amqp10_msg:body(Response),
414-
Map = lists:foldl(
415-
fun({{utf8, Key = <<"arguments">>}, {map, KVList0}}, Acc) ->
416-
Map = lists:foldl(fun({{utf8, K}, TypeVal}, M) ->
417-
M#{K => TypeVal}
418-
end, #{}, KVList0),
419-
Acc#{to_atom(Key) => Map};
420-
({{utf8, Key = <<"replicas">>}, {array, utf8, Arr}}, Acc) ->
421-
L = lists:map(fun({utf8, Replica}) ->
422-
Replica
423-
end, Arr),
424-
Acc#{to_atom(Key) => L};
425-
({{utf8, Key}, TypeVal}, Acc) ->
426-
Acc#{to_atom(Key) => amqp10_client_types:unpack(TypeVal)}
427-
end, #{}, KVList),
411+
RespMap = maps:from_list(KVList),
412+
413+
RequiredQInfo = [<<"name">>,
414+
<<"vhost">>,
415+
<<"durable">>,
416+
<<"exclusive">>,
417+
<<"auto_delete">>,
418+
<<"type">>,
419+
<<"message_count">>,
420+
<<"consumer_count">>],
421+
Map0 = lists:foldl(fun(Key, M) ->
422+
{ok, TypeVal} = maps:find({utf8, Key}, RespMap),
423+
M#{binary_to_atom(Key) => amqp10_client_types:unpack(TypeVal)}
424+
end, #{}, RequiredQInfo),
425+
426+
{ok, {map, ArgsKVList}} = maps:find({utf8, <<"arguments">>}, RespMap),
427+
ArgsMap = lists:foldl(fun({{utf8, K}, TypeVal}, M) ->
428+
M#{K => TypeVal}
429+
end, #{}, ArgsKVList),
430+
Map1 = Map0#{arguments => ArgsMap},
431+
432+
Map2 = case maps:find({utf8, <<"replicas">>}, RespMap) of
433+
{ok, {array, utf8, Arr}} ->
434+
Replicas = lists:map(fun({utf8, Replica}) ->
435+
Replica
436+
end, Arr),
437+
Map1#{replicas => Replicas};
438+
error ->
439+
Map1
440+
end,
441+
442+
Map = case maps:find({utf8, <<"leader">>}, RespMap) of
443+
{ok, {utf8, Leader}} ->
444+
Map2#{leader => Leader};
445+
error ->
446+
Map2
447+
end,
428448
{ok, Map}.
429449

430-
to_atom(<<"name">>) -> name;
431-
to_atom(<<"vhost">>) -> vhost;
432-
to_atom(<<"durable">>) -> durable;
433-
to_atom(<<"exclusive">>) -> exclusive;
434-
to_atom(<<"auto_delete">>) -> auto_delete;
435-
to_atom(<<"arguments">>) -> arguments;
436-
to_atom(<<"type">>) -> type;
437-
to_atom(<<"message_count">>) -> message_count;
438-
to_atom(<<"consumer_count">>) -> consumer_count;
439-
to_atom(<<"replicas">>) -> replicas;
440-
to_atom(<<"leader">>) -> leader.
441-
442450
-spec encode_arguments(arguments()) ->
443451
{map, list(tuple())}.
444452
encode_arguments(Arguments) ->

0 commit comments

Comments
 (0)