surrealpatch/src/sql/value/get.rs

198 lines
5.5 KiB
Rust
Raw Normal View History

use crate::dbs::Executor;
use crate::dbs::Options;
use crate::dbs::Runtime;
use crate::err::Error;
use crate::sql::field::{Field, Fields};
use crate::sql::idiom::Idiom;
use crate::sql::part::Part;
use crate::sql::statements::select::SelectStatement;
use crate::sql::value::{Value, Values};
2022-01-14 08:12:56 +00:00
use async_recursion::async_recursion;
2022-01-30 23:32:00 +00:00
use futures::future::try_join_all;
impl Value {
2022-01-14 08:12:56 +00:00
#[async_recursion]
pub async fn get(
&self,
ctx: &Runtime,
opt: &Options<'_>,
exe: &Executor<'_>,
2022-01-14 08:12:56 +00:00
path: &Idiom,
) -> Result<Self, Error> {
match path.parts.first() {
// Get the current path part
Some(p) => match self {
// Current path part is an object
Value::Object(v) => match p {
Part::Field(p) => match v.value.get(&p.name) {
2022-01-14 08:12:56 +00:00
Some(v) => v.get(ctx, opt, exe, &path.next()).await,
None => Ok(Value::None),
},
_ => Ok(Value::None),
},
// Current path part is an array
Value::Array(v) => match p {
2022-01-14 08:12:56 +00:00
Part::All => {
2022-01-30 23:32:00 +00:00
let pth = path.next();
let fut = v.value.iter().map(|v| v.get(&ctx, opt, exe, &pth));
try_join_all(fut).await.map(|v| v.into())
2022-01-14 08:12:56 +00:00
}
Part::First => match v.value.first() {
2022-01-14 08:12:56 +00:00
Some(v) => v.get(ctx, opt, exe, &path.next()).await,
None => Ok(Value::None),
},
Part::Last => match v.value.last() {
2022-01-14 08:12:56 +00:00
Some(v) => v.get(ctx, opt, exe, &path.next()).await,
None => Ok(Value::None),
},
Part::Index(i) => match v.value.get(i.to_usize()) {
2022-01-14 08:12:56 +00:00
Some(v) => v.get(ctx, opt, exe, &path.next()).await,
None => Ok(Value::None),
},
2022-01-14 08:12:56 +00:00
Part::Where(w) => {
2022-01-30 23:32:00 +00:00
let pth = path.next();
2022-01-14 08:12:56 +00:00
let mut a = Vec::new();
for v in &v.value {
if w.compute(ctx, opt, exe, Some(&v)).await?.is_truthy() {
2022-01-30 23:32:00 +00:00
a.push(v.get(ctx, opt, exe, &pth).await?)
}
2022-01-14 08:12:56 +00:00
}
Ok(a.into())
2022-01-14 08:12:56 +00:00
}
_ => Ok(Value::None),
},
// Current path part is a thing
Value::Thing(v) => match path.parts.len() {
// No remote embedded fields, so just return this
0 => Ok(Value::Thing(v.clone())),
// Remote embedded field, so fetch the thing
_ => {
let stm = SelectStatement {
expr: Fields(vec![Field::All]),
what: Values(vec![Value::Thing(v.clone())]),
..SelectStatement::default()
};
stm.compute(ctx, opt, exe, None)
.await?
.first(ctx, opt, exe)
.await?
.get(ctx, opt, exe, &path)
.await
}
},
// Ignore everything else
_ => Ok(Value::None),
},
// No more parts so get the value
None => Ok(self.clone()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dbs::test::mock;
use crate::sql::test::Parse;
use crate::sql::thing::Thing;
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn get_none() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::default();
let val = Value::parse("{ test: { other: null, something: 123 } }");
2022-01-29 16:15:30 +00:00
let res = val.get(&ctx, &opt, &exe, &idi).await.unwrap();
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn get_basic() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::parse("test.something");
let val = Value::parse("{ test: { other: null, something: 123 } }");
2022-01-29 16:15:30 +00:00
let res = val.get(&ctx, &opt, &exe, &idi).await.unwrap();
assert_eq!(res, Value::from(123));
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn get_thing() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::parse("test.other");
let val = Value::parse("{ test: { other: test:tobie, something: 123 } }");
2022-01-29 16:15:30 +00:00
let res = val.get(&ctx, &opt, &exe, &idi).await.unwrap();
assert_eq!(
res,
Value::from(Thing {
tb: String::from("test"),
id: String::from("tobie")
})
);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn get_array() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::parse("test.something[1]");
let val = Value::parse("{ test: { something: [123, 456, 789] } }");
2022-01-29 16:15:30 +00:00
let res = val.get(&ctx, &opt, &exe, &idi).await.unwrap();
assert_eq!(res, Value::from(456));
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn get_array_thing() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::parse("test.something[1]");
let val = Value::parse("{ test: { something: [test:tobie, test:jaime] } }");
2022-01-29 16:15:30 +00:00
let res = val.get(&ctx, &opt, &exe, &idi).await.unwrap();
assert_eq!(
res,
Value::from(Thing {
tb: String::from("test"),
id: String::from("jaime")
})
);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn get_array_field() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::parse("test.something[1].age");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
2022-01-29 16:15:30 +00:00
let res = val.get(&ctx, &opt, &exe, &idi).await.unwrap();
assert_eq!(res, Value::from(36));
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn get_array_fields() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::parse("test.something[*].age");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
2022-01-29 16:15:30 +00:00
let res = val.get(&ctx, &opt, &exe, &idi).await.unwrap();
assert_eq!(res, Value::from(vec![34, 36]));
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn get_array_where_field() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
2022-01-14 08:12:56 +00:00
let idi = Idiom::parse("test.something[WHERE age > 35].age");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
2022-01-29 16:15:30 +00:00
let res = val.get(&ctx, &opt, &exe, &idi).await.unwrap();
2022-01-14 08:12:56 +00:00
assert_eq!(res, Value::from(vec![36]));
}
#[tokio::test]
async fn get_array_where_fields() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
2022-01-14 08:12:56 +00:00
let idi = Idiom::parse("test.something[WHERE age > 35]");
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
2022-01-29 16:15:30 +00:00
let res = val.get(&ctx, &opt, &exe, &idi).await.unwrap();
2022-01-14 08:12:56 +00:00
assert_eq!(
res,
Value::from(vec![Value::from(map! {
"age".to_string() => Value::from(36),
})])
);
}
}