Skip to content

Commit

Permalink
Enhance docstring and tests for cases with no path
Browse files Browse the repository at this point in the history
Signed-off-by: BoazBD <boazbd@amazon.com>
  • Loading branch information
BoazBD committed Oct 22, 2024
1 parent 1e8f312 commit fec31b2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions python/python/glide/async_commands/server_modules/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ async def objlen(
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 key names will be retrieved.
Defaults to None.
Defaults to None.If not provided, the root of the document is used, equivalent to setting the path to ".".
Returns:
Optional[Union[int, List[int]]]:
For JSONPath (`path` starts with `$`):
Expand All @@ -427,6 +427,7 @@ async def objlen(
If multiple objects match the path, the length of the first object is returned.
If a value matching the path is not an object, an error is raised.
Examples:
>>> from glide import json
>>> await json.set(client, "doc", "$", '{"a": 1.0, "b": {"a": {"x": 1, "y": 2}, "b": 2.5, "c": true}}')
Expand All @@ -436,9 +437,11 @@ async def objlen(
>>> await json.objlen(client, "doc", ".")
2 # Returns the number of key-value pairs for the object matching the path '.', which has 2 keys: 'a' and 'b'.
>>> await json.objlen(client, "doc", "$.b")
3 # Returns the length of the object at path '$.b', which has 3 keys: 'a', 'b', and 'c'.
[3] # Returns the length of the object at path '$.b', which has 3 keys: 'a', 'b', and 'c'.
>>> await json.objlen(client, "doc", ".b")
3 # Returns the length of the nested object at path '.b', which has 3 keys.
>>> await json.objlen(client, "doc")
2 # Returns the number of key-value pairs for the object matching the path '.', which has 2 keys: 'a' and 'b'.
"""
args = ["JSON.OBJLEN", key]
if path:
Expand Down
3 changes: 3 additions & 0 deletions python/python/tests/tests_server_modules/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ async def test_json_objlen(self, glide_client: TGlideClient):
len = await json.objlen(glide_client, key, "..b")
assert len == 3

len = await json.objlen(glide_client, key)
assert len == 2

# path doesn't exist
assert await json.objlen(glide_client, key, "$.non_existing_path") == []
with pytest.raises(RequestError):
Expand Down

0 comments on commit fec31b2

Please sign in to comment.