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
40 changes: 15 additions & 25 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::Error;
use crate::formatter::RedisJsonFormatter;
use crate::manager::err_msg_json_path_doesnt_exist_with_param;
use crate::manager::{err_msg_json_expected, err_msg_json_path_doesnt_exist_with_param_or};
use crate::manager::{err_msg_json_path_doesnt_exist, err_msg_json_path_doesnt_exist_with_param};
use crate::manager::{AddUpdateInfo, Manager, ReadHolder, SetUpdateInfo, UpdateInfo, WriteHolder};
use crate::nodevisitor::{StaticPathElement, StaticPathParser, VisitStatus};
use crate::redisjson::{normalize_arr_indices, Format, Path};
Expand Down Expand Up @@ -97,18 +97,12 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
let v = self.get_first(path.get_path())?;
Ok(self.resp_serialize_inner(v))
} else {
let res = self.get_values(path.get_path())?;
if !res.is_empty() {
Ok(res
.iter()
.map(|v| self.resp_serialize_inner(v))
.collect::<Vec<RedisValue>>()
.into())
} else {
Err(RedisError::String(
err_msg_json_path_doesnt_exist_with_param(path.get_original()),
))
}
Ok(self
.get_values(path.get_path())?
.iter()
.map(|v| self.resp_serialize_inner(v))
.collect::<Vec<RedisValue>>()
.into())
}
}

Expand Down Expand Up @@ -469,11 +463,7 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
.into()
})
.collect::<Vec<RedisValue>>();
if res.is_empty() {
Err(Error::from(err_msg_json_path_doesnt_exist()))
} else {
Ok(res.into())
}
Ok(res.into())
}

pub fn arr_index_legacy(
Expand Down Expand Up @@ -729,9 +719,7 @@ where
let res = get_all_values_and_paths(path, doc)?;
match res.is_empty() {
false => Ok(filter_paths(res, f)),
_ => Err(RedisError::String(
err_msg_json_path_doesnt_exist_with_param(path),
)),
_ => Ok(vec![]),
}
}

Expand All @@ -746,9 +734,7 @@ where
let res = get_all_values_and_paths(path, doc)?;
match res.is_empty() {
false => Ok(filter_values(res, f)),
_ => Err(RedisError::String(
err_msg_json_path_doesnt_exist_with_param(path),
)),
_ => Ok(vec![]),
}
}

Expand Down Expand Up @@ -1548,7 +1534,11 @@ pub fn command_json_arr_len<M: Manager>(
let values = find_all_values(path.get_path(), root, |v| {
v.get_type() == SelectValueType::Array
})?;

if is_legacy && values.is_empty() {
return Err(RedisError::String(
err_msg_json_path_doesnt_exist_with_param(path.get_original()),
));
}
let mut res = vec![];
for v in values {
let cur_val: RedisValue = match v {
Expand Down
34 changes: 17 additions & 17 deletions tests/pytest/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def testObjKeysCommand(env):
r.assertEqual(res, None)

# Test missing key
r.expect('JSON.OBJKEYS', 'doc1', '$.nowhere').raiseError()
r.assertEqual(r.execute_command('JSON.OBJKEYS', 'doc1', '$.nowhere'), [])

# Test default path
res = r.execute_command('JSON.OBJKEYS', 'doc1')
Expand All @@ -664,7 +664,7 @@ def testObjLenCommand(env):
r.expect('JSON.OBJLEN', 'non_existing_doc', '$..a').raiseError().contains("does not exist")

# Test missing path
r.expect('JSON.OBJLEN', 'doc1', '$.nowhere').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.OBJLEN', 'doc1', '$.nowhere'), [])


# Test legacy
Expand Down Expand Up @@ -858,7 +858,7 @@ def testRespCommand(env):
r.assertEqual(resSingle, [['{', 'A1_B1', 10, 'A1_B2', 'false', 'A1_B3', ['{', 'A1_B3_C1', None, 'A1_B3_C2', ['[', 'A1_B3_C2_D1_1', 'A1_B3_C2_D1_2', '-19.5', 'A1_B3_C2_D1_4', 'A1_B3_C2_D1_5', ['{', 'A1_B3_C2_D1_6_E1', 'true']], 'A1_B3_C3', ['[', 1]], 'A1_B4', ['{', 'A1_B4_C1', 'foo']]])

# Test missing path
r.expect('JSON.RESP', 'doc1', '$.nowhere').raiseError()
r.assertEqual(r.execute_command('JSON.RESP', 'doc1', '$.nowhere'), [])

# Test missing key
res = r.execute_command('JSON.RESP', 'non_existing_doc', '$..a')
Expand Down Expand Up @@ -984,7 +984,7 @@ def testErrorMessage(env):

# ARRAPPEND
r.assertEqual(r.execute_command('JSON.ARRAPPEND', 'doc1', '$.string', '"abc"'), [None])
r.expect('JSON.ARRAPPEND', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.ARRAPPEND', 'doc1', '$.nowhere', '"abc"'), [])
r.expect('JSON.ARRAPPEND', 'doc_none', '$.string', '"abc"').raiseError().contains("doesn't exist")
r.expect('JSON.ARRAPPEND', 'hash_key', '$.string', '"abc"').raiseError().contains("wrong Redis type")

Expand All @@ -1004,7 +1004,7 @@ def testErrorMessage(env):

# ARRPOP
r.assertEqual(r.execute_command('JSON.ARRPOP', 'doc1', '$.string', '"abc"'), [None])
r.expect('JSON.ARRPOP', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.ARRPOP', 'doc1', '$.nowhere', '"abc"'), [])
r.expect('JSON.ARRPOP', 'doc_none', '$..string', '"abc"').raiseError().contains("doesn't exist")
r.expect('JSON.ARRPOP', 'hash_key', '$..string', '"abc"').raiseError().contains("wrong Redis type")

Expand All @@ -1024,7 +1024,7 @@ def testErrorMessage(env):

# ARRINDEX
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'doc1', '$.number', '"abc"'), [None])
r.expect('JSON.ARRINDEX', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'doc1', '$.nowhere', '"abc"'), [])
r.expect('JSON.ARRINDEX', 'doc_none', '$.number', '"abc"').raiseError().contains("does not exist")
r.expect('JSON.ARRINDEX', 'hash_key', '$.number', '"abc"').raiseError().contains("wrong Redis type")

Expand All @@ -1044,7 +1044,7 @@ def testErrorMessage(env):

# ARRINSERT
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'doc1', '$.string', 0, '"abc"'), [None])
r.expect('JSON.ARRINSERT', 'doc1', '$.nowhere', 0, '"abc"').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'doc1', '$.nowhere', 0, '"abc"'), [])
r.expect('JSON.ARRINSERT', 'doc_none', '$.string', 0, '"abc"').raiseError().contains("doesn't exist")
r.expect('JSON.ARRINSERT', 'hash_key', '$.string', 0, '"abc"').raiseError().contains("wrong Redis type")

Expand All @@ -1064,7 +1064,7 @@ def testErrorMessage(env):

# ARRLEN
r.assertEqual(r.execute_command('JSON.ARRLEN', 'doc1', '$.string', '"abc"'), [None])
r.expect('JSON.ARRLEN', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.ARRLEN', 'doc1', '$.nowhere', '"abc"'), [])
r.expect('JSON.ARRLEN', 'doc_none', '$.string', '"abc"').raiseError().contains("doesn't exist")
r.expect('JSON.ARRLEN', 'hash_key', '$.string', '"abc"').raiseError().contains("wrong Redis type")

Expand All @@ -1084,7 +1084,7 @@ def testErrorMessage(env):

# ARRTRIM
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'doc1', '$.string', 0, 1), [None])
r.expect('JSON.ARRTRIM', 'doc1', '$.nowhere', 0, 1).raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'doc1', '$.nowhere', 0, 1), [])
r.expect('JSON.ARRTRIM', 'doc_none', '$.string', 0, 1).raiseError().contains("doesn't exist")
r.expect('JSON.ARRTRIM', 'hash_key', '$.string', 0, 1).raiseError().contains("wrong Redis type")

Expand All @@ -1104,7 +1104,7 @@ def testErrorMessage(env):

# OBJKEYS
r.assertEqual(r.execute_command('JSON.OBJKEYS', 'doc1', '$.string'), [None])
r.expect('JSON.OBJKEYS', 'doc1', '$.nowhere').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.OBJKEYS', 'doc1', '$.nowhere'), [])
r.expect('JSON.OBJKEYS', 'doc_none', '$.string').raiseError().contains("doesn't exist")
r.expect('JSON.OBJKEYS', 'hash_key', '$.string').raiseError().contains("wrong Redis type")

Expand All @@ -1124,7 +1124,7 @@ def testErrorMessage(env):

# OBJLEN
r.assertEqual(r.execute_command('JSON.OBJLEN', 'doc1', '$.string'), [None])
r.expect('JSON.OBJLEN', 'doc1', '$.nowhere').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.OBJLEN', 'doc1', '$.nowhere'), [])
r.expect('JSON.OBJLEN', 'doc_none', '$.string').raiseError().contains("does not exist")
r.expect('JSON.OBJLEN', 'hash_key', '.string').raiseError().contains("wrong Redis type")

Expand All @@ -1144,7 +1144,7 @@ def testErrorMessage(env):

# NUMINCRBY
r.assertEqual(r.execute_command('JSON.NUMINCRBY', 'doc1', '$.string', 3), '[null]')
r.expect('JSON.NUMINCRBY', 'doc1', '$.nowhere', 3).raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.NUMINCRBY', 'doc1', '$.nowhere', 3), '[]')
r.expect('JSON.NUMINCRBY', 'doc_none', '$.string', 3).raiseError().contains("doesn't exist")
r.expect('JSON.NUMINCRBY', 'hash_key', '$.string', 3).raiseError().contains("wrong Redis type")

Expand All @@ -1165,7 +1165,7 @@ def testErrorMessage(env):

# NUMMULTBY
r.assertEqual(r.execute_command('JSON.NUMMULTBY', 'doc1', '$.string', 3), '[null]')
r.expect('JSON.NUMMULTBY', 'doc1', '$.nowhere', 3).raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.NUMMULTBY', 'doc1', '$.nowhere', 3), '[]')
r.expect('JSON.NUMMULTBY', 'doc_none', '$.string', 3).raiseError().contains("doesn't exist")
r.expect('JSON.NUMMULTBY', 'hash_key', '$.string', 3).raiseError().contains("wrong Redis type")

Expand All @@ -1185,7 +1185,7 @@ def testErrorMessage(env):

# STRAPPEND
r.assertEqual(r.execute_command('JSON.STRAPPEND', 'doc1', '$.number', '"abc"'), [None])
r.expect('JSON.STRAPPEND', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.STRAPPEND', 'doc1', '$.nowhere', '"abc"'), [])
r.expect('JSON.STRAPPEND', 'doc_none', '$.number', '"abc"').raiseError().contains("doesn't exist")
r.expect('JSON.STRAPPEND', 'hash_key', '$.number', '"abc"').raiseError().contains("wrong Redis type")

Expand All @@ -1206,7 +1206,7 @@ def testErrorMessage(env):

# STRLEN
r.assertEqual(r.execute_command('JSON.STRLEN', 'doc1', '$.object', '"abc"'), [None])
r.expect('JSON.STRLEN', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.STRLEN', 'doc1', '$.nowhere', '"abc"'), [])
r.expect('JSON.STRLEN', 'doc_none', '$.object', '"abc"').raiseError().contains("doesn't exist")
r.expect('JSON.STRLEN', 'hash_key', '$.object', '"abc"').raiseError().contains("wrong Redis type")

Expand All @@ -1226,7 +1226,7 @@ def testErrorMessage(env):

# TOGGLE
r.assertEqual(r.execute_command('JSON.TOGGLE', 'doc1', '$.object'), [None])
r.expect('JSON.TOGGLE', 'doc1', '$.nowhere').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.TOGGLE', 'doc1', '$.nowhere'), [])
r.expect('JSON.TOGGLE', 'doc_none', '$.object').raiseError().contains("doesn't exist")
r.expect('JSON.TOGGLE', 'hash_key', '$.object').raiseError().contains("wrong Redis type")

Expand Down Expand Up @@ -1273,7 +1273,7 @@ def testErrorMessage(env):
"""

# RESP
r.expect('JSON.RESP', 'doc1', '$.nowhere').raiseError().contains("does not exist")
r.assertEqual(r.execute_command('JSON.RESP', 'doc1', '$.nowhere'), [])
r.assertEqual(r.execute_command('JSON.RESP', 'doc_none', '$.object'), None)
r.assertEqual(r.execute_command('JSON.RESP', 'doc_none', '$.object'), None)
r.expect('JSON.RESP', 'hash_key', '$.object').raiseError().contains("wrong Redis type")
Expand Down