Skip to content

Commit e88b59e

Browse files
JSON.CLEAR
1 parent 4e9b3e4 commit e88b59e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

rejson/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs):
5151
# Set the module commands' callbacks
5252
MODULE_CALLBACKS = {
5353
'JSON.DEL': long,
54+
'JSON.CLEAR': long,
5455
'JSON.GET': self._decode,
5556
'JSON.MGET': bulk_of_jsons(self._decode),
5657
'JSON.SET': lambda r: r and nativestr(r) == 'OK',
@@ -100,6 +101,14 @@ def jsondel(self, name, path=Path.rootPath()):
100101
"""
101102
return self.execute_command('JSON.DEL', name, str_path(path))
102103

104+
def jsonclear(self, name, path=Path.rootPath()):
105+
"""
106+
Emptying arrays and objects (to have zero slots/keys without
107+
deleting the array/object) returning the count of cleared paths
108+
(ignoring non-array and non-objects paths)
109+
"""
110+
return self.execute_command('JSON.CLEAR', name, str_path(path))
111+
103112
def jsonget(self, name, *args, no_escape=False):
104113
"""
105114
Get the object stored as a JSON value at key ``name``

tests/test_rejson.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ def testMGetShouldSucceed(self):
6060
e = [1, 2]
6161
self.assertListEqual(e, r)
6262

63+
def testClearShouldSucceed(self):
64+
"Test JSONClear"
65+
66+
rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4])
67+
self.assertEqual(1, rj.jsonclear('arr', Path.rootPath()))
68+
self.assertEqual([], rj.jsonget('arr'))
69+
6370
def testTypeShouldSucceed(self):
6471
"Test JSONType"
6572

@@ -82,7 +89,7 @@ def testNumMultByShouldSucceed(self):
8289
self.assertEqual(5, rj.jsonnummultby('num', Path.rootPath(), 2.5))
8390
self.assertEqual(2.5, rj.jsonnummultby('num', Path.rootPath(), 0.5))
8491

85-
def testToggle(self):
92+
def testToggleShouldSucceed(self):
8693
"Test JSONToggle"
8794

8895
rj.jsonset('bool', Path.rootPath(), False)

0 commit comments

Comments
 (0)