@@ -45,10 +45,10 @@ pub use json_patch::{
45
45
} ;
46
46
#[ doc( no_inline) ]
47
47
use jsonptr:: index:: Index ;
48
- use jsonptr:: Token ;
49
48
pub use jsonptr:: {
50
49
Pointer ,
51
50
PointerBuf ,
51
+ Token ,
52
52
} ;
53
53
use serde_json:: {
54
54
json,
@@ -63,6 +63,7 @@ pub mod prelude {
63
63
copy_operation,
64
64
escape,
65
65
format_ptr,
66
+ matches,
66
67
move_operation,
67
68
patch_ext,
68
69
remove_operation,
@@ -79,6 +80,7 @@ pub mod prelude {
79
80
RemoveOperation ,
80
81
ReplaceOperation ,
81
82
TestOperation ,
83
+ Token ,
82
84
} ;
83
85
}
84
86
@@ -118,6 +120,47 @@ pub fn escape(input: &str) -> String {
118
120
Token :: new ( input) . encoded ( ) . into ( )
119
121
}
120
122
123
+ pub fn matches < ' a > ( path : & Pointer , value : & ' a Value ) -> Vec < ( PointerBuf , & ' a Value ) > {
124
+ let Some ( idx) = path. as_str ( ) . find ( "/*" ) else {
125
+ // Base case -- no stars;
126
+ // If we can't resolve, there's no match to be found
127
+ if let Ok ( v) = path. resolve ( value) {
128
+ return vec ! [ ( path. to_buf( ) , v) ] ;
129
+ } else {
130
+ return vec ! [ ] ;
131
+ }
132
+ } ;
133
+
134
+ // we checked the index above so unwrap is safe here
135
+ let ( head, cons) = path. split_at ( idx) . unwrap ( ) ;
136
+ let mut res = vec ! [ ] ;
137
+
138
+ // If we can't resolve the head, or it's not an array, no match found
139
+ let Ok ( head_val) = head. resolve ( value) else {
140
+ return vec ! [ ] ;
141
+ } ;
142
+ let Some ( next_array_val) = head_val. as_array ( ) else {
143
+ return vec ! [ ] ;
144
+ } ;
145
+
146
+ for ( i, v) in next_array_val. iter ( ) . enumerate ( ) {
147
+ // /1 is a valid pointer so the unwrap below is fine
148
+ let idx_str = format ! ( "/{i}" ) ;
149
+ let idx_path = PointerBuf :: parse ( & idx_str) . unwrap ( ) ;
150
+
151
+ // The cons pointer either looks like /* or /*/something, so we need to split_front
152
+ // to get the array marker out, and either return the current path if there's nothing
153
+ // else, or recurse and concatenate the subpath(s) to the head
154
+ if let Some ( ( _, c) ) = cons. split_front ( ) {
155
+ let subpaths = matches ( c, v) ;
156
+ res. extend ( subpaths. iter ( ) . map ( |( p, v) | ( head. concat ( & idx_path. concat ( p) ) , * v) ) ) ;
157
+ } else {
158
+ res. push ( ( head. concat ( & idx_path) , v) ) ;
159
+ }
160
+ }
161
+ res
162
+ }
163
+
121
164
pub fn patch_ext ( obj : & mut Value , p : PatchOperation ) -> Result < ( ) , PatchError > {
122
165
match p {
123
166
PatchOperation :: Add ( op) => add_or_replace ( obj, & op. path , & op. value , false ) ?,
@@ -217,9 +260,16 @@ fn patch_ext_helper<'a>(
217
260
PatchMode :: Skip => return Ok ( vec ! [ ] ) ,
218
261
}
219
262
}
263
+
264
+ // Head now points at what we believe is an array; if not, it's an error.
220
265
let next_array_val =
221
266
head. resolve_mut ( value) ?. as_array_mut ( ) . ok_or ( PatchError :: UnexpectedType ( head. as_str ( ) . into ( ) ) ) ?;
267
+
268
+ // Iterate over all the array values and recurse, returning all found values
222
269
for v in next_array_val {
270
+ // The cons pointer either looks like /* or /*/something, so we need to split_front
271
+ // to get the array marker out, and either return the current value if there's nothing
272
+ // else, or recurse and return all the found values
223
273
if let Some ( ( _, c) ) = cons. split_front ( ) {
224
274
res. extend ( patch_ext_helper ( c, v, mode) ?) ;
225
275
} else {
@@ -248,6 +298,46 @@ mod tests {
248
298
} )
249
299
}
250
300
301
+ #[ rstest]
302
+ fn test_matches_1 ( data : Value ) {
303
+ let path = format_ptr ! ( "/foo" ) ;
304
+ let m: Vec < _ > = matches ( & path, & data) . iter ( ) . map ( |( p, _) | p. clone ( ) ) . collect ( ) ;
305
+ assert_eq ! ( m, vec![ format_ptr!( "/foo" ) ] ) ;
306
+ }
307
+
308
+ #[ rstest]
309
+ fn test_matches_2 ( data : Value ) {
310
+ let path = format_ptr ! ( "/foo/*/baz" ) ;
311
+ let m: Vec < _ > = matches ( & path, & data) . iter ( ) . map ( |( p, _) | p. clone ( ) ) . collect ( ) ;
312
+ assert_eq ! ( m, vec![ format_ptr!( "/foo/0/baz" ) , format_ptr!( "/foo/1/baz" ) , format_ptr!( "/foo/2/baz" ) ] ) ;
313
+ }
314
+
315
+ #[ rstest]
316
+ fn test_matches_3 ( data : Value ) {
317
+ let path = format_ptr ! ( "/foo/*" ) ;
318
+ let m: Vec < _ > = matches ( & path, & data) . iter ( ) . map ( |( p, _) | p. clone ( ) ) . collect ( ) ;
319
+ assert_eq ! ( m, vec![ format_ptr!( "/foo/0" ) , format_ptr!( "/foo/1" ) , format_ptr!( "/foo/2" ) ] ) ;
320
+ }
321
+
322
+ #[ rstest]
323
+ #[ case( format_ptr!( "/foo/*/baz/fixx" ) ) ]
324
+ #[ case( format_ptr!( "/foo/2/baz/fixx" ) ) ]
325
+ fn test_matches_4 ( #[ case] path : PointerBuf , data : Value ) {
326
+ let m: Vec < _ > = matches ( & path, & data) . iter ( ) . map ( |( p, _) | p. clone ( ) ) . collect ( ) ;
327
+ assert_eq ! ( m, vec![ format_ptr!( "/foo/2/baz/fixx" ) ] ) ;
328
+ }
329
+
330
+ #[ rstest]
331
+ #[ case( format_ptr!( "/*" ) ) ]
332
+ #[ case( format_ptr!( "/food" ) ) ]
333
+ #[ case( format_ptr!( "/foo/3/baz" ) ) ]
334
+ #[ case( format_ptr!( "/foo/bar/baz" ) ) ]
335
+ #[ case( format_ptr!( "/foo/0/baz/fixx" ) ) ]
336
+ fn test_no_match ( #[ case] path : PointerBuf , data : Value ) {
337
+ let m = matches ( & path, & data) ;
338
+ assert_is_empty ! ( m) ;
339
+ }
340
+
251
341
#[ rstest]
252
342
fn test_patch_ext_add ( mut data : Value ) {
253
343
let path = format_ptr ! ( "/foo/*/baz/buzz" ) ;
0 commit comments