@@ -54,6 +54,36 @@ def get_options(self) -> List[str]:
5454 return args
5555
5656
57+ from typing import List , Optional , Union
58+
59+
60+ class JsonArrPopOptions :
61+ """
62+ Options for the JSON.ARRPOP command.
63+
64+ Args:
65+ path (TEncodable): The path within the JSON document.
66+ index (Optional[int]): The index of the element to pop. If not specified, will pop the last element.
67+ Out of boundary indexes are rounded to their respective array boundaries. Defaults to None.
68+ """
69+
70+ def __init__ (self , path : TEncodable , index : Optional [int ] = None ):
71+ self .path = path
72+ self .index = index
73+
74+ def get_options (self ) -> List [TEncodable ]:
75+ """
76+ Get the options as a list of arguments for the JSON.ARRPOP command.
77+
78+ Returns:
79+ List[TEncodable]: A list containing the path and, if specified, the index.
80+ """
81+ args = [self .path ]
82+ if self .index is not None :
83+ args .append (str (self .index ))
84+ return args
85+
86+
5787async def set (
5888 client : TGlideClient ,
5989 key : TEncodable ,
@@ -193,6 +223,66 @@ async def arrlen(
193223 )
194224
195225
226+ async def arrpop (
227+ client : TGlideClient ,
228+ key : TEncodable ,
229+ options : Optional [JsonArrPopOptions ] = None ,
230+ ) -> Optional [TJsonResponse [bytes ]]:
231+ """
232+ Pops the last element from the array located at the specified path within the JSON document stored at `key`.
233+ If `options.index` is provided, it pops the element at that index instead of the last element.
234+
235+ See https://valkey.io/commands/json.arrpop/ for more details.
236+
237+ Args:
238+ client (TGlideClient): The client to execute the command.
239+ key (TEncodable): The key of the JSON document.
240+ options (Optional[JsonArrPopOptions]): Options including the path and optional index. See `JsonArrPopOptions`.
241+
242+ Returns:
243+ Optional[TJsonResponse[bytes]]:
244+ For JSONPath (`path` starts with `$`):
245+ Returns a list of bytes string replies for every possible path, representing the popped JSON values,
246+ or None for JSON values matching the path that are not an array or are an empty array.
247+ If a value is not an array, its corresponding return value is null.
248+ For legacy path (`path` starts with `.`):
249+ Returns a bytes string representing the popped JSON value, or None if the array at `path` is empty.
250+ If multiple paths match, the value from the first matching array that is not empty is returned.
251+ If the JSON value at `path` is not a array or if `path` doesn't exist, an error is raised.
252+ If `key` doesn't exist, an error is raised.
253+
254+ Examples:
255+ >>> from glide import json
256+ >>> await json.set(client, "doc", "$", '{"a": [1, 2, true], "b": {"a": [3, 4, ["value", 3, false], 5], "c": {"a": 42}}}')
257+ b'OK' # JSON is successfully set
258+ >>> await json.arrpop(client, "doc", JsonArrPopOptions(path="$.a", index=1))
259+ [b'2'] # Pop second element from array at path $.a
260+ >>> await json.arrpop(client, "doc", JsonArrPopOptions(path="$..a"))
261+ [b'true', b'5', None] # Pop last elements from all arrays matching path `..a`
262+
263+ #### Using a legacy path (..) to pop the first matching array
264+ >>> await json.arrpop(client, "doc", JsonArrPopOptions(path="..a"))
265+ b"1" # First match popped (from array at path $.a)
266+
267+ #### Even though only one value is returned from `..a`, subsequent arrays are also affected
268+ >>> await json.get(client, "doc", "$..a")
269+ b"[[], [3, 4], 42]" # Remaining elements after pop show the changes
270+
271+ >>> await json.set(client, "doc", "$", '[[], ["a"], ["a", "b", "c"]]')
272+ b'OK' # JSON is successfully set
273+ >>> await json.arrpop(client, "doc", JsonArrPopOptions(path=".", index=-1))
274+ b'["a","b","c"]' # Pop last elements at path `.`
275+ """
276+ args = ["JSON.ARRPOP" , key ]
277+ if options :
278+ args .extend (options .get_options ())
279+
280+ return cast (
281+ Optional [TJsonResponse [bytes ]],
282+ await client .custom_command (args ),
283+ )
284+
285+
196286async def clear (
197287 client : TGlideClient ,
198288 key : TEncodable ,
0 commit comments