From 23ca19795527bf40f23140995039e905df200247 Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 12:44:36 +0300 Subject: [PATCH 01/12] add path defaults to all commands --- rejson/client.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/rejson/client.py b/rejson/client.py index 0974e68..2d54fc3 100644 --- a/rejson/client.py +++ b/rejson/client.py @@ -113,7 +113,7 @@ def jsonget(self, name, *args): pieces.append(str_path(p)) return self.execute_command('JSON.GET', *pieces) - def jsonmget(self, path, *args): + def jsonmget(self, path=Path.rootPath(), *args): """ Gets the objects stored as a JSON values under ``path`` from keys ``args`` @@ -152,6 +152,8 @@ 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): @@ -159,6 +161,8 @@ 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()): @@ -191,6 +195,8 @@ 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): @@ -198,6 +204,8 @@ 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)) @@ -222,6 +230,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()): From f81a9ce18dcb49debcf0a5bf4c7412f52b3f2a22 Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 13:05:48 +0300 Subject: [PATCH 02/12] Test Defaults --- tests/test_rejson.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/test_rejson.py b/tests/test_rejson.py index 629eb01..f02de52 100644 --- a/tests/test_rejson.py +++ b/tests/test_rejson.py @@ -46,8 +46,8 @@ 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) + r = rj.jsonmget(keys=['1', '2']) e = [1, 2] self.assertListEqual(e, r) @@ -62,7 +62,7 @@ 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): @@ -70,7 +70,7 @@ def testNumMultByShouldSucceed(self): 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): @@ -78,7 +78,8 @@ def testStrAppendShouldSucceed(self): 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(6, rj.jsonstrappend('str', 'baz')) + self.assertEqual('foobarbaz', rj.jsonget('str', Path.rootPath())) def testStrLenShouldSucceed(self): "Test JSONStrLen" @@ -86,27 +87,28 @@ def testStrLenShouldSucceed(self): 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): @@ -114,13 +116,14 @@ def testArrLenShouldSucceed(self): rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4]) self.assertEqual(5, rj.jsonarrlen('arr', Path.rootPath())) + self.assertEqual(5, rj.jsonarrlen('arr', None)) 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', None, -1)) self.assertEqual(2, rj.jsonarrpop('arr', Path.rootPath())) self.assertEqual(0, rj.jsonarrpop('arr', Path.rootPath(), 0)) self.assertListEqual([1], rj.jsonget('arr')) @@ -129,7 +132,7 @@ 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): @@ -137,18 +140,20 @@ def testObjKeysShouldSucceed(self): 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" From ca168c5254f5bf50d91dc71885d41f0f8d0be02f Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 13:12:14 +0300 Subject: [PATCH 03/12] fix copypasta error :facepalm: --- tests/test_rejson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_rejson.py b/tests/test_rejson.py index f02de52..3871f70 100644 --- a/tests/test_rejson.py +++ b/tests/test_rejson.py @@ -78,7 +78,7 @@ def testStrAppendShouldSucceed(self): rj.jsonset('str', Path.rootPath(), 'foo') self.assertEqual(6, rj.jsonstrappend('str', 'bar', Path.rootPath())) - self.assertEqual(6, rj.jsonstrappend('str', 'baz')) + self.assertEqual(9, rj.jsonstrappend('str', 'baz')) self.assertEqual('foobarbaz', rj.jsonget('str', Path.rootPath())) def testStrLenShouldSucceed(self): From 31f03af8897e676fceaf9a26ab05f53c00e4dbfc Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 13:17:22 +0300 Subject: [PATCH 04/12] fix set Bug --- rejson/client.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rejson/client.py b/rejson/client.py index 2d54fc3..d5f4585 100644 --- a/rejson/client.py +++ b/rejson/client.py @@ -130,6 +130,8 @@ def jsonset(self, name, path, obj, nx=False, xx=False): ``xx`` if set to True, set ``value`` only if it exists """ pieces = [name, str_path(path), self._encode(obj)] + if not path: + path=Path.rootPath() # Handle existential modifiers if nx and xx: From c109b69179fb91203043a0b91be39b92c9312159 Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 13:22:06 +0300 Subject: [PATCH 05/12] use defaults correctly --- tests/test_rejson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_rejson.py b/tests/test_rejson.py index 3871f70..849ba8f 100644 --- a/tests/test_rejson.py +++ b/tests/test_rejson.py @@ -116,7 +116,7 @@ def testArrLenShouldSucceed(self): rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4]) self.assertEqual(5, rj.jsonarrlen('arr', Path.rootPath())) - self.assertEqual(5, rj.jsonarrlen('arr', None)) + self.assertEqual(5, rj.jsonarrlen('arr')) def testArrPopShouldSucceed(self): "Test JSONSArrPop" From 52b043240f6b7dfc4a36d39c1e5b7ec5becaac22 Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 13:28:54 +0300 Subject: [PATCH 06/12] init default before first usage --- rejson/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rejson/client.py b/rejson/client.py index d5f4585..e119387 100644 --- a/rejson/client.py +++ b/rejson/client.py @@ -129,9 +129,9 @@ def jsonset(self, name, path, obj, nx=False, xx=False): ``nx`` if set to True, set ``value`` only if it does not exist ``xx`` if set to True, set ``value`` only if it exists """ - pieces = [name, str_path(path), self._encode(obj)] if not path: path=Path.rootPath() + pieces = [name, str_path(path), self._encode(obj)] # Handle existential modifiers if nx and xx: From 2e32b9bf372b854d2dd8544881606e1f2369e05e Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 13:32:40 +0300 Subject: [PATCH 07/12] use mget correctly --- tests/test_rejson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_rejson.py b/tests/test_rejson.py index 849ba8f..2844550 100644 --- a/tests/test_rejson.py +++ b/tests/test_rejson.py @@ -47,7 +47,7 @@ def testMGetShouldSucceed(self): rj.jsonset('1', Path.rootPath(), 1) rj.jsonset('2', None, 2) - r = rj.jsonmget(keys=['1', '2']) + r = rj.jsonmget('1', '2') e = [1, 2] self.assertListEqual(e, r) From cf502b318ce5edef535333dbbf88b5cb523979c1 Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 14:54:58 +0300 Subject: [PATCH 08/12] add jsonmgetl --- rejson/client.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/rejson/client.py b/rejson/client.py index e119387..d706d7f 100644 --- a/rejson/client.py +++ b/rejson/client.py @@ -113,16 +113,25 @@ def jsonget(self, name, *args): pieces.append(str_path(p)) return self.execute_command('JSON.GET', *pieces) - def jsonmget(self, path=Path.rootPath(), *args): + def jsonmget(self, path, *args): """ Gets the objects stored as a JSON values under ``path`` from keys ``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()): + """ + 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`` From 27f4cdcd83c5556790180cb44faab4ce40803136 Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 14:56:24 +0300 Subject: [PATCH 09/12] mark index as such and test jsonmgetl --- tests/test_rejson.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_rejson.py b/tests/test_rejson.py index 2844550..96c5f2a 100644 --- a/tests/test_rejson.py +++ b/tests/test_rejson.py @@ -47,9 +47,12 @@ def testMGetShouldSucceed(self): rj.jsonset('1', Path.rootPath(), 1) rj.jsonset('2', None, 2) - r = rj.jsonmget('1', '2') + r1 = rj.jsonmget(None, '1', '2') + r2 = rj.jsonmget(keys=['1', '2']) e = [1, 2] - self.assertListEqual(e, r) + self.assertListEqual(e, r1) + self.assertListEqual(e, r2) + def testTypeShouldSucceed(self): "Test JSONType" @@ -123,7 +126,7 @@ def testArrPopShouldSucceed(self): 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', None, -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')) From 9be918b62c14b557a098d205a9e64d0b1f7e12a6 Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 15:02:53 +0300 Subject: [PATCH 10/12] again the issue with path and args being inverted :| --- rejson/client.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rejson/client.py b/rejson/client.py index d706d7f..54d04f7 100644 --- a/rejson/client.py +++ b/rejson/client.py @@ -190,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)) From 945b449748e3e64f05996dd39011ce2809f7f422 Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 15:03:00 +0300 Subject: [PATCH 11/12] fix typo --- tests/test_rejson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_rejson.py b/tests/test_rejson.py index 96c5f2a..a68e43d 100644 --- a/tests/test_rejson.py +++ b/tests/test_rejson.py @@ -48,7 +48,7 @@ def testMGetShouldSucceed(self): rj.jsonset('1', Path.rootPath(), 1) rj.jsonset('2', None, 2) r1 = rj.jsonmget(None, '1', '2') - r2 = rj.jsonmget(keys=['1', '2']) + r2 = rj.jsonmgetl(keys=['1', '2']) e = [1, 2] self.assertListEqual(e, r1) self.assertListEqual(e, r2) From 7bf76ea5cba88d2fa9082d0dc901f18a998fe0ce Mon Sep 17 00:00:00 2001 From: Adam Lev-Libfeld Date: Tue, 23 Oct 2018 16:07:50 +0300 Subject: [PATCH 12/12] add jsonmgetl's API --- API.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/API.md b/API.md index 4c4a233..c2dfb81 100644 --- a/API.md +++ b/API.md @@ -252,6 +252,19 @@ Gets the objects stored as a JSON values under ``path`` from keys ``args`` +### jsonmgetl +```py + +def jsonmgetl(self, keys=[], path=Path.rootPath()) + +``` + + + +Gets the objects stored as a JSON values under ``path`` from +key list ``keys`` + + ### jsonnumincrby ```py