Skip to content

Commit 15dc086

Browse files
author
Daniil Fedotov
committed
Term to binary function to format stomp queue name
1 parent dd18cf3 commit 15dc086

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/term_to_binary_compat.erl

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
-include("rabbit.hrl").
2020

21-
-export([queue_name_to_binary/1]).
21+
-export([queue_name_to_binary/1, string_and_binary_tuple_2_to_binary/1]).
2222

2323
queue_name_to_binary(#resource{kind = queue} = {resource, VHost, queue, Name}) ->
2424
VHostBSize = byte_size(VHost),
@@ -30,3 +30,51 @@ queue_name_to_binary(#resource{kind = queue} = {resource, VHost, queue, Name}) -
3030
100, 0, 5, "queue", %% `queue` atom
3131
109, NameBSize:32, Name/binary>>. %% Name binary
3232

33+
string_and_binary_tuple_2_to_binary({StringOrBinary1, StringOrBinary2})
34+
when (is_list(StringOrBinary1) orelse is_binary(StringOrBinary1))
35+
andalso (is_list(StringOrBinary2) orelse is_binary(StringOrBinary2)) ->
36+
Binary1 = string_or_binary_to_binary(StringOrBinary1),
37+
Binary2 = string_or_binary_to_binary(StringOrBinary2),
38+
<<131, %% Binary format "version"
39+
104, 2, %% 2-element tuple
40+
Binary1/binary, %% first element
41+
Binary2/binary>>. %% second element
42+
43+
string_or_binary_to_binary(String) when is_list(String) ->
44+
%% length would fail on improper lists
45+
Len = length(String),
46+
case string_type(String) of
47+
empty -> <<106>>;
48+
short ->
49+
StringBin = list_to_binary(String),
50+
<<107,
51+
Len:16,
52+
StringBin/binary>>;
53+
long ->
54+
Bin = lists:foldl(
55+
fun(El, Acc) ->
56+
ElBin = format_integer(El),
57+
<<Acc/binary, ElBin/binary>>
58+
end,
59+
<<108, Len:32>>,
60+
String),
61+
<<Bin/binary, 106>>
62+
end;
63+
string_or_binary_to_binary(Binary) when is_binary(Binary) ->
64+
Size = byte_size(Binary),
65+
<<109, Size:32, Binary/binary>>.
66+
67+
string_type([]) -> empty;
68+
string_type(String) ->
69+
%% String length fit in 2 bytes
70+
case length(String) < 65535 andalso
71+
%% All characters are ASCII
72+
lists:all(fun(El) -> El < 256 end, String) of
73+
true -> short;
74+
false -> long
75+
end.
76+
77+
format_integer(Integer) when Integer < 256 ->
78+
<<97, Integer:8>>;
79+
format_integer(Integer) ->
80+
<<98, Integer:32>>.

test/term_to_binary_compat_prop_SUITE.erl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ all() ->
2727
%% The test should run on OTP < 20 (erts < 9)
2828
case erts_gt_8() of
2929
true ->
30-
[];
30+
[string_and_binary_tuple_2_to_binary];
3131
false ->
32-
[queue_name_to_binary]
32+
[queue_name_to_binary,
33+
string_and_binary_tuple_2_to_binary]
3334
end.
3435

3536
erts_gt_8() ->
@@ -47,6 +48,10 @@ end_per_suite(Config) ->
4748
init_per_testcase(Testcase, Config) ->
4849
rabbit_ct_helpers:testcase_started(Config, Testcase).
4950

51+
string_and_binary_tuple_2_to_binary(Config) ->
52+
Fun = fun() -> prop_string_and_binary_tuple_2_to_binary(Config) end,
53+
rabbit_ct_proper_helpers:run_proper(Fun, [], 10000).
54+
5055
queue_name_to_binary(Config) ->
5156
Fun = fun () -> prop_queue_name_to_binary(Config) end,
5257
rabbit_ct_proper_helpers:run_proper(Fun, [], 10000).
@@ -59,4 +64,13 @@ prop_queue_name_to_binary(_Config) ->
5964
Legacy = term_to_binary_compat:queue_name_to_binary(Resource),
6065
Current = term_to_binary(Resource),
6166
Current =:= Legacy
67+
end).
68+
69+
prop_string_and_binary_tuple_2_to_binary(_Config) ->
70+
?FORALL({First, Second}, {union([string(), binary()]), union([string(), binary()])},
71+
begin
72+
Tuple = {First, Second},
73+
Legacy = term_to_binary_compat:string_and_binary_tuple_2_to_binary(Tuple),
74+
Current = term_to_binary(Tuple),
75+
Current =:= Legacy
6276
end).

0 commit comments

Comments
 (0)