Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/com/redislabs/modules/rejson/JReJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ private enum Command implements ProtocolCommand {
ARRLEN("JSON.ARRLEN"),
ARRPOP("JSON.ARRPOP"),
ARRTRIM("JSON.ARRTRIM"),
CLEAR("JSON.CLEAR");
CLEAR("JSON.CLEAR"),
TOGGLE("JSON.TOGGLE");

private final byte[] raw;

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

public void toggle(String key, Path path) {
try (Jedis jedis = getConnection()) {
jedis.getClient().sendCommand(Command.TOGGLE, key, path.toString());
assertReplyOK(jedis.getClient().getStatusCodeReply());
}
}

/**
* Gets the class of an object at the root path
* @param key the key name
Expand Down
37 changes: 33 additions & 4 deletions src/test/java/com/redislabs/modules/rejson/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public class ClientTest {
@SuppressWarnings("unused")
private static class IRLObject {
public String str;
public boolean bTrue;
public boolean bool;

public IRLObject() {
this.str = "string";
this.bTrue = true;
this.bool = true;
}
}

Expand Down Expand Up @@ -161,7 +161,7 @@ public void noArgsConstructorReturnsClientToLocalMachine() {
public void basicSetGetShouldSucceed() {

// naive set with a path
client.set("null", null, Path.ROOT_PATH);
client.set("null", null, Path.ROOT_PATH);
assertNull(client.get("null", String.class, Path.ROOT_PATH));

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

}

@Test
public void toggle() {

IRLObject obj = new IRLObject();
client.set( "obj", obj);

Path pbool = Path.of(".bool");
// check initial value
assertTrue(client.get("obj", Boolean.class, pbool));

// true -> false
client.toggle("obj", pbool);
assertFalse(client.get("obj", Boolean.class, pbool));

// false -> true
client.toggle("obj", pbool);
assertTrue(client.get("obj", Boolean.class, pbool));

// ignore non-boolean field
Path pstr = Path.of(".str");
try {
client.toggle("obj", pstr);
fail("Path not a bool");
} catch (JedisDataException jde) {
assertTrue(jde.getMessage().contains("not a bool"));
}
assertEquals("string", client.get("obj", String.class, pstr));
}

@Test(expected = JedisDataException.class)
public void getException() {
client.set( "test", "foo", Path.ROOT_PATH);
Expand Down