Skip to content

Commit

Permalink
Term to binary function to format stomp queue name
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Fedotov committed Jun 13, 2017
1 parent dd18cf3 commit 15dc086
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
50 changes: 49 additions & 1 deletion src/term_to_binary_compat.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

-include("rabbit.hrl").

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

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

string_and_binary_tuple_2_to_binary({StringOrBinary1, StringOrBinary2})
when (is_list(StringOrBinary1) orelse is_binary(StringOrBinary1))
andalso (is_list(StringOrBinary2) orelse is_binary(StringOrBinary2)) ->
Binary1 = string_or_binary_to_binary(StringOrBinary1),
Binary2 = string_or_binary_to_binary(StringOrBinary2),
<<131, %% Binary format "version"
104, 2, %% 2-element tuple
Binary1/binary, %% first element
Binary2/binary>>. %% second element

string_or_binary_to_binary(String) when is_list(String) ->
%% length would fail on improper lists
Len = length(String),
case string_type(String) of
empty -> <<106>>;
short ->
StringBin = list_to_binary(String),
<<107,
Len:16,
StringBin/binary>>;
long ->
Bin = lists:foldl(
fun(El, Acc) ->
ElBin = format_integer(El),
<<Acc/binary, ElBin/binary>>
end,
<<108, Len:32>>,
String),
<<Bin/binary, 106>>
end;
string_or_binary_to_binary(Binary) when is_binary(Binary) ->
Size = byte_size(Binary),
<<109, Size:32, Binary/binary>>.

string_type([]) -> empty;
string_type(String) ->
%% String length fit in 2 bytes
case length(String) < 65535 andalso
%% All characters are ASCII
lists:all(fun(El) -> El < 256 end, String) of
true -> short;
false -> long
end.

format_integer(Integer) when Integer < 256 ->
<<97, Integer:8>>;
format_integer(Integer) ->
<<98, Integer:32>>.
18 changes: 16 additions & 2 deletions test/term_to_binary_compat_prop_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ all() ->
%% The test should run on OTP < 20 (erts < 9)
case erts_gt_8() of
true ->
[];
[string_and_binary_tuple_2_to_binary];
false ->
[queue_name_to_binary]
[queue_name_to_binary,
string_and_binary_tuple_2_to_binary]
end.

erts_gt_8() ->
Expand All @@ -47,6 +48,10 @@ end_per_suite(Config) ->
init_per_testcase(Testcase, Config) ->
rabbit_ct_helpers:testcase_started(Config, Testcase).

string_and_binary_tuple_2_to_binary(Config) ->
Fun = fun() -> prop_string_and_binary_tuple_2_to_binary(Config) end,
rabbit_ct_proper_helpers:run_proper(Fun, [], 10000).

queue_name_to_binary(Config) ->
Fun = fun () -> prop_queue_name_to_binary(Config) end,
rabbit_ct_proper_helpers:run_proper(Fun, [], 10000).
Expand All @@ -59,4 +64,13 @@ prop_queue_name_to_binary(_Config) ->
Legacy = term_to_binary_compat:queue_name_to_binary(Resource),
Current = term_to_binary(Resource),
Current =:= Legacy
end).

prop_string_and_binary_tuple_2_to_binary(_Config) ->
?FORALL({First, Second}, {union([string(), binary()]), union([string(), binary()])},
begin
Tuple = {First, Second},
Legacy = term_to_binary_compat:string_and_binary_tuple_2_to_binary(Tuple),
Current = term_to_binary(Tuple),
Current =:= Legacy
end).

0 comments on commit 15dc086

Please sign in to comment.