jsonpath-rust 0.1.4

The library provides the basic functionality to find the set of the data according to the filtering query.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use crate::path::{Path, PathInstance, json_path_instance, process_operand};
use serde_json::Value;
use crate::parser::model::{JsonPath, FilterSign, Operand};
use crate::path::json::*;
use serde_json::value::Value::{Array, Object};
use crate::path::top::ObjectField;

/// process the slice like [start:end:step]
#[derive(Debug)]
pub(crate) struct ArraySlice {
    start_index: i32,
    end_index: i32,
    step: usize,
}

impl ArraySlice {
    pub(crate) fn new(start_index: i32,
                      end_index: i32,
                      step: usize, ) -> ArraySlice {
        ArraySlice { start_index, end_index, step }
    }

    fn end(&self, len: i32) -> Option<usize> {
        if self.end_index >= 0 {
            if self.end_index > len { None } else { Some(self.end_index as usize) }
        } else if self.end_index < -len { None } else {
            Some((len - (-self.end_index)) as usize)
        }
    }

    fn start(&self, len: i32) -> Option<usize> {
        if self.start_index >= 0 {
            if self.start_index > len { None } else { Some(self.start_index as usize) }
        } else if self.start_index < -len { None } else {
            Some((len - -self.start_index) as usize)
        }
    }

    fn process<'a, T>(&self, elements: &'a [T]) -> Vec<&'a T> {
        let len = elements.len() as i32;
        let mut filtered_elems: Vec<&T> = vec![];
        match (self.start(len), self.end(len)) {
            (Some(start_idx), Some(end_idx)) => {
                let end_idx = if end_idx == 0 { elements.len() } else { end_idx };
                for idx in (start_idx..end_idx).step_by(self.step) {
                    if let Some(v) = elements.get(idx) {
                        filtered_elems.push(v)
                    }
                }
                filtered_elems
            }
            _ => filtered_elems
        }
    }
}

impl<'a> Path<'a> for ArraySlice {
    type Data = Value;

    fn find(&self, data: &'a Self::Data) -> Vec<&'a Self::Data> {
        data.as_array()
            .map(|elems| self.process(elems))
            .unwrap_or_default()
    }
}

/// process the simple index like [index]
pub(crate) struct ArrayIndex {
    index: usize,
}

impl ArrayIndex {
    pub(crate) fn new(index: usize) -> Self {
        ArrayIndex { index }
    }
}

impl<'a> Path<'a> for ArrayIndex {
    type Data = Value;

    fn find(&self, data: &'a Self::Data) -> Vec<&'a Self::Data> {
        data.as_array()
            .and_then(|elems| elems.get(self.index))
            .map(|e| vec![e])
            .unwrap_or_default()
    }
}

/// process @ element
pub(crate) struct Current<'a> {
    tail: Option<PathInstance<'a>>,
}

impl<'a> Current<'a> {
    pub(crate) fn from(jp: &'a JsonPath, root: &'a Value) -> Self {
        match jp {
            JsonPath::Empty => Current::none(),
            tail => Current::new(json_path_instance(tail, root))
        }
    }
    pub(crate) fn new(tail: PathInstance<'a>) -> Self {
        Current { tail: Some(tail) }
    }
    pub(crate) fn none() -> Self {
        Current { tail: None }
    }
}

impl<'a> Path<'a> for Current<'a> {
    type Data = Value;

    fn find(&self, data: &'a Self::Data) -> Vec<&'a Value> {
        self.tail.as_ref().map(|p| p.find(data)).unwrap_or_else(|| vec![data])
    }
}

/// the list of indexes like [1,2,3]
pub(crate) struct UnionIndex<'a> {
    indexes: Vec<PathInstance<'a>>,
}

impl<'a> UnionIndex<'a> {
    pub fn from_indexes(elems: &'a [Value]) -> Self {
        let mut indexes: Vec<PathInstance<'a>> = vec![];

        for idx in elems.iter() {
            indexes.push(Box::new(ArrayIndex::new(idx.as_u64().unwrap() as usize)))
        }

        UnionIndex::new(indexes)
    }
    pub fn from_keys(elems: &'a [String]) -> Self {
        let mut indexes: Vec<PathInstance<'a>> = vec![];

        for key in elems.iter() {
            indexes.push(Box::new(ObjectField::new(key)))
        }

        UnionIndex::new(indexes)
    }

    pub fn new(indexes: Vec<PathInstance<'a>>) -> Self {
        UnionIndex { indexes }
    }
}

impl<'a> Path<'a> for UnionIndex<'a> {
    type Data = Value;

    fn find(&self, data: &'a Self::Data) -> Vec<&'a Self::Data> {
        self.indexes.iter().flat_map(|e| e.find(data)).collect()
    }
}

/// process filter element like [?(op sign op)]
pub(crate) struct Filter<'a> {
    left: PathInstance<'a>,
    right: PathInstance<'a>,
    op: &'a FilterSign,
}

impl<'a> Filter<'a> {
    pub(crate) fn new(left: &'a Operand, right: &'a Operand, op: &'a FilterSign, root: &'a Value) -> Self {
        Filter {
            left: process_operand(left, root),
            right: process_operand(right, root),
            op,
        }
    }

    fn or(one: &'a FilterSign, two: &'a FilterSign, left: Vec<&'a Value>, right: Vec<&'a Value>) -> bool {
        Filter::process(one, left.clone(), right.clone())
            || Filter::process(two, left.clone(), right.clone())
    }

    fn process(op: &'a FilterSign, left: Vec<&'a Value>, right: Vec<&'a Value>) -> bool {
        match op {
            FilterSign::Equal => eq(left, right),
            FilterSign::Unequal => !Filter::process(&FilterSign::Equal, left, right),
            FilterSign::Less => less(left, right),
            FilterSign::LeOrEq => Filter::or(&FilterSign::Less, &FilterSign::Equal, left, right),
            FilterSign::Greater => !Filter::process(&FilterSign::LeOrEq, left, right),
            FilterSign::GrOrEq => !Filter::process(&FilterSign::Less, left, right),
            FilterSign::Regex => regex(left, right),
            FilterSign::In => inside(left, right),
            FilterSign::Nin => !Filter::process(&FilterSign::In, left, right),
            FilterSign::NoneOf => !Filter::process(&FilterSign::AnyOf, left, right),
            FilterSign::AnyOf => any_of(left, right),
            FilterSign::SubSetOf => sub_set_of(left, right),
            FilterSign::Exists => !left.is_empty(),
            FilterSign::Size => size(left, right)
        }
    }
}

impl<'a> Path<'a> for Filter<'a> {
    type Data = Value;

    fn find(&self, data: &'a Self::Data) -> Vec<&'a Self::Data> {
        let mut res: Vec<&Value> = vec![];

        match data {
            Array(elems) => {
                for el in elems.iter() {
                    if Filter::process(self.op, self.left.find(el), self.right.find(el)) {
                        res.push(el)
                    }
                }
                res
            }
            Object(pairs) => {
                for el in pairs.values() {
                    if Filter::process(self.op, self.left.find(el), self.right.find(el)) {
                        res.push(el)
                    }
                }
                res
            }
            _ => vec![]
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_json::Value;
    use serde_json::json;
    use crate::parser::model::{JsonPath, JsonPathIndex, Operand, FilterSign};
    use crate::path::index::{ArraySlice, ArrayIndex};
    use crate::path::{Path, json_path_instance};

    #[test]
    fn array_slice_end_start_test() {
        let array = vec![0, 1, 2, 3, 4, 5];
        let len = array.len() as i32;
        let mut slice = ArraySlice::new(0, 0, 0);

        assert_eq!(slice.start(len).unwrap(), 0);
        slice.start_index = 1;

        assert_eq!(slice.start(len).unwrap(), 1);

        slice.start_index = 2;
        assert_eq!(slice.start(len).unwrap(), 2);

        slice.start_index = 5;
        assert_eq!(slice.start(len).unwrap(), 5);

        slice.start_index = 7;
        assert_eq!(slice.start(len), None);

        slice.start_index = -1;
        assert_eq!(slice.start(len).unwrap(), 5);

        slice.start_index = -5;
        assert_eq!(slice.start(len).unwrap(), 1);

        slice.end_index = 0;
        assert_eq!(slice.end(len).unwrap(), 0);

        slice.end_index = 5;
        assert_eq!(slice.end(len).unwrap(), 5);

        slice.end_index = -1;
        assert_eq!(slice.end(len).unwrap(), 5);

        slice.end_index = -5;
        assert_eq!(slice.end(len).unwrap(), 1);
    }

    #[test]
    fn slice_test() {
        let array = json!([0,1,2,3,4,5,6,7,8,9,10]);

        let mut slice = ArraySlice::new(0, 6, 2);
        assert_eq!(slice.find(&array), vec![&json!(0), &json!(2), &json!(4)]);

        slice.step = 3;
        assert_eq!(slice.find(&array), vec![&json!(0), &json!(3)]);

        slice.start_index = -1;
        slice.end_index = 1;

        assert!(slice.find(&array).is_empty());

        slice.start_index = -10;
        slice.end_index = 10;

        assert_eq!(slice.find(&array), vec![&json!(1), &json!(4), &json!(7)]);
    }

    #[test]
    fn index_test() {
        let array = json!([0,1,2,3,4,5,6,7,8,9,10]);

        let mut index = ArrayIndex::new(0);

        assert_eq!(index.find(&array), vec![&json!(0)]);
        index.index = 10;
        assert_eq!(index.find(&array), vec![&json!(10)]);
        index.index = 100;
        assert!(index.find(&array).is_empty());
    }

    #[test]
    fn current_test() {
        let json = json!(
        {
            "object":{
                "field_1":[1,2,3],
                "field_2":42,
                "field_3":{"a":"b"}

            }
        });

        let root = JsonPath::Root;
        let object = JsonPath::Field(String::from("object"));
        let cur = JsonPath::Current(Box::new(JsonPath::Empty));

        let chain = vec![root.clone(), object.clone(), cur.clone()];
        let chain = JsonPath::Chain(chain);

        let path_inst = json_path_instance(&chain, &json);
        let res = json!({
                "field_1":[1,2,3],
                "field_2":42,
                "field_3":{"a":"b"}
            });

        let expected_res = vec![&res];
        assert_eq!(path_inst.find(&json), expected_res);

        let field_3 = JsonPath::Field(String::from("field_3"));
        let field_a = JsonPath::Field(String::from("a"));
        let chain_in = vec![field_3, field_a];
        let chain_in = JsonPath::Chain(chain_in);
        let cur = JsonPath::Current(Box::new(chain_in));

        let chain = vec![root.clone(), object.clone(), cur.clone()];
        let chain = JsonPath::Chain(chain);

        let path_inst = json_path_instance(&chain, &json);
        let res1 = json!("b");

        let expected_res = vec![&res1];
        assert_eq!(path_inst.find(&json), expected_res);
    }

    #[test]
    fn filter_exist_test() {
        let json = json!({
            "key":[{"field":[1,2,3,4,5]},{"field":42}],
        });


        let field = JsonPath::Field(String::from("field"));
        let cur = JsonPath::Current(Box::new(field.clone()));
        let operand = Operand::Dynamic(Box::new(cur));
        let empty_operand = Operand::Static(Value::Null);


        let root = JsonPath::Root;
        let key = JsonPath::Field(String::from("key"));
        let filter = JsonPathIndex::Filter(operand, FilterSign::Exists, empty_operand);
        let index = JsonPath::Index(filter);

        let chain = vec![root.clone(), key.clone(), index.clone(), field.clone()];
        let chain = JsonPath::Chain(chain);

        let path_inst = json_path_instance(&chain, &json);

        let exp1 = json!([1,2,3,4,5]);
        let exp2 = json!(42);
        let expected_res = vec![&exp1, &exp2];
        assert_eq!(path_inst.find(&json), expected_res)
    }

    #[test]
    fn filter_gr_test() {
        let json = json!({
                "key":[
                    {"field":1},
                    {"field":10},
                    {"field":5},
                    {"field":1},
                ]
            });


        let field = JsonPath::Field(String::from("field"));
        let cur = JsonPath::Current(Box::new(field));
        let operand = Operand::Dynamic(Box::new(cur));
        let right_operand = Operand::Static(json!(1));


        let root = JsonPath::Root;
        let key = JsonPath::Field(String::from("key"));
        let filter = JsonPathIndex::Filter(operand, FilterSign::Greater, right_operand);
        let index = JsonPath::Index(filter);

        let chain = vec![root, key, index];
        let chain = JsonPath::Chain(chain);

        let path_inst = json_path_instance(&chain, &json);

        let exp1 = json!( {"field":10});
        let exp2 = json!( {"field":5});
        let expected_res = vec![&exp1, &exp2];
        assert_eq!(path_inst.find(&json), expected_res)
    }

    #[test]
    fn filter_regex_test() {
        let json = json!({
                "key":[
                    {"field":"a11#"},
                    {"field":"a1#1"},
                    {"field":"a#11"},
                    {"field":"#a11"},
                ]
            });


        let field = JsonPath::Field(String::from("field"));
        let cur = JsonPath::Current(Box::new(field));
        let operand = Operand::Dynamic(Box::new(cur));
        let right_operand = Operand::Static(json!("[a-zA-Z]+[0-9]#[0-9]+"));


        let root = JsonPath::Root;
        let key = JsonPath::Field(String::from("key"));
        let filter = JsonPathIndex::Filter(operand, FilterSign::Regex, right_operand);
        let index = JsonPath::Index(filter);

        let chain = vec![root, key, index];
        let chain = JsonPath::Chain(chain);

        let path_inst = json_path_instance(&chain, &json);

        let exp2 = json!( {"field":"a1#1"});
        let expected_res = vec![&exp2];
        assert_eq!(path_inst.find(&json), expected_res)
    }

    #[test]
    fn filter_any_of_test() {
        let json = json!({
                "key":[
                    {"field":"a11#"},
                    {"field":"a1#1"},
                    {"field":"a#11"},
                    {"field":"#a11"},
                ]
            });


        let field = JsonPath::Field(String::from("field"));
        let cur = JsonPath::Current(Box::new(field));
        let operand = Operand::Dynamic(Box::new(cur));
        let right_operand = Operand::Static(json!(["a11#","aaa","111"]));


        let root = JsonPath::Root;
        let key = JsonPath::Field(String::from("key"));
        let filter = JsonPathIndex::Filter(operand, FilterSign::AnyOf, right_operand);
        let index = JsonPath::Index(filter);

        let chain = vec![root, key, index];
        let chain = JsonPath::Chain(chain);

        let path_inst = json_path_instance(&chain, &json);

        let exp2 = json!( {"field":"a11#"});
        let expected_res = vec![&exp2];
        assert_eq!(path_inst.find(&json), expected_res)
    }

    #[test]
    fn size_test() {
        let json = json!({
                "key":[
                    {"field":"aaaa"},
                    {"field":"bbb"},
                    {"field":"cc"},
                    {"field":"dddd"},
                    {"field":[1,1,1,1]},
                ]
            });


        let field = JsonPath::Field(String::from("field"));
        let cur = JsonPath::Current(Box::new(field));
        let operand = Operand::Dynamic(Box::new(cur));
        let right_operand = Operand::Static(json!(4));


        let root = JsonPath::Root;
        let key = JsonPath::Field(String::from("key"));
        let filter = JsonPathIndex::Filter(operand, FilterSign::Size, right_operand);
        let index = JsonPath::Index(filter);

        let chain = vec![root, key, index];
        let chain = JsonPath::Chain(chain);

        let path_inst = json_path_instance(&chain, &json);

        let exp2 = json!( {"field":"aaaa"});
        let exp3 = json!( {"field":"dddd"});
        let exp4 = json!( {"field":[1,1,1,1]});
        let expected_res = vec![&exp2, &exp3, &exp4];
        assert_eq!(path_inst.find(&json), expected_res)
    }
}