@@ -606,13 +606,13 @@ fn json_arr_pop(ctx: &Context, args: Vec<String>) -> RedisResult {
606
606
. next ( )
607
607
. map ( |p| {
608
608
let path = backwards_compat_path ( p) ;
609
- let index = args. next_i64 ( ) . unwrap_or ( i64 :: MAX ) ;
609
+ let index = args. next_i64 ( ) . unwrap_or ( - 1 ) ;
610
610
( path, index)
611
611
} )
612
612
. unwrap_or ( ( JSON_ROOT_PATH . to_string ( ) , i64:: MAX ) ) ;
613
613
614
614
let redis_key = ctx. open_key_writable ( & key) ;
615
- let mut res = Value :: Null ;
615
+ let mut res = None ;
616
616
617
617
redis_key
618
618
. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
@@ -629,25 +629,32 @@ fn json_arr_pop(ctx: &Context, args: Vec<String>) -> RedisResult {
629
629
)
630
630
. map_err ( |e| e. into ( ) )
631
631
} ) ?;
632
- Ok ( RedisJSON :: serialize ( & res, Format :: JSON ) ?. into ( ) )
632
+
633
+ let result = match res {
634
+ None => ( ) . into ( ) ,
635
+ Some ( r) => RedisJSON :: serialize ( & r, Format :: JSON ) ?. into ( ) ,
636
+ } ;
637
+ Ok ( result)
633
638
}
634
639
635
- fn do_json_arr_pop ( mut index : i64 , res : & mut Value , value : & mut Value ) -> Result < Value , Error > {
640
+ fn do_json_arr_pop ( index : i64 , res : & mut Option < Value > , value : & mut Value ) -> Result < Value , Error > {
636
641
if let Some ( array) = value. as_array ( ) {
637
- // Verify legel index in bounds
638
- let len = array. len ( ) as i64 ;
639
- index = index. min ( len - 1 ) ;
640
- if index < 0 {
641
- index += len;
642
+ if array. is_empty ( ) {
643
+ * res = None ;
644
+ return Ok ( value. clone ( ) ) ;
642
645
}
643
646
644
- if index >= len || index < 0 {
645
- return Err ( "ERR index out of bounds" . into ( ) ) ;
646
- }
647
+ // Verify legel index in bounds
648
+ let len = array. len ( ) as i64 ;
649
+ let index = if index < 0 {
650
+ 0 . max ( len + index)
651
+ } else {
652
+ index. min ( len - 1 )
653
+ } as usize ;
647
654
648
655
let mut new_value = value. take ( ) ;
649
656
let curr = new_value. as_array_mut ( ) . unwrap ( ) ;
650
- * res = curr. remove ( index as usize ) ;
657
+ * res = Some ( curr. remove ( index as usize ) ) ;
651
658
Ok ( new_value)
652
659
} else {
653
660
Err ( err_json ( value, "array" ) )
0 commit comments