Skip to content

Commit

Permalink
pr comments + remove redis
Browse files Browse the repository at this point in the history
Signed-off-by: Shoham Elias <[email protected]>
  • Loading branch information
shohamazon committed Oct 13, 2024
1 parent 3002739 commit ad92730
Showing 1 changed file with 68 additions and 82 deletions.
150 changes: 68 additions & 82 deletions python/python/glide/async_commands/server_modules/json.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
"""module for `RedisJSON` commands.
"""module for `JSON` commands.
Examples:
>>> from glide import json as redisJson
>>> import json
>>> from glide import json
>>> import json as jsonpy
>>> value = {'a': 1.0, 'b': 2}
>>> json_str = json.dumps(value) # Convert Python dictionary to JSON string using json.dumps()
>>> await redisJson.set(client, "doc", "$", json_str)
>>> json_str = jsonpy.dumps(value) # Convert Python dictionary to JSON string using json.dumps()
>>> await json.set(client, "doc", "$", json_str)
'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
>>> json_get = await redisJson.get(client, "doc", "$") # Returns the value at path '$' in the JSON document stored at `doc` as JSON string.
>>> json_get = await json.get(client, "doc", "$") # Returns the value at path '$' in the JSON document stored at `doc` as JSON string.
>>> print(json_get)
b"[{\"a\":1.0,\"b\":2}]"
>>> json.loads(str(json_get))
>>> jsonpy.loads(str(json_get))
[{"a": 1.0, "b" :2}] # JSON object retrieved from the key `doc` using json.loads()
"""
from typing import List, Optional, Union, cast
Expand Down Expand Up @@ -64,27 +64,25 @@ async def set(
"""
Sets the JSON value at the specified `path` stored at `key`.
See https://valkey.io/commands/json.set/ for more details.
Args:
client (TGlideClient): The Redis client to execute the command.
client (TGlideClient): The client to execute the command.
key (TEncodable): The key of the JSON document.
path (TEncodable): Represents the path within the JSON document where the value will be set.
The key will be modified only if `value` is added as the last child in the specified `path`, or if the specified `path` acts as the parent of a new child being added.
value (TEncodable): The value to set at the specific path, in JSON formatted bytes or str.
set_condition (Optional[ConditionalChange]): Set the value only if the given condition is met (within the key or path).
Equivalent to [`XX` | `NX`] in the Redis API. Defaults to None.
Equivalent to [`XX` | `NX`] in the RESP API. Defaults to None.
Returns:
Optional[TOK]: If the value is successfully set, returns OK.
If `value` isn't set because of `set_condition`, returns None.
Examples:
>>> from glide import json as redisJson
>>> import json
>>> from glide import json
>>> import json as jsonpy
>>> value = {'a': 1.0, 'b': 2}
>>> json_str = json.dumps(value)
>>> await redisJson.set(client, "doc", "$", json_str)
>>> json_str = jsonpy.dumps(value)
>>> await json.set(client, "doc", "$", json_str)
'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
"""
args = ["JSON.SET", key, path, value]
Expand All @@ -103,12 +101,10 @@ async def get(
"""
Retrieves the JSON value at the specified `paths` stored at `key`.
See https://valkey.io/commands/json.get/ for more details.
Args:
client (TGlideClient): The Redis client to execute the command.
client (TGlideClient): The client to execute the command.
key (TEncodable): The key of the JSON document.
paths (Optional[Union[TEncodable, List[TEncodable]]]): The path or list of paths within the JSON document. Default is root `$`.
paths (Optional[Union[TEncodable, List[TEncodable]]]): The path or list of paths within the JSON document. Default to None.
options (Optional[JsonGetOptions]): Options for formatting the byte representation of the JSON data. See `JsonGetOptions`.
Returns:
Expand All @@ -128,16 +124,16 @@ async def get(
For more information about the returned type, see `TJsonResponse`.
Examples:
>>> from glide import json as redisJson
>>> import json
>>> json_str = await redisJson.get(client, "doc", "$")
>>> json.loads(str(json_str)) # Parse JSON string to Python data
>>> from glide import json, JsonGetOptions
>>> import as jsonpy
>>> json_str = await json.get(client, "doc", "$")
>>> jsonpy.loads(str(json_str)) # Parse JSON string to Python data
[{"a": 1.0, "b" :2}] # JSON object retrieved from the key `doc` using json.loads()
>>> await redisJson.get(client, "doc", "$")
>>> await json.get(client, "doc", "$")
b"[{\"a\":1.0,\"b\":2}]" # Returns the value at path '$' in the JSON document stored at `doc`.
>>> await redisJson.get(client, "doc", ["$.a", "$.b"], json.JsonGetOptions(indent=" ", newline="\n", space=" "))
>>> await json.get(client, "doc", ["$.a", "$.b"], JsonGetOptions(indent=" ", newline="\n", space=" "))
b"{\n \"$.a\": [\n 1.0\n ],\n \"$.b\": [\n 2\n ]\n}" # Returns the values at paths '$.a' and '$.b' in the JSON document stored at `doc`, with specified formatting options.
>>> await redisJson.get(client, "doc", "$.non_existing_path")
>>> await json.get(client, "doc", "$.non_existing_path")
b"[]" # Returns an empty array since the path '$.non_existing_path' does not exist in the JSON document stored at `doc`.
"""
args = ["JSON.GET", key]
Expand Down Expand Up @@ -179,7 +175,7 @@ async def arrlen(
Examples:
>>> from glide import json
>>> await json.set(client, "doc", "$", '{"a": [1, 2, 3], "b": {"a": [1, 2], "c": {"a": 42}}}')
b'OK' # JSON is successfully set for doc
'OK' # JSON is successfully set for doc
>>> await json.arrlen(client, "doc", "$")
[None] # No array at the root path.
>>> await json.arrlen(client, "doc", "$.a")
Expand All @@ -192,7 +188,7 @@ async def arrlen(
None # Returns None because the key does not exist.
>>> await json.set(client, "doc", "$", '[1, 2, 3, 4]')
b'OK' # JSON is successfully set for doc
'OK' # JSON is successfully set for doc
>>> await json.arrlen(client, "doc")
4 # Retrieves lengths of arrays in root.
"""
Expand All @@ -217,7 +213,7 @@ async def clear(
Args:
client (TGlideClient): The client to execute the command.
key (TEncodable): The key of the JSON document.
path (Optional[str]): The JSON path to the arrays or objects to be cleared. Defaults to None.
path (Optional[str]): The path within the JSON document. Default to None.
Returns:
int: The number of containers cleared, numeric values zeroed, and booleans toggled to `false`,
Expand All @@ -228,7 +224,7 @@ async def clear(
Examples:
>>> from glide import json
>>> await json.set(client, "doc", "$", '{"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14, "nullVal": null}')
b'OK' # JSON document is successfully set.
'OK' # JSON document is successfully set.
>>> await json.clear(client, "doc", "$.*")
6 # 6 values are cleared (arrays/objects/strings/numbers/booleans), but `null` remains as is.
>>> await json.get(client, "doc", "$")
Expand All @@ -237,7 +233,7 @@ async def clear(
0 # No further clearing needed since the containers are already empty and the values are defaults.
>>> await json.set(client, "doc", "$", '{"a": 1, "b": {"a": [5, 6, 7], "b": {"a": true}}, "c": {"a": "value", "b": {"a": 3.5}}, "d": {"a": {"foo": "foo"}}, "nullVal": null}')
b'OK'
'OK'
>>> await json.clear(client, "doc", "b.a[1:3]")
2 # 2 elements (`6` and `7`) are cleared.
>>> await json.clear(client, "doc", "b.a[1:3]")
Expand Down Expand Up @@ -265,27 +261,25 @@ async def delete(
"""
Deletes the JSON value at the specified `path` within the JSON document stored at `key`.
See https://valkey.io/commands/json.del/ for more details.
Args:
client (TGlideClient): The Redis client to execute the command.
client (TGlideClient): The client to execute the command.
key (TEncodable): The key of the JSON document.
path (Optional[TEncodable]): Represents the path within the JSON document where the value will be deleted.
path (Optional[TEncodable]): The path within the JSON document.
If None, deletes the entire JSON document at `key`. Defaults to None.
Returns:
int: The number of elements removed.
If `key` or `path` doesn't exist, returns 0.
Examples:
>>> from glide import json as redisJson
>>> await redisJson.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
>>> from glide import json
>>> await json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
>>> await redisJson.delete(client, "doc", "$..a")
>>> await json.delete(client, "doc", "$..a")
2 # Indicates successful deletion of the specific values in the key stored at `doc`.
>>> await redisJson.get(client, "doc", "$")
>>> await json.get(client, "doc", "$")
"[{\"nested\":{\"b\":3}}]" # Returns the value at path '$' in the JSON document stored at `doc`.
>>> await redisJson.delete(client, "doc")
>>> await json.delete(client, "doc")
1 # Deletes the entire JSON document stored at `doc`.
"""

Expand All @@ -302,27 +296,25 @@ async def forget(
"""
Deletes the JSON value at the specified `path` within the JSON document stored at `key`.
See https://valkey.io/commands/json.forget/ for more details.
Args:
client (TGlideClient): The Redis client to execute the command.
client (TGlideClient): The client to execute the command.
key (TEncodable): The key of the JSON document.
path (Optional[TEncodable]): Represents the path within the JSON document where the value will be deleted.
path (Optional[TEncodable]): The path within the JSON document.
If None, deletes the entire JSON document at `key`. Defaults to None.
Returns:
int: The number of elements removed.
If `key` or `path` doesn't exist, returns 0.
Examples:
>>> from glide import json as redisJson
>>> await redisJson.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
>>> from glide import json
>>> await json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
>>> await redisJson.forget(client, "doc", "$..a")
>>> await json.forget(client, "doc", "$..a")
2 # Indicates successful deletion of the specific values in the key stored at `doc`.
>>> await redisJson.get(client, "doc", "$")
>>> await json.get(client, "doc", "$")
"[{\"nested\":{\"b\":3}}]" # Returns the value at path '$' in the JSON document stored at `doc`.
>>> await redisJson.forget(client, "doc")
>>> await json.forget(client, "doc")
1 # Deletes the entire JSON document stored at `doc`.
"""

Expand All @@ -341,13 +333,11 @@ async def strappend(
"""
Appends the specified `value` to the string stored at the specified `path` within the JSON document stored at `key`.
See https://redis.io/commands/json.strapped/ for more details.
Args:
client (TRedisClient): The Redis client to execute the command.
client (TGlideClient): The client to execute the command.
key (TEncodable): The key of the JSON document.
value (TEncodable): The value to append to the string. Must be wrapped with single quotes. For example, to append "foo", pass '"foo"'.
path (Optional[TEncodable]): The JSONPath to specify. Default is root `$`.
path (Optional[TEncodable]): The path within the JSON document. Default to None.
Returns:
TJsonResponse[int]:
Expand All @@ -363,15 +353,15 @@ async def strappend(
For more information about the returned type, see `TJsonResponse`.
Examples:
>>> from glide import json as redisJson
>>> import json
>>> await redisJson.set(client, "doc", "$", json.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
>>> from glide import json
>>> import json as jsonpy
>>> await json.set(client, "doc", "$", jsonpy.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
'OK'
>>> await redisJson.strappend(client, "doc", json.dumps("baz"), "$..a")
>>> await json.strappend(client, "doc", jsonpy.dumps("baz"), "$..a")
[6, 8, None] # The new length of the string values at path '$..a' in the key stored at `doc` after the append operation.
>>> await redisJson.strappend(client, "doc", '"foo"', "nested.a")
>>> await json.strappend(client, "doc", '"foo"', "nested.a")
11 # The length of the string value after appending "foo" to the string at path 'nested.array' in the key stored at `doc`.
>>> json.loads(await redisJson.get(client, json.dumps("doc"), "$"))
>>> jsonpy.loads(await json.get(client, jsonpy.dumps("doc"), "$"))
[{"a":"foobaz", "nested": {"a": "hellobazfoo"}, "nested2": {"a": 31}}] # The updated JSON value in the key stored at `doc`.
"""

Expand All @@ -391,12 +381,10 @@ async def strlen(
"""
Returns the length of the JSON string value stored at the specified `path` within the JSON document stored at `key`.
See https://redis.io/commands/json.strlen/ for more details.
Args:
client (TRedisClient): The Redis client to execute the command.
client (TGlideClient): The client to execute the command.
key (TEncodable): The key of the JSON document.
path (Optional[TEncodable]): The JSONPath to specify. Default is root `$`.
path (Optional[TEncodable]): The path within the JSON document. Default to None.
Returns:
TJsonResponse[Optional[int]]:
Expand All @@ -411,17 +399,17 @@ async def strlen(
For more information about the returned type, see `TJsonResponse`.
Examples:
>>> from glide import json as redisJson
>>> import json
>>> await redisJson.set(client, "doc", "$", json.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
>>> from glide import json
>>> import jsonpy
>>> await json.set(client, "doc", "$", jsonpy.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
'OK'
>>> await redisJson.strlen(client, "doc", "$..a")
>>> await json.strlen(client, "doc", "$..a")
[3, 5, None] # The length of the string values at path '$..a' in the key stored at `doc`.
>>> await redisJson.strlen(client, "doc", "nested.a")
>>> await json.strlen(client, "doc", "nested.a")
5 # The length of the JSON value at path 'nested.a' in the key stored at `doc`.
>>> await redisJson.strlen(client, "doc", "$")
>>> await json.strlen(client, "doc", "$")
[None] # Returns an array with None since the value at root path does in the JSON document stored at `doc` is not a string.
>>> await redisJson.strlen(client, "non_existing_key", ".")
>>> await json.strlen(client, "non_existing_key", ".")
None # `key` doesn't exist.
"""

Expand All @@ -441,12 +429,10 @@ async def toggle(
"""
Toggles a Boolean value stored at the specified `path` within the JSON document stored at `key`.
See https://valkey.io/commands/json.toggle/ for more details.
Args:
client (TGlideClient): The Redis client to execute the command.
client (TGlideClient): The client to execute the command.
key (TEncodable): The key of the JSON document.
path (TEncodable): The JSONPath to specify.
path (TEncodable): The path within the JSON document. Default to None.
Returns:
TJsonResponse[bool]:
Expand All @@ -461,15 +447,15 @@ async def toggle(
For more information about the returned type, see `TJsonResponse`.
Examples:
>>> from glide import json as redisJson
>>> import json
>>> await redisJson.set(client, "doc", "$", json.dumps({"bool": True, "nested": {"bool": False, "nested": {"bool": 10}}}))
>>> from glide import json
>>> import json as jsonpy
>>> await json.set(client, "doc", "$", jsonpy.dumps({"bool": True, "nested": {"bool": False, "nested": {"bool": 10}}}))
'OK'
>>> await redisJson.toggle(client, "doc", "$.bool")
>>> await json.toggle(client, "doc", "$.bool")
[False, True, None] # Indicates successful toggling of the Boolean values at path '$.bool' in the key stored at `doc`.
>>> await redisJson.toggle(client, "doc", "bool")
>>> await json.toggle(client, "doc", "bool")
True # Indicates successful toggling of the Boolean value at path 'bool' in the key stored at `doc`.
>>> json.loads(await redisJson.get(client, "doc", "$"))
>>> jsonpy.loads(await json.get(client, "doc", "$"))
[{"bool": True, "nested": {"bool": True, "nested": {"bool": 10}}}] # The updated JSON value in the key stored at `doc`.
"""

Expand All @@ -490,8 +476,7 @@ async def type(
Args:
client (TGlideClient): The client to execute the command.
key (TEncodable): The key of the JSON document.
path (Optional[TEncodable]): Represents the path within the JSON document where the type will be retrieved.
Defaults to None.
path (Optional[TEncodable]): The path within the JSON document. Default to None.
Returns:
Optional[Union[bytes, List[bytes]]]:
Expand All @@ -507,6 +492,7 @@ async def type(
Examples:
>>> from glide import json
>>> await json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
'OK'
>>> await json.type(client, "doc", "$.nested")
[b'object'] # Indicates the type of the value at path '$.nested' in the key stored at `doc`.
>>> await json.type(client, "doc", "$.nested.a")
Expand Down

0 comments on commit ad92730

Please sign in to comment.