valkey-glide 2.2.3__pp39-pypy39_pp73-macosx_10_7_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. glide/__init__.py +388 -0
  2. glide/async_commands/__init__.py +5 -0
  3. glide/async_commands/cluster_commands.py +1476 -0
  4. glide/async_commands/core.py +7818 -0
  5. glide/async_commands/ft.py +465 -0
  6. glide/async_commands/glide_json.py +1269 -0
  7. glide/async_commands/standalone_commands.py +1001 -0
  8. glide/glide.pyi +61 -0
  9. glide/glide.pypy39-pp73-darwin.so +0 -0
  10. glide/glide_client.py +821 -0
  11. glide/logger.py +97 -0
  12. glide/opentelemetry.py +185 -0
  13. glide/py.typed +0 -0
  14. glide_shared/__init__.py +330 -0
  15. glide_shared/commands/__init__.py +0 -0
  16. glide_shared/commands/batch.py +5997 -0
  17. glide_shared/commands/batch_options.py +261 -0
  18. glide_shared/commands/bitmap.py +320 -0
  19. glide_shared/commands/command_args.py +103 -0
  20. glide_shared/commands/core_options.py +407 -0
  21. glide_shared/commands/server_modules/ft_options/ft_aggregate_options.py +300 -0
  22. glide_shared/commands/server_modules/ft_options/ft_constants.py +84 -0
  23. glide_shared/commands/server_modules/ft_options/ft_create_options.py +423 -0
  24. glide_shared/commands/server_modules/ft_options/ft_profile_options.py +113 -0
  25. glide_shared/commands/server_modules/ft_options/ft_search_options.py +139 -0
  26. glide_shared/commands/server_modules/json_batch.py +820 -0
  27. glide_shared/commands/server_modules/json_options.py +93 -0
  28. glide_shared/commands/sorted_set.py +412 -0
  29. glide_shared/commands/stream.py +449 -0
  30. glide_shared/config.py +975 -0
  31. glide_shared/constants.py +124 -0
  32. glide_shared/exceptions.py +88 -0
  33. glide_shared/protobuf/command_request_pb2.py +56 -0
  34. glide_shared/protobuf/connection_request_pb2.py +56 -0
  35. glide_shared/protobuf/response_pb2.py +32 -0
  36. glide_shared/protobuf_codec.py +110 -0
  37. glide_shared/routes.py +161 -0
  38. valkey_glide-2.2.3.dist-info/METADATA +211 -0
  39. valkey_glide-2.2.3.dist-info/RECORD +40 -0
  40. valkey_glide-2.2.3.dist-info/WHEEL +4 -0
@@ -0,0 +1,1269 @@
1
+ # Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
2
+ """Glide module for `JSON` commands.
3
+
4
+ Examples:
5
+
6
+ >>> from glide import glide_json
7
+ >>> import json
8
+ >>> value = {'a': 1.0, 'b': 2}
9
+ >>> json_str = json.dumps(value) # Convert Python dictionary to JSON string using json.dumps()
10
+ >>> await glide_json.set(client, "doc", "$", json_str)
11
+ 'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
12
+ >>> json_get = await glide_json.get(client, "doc", "$") # Returns the value at path '$' in the JSON document stored at
13
+ # `doc` as JSON string.
14
+ >>> print(json_get)
15
+ b"[{\"a\":1.0,\"b\":2}]"
16
+ >>> json.loads(str(json_get))
17
+ [{"a": 1.0, "b" :2}] # JSON object retrieved from the key `doc` using json.loads()
18
+
19
+ """
20
+ from typing import List, Optional, Union, cast
21
+
22
+ from glide_shared.commands.core_options import ConditionalChange
23
+ from glide_shared.commands.server_modules.json_options import (
24
+ JsonArrIndexOptions,
25
+ JsonArrPopOptions,
26
+ JsonGetOptions,
27
+ )
28
+ from glide_shared.constants import (
29
+ TOK,
30
+ TEncodable,
31
+ TJsonResponse,
32
+ TJsonUniversalResponse,
33
+ )
34
+
35
+ from ..glide_client import TGlideClient
36
+
37
+
38
+ async def set(
39
+ client: TGlideClient,
40
+ key: TEncodable,
41
+ path: TEncodable,
42
+ value: TEncodable,
43
+ set_condition: Optional[ConditionalChange] = None,
44
+ ) -> Optional[TOK]:
45
+ """
46
+ Sets the JSON value at the specified `path` stored at `key`.
47
+
48
+ Args:
49
+ client (TGlideClient): The client to execute the command.
50
+ key (TEncodable): The key of the JSON document.
51
+ path (TEncodable): Represents the path within the JSON document where the value will be set.
52
+ The key will be modified only if `value` is added as the last child in the specified `path`, or if the specified
53
+ `path` acts as the parent of a new child being added.
54
+ value (TEncodable): The value to set at the specific path, in JSON formatted bytes or str.
55
+ set_condition (Optional[ConditionalChange]): Set the value only if the given condition is met (within the key or path).
56
+ Equivalent to [`XX` | `NX`] in the RESP API. Defaults to None.
57
+
58
+ Returns:
59
+ Optional[TOK]: If the value is successfully set, returns OK.
60
+ If `value` isn't set because of `set_condition`, returns None.
61
+
62
+ Examples:
63
+ >>> from glide import glide_json
64
+ >>> import json
65
+ >>> value = {'a': 1.0, 'b': 2}
66
+ >>> json_str = json.dumps(value)
67
+ >>> await glide_json.set(client, "doc", "$", json_str)
68
+ 'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
69
+ """
70
+ args = ["JSON.SET", key, path, value]
71
+ if set_condition:
72
+ args.append(set_condition.value)
73
+
74
+ return cast(Optional[TOK], await client.custom_command(args))
75
+
76
+
77
+ async def get(
78
+ client: TGlideClient,
79
+ key: TEncodable,
80
+ paths: Optional[Union[TEncodable, List[TEncodable]]] = None,
81
+ options: Optional[JsonGetOptions] = None,
82
+ ) -> TJsonResponse[Optional[bytes]]:
83
+ """
84
+ Retrieves the JSON value at the specified `paths` stored at `key`.
85
+
86
+ Args:
87
+ client (TGlideClient): The client to execute the command.
88
+ key (TEncodable): The key of the JSON document.
89
+ paths (Optional[Union[TEncodable, List[TEncodable]]]): The path or list of paths within the JSON document.
90
+ Default to None.
91
+ options (Optional[JsonGetOptions]): Options for formatting the byte representation of the JSON data.
92
+ See `JsonGetOptions`.
93
+
94
+ Returns:
95
+ TJsonResponse[Optional[bytes]]:
96
+ If one path is given:
97
+ For JSONPath (path starts with `$`):
98
+ Returns a stringified JSON list of bytes replies for every possible path,
99
+ or a byte string representation of an empty array, if path doesn't exists.
100
+ If `key` doesn't exist, returns None.
101
+ For legacy path (path doesn't start with `$`):
102
+ Returns a byte string representation of the value in `path`.
103
+ If `path` doesn't exist, an error is raised.
104
+ If `key` doesn't exist, returns None.
105
+ If multiple paths are given:
106
+ Returns a stringified JSON object in bytes, in which each path is a key, and it's corresponding value, is the
107
+ value as if the path was executed in the command as a single path.
108
+ In case of multiple paths, and `paths` are a mix of both JSONPath and legacy path, the command behaves as if all are
109
+ JSONPath paths.
110
+ For more information about the returned type, see `TJsonResponse`.
111
+
112
+ Examples:
113
+ >>> from glide import glide_json, JsonGetOptions
114
+ >>> import json
115
+ >>> json_str = await glide_json.get(client, "doc", "$")
116
+ >>> json.loads(str(json_str)) # Parse JSON string to Python data
117
+ [{"a": 1.0, "b" :2}] # JSON object retrieved from the key `doc` using json.loads()
118
+ >>> await glide_json.get(client, "doc", "$")
119
+ b"[{\"a\":1.0,\"b\":2}]" # Returns the value at path '$' in the JSON document stored at `doc`.
120
+ >>> await glide_json.get(client, "doc", ["$.a", "$.b"], JsonGetOptions(indent=" ", newline="\n", space=" "))
121
+ b"{\n \"$.a\": [\n 1.0\n ],\n \"$.b\": [\n 2\n ]\n}" # Returns the values at paths '$.a' and '$.b' in the JSON
122
+ # document stored at `doc`, with specified
123
+ # formatting options.
124
+ >>> await glide_json.get(client, "doc", "$.non_existing_path")
125
+ b"[]" # Returns an empty array since the path '$.non_existing_path' does not exist in the JSON document
126
+ # stored at `doc`.
127
+ """
128
+ args = ["JSON.GET", key]
129
+ if options:
130
+ args.extend(options.get_options())
131
+ if paths:
132
+ if isinstance(paths, (str, bytes)):
133
+ paths = [paths]
134
+ args.extend(paths)
135
+
136
+ return cast(TJsonResponse[Optional[bytes]], await client.custom_command(args))
137
+
138
+
139
+ async def arrappend(
140
+ client: TGlideClient,
141
+ key: TEncodable,
142
+ path: TEncodable,
143
+ values: List[TEncodable],
144
+ ) -> TJsonResponse[int]:
145
+ """
146
+ Appends one or more `values` to the JSON array at the specified `path` within the JSON document stored at `key`.
147
+
148
+ Args:
149
+ client (TGlideClient): The client to execute the command.
150
+ key (TEncodable): The key of the JSON document.
151
+ path (TEncodable): Represents the path within the JSON document where the `values` will be appended.
152
+ values (TEncodable): The values to append to the JSON array at the specified path.
153
+ JSON string values must be wrapped with quotes. For example, to append `"foo"`, pass `"\"foo\""`.
154
+
155
+ Returns:
156
+ TJsonResponse[int]:
157
+ For JSONPath (`path` starts with `$`):
158
+ Returns a list of integer replies for every possible path, indicating the new length of the array after
159
+ appending `values`,
160
+ or None for JSON values matching the path that are not an array.
161
+ If `path` doesn't exist, an empty array will be returned.
162
+ For legacy path (`path` doesn't start with `$`):
163
+ Returns the length of the array after appending `values` to the array at `path`.
164
+ If multiple paths match, the length of the first updated array is returned.
165
+ If the JSON value at `path` is not a array or if `path` doesn't exist, an error is raised.
166
+ If `key` doesn't exist, an error is raised.
167
+ For more information about the returned type, see `TJsonResponse`.
168
+
169
+ Examples:
170
+ >>> from glide import glide_json
171
+ >>> import json
172
+ >>> await glide_json.set(client, "doc", "$", '{"a": 1, "b": ["one", "two"]}')
173
+ 'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
174
+ >>> await glide_json.arrappend(client, "doc", ["three"], "$.b")
175
+ [3] # Returns the new length of the array at path '$.b' after appending the value.
176
+ >>> await glide_json.arrappend(client, "doc", ["four"], ".b")
177
+ 4 # Returns the new length of the array at path '.b' after appending the value.
178
+ >>> json.loads(await glide_json.get(client, "doc", "."))
179
+ {"a": 1, "b": ["one", "two", "three", "four"]} # Returns the updated JSON document
180
+ """
181
+ args = ["JSON.ARRAPPEND", key, path] + values
182
+ return cast(TJsonResponse[int], await client.custom_command(args))
183
+
184
+
185
+ async def arrindex(
186
+ client: TGlideClient,
187
+ key: TEncodable,
188
+ path: TEncodable,
189
+ value: TEncodable,
190
+ options: Optional[JsonArrIndexOptions] = None,
191
+ ) -> TJsonResponse[int]:
192
+ """
193
+ Searches for the first occurrence of a scalar JSON value (i.e., a value that is neither an object nor an array) within
194
+ arrays at the specified `path` in the JSON document stored at `key`.
195
+
196
+ If specified, `options.start` and `options.end` define an inclusive-to-exclusive search range within the array.
197
+ (Where `options.start` is inclusive and `options.end` is exclusive).
198
+
199
+ Out-of-range indices adjust to the nearest valid position, and negative values count from the end (e.g., `-1` is the last
200
+ element, `-2` the second last).
201
+
202
+ Setting `options.end` to `0` behaves like `-1`, extending the range to the array's end (inclusive).
203
+
204
+ If `options.start` exceeds `options.end`, `-1` is returned, indicating that the value was not found.
205
+
206
+ Args:
207
+ client (TGlideClient): The client to execute the command.
208
+ key (TEncodable): The key of the JSON document.
209
+ path (TEncodable): The path within the JSON document.
210
+ value (TEncodable): The value to search for within the arrays.
211
+ options (Optional[JsonArrIndexOptions]): Options specifying an inclusive `start` index and an optional exclusive `end`
212
+ index for a range-limited search.
213
+ Defaults to the full array if not provided. See `JsonArrIndexOptions`.
214
+
215
+ Returns:
216
+ Optional[TJsonResponse[int]]:
217
+ For JSONPath (`path` starts with `$`):
218
+ Returns an array of integers for every possible path, indicating of the first occurrence of `value`
219
+ within the array,
220
+ or None for JSON values matching the path that are not an array.
221
+ A returned value of `-1` indicates that the value was not found in that particular array.
222
+ If `path` does not exist, an empty array will be returned.
223
+ For legacy path (`path` doesn't start with `$`):
224
+ Returns an integer representing the index of the first occurrence of `value` within the array at the
225
+ specified path.
226
+ A returned value of `-1` indicates that the value was not found in that particular array.
227
+ If multiple paths match, the index of the value from the first matching array is returned.
228
+ If the JSON value at the `path` is not an array or if `path` does not exist, an error is raised.
229
+ If `key` does not exist, an error is raised.
230
+ For more information about the returned type, see `TJsonResponse`.
231
+
232
+ Examples:
233
+ >>> from glide import glide_json
234
+ >>> await glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"], ["a", "b", "c"]]')
235
+ 'OK'
236
+ >>> await glide_json.arrindex(client, "doc", "$[*]", '"b"')
237
+ [-1, -1, 1, 1]
238
+ >>> await glide_json.set(client, "doc", ".", '{"children": ["John", "Jack", "Tom", "Bob", "Mike"]}')
239
+ 'OK'
240
+ >>> await glide_json.arrindex(client, "doc", ".children", '"Tom"')
241
+ 2
242
+ >>> await glide_json.set(client, "doc", "$", '{"fruits": ["apple", "banana", "cherry", "banana", "grape"]}')
243
+ 'OK'
244
+ >>> await glide_json.arrindex(client, "doc", "$.fruits", '"banana"', JsonArrIndexOptions(start=2, end=4))
245
+ 3
246
+ >>> await glide_json.set(client, "k", ".", '[1, 2, "a", 4, "a", 6, 7, "b"]')
247
+ 'OK'
248
+ >>> await glide_json.arrindex(client, "k", ".", '"b"', JsonArrIndexOptions(start=4, end=0))
249
+ 7 # "b" found at index 7 within the specified range, treating end=0 as the entire array's end.
250
+ >>> await glide_json.arrindex(client, "k", ".", '"b"', JsonArrIndexOptions(start=4, end=-1))
251
+ 7 # "b" found at index 7, with end=-1 covering the full array to its last element.
252
+ >>> await glide_json.arrindex(client, "k", ".", '"b"', JsonArrIndexOptions(start=4, end=7))
253
+ -1 # "b" not found within the range from index 4 to exclusive end at index 7.
254
+ """
255
+ args = ["JSON.ARRINDEX", key, path, value]
256
+
257
+ if options:
258
+ args.extend(options.to_args())
259
+
260
+ return cast(TJsonResponse[int], await client.custom_command(args))
261
+
262
+
263
+ async def arrinsert(
264
+ client: TGlideClient,
265
+ key: TEncodable,
266
+ path: TEncodable,
267
+ index: int,
268
+ values: List[TEncodable],
269
+ ) -> TJsonResponse[int]:
270
+ """
271
+ Inserts one or more values into the array at the specified `path` within the JSON document stored at `key`,
272
+ before the given `index`.
273
+
274
+ Args:
275
+ client (TGlideClient): The client to execute the command.
276
+ key (TEncodable): The key of the JSON document.
277
+ path (TEncodable): The path within the JSON document.
278
+ index (int): The array index before which values are inserted.
279
+ values (List[TEncodable]): The JSON values to be inserted into the array, in JSON formatted bytes or str.
280
+ Json string values must be wrapped with single quotes. For example, to append "foo", pass '"foo"'.
281
+
282
+ Returns:
283
+ TJsonResponse[int]:
284
+ For JSONPath (`path` starts with '$'):
285
+ Returns a list of integer replies for every possible path, indicating the new length of the array,
286
+ or None for JSON values matching the path that are not an array.
287
+ If `path` does not exist, an empty array will be returned.
288
+ For legacy path (`path` doesn't start with '$'):
289
+ Returns an integer representing the new length of the array.
290
+ If multiple paths are matched, returns the length of the first modified array.
291
+ If `path` doesn't exist or the value at `path` is not an array, an error is raised.
292
+ If the index is out of bounds, an error is raised.
293
+ If `key` doesn't exist, an error is raised.
294
+ For more information about the returned type, see `TJsonResponse`.
295
+
296
+ Examples:
297
+ >>> from glide import glide_json
298
+ >>> await glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"]]')
299
+ 'OK'
300
+ >>> await glide_json.arrinsert(client, "doc", "$[*]", 0, ['"c"', '{"key": "value"}', "true", "null", '["bar"]'])
301
+ [5, 6, 7] # New lengths of arrays after insertion
302
+ >>> await glide_json.get(client, "doc")
303
+ b'[["c",{"key":"value"},true,null,["bar"]],["c",{"key":"value"},true,null,["bar"],"a"],["c",{"key":"value"},true,null,["bar"],"a","b"]]'
304
+
305
+ >>> await glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"]]')
306
+ 'OK'
307
+ >>> await glide_json.arrinsert(client, "doc", ".", 0, ['"c"'])
308
+ 4 # New length of the root array after insertion
309
+ >>> await glide_json.get(client, "doc")
310
+ b'[\"c\",[],[\"a\"],[\"a\",\"b\"]]'
311
+ """
312
+ args = ["JSON.ARRINSERT", key, path, str(index)] + values
313
+ return cast(TJsonResponse[int], await client.custom_command(args))
314
+
315
+
316
+ async def arrlen(
317
+ client: TGlideClient,
318
+ key: TEncodable,
319
+ path: Optional[TEncodable] = None,
320
+ ) -> Optional[TJsonResponse[int]]:
321
+ """
322
+ Retrieves the length of the array at the specified `path` within the JSON document stored at `key`.
323
+
324
+ Args:
325
+ client (TGlideClient): The client to execute the command.
326
+ key (TEncodable): The key of the JSON document.
327
+ path (Optional[TEncodable]): The path within the JSON document. Defaults to None.
328
+
329
+ Returns:
330
+ Optional[TJsonResponse[int]]:
331
+ For JSONPath (`path` starts with `$`):
332
+ Returns a list of integer replies for every possible path, indicating the length of the array,
333
+ or None for JSON values matching the path that are not an array.
334
+ If `path` doesn't exist, an empty array will be returned.
335
+ For legacy path (`path` doesn't starts with `$`):
336
+ Returns the length of the array at `path`.
337
+ If multiple paths match, the length of the first array match is returned.
338
+ If the JSON value at `path` is not a array or if `path` doesn't exist, an error is raised.
339
+ If `key` doesn't exist, None is returned.
340
+ For more information about the returned type, see `TJsonResponse`.
341
+
342
+ Examples:
343
+ >>> from glide import glide_json
344
+ >>> await glide_json.set(client, "doc", "$", '{"a": [1, 2, 3], "b": {"a": [1, 2], "c": {"a": 42}}}')
345
+ 'OK' # JSON is successfully set for doc
346
+ >>> await glide_json.arrlen(client, "doc", "$")
347
+ [None] # No array at the root path.
348
+ >>> await glide_json.arrlen(client, "doc", "$.a")
349
+ [3] # Retrieves the length of the array at path $.a.
350
+ >>> await glide_json.arrlen(client, "doc", "$..a")
351
+ [3, 2, None] # Retrieves lengths of arrays found at all levels of the path `$..a`.
352
+ >>> await glide_json.arrlen(client, "doc", "..a")
353
+ 3 # Legacy path retrieves the first array match at path `..a`.
354
+ >>> await glide_json.arrlen(client, "non_existing_key", "$.a")
355
+ None # Returns None because the key does not exist.
356
+
357
+ >>> await glide_json.set(client, "doc", "$", '[1, 2, 3, 4]')
358
+ 'OK' # JSON is successfully set for doc
359
+ >>> await glide_json.arrlen(client, "doc")
360
+ 4 # Retrieves lengths of array in root.
361
+ """
362
+ args = ["JSON.ARRLEN", key]
363
+ if path:
364
+ args.append(path)
365
+ return cast(
366
+ Optional[TJsonResponse[int]],
367
+ await client.custom_command(args),
368
+ )
369
+
370
+
371
+ async def arrpop(
372
+ client: TGlideClient,
373
+ key: TEncodable,
374
+ options: Optional[JsonArrPopOptions] = None,
375
+ ) -> Optional[TJsonResponse[bytes]]:
376
+ """
377
+ Pops an element from the array located at the specified path within the JSON document stored at `key`.
378
+ If `options.index` is provided, it pops the element at that index instead of the last element.
379
+
380
+ Args:
381
+ client (TGlideClient): The client to execute the command.
382
+ key (TEncodable): The key of the JSON document.
383
+ options (Optional[JsonArrPopOptions]): Options including the path and optional index. See `JsonArrPopOptions`.
384
+ Default to None.
385
+ If not specified, attempts to pop the last element from the root value if it's an array.
386
+ If the root value is not an array, an error will be raised.
387
+
388
+ Returns:
389
+ Optional[TJsonResponse[bytes]]:
390
+ For JSONPath (`options.path` starts with `$`):
391
+ Returns a list of bytes string replies for every possible path, representing the popped JSON values,
392
+ or None for JSON values matching the path that are not an array or are an empty array.
393
+ If `options.path` doesn't exist, an empty list will be returned.
394
+ For legacy path (`options.path` doesn't starts with `$`):
395
+ Returns a bytes string representing the popped JSON value, or None if the array at `options.path` is empty.
396
+ If multiple paths match, the value from the first matching array that is not empty is returned.
397
+ If the JSON value at `options.path` is not a array or if `options.path` doesn't exist, an error is raised.
398
+ If `key` doesn't exist, an error is raised.
399
+ For more information about the returned type, see `TJsonResponse`.
400
+
401
+ Examples:
402
+ >>> from glide import glide_json
403
+ >>> await glide_json.set(
404
+ ... client,
405
+ ... "doc",
406
+ ... "$",
407
+ ... '{"a": [1, 2, true], "b": {"a": [3, 4, ["value", 3, false], 5], "c": {"a": 42}}}'
408
+ ... )
409
+ b'OK'
410
+ >>> await glide_json.arrpop(client, "doc", JsonArrPopOptions(path="$.a", index=1))
411
+ [b'2'] # Pop second element from array at path $.a
412
+ >>> await glide_json.arrpop(client, "doc", JsonArrPopOptions(path="$..a"))
413
+ [b'true', b'5', None] # Pop last elements from all arrays matching path `$..a`
414
+
415
+ #### Using a legacy path (..) to pop the first matching array
416
+ >>> await glide_json.arrpop(client, "doc", JsonArrPopOptions(path="..a"))
417
+ b"1" # First match popped (from array at path ..a)
418
+
419
+ #### Even though only one value is returned from `..a`, subsequent arrays are also affected
420
+ >>> await glide_json.get(client, "doc", "$..a")
421
+ b"[[], [3, 4], 42]" # Remaining elements after pop show the changes
422
+
423
+ >>> await glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b", "c"]]')
424
+ b'OK' # JSON is successfully set
425
+ >>> await glide_json.arrpop(client, "doc", JsonArrPopOptions(path=".", index=-1))
426
+ b'["a","b","c"]' # Pop last elements at path `.`
427
+ >>> await glide_json.arrpop(client, "doc")
428
+ b'["a"]' # Pop last elements at path `.`
429
+ """
430
+ args = ["JSON.ARRPOP", key]
431
+ if options:
432
+ args.extend(options.to_args())
433
+
434
+ return cast(
435
+ Optional[TJsonResponse[bytes]],
436
+ await client.custom_command(args),
437
+ )
438
+
439
+
440
+ async def arrtrim(
441
+ client: TGlideClient,
442
+ key: TEncodable,
443
+ path: TEncodable,
444
+ start: int,
445
+ end: int,
446
+ ) -> TJsonResponse[int]:
447
+ """
448
+ Trims an array at the specified `path` within the JSON document stored at `key` so that it becomes a subarray [start, end],
449
+ both inclusive.
450
+ If `start` < 0, it is treated as 0.
451
+ If `end` >= size (size of the array), it is treated as size-1.
452
+ If `start` >= size or `start` > `end`, the array is emptied and 0 is returned.
453
+
454
+ Args:
455
+ client (TGlideClient): The client to execute the command.
456
+ key (TEncodable): The key of the JSON document.
457
+ path (TEncodable): The path within the JSON document.
458
+ start (int): The start index, inclusive.
459
+ end (int): The end index, inclusive.
460
+
461
+ Returns:
462
+ TJsonResponse[int]:
463
+ For JSONPath (`path` starts with '$'):
464
+ Returns a list of integer replies for every possible path, indicating the new length of the array, or None for
465
+ JSON values matching the path that are not an array.
466
+ If a value is an empty array, its corresponding return value is 0.
467
+ If `path` doesn't exist, an empty array will be returned.
468
+ For legacy path (`path` doesn't starts with `$`):
469
+ Returns an integer representing the new length of the array.
470
+ If the array is empty, returns 0.
471
+ If multiple paths match, the length of the first trimmed array match is returned.
472
+ If `path` doesn't exist, or the value at `path` is not an array, an error is raised.
473
+ If `key` doesn't exist, an error is raised.
474
+ For more information about the returned type, see `TJsonResponse`.
475
+
476
+ Examples:
477
+ >>> from glide import glide_json
478
+ >>> await glide_json.set(client, "doc", "$", '[[], ["a"], ["a", "b"], ["a", "b", "c"]]')
479
+ 'OK'
480
+ >>> await glide_json.arrtrim(client, "doc", "$[*]", 0, 1)
481
+ [0, 1, 2, 2]
482
+ >>> await glide_json.get(client, "doc")
483
+ b'[[],[\"a\"],[\"a\",\"b\"],[\"a\",\"b\"]]'
484
+
485
+ >>> await glide_json.set(client, "doc", "$", '{"children": ["John", "Jack", "Tom", "Bob", "Mike"]}')
486
+ 'OK'
487
+ >>> await glide_json.arrtrim(client, "doc", ".children", 0, 1)
488
+ 2
489
+ >>> await glide_json.get(client, "doc", ".children")
490
+ b'["John","Jack"]'
491
+ """
492
+ return cast(
493
+ TJsonResponse[int],
494
+ await client.custom_command(["JSON.ARRTRIM", key, path, str(start), str(end)]),
495
+ )
496
+
497
+
498
+ async def clear(
499
+ client: TGlideClient,
500
+ key: TEncodable,
501
+ path: Optional[str] = None,
502
+ ) -> int:
503
+ """
504
+ Clears arrays or objects at the specified JSON path in the document stored at `key`.
505
+ Numeric values are set to `0`, and boolean values are set to `False`, and string values are converted to empty strings.
506
+
507
+ Args:
508
+ client (TGlideClient): The client to execute the command.
509
+ key (TEncodable): The key of the JSON document.
510
+ path (Optional[str]): The path within the JSON document. Default to None.
511
+
512
+ Returns:
513
+ int: The number of containers cleared, numeric values zeroed, and booleans toggled to `false`,
514
+ and string values converted to empty strings.
515
+ If `path` doesn't exist, or the value at `path` is already empty (e.g., an empty array, object, or string),
516
+ 0 is returned.
517
+ If `key doesn't exist, an error is raised.
518
+
519
+ Examples:
520
+ >>> from glide import glide_json
521
+ >>> await glide_json.set(
522
+ ... client,
523
+ ... "doc",
524
+ ... "$",
525
+ ... '{"obj":{"a":1, "b":2}, "arr":[1,2,3], "str": "foo", "bool": true, "int": 42, "float": 3.14, "nullVal": null}'
526
+ ... )
527
+ 'OK' # JSON document is successfully set.
528
+ >>> await glide_json.clear(client, "doc", "$.*")
529
+ 6 # 6 values are cleared (arrays/objects/strings/numbers/booleans), but `null` remains as is.
530
+ >>> await glide_json.get(client, "doc", "$")
531
+ b'[{"obj":{},"arr":[],"str":"","bool":false,"int":0,"float":0.0,"nullVal":null}]'
532
+ >>> await glide_json.clear(client, "doc", "$.*")
533
+ 0 # No further clearing needed since the containers are already empty and the values are defaults.
534
+
535
+ >>> await glide_json.set(
536
+ ... client,
537
+ ... "doc",
538
+ ... "$",
539
+ ... (
540
+ ... '{"a": 1, '
541
+ ... '"b": {"a": [5, 6, 7], "b": {"a": true}}, '
542
+ ... '"c": {"a": "value", "b": {"a": 3.5}}, '
543
+ ... '"d": {"a": {"foo": "foo"}}, '
544
+ ... '"nullVal": null}'
545
+ ... )
546
+ ... )
547
+ 'OK'
548
+ >>> await glide_json.clear(client, "doc", "b.a[1:3]")
549
+ 2 # 2 elements (`6` and `7`) are cleared.
550
+ >>> await glide_json.clear(client, "doc", "b.a[1:3]")
551
+ 0 # No elements cleared since specified slice has already been cleared.
552
+ >>> await glide_json.get(client, "doc", "$..a")
553
+ b'[1,[5,0,0],true,"value",3.5,{"foo":"foo"}]'
554
+
555
+ >>> await glide_json.clear(client, "doc", "$..a")
556
+ 6 # All numeric, boolean, and string values across paths are cleared.
557
+ >>> await glide_json.get(client, "doc", "$..a")
558
+ b'[0,[],false,"",0.0,{}]'
559
+ """
560
+ args = ["JSON.CLEAR", key]
561
+ if path:
562
+ args.append(path)
563
+
564
+ return cast(int, await client.custom_command(args))
565
+
566
+
567
+ async def debug_fields(
568
+ client: TGlideClient,
569
+ key: TEncodable,
570
+ path: Optional[TEncodable] = None,
571
+ ) -> Optional[TJsonUniversalResponse[int]]:
572
+ """
573
+ Returns the number of fields of the JSON value at the specified `path` within the JSON document stored at `key`.
574
+ - **Primitive Values**: Each non-container JSON value (e.g., strings, numbers, booleans, and null) counts as one field.
575
+ - **Arrays and Objects:**: Each item in an array and each key-value pair in an object is counted as one field.
576
+ (Each top-level value counts as one field, regardless of it's type.)
577
+ - Their nested values are counted recursively and added to the total.
578
+ - **Example**: For the JSON `{"a": 1, "b": [2, 3, {"c": 4}]}`, the count would be:
579
+ - Top-level: 2 fields (`"a"` and `"b"`)
580
+ - Nested: 3 fields in the array (`2`, `3`, and `{"c": 4}`) plus 1 for the object (`"c"`)
581
+ - Total: 2 (top-level) + 3 (from array) + 1 (from nested object) = 6 fields.
582
+
583
+ Args:
584
+ client (TGlideClient): The client to execute the command.
585
+ key (TEncodable): The key of the JSON document.
586
+ path (Optional[TEncodable]): The path within the JSON document. Defaults to root if not provided.
587
+
588
+ Returns:
589
+ Optional[TJsonUniversalResponse[int]]:
590
+ For JSONPath (`path` starts with `$`):
591
+ Returns an array of integers, each indicating the number of fields for each matched `path`.
592
+ If `path` doesn't exist, an empty array will be returned.
593
+ For legacy path (`path` doesn't start with `$`):
594
+ Returns an integer indicating the number of fields for each matched `path`.
595
+ If multiple paths match, number of fields of the first JSON value match is returned.
596
+ If `path` doesn't exist, an error is raised.
597
+ If `path` is not provided, it reports the total number of fields in the entire JSON document.
598
+ If `key` doesn't exist, None is returned.
599
+ For more information about the returned type, see `TJsonUniversalResponse`.
600
+
601
+ Examples:
602
+ >>> from glide import glide_json
603
+ >>> await glide_json.set(client, "k1", "$", '[1, 2.3, "foo", true, null, {}, [], {"a":1, "b":2}, [1,2,3]]')
604
+ 'OK'
605
+ >>> await glide_json.debug_fields(client, "k1", "$[*]")
606
+ [1, 1, 1, 1, 1, 0, 0, 2, 3]
607
+ >>> await glide_json.debug_fields(client, "k1", ".")
608
+ 14 # 9 top-level fields + 5 nested address fields
609
+
610
+ >>> await glide_json.set(
611
+ ... client,
612
+ ... "k1",
613
+ ... "$",
614
+ ... (
615
+ ... '{"firstName":"John", '
616
+ ... '"lastName":"Smith", '
617
+ ... '"age":27, '
618
+ ... '"weight":135.25, '
619
+ ... '"isAlive":true, '
620
+ ... '"address":{"street":"21 2nd Street","city":"New York","state":"NY","zipcode":"10021-3100"}, '
621
+ ... '"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}], '
622
+ ... '"children":[], '
623
+ ... '"spouse":null}'
624
+ ... )
625
+ ... )
626
+ 'OK'
627
+ >>> await glide_json.debug_fields(client, "k1")
628
+ 19
629
+ >>> await glide_json.debug_fields(client, "k1", ".address")
630
+ 4
631
+ """
632
+ args = ["JSON.DEBUG", "FIELDS", key]
633
+ if path:
634
+ args.append(path)
635
+
636
+ return cast(
637
+ Optional[TJsonUniversalResponse[int]], await client.custom_command(args)
638
+ )
639
+
640
+
641
+ async def debug_memory(
642
+ client: TGlideClient,
643
+ key: TEncodable,
644
+ path: Optional[TEncodable] = None,
645
+ ) -> Optional[TJsonUniversalResponse[int]]:
646
+ """
647
+ Reports memory usage in bytes of a JSON value at the specified `path` within the JSON document stored at `key`.
648
+
649
+ Args:
650
+ client (TGlideClient): The client to execute the command.
651
+ key (TEncodable): The key of the JSON document.
652
+ path (Optional[TEncodable]): The path within the JSON document. Defaults to None.
653
+
654
+ Returns:
655
+ Optional[TJsonUniversalResponse[int]]:
656
+ For JSONPath (`path` starts with `$`):
657
+ Returns an array of integers, indicating the memory usage in bytes of a JSON value for each matched `path`.
658
+ If `path` doesn't exist, an empty array will be returned.
659
+ For legacy path (`path` doesn't start with `$`):
660
+ Returns an integer, indicating the memory usage in bytes for the JSON value in `path`.
661
+ If multiple paths match, the memory usage of the first JSON value match is returned.
662
+ If `path` doesn't exist, an error is raised.
663
+ If `path` is not provided, it reports the total memory usage in bytes in the entire JSON document.
664
+ If `key` doesn't exist, None is returned.
665
+ For more information about the returned type, see `TJsonUniversalResponse`.
666
+
667
+ Examples:
668
+ >>> from glide import glide_json
669
+ >>> await glide_json.set(client, "k1", "$", '[1, 2.3, "foo", true, null, {}, [], {"a":1, "b":2}, [1,2,3]]')
670
+ 'OK'
671
+ >>> await glide_json.debug_memory(client, "k1", "$[*]")
672
+ [16, 16, 19, 16, 16, 16, 16, 66, 64]
673
+
674
+ >>> await glide_json.set(
675
+ ... client,
676
+ ... "k1",
677
+ ... "$",
678
+ ... (
679
+ ... '{"firstName":"John", '
680
+ ... '"lastName":"Smith", '
681
+ ... '"age":27, '
682
+ ... '"weight":135.25, '
683
+ ... '"isAlive":true, '
684
+ ... '"address":{"street":"21 2nd Street","city":"New York","state":"NY","zipcode":"10021-3100"}, '
685
+ ... '"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}], '
686
+ ... '"children":[], '
687
+ ... '"spouse":null}'
688
+ ... )
689
+ ... )
690
+ 'OK'
691
+ >>> await glide_json.debug_memory(client, "k1")
692
+ 472
693
+ >>> await glide_json.debug_memory(client, "k1", ".phoneNumbers")
694
+ 164
695
+ """
696
+ args = ["JSON.DEBUG", "MEMORY", key]
697
+ if path:
698
+ args.append(path)
699
+
700
+ return cast(
701
+ Optional[TJsonUniversalResponse[int]], await client.custom_command(args)
702
+ )
703
+
704
+
705
+ async def delete(
706
+ client: TGlideClient,
707
+ key: TEncodable,
708
+ path: Optional[TEncodable] = None,
709
+ ) -> int:
710
+ """
711
+ Deletes the JSON value at the specified `path` within the JSON document stored at `key`.
712
+
713
+ Args:
714
+ client (TGlideClient): The client to execute the command.
715
+ key (TEncodable): The key of the JSON document.
716
+ path (Optional[TEncodable]): The path within the JSON document.
717
+ If None, deletes the entire JSON document at `key`. Defaults to None.
718
+
719
+ Returns:
720
+ int: The number of elements removed.
721
+ If `key` or `path` doesn't exist, returns 0.
722
+
723
+ Examples:
724
+ >>> from glide import glide_json
725
+ >>> await glide_json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
726
+ 'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
727
+ >>> await glide_json.delete(client, "doc", "$..a")
728
+ 2 # Indicates successful deletion of the specific values in the key stored at `doc`.
729
+ >>> await glide_json.get(client, "doc", "$")
730
+ "[{\"nested\":{\"b\":3}}]" # Returns the value at path '$' in the JSON document stored at `doc`.
731
+ >>> await glide_json.delete(client, "doc")
732
+ 1 # Deletes the entire JSON document stored at `doc`.
733
+ """
734
+
735
+ return cast(
736
+ int, await client.custom_command(["JSON.DEL", key] + ([path] if path else []))
737
+ )
738
+
739
+
740
+ async def forget(
741
+ client: TGlideClient,
742
+ key: TEncodable,
743
+ path: Optional[TEncodable] = None,
744
+ ) -> Optional[int]:
745
+ """
746
+ Deletes the JSON value at the specified `path` within the JSON document stored at `key`.
747
+
748
+ Args:
749
+ client (TGlideClient): The client to execute the command.
750
+ key (TEncodable): The key of the JSON document.
751
+ path (Optional[TEncodable]): The path within the JSON document.
752
+ If None, deletes the entire JSON document at `key`. Defaults to None.
753
+
754
+ Returns:
755
+ int: The number of elements removed.
756
+ If `key` or `path` doesn't exist, returns 0.
757
+
758
+ Examples:
759
+ >>> from glide import glide_json
760
+ >>> await glide_json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
761
+ 'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
762
+ >>> await glide_json.forget(client, "doc", "$..a")
763
+ 2 # Indicates successful deletion of the specific values in the key stored at `doc`.
764
+ >>> await glide_json.get(client, "doc", "$")
765
+ "[{\"nested\":{\"b\":3}}]" # Returns the value at path '$' in the JSON document stored at `doc`.
766
+ >>> await glide_json.forget(client, "doc")
767
+ 1 # Deletes the entire JSON document stored at `doc`.
768
+ """
769
+
770
+ return cast(
771
+ Optional[int],
772
+ await client.custom_command(["JSON.FORGET", key] + ([path] if path else [])),
773
+ )
774
+
775
+
776
+ async def mget(
777
+ client: TGlideClient,
778
+ keys: List[TEncodable],
779
+ path: TEncodable,
780
+ ) -> List[Optional[bytes]]:
781
+ """
782
+ Retrieves the JSON values at the specified `path` stored at multiple `keys`.
783
+
784
+ Note:
785
+ In cluster mode, if keys in `keys` map to different hash slots, the command
786
+ will be split across these slots and executed separately for each. This means the command
787
+ is atomic only at the slot level. If one or more slot-specific requests fail, the entire
788
+ call will return the first encountered error, even though some requests may have succeeded
789
+ while others did not. If this behavior impacts your application logic, consider splitting
790
+ the request into sub-requests per slot to ensure atomicity.
791
+
792
+ Args:
793
+ client (TGlideClient): The client to execute the command.
794
+ keys (List[TEncodable]): A list of keys for the JSON documents.
795
+ path (TEncodable): The path within the JSON documents.
796
+
797
+ Returns:
798
+ List[Optional[bytes]]:
799
+ For JSONPath (`path` starts with `$`):
800
+ Returns a list of byte representations of the values found at the given path for each key.
801
+ If `path` does not exist within the key, the entry will be an empty array.
802
+ For legacy path (`path` doesn't starts with `$`):
803
+ Returns a list of byte representations of the values found at the given path for each key.
804
+ If `path` does not exist within the key, the entry will be None.
805
+ If a key doesn't exist, the corresponding list element will be None.
806
+
807
+
808
+ Examples:
809
+ >>> from glide import glide_json
810
+ >>> import json
811
+ >>> json_strs = await glide_json.mget(client, ["doc1", "doc2"], "$")
812
+ >>> [json.loads(js) for js in json_strs] # Parse JSON strings to Python data
813
+ [[{"a": 1.0, "b": 2}], [{"a": 2.0, "b": {"a": 3.0, "b" : 4.0}}]] # JSON objects retrieved from keys
814
+ # `doc1` and `doc2`
815
+ >>> await glide_json.mget(client, ["doc1", "doc2"], "$.a")
816
+ [b"[1.0]", b"[2.0]"] # Returns values at path '$.a' for the JSON documents stored at `doc1` and `doc2`.
817
+ >>> await glide_json.mget(client, ["doc1"], "$.non_existing_path")
818
+ [None] # Returns an empty array since the path '$.non_existing_path' does not exist in the JSON document
819
+ # stored at `doc1`.
820
+ """
821
+ args = ["JSON.MGET"] + keys + [path]
822
+ return cast(List[Optional[bytes]], await client.custom_command(args))
823
+
824
+
825
+ async def numincrby(
826
+ client: TGlideClient,
827
+ key: TEncodable,
828
+ path: TEncodable,
829
+ number: Union[int, float],
830
+ ) -> bytes:
831
+ """
832
+ Increments or decrements the JSON value(s) at the specified `path` by `number` within the JSON document stored at `key`.
833
+
834
+ Args:
835
+ client (TGlideClient): The client to execute the command.
836
+ key (TEncodable): The key of the JSON document.
837
+ path (TEncodable): The path within the JSON document.
838
+ number (Union[int, float]): The number to increment or decrement by.
839
+
840
+ Returns:
841
+ bytes:
842
+ For JSONPath (`path` starts with `$`):
843
+ Returns a bytes string representation of an array of bulk strings, indicating the new values after
844
+ incrementing for each matched `path`.
845
+ If a value is not a number, its corresponding return value will be `null`.
846
+ If `path` doesn't exist, a byte string representation of an empty array will be returned.
847
+ For legacy path (`path` doesn't start with `$`):
848
+ Returns a bytes string representation of the resulting value after the increment or decrement.
849
+ If multiple paths match, the result of the last updated value is returned.
850
+ If the value at the `path` is not a number or `path` doesn't exist, an error is raised.
851
+ If `key` does not exist, an error is raised.
852
+ If the result is out of the range of 64-bit IEEE double, an error is raised.
853
+
854
+ Examples:
855
+ >>> from glide import glide_json
856
+ >>> await glide_json.set(client, "doc", "$", '{"a": [], "b": [1], "c": [1, 2], "d": [1, 2, 3]}')
857
+ 'OK'
858
+ >>> await glide_json.numincrby(client, "doc", "$.d[*]", 10)
859
+ b'[11,12,13]' # Increment each element in `d` array by 10.
860
+ >>> await glide_json.numincrby(client, "doc", ".c[1]", 10)
861
+ b'12' # Increment the second element in the `c` array by 10.
862
+ """
863
+ args = ["JSON.NUMINCRBY", key, path, str(number)]
864
+
865
+ return cast(bytes, await client.custom_command(args))
866
+
867
+
868
+ async def nummultby(
869
+ client: TGlideClient,
870
+ key: TEncodable,
871
+ path: TEncodable,
872
+ number: Union[int, float],
873
+ ) -> bytes:
874
+ """
875
+ Multiplies the JSON value(s) at the specified `path` by `number` within the JSON document stored at `key`.
876
+
877
+ Args:
878
+ client (TGlideClient): The client to execute the command.
879
+ key (TEncodable): The key of the JSON document.
880
+ path (TEncodable): The path within the JSON document.
881
+ number (Union[int, float]): The number to multiply by.
882
+
883
+ Returns:
884
+ bytes:
885
+ For JSONPath (`path` starts with `$`):
886
+ Returns a bytes string representation of an array of bulk strings, indicating the new values after
887
+ multiplication for each matched `path`.
888
+ If a value is not a number, its corresponding return value will be `null`.
889
+ If `path` doesn't exist, a byte string representation of an empty array will be returned.
890
+ For legacy path (`path` doesn't start with `$`):
891
+ Returns a bytes string representation of the resulting value after multiplication.
892
+ If multiple paths match, the result of the last updated value is returned.
893
+ If the value at the `path` is not a number or `path` doesn't exist, an error is raised.
894
+ If `key` does not exist, an error is raised.
895
+ If the result is out of the range of 64-bit IEEE double, an error is raised.
896
+
897
+ Examples:
898
+ >>> from glide import glide_json
899
+ >>> await glide_json.set(client, "doc", "$", '{"a": [], "b": [1], "c": [1, 2], "d": [1, 2, 3]}')
900
+ 'OK'
901
+ >>> await glide_json.nummultby(client, "doc", "$.d[*]", 2)
902
+ b'[2,4,6]' # Multiplies each element in the `d` array by 2.
903
+ >>> await glide_json.nummultby(client, "doc", ".c[1]", 2)
904
+ b'4' # Multiplies the second element in the `c` array by 2.
905
+ """
906
+ args = ["JSON.NUMMULTBY", key, path, str(number)]
907
+
908
+ return cast(bytes, await client.custom_command(args))
909
+
910
+
911
+ async def objlen(
912
+ client: TGlideClient,
913
+ key: TEncodable,
914
+ path: Optional[TEncodable] = None,
915
+ ) -> Optional[TJsonResponse[int]]:
916
+ """
917
+ Retrieves the number of key-value pairs in the object stored at the specified `path` within the JSON document stored at
918
+ `key`.
919
+
920
+ Args:
921
+ client (TGlideClient): The client to execute the command.
922
+ key (TEncodable): The key of the JSON document.
923
+ path (Optional[TEncodable]): The path within the JSON document. Defaults to None.
924
+
925
+ Returns:
926
+ Optional[TJsonResponse[int]]:
927
+ For JSONPath (`path` starts with `$`):
928
+ Returns a list of integer replies for every possible path, indicating the length of the object,
929
+ or None for JSON values matching the path that are not an object.
930
+ If `path` doesn't exist, an empty array will be returned.
931
+ For legacy path (`path` doesn't starts with `$`):
932
+ Returns the length of the object at `path`.
933
+ If multiple paths match, the length of the first object match is returned.
934
+ If the JSON value at `path` is not an object or if `path` doesn't exist, an error is raised.
935
+ If `key` doesn't exist, None is returned.
936
+ For more information about the returned type, see `TJsonResponse`.
937
+
938
+
939
+ Examples:
940
+ >>> from glide import glide_json
941
+ >>> await glide_json.set(client, "doc", "$", '{"a": 1.0, "b": {"a": {"x": 1, "y": 2}, "b": 2.5, "c": true}}')
942
+ b'OK' # Indicates successful setting of the value at the root path '$' in the key `doc`.
943
+ >>> await glide_json.objlen(client, "doc", "$")
944
+ [2] # Returns the number of key-value pairs at the root object, which has 2 keys: 'a' and 'b'.
945
+ >>> await glide_json.objlen(client, "doc", ".")
946
+ 2 # Returns the number of key-value pairs for the object matching the path '.', which has 2 keys: 'a' and 'b'.
947
+ >>> await glide_json.objlen(client, "doc", "$.b")
948
+ [3] # Returns the length of the object at path '$.b', which has 3 keys: 'a', 'b', and 'c'.
949
+ >>> await glide_json.objlen(client, "doc", ".b")
950
+ 3 # Returns the length of the nested object at path '.b', which has 3 keys.
951
+ >>> await glide_json.objlen(client, "doc", "$..a")
952
+ [None, 2]
953
+ >>> await glide_json.objlen(client, "doc")
954
+ 2 # Returns the number of key-value pairs for the object matching the path '.', which has 2 keys: 'a' and 'b'.
955
+ """
956
+ args = ["JSON.OBJLEN", key]
957
+ if path:
958
+ args.append(path)
959
+ return cast(
960
+ Optional[TJsonResponse[int]],
961
+ await client.custom_command(args),
962
+ )
963
+
964
+
965
+ async def objkeys(
966
+ client: TGlideClient,
967
+ key: TEncodable,
968
+ path: Optional[TEncodable] = None,
969
+ ) -> Optional[TJsonUniversalResponse[List[bytes]]]:
970
+ """
971
+ Retrieves key names in the object values at the specified `path` within the JSON document stored at `key`.
972
+
973
+ Args:
974
+ client (TGlideClient): The client to execute the command.
975
+ key (TEncodable): The key of the JSON document.
976
+ path (Optional[TEncodable]): Represents the path within the JSON document where the key names will be retrieved.
977
+ Defaults to None.
978
+
979
+ Returns:
980
+ Optional[TJsonUniversalResponse[List[bytes]]]:
981
+ For JSONPath (`path` starts with `$`):
982
+ Returns a list of arrays containing key names for each matching object.
983
+ If a value matching the path is not an object, an empty array is returned.
984
+ If `path` doesn't exist, an empty array is returned.
985
+ For legacy path (`path` starts with `.`):
986
+ Returns a list of key names for the object value matching the path.
987
+ If multiple objects match the path, the key names of the first object are returned.
988
+ If a value matching the path is not an object, an error is raised.
989
+ If `path` doesn't exist, None is returned.
990
+ If `key` doesn't exist, None is returned.
991
+ For more information about the returned type, see `TJsonUniversalResponse`.
992
+
993
+ Examples:
994
+ >>> from glide import glide_json
995
+ >>> await glide_json.set(client, "doc", "$", '{"a": 1.0, "b": {"a": {"x": 1, "y": 2}, "b": 2.5, "c": true}}')
996
+ b'OK' # Indicates successful setting of the value at the root path '$' in the key `doc`.
997
+ >>> await glide_json.objkeys(client, "doc", "$")
998
+ [[b"a", b"b"]] # Returns a list of arrays containing the key names for objects matching the path '$'.
999
+ >>> await glide_json.objkeys(client, "doc", ".")
1000
+ [b"a", b"b"] # Returns key names for the object matching the path '.' as it is the only match.
1001
+ >>> await glide_json.objkeys(client, "doc", "$.b")
1002
+ [[b"a", b"b", b"c"]] # Returns key names as a nested list for objects matching the JSONPath '$.b'.
1003
+ >>> await glide_json.objkeys(client, "doc", ".b")
1004
+ [b"a", b"b", b"c"] # Returns key names for the nested object at path '.b'.
1005
+ """
1006
+ args = ["JSON.OBJKEYS", key]
1007
+ if path:
1008
+ args.append(path)
1009
+ return cast(
1010
+ Optional[Union[List[bytes], List[List[bytes]]]],
1011
+ await client.custom_command(args),
1012
+ )
1013
+
1014
+
1015
+ async def resp(
1016
+ client: TGlideClient, key: TEncodable, path: Optional[TEncodable] = None
1017
+ ) -> TJsonUniversalResponse[
1018
+ Optional[Union[bytes, int, List[Optional[Union[bytes, int]]]]]
1019
+ ]:
1020
+ """
1021
+ Retrieve the JSON value at the specified `path` within the JSON document stored at `key`.
1022
+ The returning result is in the Valkey or Redis OSS Serialization Protocol (RESP).\n
1023
+ JSON null is mapped to the RESP Null Bulk String.\n
1024
+ JSON Booleans are mapped to RESP Simple string.\n
1025
+ JSON integers are mapped to RESP Integers.\n
1026
+ JSON doubles are mapped to RESP Bulk Strings.\n
1027
+ JSON strings are mapped to RESP Bulk Strings.\n
1028
+ JSON arrays are represented as RESP arrays, where the first element is the simple string [, followed by the array's
1029
+ elements.\n
1030
+ JSON objects are represented as RESP object, where the first element is the simple string {, followed by key-value pairs,
1031
+ each of which is a RESP bulk string.\n
1032
+
1033
+
1034
+ Args:
1035
+ client (TGlideClient): The client to execute the command.
1036
+ key (TEncodable): The key of the JSON document.
1037
+ path (Optional[TEncodable]): The path within the JSON document. Default to None.
1038
+
1039
+ Returns:
1040
+ TJsonUniversalResponse[Optional[Union[bytes, int, List[Optional[Union[bytes, int]]]]]]
1041
+ For JSONPath ('path' starts with '$'):
1042
+ Returns a list of replies for every possible path, indicating the RESP form of the JSON value.
1043
+ If `path` doesn't exist, returns an empty list.
1044
+ For legacy path (`path` doesn't starts with `$`):
1045
+ Returns a single reply for the JSON value at the specified path, in its RESP form.
1046
+ This can be a bytes object, an integer, None, or a list representing complex structures.
1047
+ If multiple paths match, the value of the first JSON value match is returned.
1048
+ If `path` doesn't exist, an error is raised.
1049
+ If `key` doesn't exist, an None is returned.
1050
+ For more information about the returned type, see `TJsonUniversalResponse`.
1051
+
1052
+ Examples:
1053
+ >>> from glide import glide_json
1054
+ >>> await glide_json.set(client, "doc", "$", '{"a": [1, 2, 3], "b": {"a": [1, 2], "c": {"a": 42}}}')
1055
+ 'OK'
1056
+ >>> await glide_json.resp(client, "doc", "$..a")
1057
+ [[b"[", 1, 2, 3],[b"[", 1, 2],42]
1058
+ >>> await glide_json.resp(client, "doc", "..a")
1059
+ [b"[", 1, 2, 3]
1060
+ """
1061
+ args = ["JSON.RESP", key]
1062
+ if path:
1063
+ args.append(path)
1064
+
1065
+ return cast(
1066
+ TJsonUniversalResponse[
1067
+ Optional[Union[bytes, int, List[Optional[Union[bytes, int]]]]]
1068
+ ],
1069
+ await client.custom_command(args),
1070
+ )
1071
+
1072
+
1073
+ async def strappend(
1074
+ client: TGlideClient,
1075
+ key: TEncodable,
1076
+ value: TEncodable,
1077
+ path: Optional[TEncodable] = None,
1078
+ ) -> TJsonResponse[int]:
1079
+ """
1080
+ Appends the specified `value` to the string stored at the specified `path` within the JSON document stored at `key`.
1081
+
1082
+ Args:
1083
+ client (TGlideClient): The client to execute the command.
1084
+ key (TEncodable): The key of the JSON document.
1085
+ value (TEncodable): The value to append to the string. Must be wrapped with single quotes. For example,
1086
+ to append "foo", pass '"foo"'.
1087
+ path (Optional[TEncodable]): The path within the JSON document. Default to None.
1088
+
1089
+ Returns:
1090
+ TJsonResponse[int]:
1091
+ For JSONPath (`path` starts with `$`):
1092
+ Returns a list of integer replies for every possible path, indicating the length of the resulting string after
1093
+ appending `value`,
1094
+ or None for JSON values matching the path that are not string.
1095
+ If `key` doesn't exist, an error is raised.
1096
+ For legacy path (`path` doesn't start with `$`):
1097
+ Returns the length of the resulting string after appending `value` to the string at `path`.
1098
+ If multiple paths match, the length of the last updated string is returned.
1099
+ If the JSON value at `path` is not a string of if `path` doesn't exist, an error is raised.
1100
+ If `key` doesn't exist, an error is raised.
1101
+ For more information about the returned type, see `TJsonResponse`.
1102
+
1103
+ Examples:
1104
+ >>> from glide import glide_json
1105
+ >>> import json
1106
+ >>> await glide_json.set(client, "doc", "$", json.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
1107
+ 'OK'
1108
+ >>> await glide_json.strappend(client, "doc", json.dumps("baz"), "$..a")
1109
+ [6, 8, None] # The new length of the string values at path '$..a' in the key stored at `doc` after the append
1110
+ # operation.
1111
+ >>> await glide_json.strappend(client, "doc", '"foo"', "nested.a")
1112
+ 11 # The length of the string value after appending "foo" to the string at path 'nested.array' in the key stored
1113
+ # at `doc`.
1114
+ >>> json.loads(await glide_json.get(client, json.dumps("doc"), "$"))
1115
+ [{"a":"foobaz", "nested": {"a": "hellobazfoo"}, "nested2": {"a": 31}}] # The updated JSON value in the key stored
1116
+ # at `doc`.
1117
+ """
1118
+
1119
+ return cast(
1120
+ TJsonResponse[int],
1121
+ await client.custom_command(
1122
+ ["JSON.STRAPPEND", key] + ([path, value] if path else [value])
1123
+ ),
1124
+ )
1125
+
1126
+
1127
+ async def strlen(
1128
+ client: TGlideClient,
1129
+ key: TEncodable,
1130
+ path: Optional[TEncodable] = None,
1131
+ ) -> TJsonResponse[Optional[int]]:
1132
+ """
1133
+ Returns the length of the JSON string value stored at the specified `path` within the JSON document stored at `key`.
1134
+
1135
+ Args:
1136
+ client (TGlideClient): The client to execute the command.
1137
+ key (TEncodable): The key of the JSON document.
1138
+ path (Optional[TEncodable]): The path within the JSON document. Default to None.
1139
+
1140
+ Returns:
1141
+ TJsonResponse[Optional[int]]:
1142
+ For JSONPath (`path` starts with `$`):
1143
+ Returns a list of integer replies for every possible path, indicating the length of the JSON string value,
1144
+ or None for JSON values matching the path that are not string.
1145
+ For legacy path (`path` doesn't start with `$`):
1146
+ Returns the length of the JSON value at `path` or None if `key` doesn't exist.
1147
+ If multiple paths match, the length of the first mached string is returned.
1148
+ If the JSON value at `path` is not a string of if `path` doesn't exist, an error is raised.
1149
+ If `key` doesn't exist, None is returned.
1150
+ For more information about the returned type, see `TJsonResponse`.
1151
+
1152
+ Examples:
1153
+ >>> from glide import glide_json
1154
+ >>> import json
1155
+ >>> await glide_json.set(client, "doc", "$", json.dumps({"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}))
1156
+ 'OK'
1157
+ >>> await glide_json.strlen(client, "doc", "$..a")
1158
+ [3, 5, None] # The length of the string values at path '$..a' in the key stored at `doc`.
1159
+ >>> await glide_json.strlen(client, "doc", "nested.a")
1160
+ 5 # The length of the JSON value at path 'nested.a' in the key stored at `doc`.
1161
+ >>> await glide_json.strlen(client, "doc", "$")
1162
+ [None] # Returns an array with None since the value at root path does in the JSON document stored at `doc` is not
1163
+ # a string.
1164
+ >>> await glide_json.strlen(client, "non_existing_key", ".")
1165
+ None # `key` doesn't exist.
1166
+ """
1167
+
1168
+ return cast(
1169
+ TJsonResponse[Optional[int]],
1170
+ await client.custom_command(
1171
+ ["JSON.STRLEN", key, path] if path else ["JSON.STRLEN", key]
1172
+ ),
1173
+ )
1174
+
1175
+
1176
+ async def toggle(
1177
+ client: TGlideClient,
1178
+ key: TEncodable,
1179
+ path: TEncodable,
1180
+ ) -> TJsonResponse[bool]:
1181
+ """
1182
+ Toggles a Boolean value stored at the specified `path` within the JSON document stored at `key`.
1183
+
1184
+ Args:
1185
+ client (TGlideClient): The client to execute the command.
1186
+ key (TEncodable): The key of the JSON document.
1187
+ path (TEncodable): The path within the JSON document. Default to None.
1188
+
1189
+ Returns:
1190
+ TJsonResponse[bool]:
1191
+ For JSONPath (`path` starts with `$`):
1192
+ Returns a list of boolean replies for every possible path, with the toggled boolean value,
1193
+ or None for JSON values matching the path that are not boolean.
1194
+ If `key` doesn't exist, an error is raised.
1195
+ For legacy path (`path` doesn't start with `$`):
1196
+ Returns the value of the toggled boolean in `path`.
1197
+ If the JSON value at `path` is not a boolean of if `path` doesn't exist, an error is raised.
1198
+ If `key` doesn't exist, an error is raised.
1199
+ For more information about the returned type, see `TJsonResponse`.
1200
+
1201
+ Examples:
1202
+ >>> from glide import glide_json
1203
+ >>> import json
1204
+ >>> await glide_json.set(
1205
+ ... client,
1206
+ ... "doc",
1207
+ ... "$",
1208
+ ... json.dumps({"bool": True, "nested": {"bool": False, "nested": {"bool": 10}}})
1209
+ ... )
1210
+ 'OK'
1211
+ >>> await glide_json.toggle(client, "doc", "$.bool")
1212
+ [False, True, None] # Indicates successful toggling of the Boolean values at path '$.bool' in the key stored at
1213
+ # `doc`.
1214
+ >>> await glide_json.toggle(client, "doc", "bool")
1215
+ True # Indicates successful toggling of the Boolean value at path 'bool' in the key stored at `doc`.
1216
+ >>> json.loads(await glide_json.get(client, "doc", "$"))
1217
+ [{"bool": True, "nested": {"bool": True, "nested": {"bool": 10}}}] # The updated JSON value in the key stored at
1218
+ # `doc`.
1219
+ """
1220
+
1221
+ return cast(
1222
+ TJsonResponse[bool],
1223
+ await client.custom_command(["JSON.TOGGLE", key, path]),
1224
+ )
1225
+
1226
+
1227
+ async def type(
1228
+ client: TGlideClient,
1229
+ key: TEncodable,
1230
+ path: Optional[TEncodable] = None,
1231
+ ) -> Optional[TJsonUniversalResponse[bytes]]:
1232
+ """
1233
+ Retrieves the type of the JSON value at the specified `path` within the JSON document stored at `key`.
1234
+
1235
+ Args:
1236
+ client (TGlideClient): The client to execute the command.
1237
+ key (TEncodable): The key of the JSON document.
1238
+ path (Optional[TEncodable]): The path within the JSON document. Default to None.
1239
+
1240
+ Returns:
1241
+ Optional[TJsonUniversalResponse[bytes]]:
1242
+ For JSONPath ('path' starts with '$'):
1243
+ Returns a list of byte string replies for every possible path, indicating the type of the JSON value.
1244
+ If `path` doesn't exist, an empty array will be returned.
1245
+ For legacy path (`path` doesn't starts with `$`):
1246
+ Returns the type of the JSON value at `path`.
1247
+ If multiple paths match, the type of the first JSON value match is returned.
1248
+ If `path` doesn't exist, None will be returned.
1249
+ If `key` doesn't exist, None is returned.
1250
+ For more information about the returned type, see `TJsonUniversalResponse`.
1251
+
1252
+ Examples:
1253
+ >>> from glide import glide_json
1254
+ >>> await glide_json.set(client, "doc", "$", '{"a": 1, "nested": {"a": 2, "b": 3}}')
1255
+ 'OK'
1256
+ >>> await glide_json.type(client, "doc", "$.nested")
1257
+ [b'object'] # Indicates the type of the value at path '$.nested' in the key stored at `doc`.
1258
+ >>> await glide_json.type(client, "doc", "$.nested.a")
1259
+ [b'integer'] # Indicates the type of the value at path '$.nested.a' in the key stored at `doc`.
1260
+ >>> await glide_json.type(client, "doc", "$[*]")
1261
+ [b'integer', b'object'] # Array of types in all top level elements.
1262
+ """
1263
+ args = ["JSON.TYPE", key]
1264
+ if path:
1265
+ args.append(path)
1266
+
1267
+ return cast(
1268
+ Optional[TJsonUniversalResponse[bytes]], await client.custom_command(args)
1269
+ )