|
| 1 | +/* |
| 2 | + * Copyright 2020 VMware, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +use serde_json::Value; |
| 8 | + |
| 9 | +/// A path is a tree of selector nodes. |
| 10 | +/// |
| 11 | +/// For example, the JSONPath `$.foo.bar` yields this AST: |
| 12 | +/// |
| 13 | +/// ```text |
| 14 | +/// ^ |
| 15 | +/// / \ |
| 16 | +/// ^ \___ DotName("bar") |
| 17 | +/// / \ |
| 18 | +/// Root ___ / \___ DotName("foo") |
| 19 | +/// ``` |
| 20 | +/// |
| 21 | +/// A more complicated example: `$.foo[1,2]["bar"]`: |
| 22 | +/// |
| 23 | +/// ```text |
| 24 | +/// ^ |
| 25 | +/// / \ |
| 26 | +/// ^ \___ Union |
| 27 | +/// / \ \ |
| 28 | +/// / \___ Union \___ [Name("bar")] |
| 29 | +/// / \ |
| 30 | +/// ^ \___ [Index(1), Index(2)] |
| 31 | +/// / \ |
| 32 | +/// Root ___/ \___ DotName("foo") |
| 33 | +/// ``` |
| 34 | +/// |
| 35 | +/// Selectors are left associative, thus `$.foo[1,2]["bar"]` behaves |
| 36 | +/// like (pseudocode) `(($.foo)[1,2])["bar"]`; thus the root of the resulting |
| 37 | +/// tree is actually the right-most selector (the last one to be applied). |
| 38 | +/// |
| 39 | +/// The Path::Root AST node is called "root" because that's the |
| 40 | +/// name of the node in the JSONPath grammar. It represents the source of |
| 41 | +/// the json value stream which gets operated upon by Selector nodes. |
| 42 | +/// This is why despite being called "root", this node doesn't lie at the root |
| 43 | +/// of the AST tree. |
| 44 | +#[derive(Debug)] |
| 45 | +pub enum Path { |
| 46 | + Root, |
| 47 | + Sel(Box<Path>, Selector), |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Debug)] |
| 51 | +pub enum Selector { |
| 52 | + Union(Vec<UnionElement>), |
| 53 | + DotName(String), |
| 54 | + DotWildcard, |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Debug)] |
| 58 | +pub enum UnionElement { |
| 59 | + Name(String), |
| 60 | + Index(i64), |
| 61 | +} |
| 62 | + |
| 63 | +type Iter<'a> = Box<dyn Iterator<Item = &'a Value> + 'a>; |
| 64 | + |
| 65 | +impl Path { |
| 66 | + pub fn find<'a>(&'a self, input: &'a Value) -> Iter<'a> { |
| 67 | + match self { |
| 68 | + Path::Root => Box::new(std::iter::once(input)), |
| 69 | + Path::Sel(left, sel) => Box::new(left.find(input).flat_map(move |v| sel.find(v))), |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl Selector { |
| 75 | + pub fn find<'a>(&'a self, input: &'a Value) -> Iter<'a> { |
| 76 | + match self { |
| 77 | + Selector::Union(indices) => Box::new(indices.iter().flat_map(move |i| i.get(input))), |
| 78 | + Selector::DotName(name) => Box::new(input.get(name).into_iter()), |
| 79 | + Selector::DotWildcard => match input { |
| 80 | + Value::Object(m) => Box::new(m.values()), |
| 81 | + Value::Array(a) => Box::new(a.iter()), |
| 82 | + _ => Box::new(std::iter::empty()), |
| 83 | + }, |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl UnionElement { |
| 89 | + pub fn get<'a>(&self, v: &'a Value) -> Iter<'a> { |
| 90 | + match self { |
| 91 | + UnionElement::Name(name) => Box::new(v.get(name).into_iter()), |
| 92 | + UnionElement::Index(num) => Box::new(v.get(abs_index(*num, v)).into_iter()), |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn abs_index(index: i64, node: &Value) -> usize { |
| 98 | + if index >= 0 { |
| 99 | + index as usize |
| 100 | + } else { |
| 101 | + let len = if let Value::Array(a) = node { |
| 102 | + a.len() as i64 |
| 103 | + } else { |
| 104 | + 0 |
| 105 | + }; |
| 106 | + (len + index) as usize |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[cfg(test)] |
| 111 | +mod test { |
| 112 | + use super::*; |
| 113 | + use crate::parser::parse; |
| 114 | + use serde_json::json; |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn demo() { |
| 118 | + let a1 = Path::Sel(Box::new(Path::Root), Selector::DotName("foo".to_owned())); |
| 119 | + let a2 = Path::Sel(Box::new(a1), Selector::DotName("bar".to_owned())); |
| 120 | + let a3 = Path::Sel( |
| 121 | + Box::new(a2), |
| 122 | + Selector::Union(vec![UnionElement::Name("baz".to_owned())]), |
| 123 | + ); |
| 124 | + let a4 = Path::Sel(Box::new(a3), Selector::Union(vec![UnionElement::Index(4)])); |
| 125 | + |
| 126 | + let j = json!({"foo":{"bar":{"baz":[10,20,30,40,50,60]}}}); |
| 127 | + println!("j: {}", j); |
| 128 | + |
| 129 | + let v = a4.find(&j).collect::<Vec<_>>(); |
| 130 | + assert_eq!(v[0], 50); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn parse_demo() -> Result<(), String> { |
| 135 | + let p = parse("$.foo['bar'].*[4,-1]")?; |
| 136 | + println!("AST: {:?}", &p); |
| 137 | + let j = json!({"foo":{"bar":{"baz":[10,20,30,40,50,60]}}}); |
| 138 | + |
| 139 | + let v = p.find(&j).collect::<Vec<_>>(); |
| 140 | + println!("RES: {:?}", v); |
| 141 | + |
| 142 | + assert_eq!(v[0], 50); |
| 143 | + assert_eq!(v[1], 60); |
| 144 | + Ok(()) |
| 145 | + } |
| 146 | +} |
0 commit comments