Added support for nested lists, and longs in lists.

This commit is contained in:
Daniel Josefsson
2013-11-08 22:25:37 +00:00
parent 597366343d
commit 17f6ee63dc
2 changed files with 28 additions and 10 deletions

View File

@@ -2783,17 +2783,18 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand
}
private Object getEvalResult() {
Object result = client.getOne();
return evalResult(client.getOne());
}
private Object evalResult(Object result) {
if (result instanceof byte[])
return SafeEncoder.encode((byte[]) result);
if (result instanceof List<?>) {
List<?> list = (List<?>) result;
List<String> listResult = new ArrayList<String>(list.size());
List<Object> listResult = new ArrayList<Object>(list.size());
for (Object bin : list) {
listResult.add((bin == null ? null : SafeEncoder
.encode((byte[]) bin)));
listResult.add(evalResult(bin));
}
return listResult;

View File

@@ -1,12 +1,17 @@
package redis.clients.jedis.tests.commands;
import org.hamcrest.CoreMatchers;
import org.hamcrest.core.CombinableMatcher;
import org.junit.Test;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.util.SafeEncoder;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.util.SafeEncoder;
import static org.hamcrest.CoreMatchers.both;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItem;
public class ScriptingCommandsTest extends JedisCommandTestBase {
@@ -57,6 +62,15 @@ public class ScriptingCommandsTest extends JedisCommandTestBase {
assertEquals(new Long(2), response);
}
@Test
public void evalNestedLists() {
String script = "return { {KEYS[1]} , {2} }";
List<?> results = (List<?>) jedis.eval(script, 1, "key1");
assertThat((List<String>) results.get(0), listWithItem("key1"));
assertThat((List<Long>) results.get(1), listWithItem(2L));
}
@Test
public void evalNoArgs() {
String script = "return KEYS[1]";
@@ -156,7 +170,10 @@ public class ScriptingCommandsTest extends JedisCommandTestBase {
assertEquals(results.get(1), "key2");
assertEquals(results.get(2), "1");
assertEquals(results.get(3), "2");
}
private <T> CombinableMatcher<List<T>> listWithItem(T expected) {
return both(CoreMatchers.<List<T>>instanceOf(List.class)).and(hasItem(equalTo(expected)));
}
}