Skip to content

Commit

Permalink
Merge pull request #31886 from cescoffier/missing-json-set-method-in-…
Browse files Browse the repository at this point in the history
…json-tx

Add missing default methods in the JSON Transactional commands
  • Loading branch information
gsmet authored Mar 16, 2023
2 parents 44a2be0 + 0909984 commit c6d7ce9
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ public interface ReactiveTransactionalJsonCommands<K> extends ReactiveTransactio
*/
<T> Uni<Void> jsonSet(K key, String path, T value);

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
* Group: json
*
* @param key the key, must not be {@code null}
* @param value the value, encoded to JSON
* @param <T> the type for the value
* @return A {@code Uni} emitting {@code null} when the command has been enqueued successfully in the transaction, a failure
* otherwise. In the case of failure, the transaction is discarded.
**/
default <T> Uni<Void> jsonSet(K key, T value) {
return jsonSet(key, "$", value);
}

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
Expand Down Expand Up @@ -48,6 +63,22 @@ public interface ReactiveTransactionalJsonCommands<K> extends ReactiveTransactio
*/
Uni<Void> jsonSet(K key, String path, JsonObject json, JsonSetArgs args);

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
* Group: json
* <p>
* This variant uses {@code $} as path.
*
* @param key the key, must not be {@code null}
* @param json the JSON object to store, must not be {@code null}
* @return A {@code Uni} emitting {@code null} when the command has been enqueued successfully in the transaction, a failure
* otherwise. In the case of failure, the transaction is discarded.
**/
default Uni<Void> jsonSet(K key, JsonObject json) {
return jsonSet(key, "$", json);
}

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
Expand All @@ -61,6 +92,20 @@ public interface ReactiveTransactionalJsonCommands<K> extends ReactiveTransactio
*/
Uni<Void> jsonSet(K key, String path, JsonArray json);

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
* Group: json
*
* @param key the key, must not be {@code null}
* @param json the JSON array to store, must not be {@code null}
* @return A {@code Uni} emitting {@code null} when the command has been enqueued successfully in the transaction, a failure
* otherwise. In the case of failure, the transaction is discarded.
**/
default Uni<Void> jsonSet(K key, JsonArray json) {
return jsonSet(key, "$", json);
}

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
Expand Down Expand Up @@ -255,6 +300,22 @@ default <T> Uni<Void> jsonArrIndex(K key, String path, T value) {
*/
<T> Uni<Void> jsonArrPop(K key, Class<T> clazz, String path, int index);

/**
* Execute the command <a href="https://redis.io/commands/json.arrpop/">JSON.ARRPOP</a>.
* Summary: Removes and returns an element from the index in the array.
* Group: json
* <p>
*
* @param key the key, must not be {@code null}
* @param clazz the type of the popped object
* @param path path the path, defaults to root if not provided.
* @return A {@code Uni} emitting {@code null} when the command has been enqueued successfully in the transaction, a failure
* otherwise. In the case of failure, the transaction is discarded.
**/
default <T> Uni<Void> jsonArrPop(K key, Class<T> clazz, String path) {
return jsonArrPop(key, clazz, path, -1);
}

/**
* Execute the command <a href="https://redis.io/commands/json.arrtrim/">JSON.ARRTRIM</a>.
* Summary: Trims an array so that it contains only the specified inclusive range of elements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public interface TransactionalJsonCommands<K> extends TransactionalRedisCommands
*/
<T> void jsonSet(K key, String path, T value);

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
* Group: json
*
* @param key the key, must not be {@code null}
* @param value the value, encoded to JSON
* @param <T> the type for the value
**/
default <T> void jsonSet(K key, T value) {
jsonSet(key, "$", value);
}

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
Expand All @@ -29,6 +42,32 @@ public interface TransactionalJsonCommands<K> extends TransactionalRedisCommands
*/
void jsonSet(K key, String path, JsonObject json);

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
* Group: json
* <p>
* This variant uses {@code $} as path.
*
* @param key the key, must not be {@code null}
* @param json the JSON object to store, must not be {@code null}
**/
default void jsonSet(K key, JsonObject json) {
jsonSet(key, "$", json);
}

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
* Group: json
*
* @param key the key, must not be {@code null}
* @param json the JSON array to store, must not be {@code null}
**/
default void jsonSet(K key, JsonArray json) {
jsonSet(key, "$", json);
}

/**
* Execute the command <a href="https://redis.io/commands/json.set/">JSON.SET</a>.
* Summary: Sets the JSON value at path in key.
Expand Down Expand Up @@ -220,6 +259,20 @@ default <T> void jsonArrIndex(K key, String path, T value) {
*/
<T> void jsonArrPop(K key, Class<T> clazz, String path, int index);

/**
* Execute the command <a href="https://redis.io/commands/json.arrpop/">JSON.ARRPOP</a>.
* Summary: Removes and returns an element from the index in the array.
* Group: json
* <p>
*
* @param key the key, must not be {@code null}
* @param clazz the type of the popped object
* @param path path the path, defaults to root if not provided.
**/
default <T> void jsonArrPop(K key, Class<T> clazz, String path) {
jsonArrPop(key, clazz, path, -1);
}

/**
* Execute the command <a href="https://redis.io/commands/json.arrtrim/">JSON.ARRTRIM</a>.
* Summary: Trims an array so that it contains only the specified inclusive range of elements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class TransactionalJsonCommandsTest extends DatasourceTestBase {

Person person = new Person("luke", "skywalker");
Person person2 = new Person("leia", "skywalker");
Person person3 = new Person("anakin", "skywalker");

@BeforeEach
void initialize() {
Expand Down Expand Up @@ -62,8 +63,12 @@ public void setBlocking() {

json.jsonSet("sister", "$", new JsonObject(Json.encode(person2)));
json.jsonGet("sister", Person.class);

json.jsonSet("someone", person3);
json.jsonGetObject("someone");

});
assertThat(result.size()).isEqualTo(12);
assertThat(result.size()).isEqualTo(14);
assertThat(result.discarded()).isFalse();

assertThat((Void) result.get(0)).isNull();
Expand All @@ -82,6 +87,7 @@ public void setBlocking() {
assertThat(actual.getJsonArray("a")).isEmpty();// cleared
assertThat((Void) result.get(10)).isNull();
assertThat((Person) result.get(11)).isEqualTo(person2);
assertThat(((JsonObject) result.get(13)).mapTo(Person.class)).isEqualTo(person3);
}

@Test
Expand All @@ -100,9 +106,11 @@ public void setReactive() {
.chain(() -> json.jsonStrLen(key, "$.sister.lastname")) // 8 -> 10
.chain(() -> json.jsonGet(key)) // 9 {...}
.chain(() -> json.jsonSet("sister", "$", new JsonObject(Json.encode(person2))))
.chain(() -> json.jsonGet("sister", Person.class));
.chain(() -> json.jsonGet("sister", Person.class))
.chain(() -> json.jsonSet("someone", person3))
.chain(() -> json.jsonGetObject("someone"));
}).await().atMost(Duration.ofSeconds(5));
assertThat(result.size()).isEqualTo(12);
assertThat(result.size()).isEqualTo(14);
assertThat(result.discarded()).isFalse();

assertThat((Void) result.get(0)).isNull();
Expand All @@ -121,6 +129,8 @@ public void setReactive() {
assertThat(actual.getJsonArray("a")).isEmpty();// cleared
assertThat((Void) result.get(10)).isNull();
assertThat((Person) result.get(11)).isEqualTo(person2);
assertThat((Person) result.get(11)).isEqualTo(person2);
assertThat(((JsonObject) result.get(13)).mapTo(Person.class)).isEqualTo(person3);
}

}

0 comments on commit c6d7ce9

Please sign in to comment.