Skip to content

Commit

Permalink
Fix sending lists of data inside the argument list
Browse files Browse the repository at this point in the history
Since we can't do varargs in Erlang, we use lists:
  send(Cxn, Key, [Value1, Value2, Value3])
But sometimes we want to be lazy and just do:
  send(Cxn, Key, "set a value")

This patch allows both cases to co-exist.  Instead of (as
previously) crashing, we convert the inner values to
binaries then return the converted list.  At the end, the deep
list of binaries is flattened to perform redis protocol
calculations and construction.
  • Loading branch information
mattsta committed May 28, 2010
1 parent ab22e63 commit 435d936
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/erldis.erl
Expand Up @@ -346,11 +346,12 @@ exec(Client, Fun) ->
multibulk_cmd(Args) when is_binary(Args) ->
multibulk_cmd([Args]);
multibulk_cmd(Args) when is_list(Args) ->
TotalLength = length(Args),

ConvertedArgs = lists:flatten([erldis_binaries:to_binary(B) || B <- Args]),
TotalLength = length(ConvertedArgs),

ArgCount = [<<"*">>, ?i2l(TotalLength), <<"\r\n">>],
ArgBin = [[<<"$">>, ?i2l(iolist_size(A)), <<"\r\n">>,
A, <<"\r\n">>] || A <- [erldis_binaries:to_binary(B) || B <- Args]],
ArgBin = [[<<"$">>, ?i2l(iolist_size(A)), <<"\r\n">>, A, <<"\r\n">>]
|| A <- ConvertedArgs],

[ArgCount, ArgBin].

Expand Down
5 changes: 4 additions & 1 deletion src/erldis_binaries.erl
Expand Up @@ -3,6 +3,9 @@
-export([to_binary/1]).

to_binary(X) when is_binary(X) -> X;
to_binary(X) when is_list(X) -> list_to_binary(X);
% Basic determination of a char list: "abc"
to_binary(X) when is_list(X) andalso is_integer(hd(X)) -> list_to_binary(X);
% Basic determination of a list of stuff: [abc, def, <<"other">>, 12]
to_binary(X) when is_list(X) -> [to_binary(A) || A <- X];
to_binary(X) when is_atom(X) -> list_to_binary(atom_to_list(X));
to_binary(X) when is_integer(X) -> list_to_binary(integer_to_list(X)).

0 comments on commit 435d936

Please sign in to comment.