#![deny(clippy::wildcard_enum_match_arm)]
use crate::expr::part::{Next, Part};
use crate::val::Value;
impl Value {
pub fn pick(&self, path: &[Part]) -> Self {
match path.first() {
Some(p) => match self {
Value::Object(v) => match p {
Part::Field(f) => match v.get(f as &str) {
Some(v) => v.pick(path.next()),
None => Value::None,
},
Part::All => v.values().map(|v| v.pick(path.next())).collect::<Vec<_>>().into(),
Part::Flatten
| Part::Last
| Part::First
| Part::Where(_)
| Part::Lookup(_)
| Part::Value(_)
| Part::Start(_)
| Part::Method(_, _)
| Part::Destructure(_)
| Part::Optional
| Part::Recurse(_, _, _)
| Part::Doc
| Part::RepeatRecurse => {
if let Some(idx) = p.as_old_index() {
match v.get(&idx.to_string()) {
Some(v) => v.pick(path.next()),
None => Value::None,
}
} else {
Value::None
}
}
},
Value::Array(v) => match p {
Part::All => v.iter().map(|v| v.pick(path.next())).collect::<Vec<_>>().into(),
Part::First => match v.first() {
Some(v) => v.pick(path.next()),
None => Value::None,
},
Part::Last => match v.last() {
Some(v) => v.pick(path.next()),
None => Value::None,
},
Part::Flatten
| Part::Field(_)
| Part::Where(_)
| Part::Lookup(_)
| Part::Value(_)
| Part::Start(_)
| Part::Method(_, _)
| Part::Destructure(_)
| Part::Optional
| Part::Recurse(_, _, _)
| Part::Doc
| Part::RepeatRecurse => {
if let Some(idx) = p.as_old_index() {
match v.get(idx) {
Some(v) => v.pick(path.next()),
None => Value::None,
}
} else {
v.iter().map(|v| v.pick(path)).collect::<Vec<_>>().into()
}
}
},
Value::Set(v) => match p {
Part::All => Value::Set(v.iter().map(|v| v.pick(path.next())).collect()),
Part::First => match v.first() {
Some(v) => v.pick(path.next()),
None => Value::None,
},
Part::Last => match v.last() {
Some(v) => v.pick(path.next()),
None => Value::None,
},
Part::Flatten
| Part::Field(_)
| Part::Where(_)
| Part::Lookup(_)
| Part::Value(_)
| Part::Start(_)
| Part::Method(_, _)
| Part::Destructure(_)
| Part::Optional
| Part::Recurse(_, _, _)
| Part::Doc
| Part::RepeatRecurse => {
if let Some(idx) = p.as_old_index() {
match v.nth(idx) {
Some(v) => v.pick(path.next()),
None => Value::None,
}
} else {
Value::Set(v.iter().map(|v| v.pick(path)).collect())
}
}
},
Value::None
| Value::Null
| Value::Bool(_)
| Value::Number(_)
| Value::String(_)
| Value::Duration(_)
| Value::Datetime(_)
| Value::Date(_)
| Value::Time(_)
| Value::Timestamp(_)
| Value::Interval(_)
| Value::Uuid(_)
| Value::Geometry(_)
| Value::Bytes(_)
| Value::Table(_)
| Value::RecordId(_)
| Value::File(_)
| Value::Regex(_)
| Value::Range(_)
| Value::Closure(_) => Value::None,
},
None => self.clone(),
}
}
}
#[cfg(test)]
mod tests {
use surrealdb_strand::Strand;
use super::*;
use crate::expr::idiom::Idiom;
use crate::syn;
use crate::val::{RecordId, RecordIdKey};
macro_rules! parse_val {
($input:expr) => {
crate::val::convert_public_value_to_internal(syn::value($input).unwrap())
};
}
#[test]
fn pick_none() {
let idi: Idiom = Idiom::default();
let val = parse_val!("{ test: { other: null, something: 123 } }");
let res = val.pick(&idi);
assert_eq!(res, val);
}
#[test]
fn pick_basic() {
let idi: Idiom = syn::idiom("test.something").unwrap();
let val = parse_val!("{ test: { other: null, something: 123 } }");
let res = val.pick(&idi);
assert_eq!(res, Value::from(123));
}
#[test]
fn pick_thing() {
let idi: Idiom = syn::idiom("test.other").unwrap();
let val = parse_val!("{ test: { other: test:tobie, something: 123 } }");
let res = val.pick(&idi);
assert_eq!(
res,
Value::from(RecordId {
table: "test".into(),
key: RecordIdKey::String(Strand::new_static("tobie"))
})
);
}
#[test]
fn pick_array() {
let idi: Idiom = syn::idiom("test.something[1]").unwrap();
let val = parse_val!("{ test: { something: [123, 456, 789] } }");
let res = val.pick(&idi);
assert_eq!(res, Value::from(456));
}
#[test]
fn pick_array_thing() {
let idi: Idiom = syn::idiom("test.something[1]").unwrap();
let val = parse_val!("{ test: { something: [test:tobie, test:jaime] } }");
let res = val.pick(&idi);
assert_eq!(
res,
Value::from(RecordId {
table: "test".into(),
key: RecordIdKey::String(Strand::new_static("jaime"))
})
);
}
#[test]
fn pick_array_field() {
let idi: Idiom = syn::idiom("test.something[1].age").unwrap();
let val = parse_val!("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let res = val.pick(&idi);
assert_eq!(res, Value::from(36));
}
#[test]
fn pick_array_fields() {
let idi: Idiom = syn::idiom("test.something[*].age").unwrap();
let val = parse_val!("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let res = val.pick(&idi);
assert_eq!(res, [Value::from(34i64), Value::from(36i64)].into_iter().collect::<Value>());
}
#[test]
fn pick_array_fields_flat() {
let idi: Idiom = syn::idiom("test.something.age").unwrap();
let val = parse_val!("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let res = val.pick(&idi);
assert_eq!(res, [Value::from(34i64), Value::from(36i64)].into_iter().collect::<Value>());
}
}