Skip to content

Commit

Permalink
Bind byte arrays nested in an Iterable argument #627
Browse files Browse the repository at this point in the history
Byte arrays passed within an Iterable are now unrolled and bound correctly. Previously, byte arrays were not bound and silently dropped.
  • Loading branch information
mp911de committed Oct 12, 2017
1 parent e344a0a commit c1774ea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/io/lettuce/core/dynamic/ParameterBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,15 @@ private <K, V> void bind(CommandArgs<K, V> args, RedisCodec<K, V> codec, Object
}

if (argument instanceof Iterable) {

for (Object argumentElement : (Iterable<Object>) argument) {
bindArgument(args, argumentElement);
}
} else {
bindArgument(args, argument);

return;
}

bindArgument(args, argument);
}

/*
Expand All @@ -151,14 +154,22 @@ private <K, V> void bind(CommandArgs<K, V> args, RedisCodec<K, V> codec, Object
@SuppressWarnings("unchecked")
private static <K, V> void bindArgument(CommandArgs<K, V> args, Object argument) {

if (argument instanceof byte[]) {
args.add((byte[]) argument);
return;
}

if (argument instanceof String) {
args.add((String) argument);
return;
}

if (argument instanceof Double) {
args.add(((Double) argument));
return;
} else if (argument instanceof Number) {
args.add(((Number) argument).longValue());
return;
}

if (argument instanceof ProtocolKeyword) {
Expand Down Expand Up @@ -229,7 +240,10 @@ private static <K, V> void bindArgument(CommandArgs<K, V> args, Object argument)

if (argument instanceof CompositeArgument) {
((CompositeArgument) argument).build(args);
return;
}

throw new IllegalArgumentException("Cannot bind unsupported command argument " + args);
}

private static <K, V> void bindValueRange(CommandArgs<K, V> args, RedisCodec<K, V> codec, Range<? extends V> range) {
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/io/lettuce/core/dynamic/RedisCommandsSyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -79,8 +80,23 @@ public void mgetAsValues() {
assertThat(values.get(1)).isEqualTo(Value.empty());
}

@Test
public void mgetByteArray() {

redis.set(key, value);

RedisCommandFactory factory = new RedisCommandFactory(redis.getStatefulConnection());

MultipleExecutionModels api = factory.getCommands(MultipleExecutionModels.class);

List<byte[]> values = api.mget(Collections.singleton(key.getBytes()));
assertThat(values).hasSize(1).contains(value.getBytes());
}

interface MultipleExecutionModels extends Commands {

List<byte[]> mget(Iterable<byte[]> keys);

String get(String key);

default byte[] getAsBytes() {
Expand Down

0 comments on commit c1774ea

Please sign in to comment.