Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: adds JSON.ARRAPPEND command (WIP) #2474

Closed
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Java: Added `FT.SEARCH` ([#2439](https://github.com/valkey-io/valkey-glide/pull/2439))
* Java: Added `JSON.SET` and `JSON.GET` ([#2462](https://github.com/valkey-io/valkey-glide/pull/2462))
* Core: Update routing for commands from server modules ([#2461](https://github.com/valkey-io/valkey-glide/pull/2461))
* Java: Added `JSON.ARRAPPEND` ([#2474](https://github.com/valkey-io/valkey-glide/pull/2474))

#### Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Json {
public static final String JSON_PREFIX = "JSON.";
public static final String JSON_SET = JSON_PREFIX + "SET";
public static final String JSON_GET = JSON_PREFIX + "GET";
public static final String JSON_ARRAPPEND = JSON_PREFIX + "ARRAPPEND";

private Json() {}

Expand Down Expand Up @@ -394,6 +395,88 @@ public static CompletableFuture<GlideString> get(
new ArgsBuilder().add(gs(JSON_GET)).add(key).add(options.toArgs()).add(paths).toArray());
}

/**
* Appends one or more <code>values</code> to the JSON array at the specified <code>path</code> within the JSON
* document stored at <code>key</code>.
*
* @param client The Valkey GLIDE client to execute the command.
* @param key The <code>key</code> of the JSON document.
* @param path Represents the <code>path</code> within the JSON document where the <code>values</code> will be
* appended. Defaults to None.
* @param values The <code>values</code> to append to the JSON array at the specified <code>path</code>.
* @return
* <ul>
* <li>For JSONPath (path starts with <code>$</code>): Returns a list of integer replies for every possible
* path, indicating the new length of the new array after appending <code>values</code>, or None for JSON
* values matching the path that are not an array.
* If <code>key</code> doesn't exist, an error is raised.
* <li>For legacy path (path doesn't start with <code>$</code>): Returns the length of the new array after
* appending <code>values</code> to the array at <code>path</code>. If multiple paths match, the length of
* the last updated array is returned. If the JSON value at <code>path</code> is not a array or if
* <code>path</code> doesn't exist, an error is raised.
* If <code>key</code> doesn't exist, an error is raised.
* @example
* <pre>{@code
* String value = client.Json.set(client, "doc", "$", "{'a': 1, 'b': ['one', 'two']}").get();
* assert value.equals("OK");
* String length = client.Json.arrapend(client, "doc", "$.b", "['three']").get();
* assert length.equals([3]);
* length = client.Json.arrapend(client, "doc", ".b", "['four']").get();
* assert length.equals(4);
* }</pre>
*/
public static CompletableFuture<String> arrappend(
@NonNull BaseClient client,
@NonNull String key,
@NonNull String path,
@NonNull String[] values) {
return executeCommand(
client,
ArrayTransformUtils.concatenateArrays(
new String[] {JSON_ARRAPPEND, key, path}, values));
}

/**
* Appends one or more <code>values</code> to the JSON array at the specified <code>path</code> within the JSON
* document stored at <code>key</code>.
*
* @param client The Valkey GLIDE client to execute the command.
* @param key The <code>key</code> of the JSON document.
* @param path Represents the <code>path</code> within the JSON document where the <code>values</code> will be
* appended. Defaults to None.
* @param values The <code>values</code> to append to the JSON array at the specified <code>path</code>.
* @return
* <ul>
* <li>For JSONPath (path starts with <code>$</code>): Returns a list of integer replies for every possible
* path, indicating the new length of the new array after appending <code>values</code>, or None for JSON
* values matching the path that are not an array.
* If <code>key</code> doesn't exist, an error is raised.
* <li>For legacy path (path doesn't start with <code>$</code>): Returns the length of the new array after
* appending <code>values</code> to the array at <code>path</code>. If multiple paths match, the length of
* the last updated array is returned. If the JSON value at <code>path</code> is not a array or if
* <code>path</code> doesn't exist, an error is raised.
* If <code>key</code> doesn't exist, an error is raised.
* @example
* <pre>{@code
* GlideString value = client.Json.set(client, gs("doc"), gs("$"), gs("{'a': 1, 'b': ['one', 'two']}")).get();
* assert value.equals("OK");
* GlideString length = client.Json.arrapend(client, gs("doc"), gs("$.b"), gs("['three']")).get();
* assert length.equals([3]);
* length = client.Json.arrapend(client, gs("doc"), gs(".b"), gs("['four']")).get();
* assert length.equals(4);
* }</pre>
*/
public static CompletableFuture<GlideString> arrappend(
@NonNull BaseClient client,
@NonNull GlideString key,
@NonNull GlideString path,
@NonNull GlideString[] values) {
return executeCommand(
client,
ArrayTransformUtils.concatenateArrays(
new GlideString[] {gs(JSON_ARRAPPEND), key, path}, values));
}

/**
* A wrapper for custom command API.
*
Expand Down
Loading