File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -284,3 +284,20 @@ fn space_test() -> Queried<()> {
284284
285285 Ok ( ( ) )
286286}
287+ #[ test]
288+ fn neg_idx ( ) -> Queried < ( ) > {
289+ let json = json ! ( [
290+ "first" ,
291+ "second"
292+ ] ) ;
293+
294+ let vec = json. query_with_path ( "$[-2]" ) ?;
295+ assert_eq ! (
296+ vec,
297+ vec![
298+ ( & json!( "first" ) , "$[0]" . to_string( ) ) . into( ) ,
299+ ]
300+ ) ;
301+
302+ Ok ( ( ) )
303+ }
Original file line number Diff line number Diff line change 11Total; Passed; Failed; Date
2- 687; 650; 27; 2025-03-16 11:36:02
3- 687; 650; 27; 2025-03-16 11:40:04
42687; 650; 26; 2025-03-16 11:50:00
53687; 650; 25; 2025-03-16 11:51:32
64687; 650; 25; 2025-03-16 11:54:46
@@ -9,3 +7,5 @@ Total; Passed; Failed; Date
97687; 650; 25; 2025-03-16 11:56:18
108687; 650; 25; 2025-03-16 11:56:31
119687; 650; 25; 2025-03-16 11:58:52
10+ 687; 650; 25; 2025-03-16 13:02:53
11+ 687; 651; 24; 2025-03-16 19:37:57
Original file line number Diff line number Diff line change @@ -190,15 +190,21 @@ pub fn process_index<'a, T: Queryable>(
190190 inner
191191 . as_array ( )
192192 . map ( |array| {
193- if ( idx. abs ( ) as usize ) < array . len ( ) {
194- let i = if * idx < 0 {
195- array . len ( ) - idx . abs ( ) as usize
193+ if * idx >= 0 {
194+ if * idx >= array . len ( ) as i64 {
195+ Data :: Nothing
196196 } else {
197- * idx as usize
198- } ;
199- Data :: new_ref ( Pointer :: idx ( & array [ i ] , path , i ) )
197+ let i = * idx as usize ;
198+ Data :: new_ref ( Pointer :: idx ( & array [ i ] , path , i ) )
199+ }
200200 } else {
201- Data :: Nothing
201+ let abs_idx = idx. abs ( ) as usize ;
202+ if abs_idx > array. len ( ) {
203+ Data :: Nothing
204+ } else {
205+ let i = array. len ( ) - abs_idx;
206+ Data :: new_ref ( Pointer :: idx ( & array[ i] , path, i) )
207+ }
202208 }
203209 } )
204210 . unwrap_or_default ( )
You can’t perform that action at this time.
0 commit comments