2022-04-07 10:12:48 +00:00
|
|
|
use crate::sql::idiom::Idiom;
|
|
|
|
use crate::sql::part::Part;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
|
|
|
impl Value {
|
2022-09-29 10:09:20 +00:00
|
|
|
pub fn every(&self, split: bool) -> Vec<Idiom> {
|
|
|
|
self._every(split, Idiom::default())
|
2022-04-07 10:12:48 +00:00
|
|
|
}
|
2022-09-29 10:09:20 +00:00
|
|
|
fn _every(&self, split: bool, prev: Idiom) -> Vec<Idiom> {
|
2022-04-07 10:12:48 +00:00
|
|
|
match self {
|
|
|
|
// Current path part is an object
|
|
|
|
Value::Object(v) => v
|
|
|
|
.iter()
|
2022-05-27 12:21:41 +00:00
|
|
|
.flat_map(|(k, v)| {
|
|
|
|
let p = Part::from(k.to_owned());
|
2022-09-29 10:09:20 +00:00
|
|
|
v._every(split, prev.clone().push(p))
|
2022-05-27 12:21:41 +00:00
|
|
|
})
|
2022-04-07 10:12:48 +00:00
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
// Current path part is an array
|
2022-09-29 10:09:20 +00:00
|
|
|
Value::Array(v) => match split {
|
|
|
|
true => v
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.flat_map(|(i, v)| {
|
|
|
|
let p = Part::from(i.to_owned());
|
|
|
|
v._every(split, prev.clone().push(p))
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
false => vec![prev],
|
|
|
|
},
|
2022-04-07 10:12:48 +00:00
|
|
|
// Process everything else
|
|
|
|
_ => vec![prev],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::sql::idiom::Idiom;
|
|
|
|
use crate::sql::test::Parse;
|
|
|
|
|
|
|
|
#[test]
|
2022-09-29 10:09:20 +00:00
|
|
|
fn every_without_array_indexes() {
|
|
|
|
let val = Value::parse("{ test: { something: [{ age: 34, tags: ['code', 'databases'] }, { age: 36, tags: ['design', 'operations'] }] } }");
|
|
|
|
let res = vec![Idiom::parse("test.something")];
|
|
|
|
assert_eq!(res, val.every(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn every_including_array_indexes() {
|
2022-04-07 10:12:48 +00:00
|
|
|
let val = Value::parse("{ test: { something: [{ age: 34, tags: ['code', 'databases'] }, { age: 36, tags: ['design', 'operations'] }] } }");
|
|
|
|
let res = vec![
|
|
|
|
Idiom::parse("test.something[0].age"),
|
|
|
|
Idiom::parse("test.something[0].tags[0]"),
|
|
|
|
Idiom::parse("test.something[0].tags[1]"),
|
|
|
|
Idiom::parse("test.something[1].age"),
|
|
|
|
Idiom::parse("test.something[1].tags[0]"),
|
|
|
|
Idiom::parse("test.something[1].tags[1]"),
|
|
|
|
];
|
2022-09-29 10:09:20 +00:00
|
|
|
assert_eq!(res, val.every(true));
|
2022-04-07 10:12:48 +00:00
|
|
|
}
|
|
|
|
}
|