Skip to content

Commit

Permalink
Allow for extra unknown fields
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ansd committed Mar 28, 2024
1 parent 8a3f3c6 commit a59fdea
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions deps/rabbitmq_amqp_client/src/rabbitmq_amqp_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,8 @@ purge_or_delete_queue(LinkPair, QueueName, PathSuffix) ->
{ok, Resp} ->
case is_success(Resp) of
true ->
#'v1_0.amqp_value'{content = Content} = amqp10_msg:body(Resp),
{map, [
{{utf8, <<"message_count">>}, {ulong, Count}}
]
} = Content,
#'v1_0.amqp_value'{content = {map, KVList}} = amqp10_msg:body(Resp),
#{{utf8, <<"message_count">>} := {ulong, Count}} = maps:from_list(KVList),
{ok, #{message_count => Count}};
false ->
{error, Resp}
Expand Down Expand Up @@ -411,34 +408,45 @@ request(#link_pair{session = Session,
{ok, queue_info()}.
get_queue_info(Response) ->
#'v1_0.amqp_value'{content = {map, KVList}} = amqp10_msg:body(Response),
Map = lists:foldl(
fun({{utf8, Key = <<"arguments">>}, {map, KVList0}}, Acc) ->
Map = lists:foldl(fun({{utf8, K}, TypeVal}, M) ->
M#{K => TypeVal}
end, #{}, KVList0),
Acc#{to_atom(Key) => Map};
({{utf8, Key = <<"replicas">>}, {array, utf8, Arr}}, Acc) ->
L = lists:map(fun({utf8, Replica}) ->
Replica
end, Arr),
Acc#{to_atom(Key) => L};
({{utf8, Key}, TypeVal}, Acc) ->
Acc#{to_atom(Key) => amqp10_client_types:unpack(TypeVal)}
end, #{}, KVList),
RespMap = maps:from_list(KVList),

RequiredQInfo = [<<"name">>,
<<"vhost">>,
<<"durable">>,
<<"exclusive">>,
<<"auto_delete">>,
<<"type">>,
<<"message_count">>,
<<"consumer_count">>],
Map0 = lists:foldl(fun(Key, M) ->
{ok, TypeVal} = maps:find({utf8, Key}, RespMap),
M#{binary_to_atom(Key) => amqp10_client_types:unpack(TypeVal)}
end, #{}, RequiredQInfo),

{ok, {map, ArgsKVList}} = maps:find({utf8, <<"arguments">>}, RespMap),
ArgsMap = lists:foldl(fun({{utf8, K}, TypeVal}, M) ->
M#{K => TypeVal}
end, #{}, ArgsKVList),
Map1 = Map0#{arguments => ArgsMap},

Map2 = case maps:find({utf8, <<"replicas">>}, RespMap) of
{ok, {array, utf8, Arr}} ->
Replicas = lists:map(fun({utf8, Replica}) ->
Replica
end, Arr),
Map1#{replicas => Replicas};
error ->
Map1
end,

Map = case maps:find({utf8, <<"leader">>}, RespMap) of
{ok, {utf8, Leader}} ->
Map2#{leader => Leader};
error ->
Map2
end,
{ok, Map}.

to_atom(<<"name">>) -> name;
to_atom(<<"vhost">>) -> vhost;
to_atom(<<"durable">>) -> durable;
to_atom(<<"exclusive">>) -> exclusive;
to_atom(<<"auto_delete">>) -> auto_delete;
to_atom(<<"arguments">>) -> arguments;
to_atom(<<"type">>) -> type;
to_atom(<<"message_count">>) -> message_count;
to_atom(<<"consumer_count">>) -> consumer_count;
to_atom(<<"replicas">>) -> replicas;
to_atom(<<"leader">>) -> leader.

-spec encode_arguments(arguments()) ->
{map, list(tuple())}.
encode_arguments(Arguments) ->
Expand Down

0 comments on commit a59fdea

Please sign in to comment.