Skip to content

Commit

Permalink
address comment 2
Browse files Browse the repository at this point in the history
Signed-off-by: James Xin <[email protected]>
  • Loading branch information
jamesx-improving committed Oct 17, 2024
1 parent 1881895 commit ae856fc
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 88 deletions.
148 changes: 92 additions & 56 deletions java/client/src/main/java/glide/api/commands/servermodules/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import glide.api.models.GlideString;
import glide.api.models.commands.ConditionalChange;
import glide.api.models.commands.json.JsonGetOptions;
import glide.api.models.commands.json.JsonGetOptionsBinary;
import glide.utils.ArgsBuilder;
import glide.utils.ArrayTransformUtils;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -138,43 +139,34 @@ public static CompletableFuture<String> set(
*
* @param client The Valkey GLIDE client to execute the command.
* @param key The <code>key</code> of the JSON document.
* @param path The path within the JSON document.
* @return For JSONPath (path starts with <code>$</code>): Returns a stringifies JSON list of
* bytes replies for every possible path, or a byte string representation of an empty array,
* if path doesn't exist. For legacy path (path doesn't start with <code>$</code>): Returns a
* byte string representation of the value in <code>path</code>. If <code>path</code> doesn't
* exist, an error is raised. If <code>key</code> doesn't exist, returns null.
* @return Returns a string representation of the JSON document. If <code>key</code> doesn't
* exist, returns null.
* @example
* <pre>{@code
* String value = client.Json.get(client, "doc", "$").get();
* String value = client.Json.get(client, "doc").get();
* assert value.equals("{'a': 1.0, 'b': 2}");
* }</pre>
*/
public static CompletableFuture<String> get(
@NonNull BaseClient client, @NonNull String key, @NonNull String path) {
return executeCommand(client, new String[] {JSON_GET, key, path});
public static CompletableFuture<String> get(@NonNull BaseClient client, @NonNull String key) {
return executeCommand(client, new String[] {JSON_GET, key});
}

/**
* Retrieves the JSON value at the specified <code>path</code> 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 The path within the JSON document.
* @return For JSONPath (path starts with <code>$</code>): Returns a stringifies JSON list of
* bytes replies for every possible path, or a byte string representation of an empty array,
* if path doesn't exist. For legacy path (path doesn't start with <code>$</code>): Returns a
* byte string representation of the value in <code>path</code>. If <code>path</code> doesn't
* exist, an error is raised. If <code>key</code> doesn't exist, returns null.
* @return Returns a string representation of the JSON document. If <code>key</code> doesn't
* exist, returns null.
* @example
* <pre>{@code
* GlideString value = client.Json.get(client, gs("doc"), gs("$")).get();
* GlideString value = client.Json.get(client, gs("doc")).get();
* assert value.equals(gs("{'a': 1.0, 'b': 2}"));
* }</pre>
*/
public static CompletableFuture<GlideString> get(
@NonNull BaseClient client, @NonNull GlideString key, @NonNull GlideString path) {
return executeCommand(client, new GlideString[] {gs(JSON_GET), key, path});
@NonNull BaseClient client, @NonNull GlideString key) {
return executeCommand(client, new GlideString[] {gs(JSON_GET), key});
}

/**
Expand All @@ -183,9 +175,24 @@ public static CompletableFuture<GlideString> get(
* @param client The Valkey GLIDE client to execute the command.
* @param key The <code>key</code> of the JSON document.
* @param paths List of paths within the JSON document.
* @return Returns a stringifies JSON object in bytes, in which each path is a key, and it's
* corresponding value, is the value as if the path was executed in the command as a single
* path. If <code>key</code> doesn't exist, returns null.
* @return
* <ul>
* <li>If one path is given:
* <ul>
* <li>For JSONPath (path starts with <code>$</code>): Returns a stringified JSON list
* replies for every possible path, or a string representation of an empty array,
* if path doesn't exist. If <code>key</code> doesn't exist, returns None.
* <li>For legacy path (path doesn't start with <code>$</code>): Returns a string
* representation of the value in <code>paths</code>. If <code>paths</code>
* doesn't exist, an error is raised. If <code>key</code> doesn't exist, returns
* None.
* </ul>
* <li>If multiple paths are given: Returns a stringified JSON, in which each path is a key,
* and it's corresponding value, is the value as if the path was executed in the command
* as a single path.
* </ul>
* In case of multiple paths, and <code>paths</code> are a mix of both JSONPath and legacy
* path, the command behaves as if all are JSONPath paths.
* @example
* <pre>{@code
* String value = client.Json.get(client, "doc", new String[] {"$.a", "$.b"}).get();
Expand All @@ -204,9 +211,24 @@ public static CompletableFuture<String> get(
* @param client The Valkey GLIDE client to execute the command.
* @param key The <code>key</code> of the JSON document.
* @param paths List of paths within the JSON document.
* @return Returns a stringifies JSON object in bytes, in which each path is a key, and it's
* corresponding value, is the value as if the path was executed in the command as a single
* path. If <code>key</code> doesn't exist, returns null.
* @return
* <ul>
* <li>If one path is given:
* <ul>
* <li>For JSONPath (path starts with <code>$</code>): Returns a stringified JSON list
* replies for every possible path, or a string representation of an empty array,
* if path doesn't exist. If <code>key</code> doesn't exist, returns None.
* <li>For legacy path (path doesn't start with <code>$</code>): Returns a string
* representation of the value in <code>paths</code>. If <code>paths</code>
* doesn't exist, an error is raised. If <code>key</code> doesn't exist, returns
* None.
* </ul>
* <li>If multiple paths are given: Returns a stringified JSON, in which each path is a key,
* and it's corresponding value, is the value as if the path was executed in the command
* as a single path.
* </ul>
* In case of multiple paths, and <code>paths</code> are a mix of both JSONPath and legacy
* path, the command behaves as if all are JSONPath paths.
* @example
* <pre>{@code
* GlideString value = client.Json.get(client, gs("doc"), new GlideString[] {gs("$.a"), gs("$.b")}).get();
Expand All @@ -225,14 +247,10 @@ public static CompletableFuture<GlideString> get(
*
* @param client The Valkey GLIDE client to execute the command.
* @param key The <code>key</code> of the JSON document.
* @param path The path within the JSON document.
* @param options Options for formatting the byte representation of the JSON data. See <code>
* JsonGetOptions</code>.
* @return For JSONPath (path starts with <code>$</code>): Returns a stringifies JSON list of
* bytes replies for every possible path, or a byte string representation of an empty array,
* if path doesn't exist. For legacy path (path doesn't start with <code>$</code>): Returns a
* byte string representation of the value in <code>path</code>. If <code>path</code> doesn't
* exist, an error is raised. If <code>key</code> doesn't exist, returns null.
* @return Returns a string representation of the JSON document. If <code>key</code> doesn't
* exist, returns null.
* @example
* <pre>{@code
* JsonGetOptions options = JsonGetOptions.builder()
Expand All @@ -245,29 +263,21 @@ public static CompletableFuture<GlideString> get(
* }</pre>
*/
public static CompletableFuture<String> get(
@NonNull BaseClient client,
@NonNull String key,
@NonNull String path,
@NonNull JsonGetOptions options) {
@NonNull BaseClient client, @NonNull String key, @NonNull JsonGetOptions options) {
return executeCommand(
client,
ArrayTransformUtils.concatenateArrays(
new String[] {JSON_GET, key}, options.toArgs(), new String[] {path}));
ArrayTransformUtils.concatenateArrays(new String[] {JSON_GET, key}, options.toArgs()));
}

/**
* Retrieves the JSON value at the specified <code>path</code> 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 The path within the JSON document.
* @param options Options for formatting the byte representation of the JSON data. See <code>
* JsonGetOptions</code>.
* @return For JSONPath (path starts with <code>$</code>): Returns a stringifies JSON list of
* bytes replies for every possible path, or a byte string representation of an empty array,
* if path doesn't exist. For legacy path (path doesn't start with <code>$</code>): Returns a
* byte string representation of the value in <code>path</code>. If <code>path</code> doesn't
* exist, an error is raised. If <code>key</code> doesn't exist, returns null.
* @return Returns a string representation of the JSON document. If <code>key</code> doesn't
* exist, returns null.
* @example
* <pre>{@code
* JsonGetOptions options = JsonGetOptions.builder()
Expand All @@ -280,13 +290,9 @@ public static CompletableFuture<String> get(
* }</pre>
*/
public static CompletableFuture<GlideString> get(
@NonNull BaseClient client,
@NonNull GlideString key,
@NonNull GlideString path,
@NonNull JsonGetOptions options) {
@NonNull BaseClient client, @NonNull GlideString key, @NonNull JsonGetOptionsBinary options) {
return executeCommand(
client,
new ArgsBuilder().add(gs(JSON_GET)).add(key).add(options.toArgs()).add(path).toArray());
client, new ArgsBuilder().add(gs(JSON_GET)).add(key).add(options.toArgs()).toArray());
}

/**
Expand All @@ -297,9 +303,24 @@ public static CompletableFuture<GlideString> get(
* @param paths List of paths within the JSON document.
* @param options Options for formatting the byte representation of the JSON data. See <code>
* JsonGetOptions</code>.
* @return Returns a stringifies JSON object in bytes, in which each path is a key, and it's
* corresponding value, is the value as if the path was executed in the command as a single
* path. If <code>key</code> doesn't exist, returns null.
* @return
* <ul>
* <li>If one path is given:
* <ul>
* <li>For JSONPath (path starts with <code>$</code>): Returns a stringified JSON list
* replies for every possible path, or a string representation of an empty array,
* if path doesn't exist. If <code>key</code> doesn't exist, returns None.
* <li>For legacy path (path doesn't start with <code>$</code>): Returns a string
* representation of the value in <code>paths</code>. If <code>paths</code>
* doesn't exist, an error is raised. If <code>key</code> doesn't exist, returns
* None.
* </ul>
* <li>If multiple paths are given: Returns a stringified JSON, in which each path is a key,
* and it's corresponding value, is the value as if the path was executed in the command
* as a single path.
* </ul>
* In case of multiple paths, and <code>paths</code> are a mix of both JSONPath and legacy
* path, the command behaves as if all are JSONPath paths.
* @example
* <pre>{@code
* JsonGetOptions options = JsonGetOptions.builder()
Expand Down Expand Up @@ -330,9 +351,24 @@ public static CompletableFuture<String> get(
* @param paths List of paths within the JSON document.
* @param options Options for formatting the byte representation of the JSON data. See <code>
* JsonGetOptions</code>.
* @return Returns a stringifies JSON object in bytes, in which each path is a key, and it's
* corresponding value, is the value as if the path was executed in the command as a single
* path. If <code>key</code> doesn't exist, returns null.
* @return
* <ul>
* <li>If one path is given:
* <ul>
* <li>For JSONPath (path starts with <code>$</code>): Returns a stringified JSON list
* replies for every possible path, or a string representation of an empty array,
* if path doesn't exist. If <code>key</code> doesn't exist, returns None.
* <li>For legacy path (path doesn't start with <code>$</code>): Returns a string
* representation of the value in <code>paths</code>. If <code>paths</code>
* doesn't exist, an error is raised. If <code>key</code> doesn't exist, returns
* None.
* </ul>
* <li>If multiple paths are given: Returns a stringified JSON, in which each path is a key,
* and it's corresponding value, is the value as if the path was executed in the command
* as a single path.
* </ul>
* In case of multiple paths, and <code>paths</code> are a mix of both JSONPath and legacy
* path, the command behaves as if all are JSONPath paths.
* @example
* <pre>{@code
* JsonGetOptions options = JsonGetOptions.builder()
Expand All @@ -348,7 +384,7 @@ public static CompletableFuture<GlideString> get(
@NonNull BaseClient client,
@NonNull GlideString key,
@NonNull GlideString[] paths,
@NonNull JsonGetOptions options) {
@NonNull JsonGetOptionsBinary options) {
return executeCommand(
client,
new ArgsBuilder().add(gs(JSON_GET)).add(key).add(options.toArgs()).add(paths).toArray());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */
package glide.api.models.commands.json;

import static glide.api.models.GlideString.gs;

import glide.api.commands.servermodules.Json;
import glide.api.models.GlideString;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;

/** GlideString version of additional parameters for {@link Json#get} command. */
@Builder
public final class JsonGetOptionsBinary {
/** ValKey API string to designate INDENT */
public static final GlideString INDENT_VALKEY_API = gs("INDENT");

/** ValKey API string to designate NEWLINE */
public static final GlideString NEWLINE_VALKEY_API = gs("NEWLINE");

/** ValKey API string to designate SPACE */
public static final GlideString SPACE_VALKEY_API = gs("SPACE");

/** ValKey API string to designate SPACE */
public static final GlideString NOESCAPE_VALKEY_API = gs("NOESCAPE");

/** Sets an indentation string for nested levels. */
private GlideString indent;

/** Sets a string that's printed at the end of each line. */
private GlideString newline;

/** Sets a string that's put between a key and a value. */
private GlideString space;

/** Allowed to be present for legacy compatibility and has no other effect. */
private boolean noescape;

/**
* Converts JsonGetOptions into a GlideString[].
*
* @return GlideString[]
*/
public GlideString[] toArgs() {
List<GlideString> args = new ArrayList<>();
if (indent != null) {
args.add(INDENT_VALKEY_API);
args.add(indent);
}

if (newline != null) {
args.add(NEWLINE_VALKEY_API);
args.add(newline);
}

if (space != null) {
args.add(SPACE_VALKEY_API);
args.add(space);
}

if (noescape) {
args.add(NOESCAPE_VALKEY_API);
}

return args.toArray(new GlideString[0]);
}
}
Loading

0 comments on commit ae856fc

Please sign in to comment.