Skip to content
13 changes: 13 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ Gets the objects stored as a JSON values under ``path`` from
keys ``args``


### jsonmgetl
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate PR

```py

def jsonmgetl(self, keys=[], path=Path.rootPath())

```



Gets the objects stored as a JSON values under ``path`` from
key list ``keys``


### jsonnumincrby
```py

Expand Down
25 changes: 24 additions & 1 deletion rejson/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,26 @@ def jsonmget(self, path, *args):
"""
pieces = []
pieces.extend(args)
if not path:
path=Path.rootPath()
pieces.append(str_path(path))
return self.execute_command('JSON.MGET', *pieces)

def jsonmgetl(self, keys=[], path=Path.rootPath()):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate PR - jsonmget's signature should change (breaking bw compat) to jsonmget(self, path=Path.rootPath(), keys=[], *args)

"""
Gets the objects stored as a JSON values under ``path`` from
key list ``keys``
"""
return self.jsonmget(path, *keys)

def jsonset(self, name, path, obj, nx=False, xx=False):
"""
Set the JSON value at key ``name`` under the ``path`` to ``obj``
``nx`` if set to True, set ``value`` only if it does not exist
``xx`` if set to True, set ``value`` only if it exists
"""
if not path:
path=Path.rootPath()
pieces = [name, str_path(path), self._encode(obj)]

# Handle existential modifiers
Expand All @@ -152,13 +163,17 @@ def jsonnumincrby(self, name, path, number):
Increments the numeric (integer or floating point) JSON value under
``path`` at key ``name`` by the provided ``number``
"""
if not path:
path=Path.rootPath()
return self.execute_command('JSON.NUMINCRBY', name, str_path(path), self._encode(number))

def jsonnummultby(self, name, path, number):
"""
Multiplies the numeric (integer or floating point) JSON value under
``path`` at key ``name`` with the provided ``number``
"""
if not path:
path=Path.rootPath()
return self.execute_command('JSON.NUMMULTBY', name, str_path(path), self._encode(number))

def jsonstrappend(self, name, string, path=Path.rootPath()):
Expand All @@ -175,11 +190,13 @@ def jsonstrlen(self, name, path=Path.rootPath()):
"""
return self.execute_command('JSON.STRLEN', name, str_path(path))

def jsonarrappend(self, name, path=Path.rootPath(), *args):
def jsonarrappend(self, name, path, *args):
"""
Appends the objects ``args`` to the array under the ``path` in key
``name``
"""
if not path:
path=Path.rootPath()
pieces = [name, str_path(path)]
for o in args:
pieces.append(self._encode(o))
Expand All @@ -191,13 +208,17 @@ def jsonarrindex(self, name, path, scalar, start=0, stop=-1):
``name``. The search can be limited using the optional inclusive
``start`` and exclusive ``stop`` indices.
"""
if not path:
path=Path.rootPath()
return self.execute_command('JSON.ARRINDEX', name, str_path(path), self._encode(scalar), start, stop)

def jsonarrinsert(self, name, path, index, *args):
"""
Inserts the objects ``args`` to the array at index ``index`` under the
``path` in key ``name``
"""
if not path:
path=Path.rootPath()
pieces = [name, str_path(path), index]
for o in args:
pieces.append(self._encode(o))
Expand All @@ -222,6 +243,8 @@ def jsonarrtrim(self, name, path, start, stop):
Trim the array JSON value under ``path`` at key ``name`` to the
inclusive range given by ``start`` and ``stop``
"""
if not path:
path=Path.rootPath()
return self.execute_command('JSON.ARRTRIM', name, str_path(path), start, stop)

def jsonobjkeys(self, name, path=Path.rootPath()):
Expand Down
34 changes: 21 additions & 13 deletions tests/test_rejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ def testMGetShouldSucceed(self):
"Test JSONMGet"

rj.jsonset('1', Path.rootPath(), 1)
rj.jsonset('2', Path.rootPath(), 2)
r = rj.jsonmget(Path.rootPath(), '1', '2')
rj.jsonset('2', None, 2)
r1 = rj.jsonmget(None, '1', '2')
r2 = rj.jsonmgetl(keys=['1', '2'])
e = [1, 2]
self.assertListEqual(e, r)
self.assertListEqual(e, r1)
self.assertListEqual(e, r2)


def testTypeShouldSucceed(self):
"Test JSONType"
Expand All @@ -62,65 +65,68 @@ def testNumIncrByShouldSucceed(self):

rj.jsonset('num', Path.rootPath(), 1)
self.assertEqual(2, rj.jsonnumincrby('num', Path.rootPath(), 1))
self.assertEqual(2.5, rj.jsonnumincrby('num', Path.rootPath(), 0.5))
self.assertEqual(2.5, rj.jsonnumincrby('num', None, 0.5))
self.assertEqual(1.25, rj.jsonnumincrby('num', Path.rootPath(), -1.25))

def testNumMultByShouldSucceed(self):
"Test JSONNumIncrBy"

rj.jsonset('num', Path.rootPath(), 1)
self.assertEqual(2, rj.jsonnummultby('num', Path.rootPath(), 2))
self.assertEqual(5, rj.jsonnummultby('num', Path.rootPath(), 2.5))
self.assertEqual(5, rj.jsonnummultby('num', None, 2.5))
self.assertEqual(2.5, rj.jsonnummultby('num', Path.rootPath(), 0.5))

def testStrAppendShouldSucceed(self):
"Test JSONStrAppend"

rj.jsonset('str', Path.rootPath(), 'foo')
self.assertEqual(6, rj.jsonstrappend('str', 'bar', Path.rootPath()))
self.assertEqual('foobar', rj.jsonget('str', Path.rootPath()))
self.assertEqual(9, rj.jsonstrappend('str', 'baz'))
self.assertEqual('foobarbaz', rj.jsonget('str', Path.rootPath()))

def testStrLenShouldSucceed(self):
"Test JSONStrLen"

rj.jsonset('str', Path.rootPath(), 'foo')
self.assertEqual(3, rj.jsonstrlen('str', Path.rootPath()))
rj.jsonstrappend('str', 'bar', Path.rootPath())
self.assertEqual(6, rj.jsonstrlen('str', Path.rootPath()))
self.assertEqual(6, rj.jsonstrlen('str'))

def testArrAppendShouldSucceed(self):
"Test JSONSArrAppend"

rj.jsonset('arr', Path.rootPath(), [1])
self.assertEqual(2, rj.jsonarrappend('arr', Path.rootPath(), 2))
self.assertEqual(3, rj.jsonarrappend('arr', None, 3))

def testArrIndexShouldSucceed(self):
"Test JSONSArrIndex"

rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4])
self.assertEqual(1, rj.jsonarrindex('arr', Path.rootPath(), 1))
self.assertEqual(-1, rj.jsonarrindex('arr', Path.rootPath(), 1, 2))
self.assertEqual(-1, rj.jsonarrindex('arr', None, 1, 2))

def testArrInsertShouldSucceed(self):
"Test JSONSArrInsert"

rj.jsonset('arr', Path.rootPath(), [0, 4])
self.assertEqual(5, rj.jsonarrinsert('arr',
Path.rootPath(), 1, *[1, 2, 3, ]))
None, 1, *[1, 2, 3, ]))
self.assertListEqual([0, 1, 2, 3, 4], rj.jsonget('arr'))

def testArrLenShouldSucceed(self):
"Test JSONSArrLen"

rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4])
self.assertEqual(5, rj.jsonarrlen('arr', Path.rootPath()))
self.assertEqual(5, rj.jsonarrlen('arr'))

def testArrPopShouldSucceed(self):
"Test JSONSArrPop"

rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4])
self.assertEqual(4, rj.jsonarrpop('arr', Path.rootPath(), 4))
self.assertEqual(3, rj.jsonarrpop('arr', Path.rootPath(), -1))
self.assertEqual(3, rj.jsonarrpop('arr', index=-1))
self.assertEqual(2, rj.jsonarrpop('arr', Path.rootPath()))
self.assertEqual(0, rj.jsonarrpop('arr', Path.rootPath(), 0))
self.assertListEqual([1], rj.jsonget('arr'))
Expand All @@ -129,26 +135,28 @@ def testArrTrimShouldSucceed(self):
"Test JSONSArrPop"

rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4])
self.assertEqual(3, rj.jsonarrtrim('arr', Path.rootPath(), 1, 3))
self.assertEqual(3, rj.jsonarrtrim('arr', None, 1, 3))
self.assertListEqual([1, 2, 3], rj.jsonget('arr'))

def testObjKeysShouldSucceed(self):
"Test JSONSObjKeys"

obj = {'foo': 'bar', 'baz': 'qaz'}
rj.jsonset('obj', Path.rootPath(), obj)
keys = rj.jsonobjkeys('obj', Path.rootPath())
keys = rj.jsonobjkeys('obj')
keys.sort()
exp = [k for k in six.iterkeys(obj)]
exp.sort()
self.assertListEqual(exp, keys)
# TODO: maybe add a test for subobjects here

def testObjLenShouldSucceed(self):
"Test JSONSObjLen"

obj = {'foo': 'bar', 'baz': 'qaz'}
rj.jsonset('obj', Path.rootPath(), obj)
self.assertEqual(len(obj), rj.jsonobjlen('obj', Path.rootPath()))
self.assertEqual(len(obj), rj.jsonobjlen('obj'))
# TODO: maybe add a test for subobjects here

def testPipelineShouldSucceed(self):
"Test pipeline"
Expand Down