Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,20 +1135,19 @@ pub fn command_json_clear<M: Manager>(
.get_value()?
.ok_or_else(RedisError::nonexistent_key)?;

let paths = find_paths(path, root, |_v| true)?;
let paths = find_paths(path, root, |v| {
v.get_type() == SelectValueType::Array || v.get_type() == SelectValueType::Object
})?;
let mut cleared = 0;
if !paths.is_empty() {
let mut res = None;
for p in paths {
res = Some(redis_key.clear(p)?);
cleared += redis_key.clear(p)?;
}
}
if cleared > 0 {
redis_key.apply_changes(ctx, "json.clear")?;
Ok(res.unwrap().into())
} else {
Err(RedisError::String(format!(
"Path '{}' does not exist",
path
)))
}
Ok(cleared.into())
}

pub fn command_json_debug<M: Manager>(
Expand Down
19 changes: 18 additions & 1 deletion tests/pytest/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,15 @@ def testClear(env):
r.expect('JSON.GET', 'test', '$.arr[3].n2.n').equal('[[]]')
r.expect('JSON.GET', 'test', '.arr[3].n2.n').equal('[]')

# Make sure only appropriate content (obj and arr) was cleared - and that errors were printed for inappropriate content (string and numeric)
# Make sure only appropriate content (obj and arr) was cleared
r.expect('JSON.GET', 'test', '$..n').equal('[42,44,{},[]]')

# Clear dynamic path
r.expect('JSON.SET', 'test', '.', r'{"n":42,"s":"42","arr":[{"n":44},"s",{"n":{"a":1,"b":2}},{"n2":{"x":3.02,"n":["to","be","cleared",4],"y":4.91}}]}') \
.ok()
r.expect('JSON.CLEAR', 'test', '$.arr.*').equal(3)
r.expect('JSON.GET', 'test', '$').equal('[{"n":42,"s":"42","arr":[{},"s",{},{}]}]')

# Clear root
r.expect('JSON.SET', 'test', '.', r'{"n":42,"s":"42","arr":[{"n":44},"s",{"n":{"a":1,"b":2}},{"n2":{"x":3.02,"n":["to","be","cleared",4],"y":4.91}}]}') \
.ok()
Expand All @@ -496,6 +502,17 @@ def testClear(env):
r.expect('JSON.CLEAR', 'test').equal(1)
r.expect('JSON.GET', 'test', '$').equal('[{}]')

# Clear none existing path
r.expect('JSON.SET', 'test', '.', r'{"a":[1,2], "b":{"c":"d"}}').ok()
r.expect('JSON.CLEAR', 'test', '$.c').equal(0)
r.expect('JSON.GET', 'test', '$').equal('[{"a":[1,2],"b":{"c":"d"}}]')

r.expect('JSON.CLEAR', 'test', '$.b..a').equal(0)
r.expect('JSON.GET', 'test', '$').equal('[{"a":[1,2],"b":{"c":"d"}}]')

# Key doesn't exist
r.expect('JSON.CLEAR', 'not_test_key', '$').raiseError()

def testArrayCRUD(env):
"""Test JSON Array CRUDness"""

Expand Down