Skip to content

Commit 66704bc

Browse files
shacharPashchayimdvora-h
authored andcommitted
Support JSON.MSET Command (redis#2766)
* support JSON.MERGE Command * linters * try with abc instead person * change @skip_ifmodversion_lt to latest ReJSON 2.4.7 * change version * fix test * linters * add async test * Support JSON.MSET command * trying to run CI * linters * add async test * reminder do delete the integration changes * delete the line from integration * fix the interface * change docstring --------- Co-authored-by: Chayim <[email protected]> Co-authored-by: dvora-h <[email protected]>
1 parent 7c7e1ef commit 66704bc

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

redis/commands/json/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(
3838
"JSON.GET": self._decode,
3939
"JSON.MGET": bulk_of_jsons(self._decode),
4040
"JSON.SET": lambda r: r and nativestr(r) == "OK",
41+
"JSON.MSET": lambda r: r and nativestr(r) == "OK",
4142
"JSON.MERGE": lambda r: r and nativestr(r) == "OK",
4243
"JSON.NUMINCRBY": self._decode,
4344
"JSON.NUMMULTBY": self._decode,

redis/commands/json/commands.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from json import JSONDecodeError, loads
3-
from typing import Dict, List, Optional, Union
3+
from typing import Dict, List, Optional, Tuple, Union
44

55
from redis.exceptions import DataError
66
from redis.utils import deprecated_function
@@ -253,6 +253,23 @@ def set(
253253
pieces.append("XX")
254254
return self.execute_command("JSON.SET", *pieces)
255255

256+
def mset(self, triplets: List[Tuple[str, str, JsonType]]) -> Optional[str]:
257+
"""
258+
Set the JSON value at key ``name`` under the ``path`` to ``obj``
259+
for one or more keys.
260+
261+
``triplets`` is a list of one or more triplets of key, path, value.
262+
263+
For the purpose of using this within a pipeline, this command is also
264+
aliased to JSON.MSET.
265+
266+
For more information see `JSON.MSET <https://redis.io/commands/json.mset>`_.
267+
"""
268+
pieces = []
269+
for triplet in triplets:
270+
pieces.extend([triplet[0], str(triplet[1]), self._encode(triplet[2])])
271+
return self.execute_command("JSON.MSET", *pieces)
272+
256273
def merge(
257274
self,
258275
name: str,

tests/test_asyncio/test_json.py

+11
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ async def test_mgetshouldsucceed(modclient: redis.Redis):
109109
assert await modclient.json().mget([1, 2], Path.root_path()) == [1, 2]
110110

111111

112+
@pytest.mark.redismod
113+
@skip_ifmodversion_lt("2.6.0", "ReJSON") # todo: update after the release
114+
async def test_mset(modclient: redis.Redis):
115+
await modclient.json().mset(
116+
[("1", Path.root_path(), 1), ("2", Path.root_path(), 2)]
117+
)
118+
119+
assert await modclient.json().mget(["1"], Path.root_path()) == [1]
120+
assert await modclient.json().mget(["1", "2"], Path.root_path()) == [1, 2]
121+
122+
112123
@pytest.mark.redismod
113124
@skip_ifmodversion_lt("99.99.99", "ReJSON") # todo: update after the release
114125
async def test_clear(modclient: redis.Redis):

tests/test_json.py

+9
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ def test_mgetshouldsucceed(client):
115115
assert client.json().mget([1, 2], Path.root_path()) == [1, 2]
116116

117117

118+
@pytest.mark.redismod
119+
@skip_ifmodversion_lt("2.6.0", "ReJSON") # todo: update after the release
120+
def test_mset(client):
121+
client.json().mset([("1", Path.root_path(), 1), ("2", Path.root_path(), 2)])
122+
123+
assert client.json().mget(["1"], Path.root_path()) == [1]
124+
assert client.json().mget(["1", "2"], Path.root_path()) == [1, 2]
125+
126+
118127
@pytest.mark.redismod
119128
@skip_ifmodversion_lt("99.99.99", "ReJSON") # todo: update after the release
120129
def test_clear(client):

0 commit comments

Comments
 (0)