Skip to content

Commit 8f7a2a4

Browse files
committed
Get gets variadic paths
1 parent cbf1474 commit 8f7a2a4

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/main/java/io/rejson/Client.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.rejson;
22

3-
import io.rejson.Path;
4-
53
import redis.clients.jedis.Jedis;
64
import redis.clients.jedis.JedisPool;
75
import redis.clients.jedis.JedisPoolConfig;
@@ -103,16 +101,17 @@ public Long del(final String key, final Path path) throws Exception {
103101
/**
104102
* Gets an object
105103
* @param key the key name
106-
* @param path a path in the object
104+
* @param paths a path in the object
107105
* @return the requested object
108106
*/
109-
public Object get(final String key, final Path path) throws Exception {
110-
// TODO: need the variadic paths variant?
107+
public Object get(final String key, final Path... paths) throws Exception {
111108
Jedis conn = _conn();
112109
ArrayList<byte[]> args = new ArrayList(2);
113110

114111
args.add(SafeEncoder.encode(key));
115-
args.add(SafeEncoder.encode(path.toString()));
112+
for (Path p :paths) {
113+
args.add(SafeEncoder.encode(p.toString()));
114+
}
116115

117116
String rep = conn.getClient()
118117
.sendCommand(Command.GET, args.toArray(new byte[args.size()][]))

src/test/java/io/rejson/ClientTest.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.rejson;
22

3+
import com.google.gson.Gson;
4+
35
import org.junit.Before;
46
import org.junit.Test;
57

@@ -13,12 +15,25 @@ public FooBarObject() {
1315
}
1416
}
1517

18+
class IRLObject extends Object {
19+
public String str;
20+
public boolean bTrue;
21+
22+
public IRLObject() {
23+
this.str = "string";
24+
this.bTrue = true;
25+
}
26+
}
27+
28+
1629
public class ClientTest {
1730

1831
private Client c;
32+
private Gson g;
1933

2034
@Before
2135
public void initialize() {
36+
g = new Gson();
2237
c = new Client("localhost", 6379);
2338
}
2439

@@ -41,8 +56,23 @@ public void setException() throws Exception {
4156
@Test
4257
public void get() throws Exception {
4358
c._conn().flushDB();
44-
c.set("test", Path.RootPath(), "foo");
45-
assertEquals("foo", c.get("test", Path.RootPath()));
59+
60+
// check naive path
61+
c.set("str", Path.RootPath(), "foo");
62+
assertEquals("foo", c.get("str", Path.RootPath()));
63+
64+
// check multiple paths
65+
IRLObject irlObj = new IRLObject();
66+
c.set("irlobj", Path.RootPath(), irlObj);
67+
Object expected = g.fromJson(g.toJson(irlObj), Object.class);
68+
assertTrue(
69+
expected.equals(
70+
c.get("irlobj", new Path("bTrue"), new Path("str"))));
71+
72+
// check default root path
73+
assertTrue(
74+
expected.equals(
75+
c.get("irlobj")));
4676
}
4777

4878
@Test(expected = Exception.class)

0 commit comments

Comments
 (0)