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
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,30 @@ fn json_num_powby(ctx: &Context, args: Vec<String>) -> RedisResult {
|f1, f2| f1.powf(f2),
)
}
//
/// JSON.TOGGLE <key> <path>
fn json_bool_toggle(ctx: &Context, args: Vec<String>) -> RedisResult {
let mut args = args.into_iter().skip(1);
let key = args.next_string()?;
let path = backwards_compat_path(args.next_string()?);
let key = ctx.open_key_writable(&key);

key.get_value::<RedisJSON>(&REDIS_JSON_TYPE)?
.ok_or_else(RedisError::nonexistent_key)
.and_then(|doc| {
doc.value_op(&path, |value| {
value
.as_bool()
.ok_or_else(|| err_json(value, "boolean"))
.and_then(|curr| {
let result = curr ^ true;
Ok(Value::Bool(result))
})
})
.map(|v| v.to_string().into())
.map_err(|e| e.into())
})
}
fn json_num_op<I, F>(
ctx: &Context,
cmd: &str,
Expand Down Expand Up @@ -799,6 +822,7 @@ redis_module! {
["json.set", json_set, "write deny-oom", 1,1,1],
["json.type", json_type, "readonly", 1,1,1],
["json.numincrby", json_num_incrby, "write", 1,1,1],
["json.toggle", json_bool_toggle, "write deny-oom", 1,1,1],
["json.nummultby", json_num_multby, "write", 1,1,1],
["json.numpowby", json_num_powby, "write", 1,1,1],
["json.strappend", json_str_append, "write deny-oom", 1,1,1],
Expand Down
15 changes: 15 additions & 0 deletions tests/pytest/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,21 @@ def testMgetCommand(env):
# Test that MGET fails on path errors
r.expect('JSON.MGET', 'doc:0', 'doc:1', '42isnotapath').raiseError()

def testToggleCommand(env):
"""Test REJSON.TOGGLE command"""
r = env
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo":true}'))
r.assertEqual(r.execute_command('JSON.TOGGLE','test','.foo'), 'false')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would add another toggle to 100% verify the alternation
r.assertEqual(r.execute_command('JSON.TOGGLE','test','.foo'), 'true')

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

r.assertEqual(r.execute_command('JSON.TOGGLE','test','.foo'), 'true')

# Test Toggeling Empty Path
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo":"bar"}'))
r.assertEqual(r.execute_command('JSON.TOGGLE', 'test', '.bar'), 'null')

# Test Toggeling Non Boolean
r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{"foo":"bar"}'))
r.expect('JSON.TOGGLE','test','.foo').raiseError()

def testDelCommand(env):
"""Test REJSON.DEL command"""
r = env
Expand Down