Skip to content

Commit 0189a89

Browse files
authored
Support JSON.TOGGLE command (#51)
* Support JSON.TOGGLE command * Error thrown for non-boolean field
1 parent 336cc5a commit 0189a89

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/main/java/com/redislabs/modules/rejson/JReJSON.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ private enum Command implements ProtocolCommand {
6464
ARRLEN("JSON.ARRLEN"),
6565
ARRPOP("JSON.ARRPOP"),
6666
ARRTRIM("JSON.ARRTRIM"),
67-
CLEAR("JSON.CLEAR");
67+
CLEAR("JSON.CLEAR"),
68+
TOGGLE("JSON.TOGGLE");
6869

6970
private final byte[] raw;
7071

@@ -329,6 +330,13 @@ public void set(String key, Object object, ExistenceModifier flag, Path path) {
329330
assertReplyOK(status);
330331
}
331332

333+
public void toggle(String key, Path path) {
334+
try (Jedis jedis = getConnection()) {
335+
jedis.getClient().sendCommand(Command.TOGGLE, key, path.toString());
336+
assertReplyOK(jedis.getClient().getStatusCodeReply());
337+
}
338+
}
339+
332340
/**
333341
* Gets the class of an object at the root path
334342
* @param key the key name

src/test/java/com/redislabs/modules/rejson/ClientTest.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public class ClientTest {
5656
@SuppressWarnings("unused")
5757
private static class IRLObject {
5858
public String str;
59-
public boolean bTrue;
59+
public boolean bool;
6060

6161
public IRLObject() {
6262
this.str = "string";
63-
this.bTrue = true;
63+
this.bool = true;
6464
}
6565
}
6666

@@ -161,7 +161,7 @@ public void noArgsConstructorReturnsClientToLocalMachine() {
161161
public void basicSetGetShouldSucceed() {
162162

163163
// naive set with a path
164-
client.set("null", null, Path.ROOT_PATH);
164+
client.set("null", null, Path.ROOT_PATH);
165165
assertNull(client.get("null", String.class, Path.ROOT_PATH));
166166

167167
// real scalar value and no path
@@ -229,10 +229,39 @@ public void getMultiplePathsShouldSucceed() {
229229
IRLObject obj = new IRLObject();
230230
client.set( "obj", obj);
231231
Object expected = g.fromJson(g.toJson(obj), Object.class);
232-
assertTrue(expected.equals(client.get( "obj", Object.class, Path.of("bTrue"), Path.of("str"))));
232+
assertTrue(expected.equals(client.get( "obj", Object.class, Path.of("bool"), Path.of("str"))));
233233

234234
}
235235

236+
@Test
237+
public void toggle() {
238+
239+
IRLObject obj = new IRLObject();
240+
client.set( "obj", obj);
241+
242+
Path pbool = Path.of(".bool");
243+
// check initial value
244+
assertTrue(client.get("obj", Boolean.class, pbool));
245+
246+
// true -> false
247+
client.toggle("obj", pbool);
248+
assertFalse(client.get("obj", Boolean.class, pbool));
249+
250+
// false -> true
251+
client.toggle("obj", pbool);
252+
assertTrue(client.get("obj", Boolean.class, pbool));
253+
254+
// ignore non-boolean field
255+
Path pstr = Path.of(".str");
256+
try {
257+
client.toggle("obj", pstr);
258+
fail("Path not a bool");
259+
} catch (JedisDataException jde) {
260+
assertTrue(jde.getMessage().contains("not a bool"));
261+
}
262+
assertEquals("string", client.get("obj", String.class, pstr));
263+
}
264+
236265
@Test(expected = JedisDataException.class)
237266
public void getException() {
238267
client.set( "test", "foo", Path.ROOT_PATH);

0 commit comments

Comments
 (0)