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
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,12 @@ where
if let Some(array) = value.as_array() {
// Verify legal index in bounds
let len = array.len() as i64;
if !(-len..len).contains(&index) {
let index = if index < 0 { len + index } else { index };
if !(0..=len).contains(&index) {
return Err("ERR index out of bounds".into());
}
let index = index.normalize(len);
let index = index as usize;

let items: Vec<Value> = args
.map(|json| serde_json::from_str(&json))
.collect::<Result<_, _>>()?;
Expand Down
21 changes: 21 additions & 0 deletions tests/pytest/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,27 @@ def testArrIndexCommand(env):
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', 2, 3), 5)
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'test', '.arr', '[4]'), 4)

def testArrInsertCommand(env):
"""Test JSON.ARRINSERT command"""
r = env

r.assertOk(r.execute_command('JSON.SET', 'test', '.', '{ "arr": [] }'))
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', 0, '1'), 1)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', -1, '2'), 2)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', -2, '3'), 3)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', 3, '4'), 4)
r.assertEqual(r.execute_command('JSON.GET', 'test', '.arr'), "[3,2,1,4]")

r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', 1, '5'), 5)
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', -2, '6'), 6)
r.assertEqual(r.execute_command('JSON.GET', 'test', '.arr'), "[3,5,2,6,1,4]")

r.assertEqual(r.execute_command('JSON.ARRINSERT', 'test', '.arr', -3, '7', '{"A":"Z"}', '9'), 9)
r.assertEqual(r.execute_command('JSON.GET', 'test', '.arr'), '[3,5,2,7,{"A":"Z"},9,6,1,4]')

r.expect('JSON.ARRINSERT', 'test', '.arr', -10, '10').raiseError()
r.expect('JSON.ARRINSERT', 'test', '.arr', 10, '10').raiseError()

def testArrIndexMixCommand(env):
"""Test JSON.ARRINDEX command with mixed values"""
r = env
Expand Down