Skip to content

Commit 01e4583

Browse files
Merge pull request #10567 from rabbitmq/amqp-boolean
Parse 2 bytes encoded AMQP boolean to Erlang boolean
2 parents b8173c9 + 8b151f4 commit 01e4583

File tree

10 files changed

+47
-32
lines changed

10 files changed

+47
-32
lines changed

deps/amqp10_common/src/amqp10_binary_parser.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ parse_primitive(16#52, <<V:8/unsigned, R/binary>>) -> {{uint, V}, R};
5252
parse_primitive(16#53, <<V:8/unsigned, R/binary>>) -> {{ulong, V}, R};
5353
parse_primitive(16#54, <<V:8/signed, R/binary>>) -> {{int, V}, R};
5454
parse_primitive(16#55, <<V:8/signed, R/binary>>) -> {{long, V}, R};
55-
parse_primitive(16#56, <<0:8/unsigned, R/binary>>) -> {{boolean, false},R};
56-
parse_primitive(16#56, <<1:8/unsigned, R/binary>>) -> {{boolean, true}, R};
55+
parse_primitive(16#56, <<0:8/unsigned, R/binary>>) -> {false, R};
56+
parse_primitive(16#56, <<1:8/unsigned, R/binary>>) -> {true, R};
5757
parse_primitive(16#60, <<V:16/unsigned, R/binary>>) -> {{ushort, V}, R};
5858
parse_primitive(16#61, <<V:16/signed, R/binary>>) -> {{short, V}, R};
5959
parse_primitive(16#70, <<V:32/unsigned, R/binary>>) -> {{uint, V}, R};
@@ -224,8 +224,8 @@ parse_all(<<16#52, V:8/unsigned, R/binary>>) -> [{uint, V} | parse_all(R)];
224224
parse_all(<<16#53, V:8/unsigned, R/binary>>) -> [{ulong, V} | parse_all(R)];
225225
parse_all(<<16#54, V:8/signed, R/binary>>) -> [{int, V} | parse_all(R)];
226226
parse_all(<<16#55, V:8/signed, R/binary>>) -> [{long, V} | parse_all(R)];
227-
parse_all(<<16#56, 0:8/unsigned, R/binary>>) -> [{boolean, false} | parse_all(R)];
228-
parse_all(<<16#56, 1:8/unsigned, R/binary>>) -> [{boolean, true} | parse_all(R)];
227+
parse_all(<<16#56, 0:8/unsigned, R/binary>>) -> [false | parse_all(R)];
228+
parse_all(<<16#56, 1:8/unsigned, R/binary>>) -> [true | parse_all(R)];
229229
parse_all(<<16#60, V:16/unsigned, R/binary>>) -> [{ushort, V} | parse_all(R)];
230230
parse_all(<<16#61, V:16/signed, R/binary>>) -> [{short, V} | parse_all(R)];
231231
parse_all(<<16#70, V:32/unsigned, R/binary>>) -> [{uint, V} | parse_all(R)];

deps/amqp10_common/test/binary_generator_SUITE.erl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ null(_Config) ->
6767
booleans(_Config) ->
6868
roundtrip(true),
6969
roundtrip(false),
70-
roundtrip({boolean, false}),
71-
roundtrip({boolean, true}),
72-
ok.
70+
?assertEqual(true, roundtrip_return({boolean, true})),
71+
?assertEqual(false, roundtrip_return({boolean, false})).
7372

7473
symbol(_Config) ->
7574
roundtrip({symbol, <<"SYMB">>}),
@@ -164,7 +163,11 @@ array(_Config) ->
164163
roundtrip({array, ulong, [{ulong, 0}, {ulong, 16#FFFFFFFFFFFFFFFF}]}),
165164
roundtrip({array, long, [{long, 0}, {long, -16#8000000000000},
166165
{long, 16#7FFFFFFFFFFFFF}]}),
167-
roundtrip({array, boolean, [{boolean, true}, {boolean, false}]}),
166+
roundtrip({array, boolean, [true, false]}),
167+
168+
?assertEqual({array, boolean, [true, false]},
169+
roundtrip_return({array, boolean, [{boolean, true}, {boolean, false}]})),
170+
168171
% array of arrays
169172
% TODO: does the inner type need to be consistent across the array?
170173
roundtrip({array, array, []}),
@@ -175,13 +178,22 @@ array(_Config) ->
175178
[{described, Desc, {utf8, <<"http://example.org/hello">>}}]}),
176179
roundtrip({array, {described, Desc, utf8}, []}),
177180
%% array:array32
178-
roundtrip({array, boolean, [{boolean, true} || _ <- lists:seq(1, 256)]}),
181+
roundtrip({array, boolean, [true || _ <- lists:seq(1, 256)]}),
179182
ok.
180183

181184
%% Utility
182185

183186
roundtrip(Term) ->
184187
Bin = iolist_to_binary(amqp10_binary_generator:generate(Term)),
185188
% generate returns an iolist but parse expects a binary
186-
?assertMatch({Term, _}, amqp10_binary_parser:parse(Bin)),
187-
?assertMatch([Term | _], amqp10_binary_parser:parse_all(Bin)).
189+
?assertEqual({Term, <<>>}, amqp10_binary_parser:parse(Bin)),
190+
?assertEqual([Term], amqp10_binary_parser:parse_all(Bin)).
191+
192+
%% Return the roundtripped term.
193+
roundtrip_return(Term) ->
194+
Bin = iolist_to_binary(amqp10_binary_generator:generate(Term)),
195+
%% We assert only that amqp10_binary_parser:parse/1 and
196+
%% amqp10_binary_parser:parse_all/1 return the same term.
197+
{RoundTripTerm, <<>>} = amqp10_binary_parser:parse(Bin),
198+
?assertEqual([RoundTripTerm], amqp10_binary_parser:parse_all(Bin)),
199+
RoundTripTerm.

deps/amqp10_common/test/binary_parser_SUITE.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ roundtrip(_Config) ->
6161
{symbol, <<"URL">>},
6262
{binary, <<"https://rabbitmq.com">>}},
6363
{array, ubyte, [{ubyte, 1}, {ubyte, 255}]},
64-
{boolean, false},
64+
true,
6565
{list, [{utf8, <<"hi">>},
6666
{described,
6767
{symbol, <<"URL">>},

deps/rabbit/src/mc_amqp.erl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,9 @@ application_properties_as_simple_map(#msg{application_properties = Content},
313313
({{utf8, K}, {_T, V}}, Acc)
314314
when ?SIMPLE_VALUE(V) ->
315315
Acc#{K => V};
316-
({{utf8, K}, undefined}, Acc) ->
317-
Acc#{K => undefined};
316+
({{utf8, K}, V}, Acc)
317+
when V =:= undefined orelse is_boolean(V) ->
318+
Acc#{K => V};
318319
(_, Acc)->
319320
Acc
320321
end, M, Content).

deps/rabbit/test/mc_unit_SUITE.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ amqpl_amqp_bin_amqpl(_Config) ->
333333

334334
?assertEqual({long, 99}, Get(<<"a-stream-offset">>, AP10)),
335335
?assertEqual({utf8, <<"a string">>}, Get(<<"a-string">>, AP10)),
336-
?assertEqual({boolean, false}, Get(<<"a-bool">>, AP10)),
336+
?assertEqual(false, Get(<<"a-bool">>, AP10)),
337337
?assertEqual({ubyte, 1}, Get(<<"a-unsignedbyte">>, AP10)),
338338
?assertEqual({ushort, 1}, Get(<<"a-unsignedshort">>, AP10)),
339339
?assertEqual({uint, 1}, Get(<<"a-unsignedint">>, AP10)),

deps/rabbitmq_amqp1_0/src/rabbit_amqp1_0_message.erl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,20 @@ amqp10_app_props_to_amqp091_headers(CurrentHeaders, AppPropsBin) ->
322322
end,
323323
lists:foldl(fun(Prop, Acc) ->
324324
case Prop of
325-
{{utf8, Key}, {ValueType, Value}} ->
326-
case type10_to_type091(Key, ValueType, Value) of
325+
{{utf8, Key}, TypeVal} ->
326+
case type10_to_type091(Key, TypeVal) of
327327
undefined -> Acc;
328328
Typed -> [Typed |Acc]
329329
end;
330-
_ -> Acc
330+
_ ->
331+
Acc
331332
end
332333
end, Hs, AppProps);
333334
_ -> CurrentHeaders
334335
end.
335-
type10_to_type091(Key, Type, Value) ->
336+
type10_to_type091(Key, TypeVal) ->
336337
try
337-
rabbit_msg_record:to_091(Key, {Type, Value})
338+
rabbit_msg_record:to_091(Key, TypeVal)
338339
catch
339340
_:function_clause -> undefined
340341
end.

deps/rabbitmq_amqp1_0/test/amqp10_client_SUITE.erl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ reliable_send_receive(Config, Outcome) ->
128128
%% create an unsettled message,
129129
%% link will be in "mixed" mode by default
130130
Msg1 = amqp10_msg:new(DTag1, <<"body-1">>, false),
131-
ok = amqp10_client:send_msg(Sender, Msg1),
131+
%% Use the 2 byte AMQP boolean encoding, see AMQP §1.6.2
132+
True = {boolean, true},
133+
Msg2 = amqp10_msg:set_headers(#{durable => True}, Msg1),
134+
ok = amqp10_client:send_msg(Sender, Msg2),
132135
ok = wait_for_settlement(DTag1),
133136

134137
ok = amqp10_client:detach_link(Sender),
@@ -143,8 +146,8 @@ reliable_send_receive(Config, Outcome) ->
143146
Address,
144147
unsettled),
145148
{ok, Msg} = amqp10_client:get_msg(Receiver),
146-
147149
ct:pal("got ~p", [amqp10_msg:body(Msg)]),
150+
?assertEqual(true, amqp10_msg:header(durable, Msg)),
148151

149152
ok = amqp10_client:settle_msg(Receiver, Msg, Outcome),
150153

deps/rabbitmq_mqtt/src/mc_mqtt.erl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,13 +542,9 @@ amqp_to_utf8_string({T, Val})
542542
when T =:= double;
543543
T =:= float ->
544544
float_to_binary(Val);
545-
amqp_to_utf8_string(Val)
546-
when Val =:= true;
547-
Val =:= {boolean, true} ->
545+
amqp_to_utf8_string(true) ->
548546
<<"true">>;
549-
amqp_to_utf8_string(Val)
550-
when Val =:= false;
551-
Val =:= {boolean, false} ->
547+
amqp_to_utf8_string(false) ->
552548
<<"false">>;
553549
amqp_to_utf8_string({T, _Val})
554550
when T =:= map;

deps/rabbitmq_mqtt/test/mc_mqtt_SUITE.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ amqp_to_mqtt_amqp_value_section_boolean(_Config) ->
219219
?assertEqual(#{'Payload-Format-Indicator' => 1}, Props1),
220220
?assertEqual(<<"true">>, iolist_to_binary(Payload1)),
221221

222-
Val2 = amqp_value({boolean, false}),
222+
Val2 = amqp_value(false),
223223
#mqtt_msg{props = Props2,
224224
payload = Payload2} = amqp_to_mqtt([Val2]),
225225
?assertEqual(#{'Payload-Format-Indicator' => 1}, Props2),
@@ -460,7 +460,7 @@ amqp_mqtt(_Config) ->
460460
thead(float, 6.0),
461461
thead(timestamp, 7000),
462462
thead(byte, 128),
463-
thead(boolean, true),
463+
{{utf8, <<"boolean1">>}, true},
464464
{{utf8, <<"boolean2">>}, false},
465465
{utf8(<<"null">>), null}
466466
],
@@ -493,7 +493,7 @@ amqp_mqtt(_Config) ->
493493
<<"6.00000000000000000000e+00">>},
494494
{<<"timestamp">>,<<"7">>},
495495
{<<"byte">>,<<"128">>},
496-
{<<"boolean">>,<<"true">>},
496+
{<<"boolean1">>,<<"true">>},
497497
{<<"boolean2">>,<<"false">>},
498498
{<<"null">>,<<>>}],
499499
'Correlation-Data' := CorrIdOut

deps/rabbitmq_mqtt/test/protocol_interop_SUITE.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ amqp(Config) ->
220220
#{correlation_id => Correlation,
221221
content_type => ContentType},
222222
Msg2a),
223-
Msg2 = amqp10_msg:set_headers(#{durable => true}, Msg2b),
223+
%% Use the 2 byte AMQP boolean encoding, see AMQP §1.6.2
224+
True = {boolean, true},
225+
Msg2 = amqp10_msg:set_headers(#{durable => True}, Msg2b),
224226
ok = amqp10_client:send_msg(Sender, Msg2),
225227
receive {amqp10_disposition, {accepted, DTag}} -> ok
226228
after 1000 -> ct:fail(settled_timeout)

0 commit comments

Comments
 (0)