2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::dbs::Options;
|
2022-02-15 01:00:30 +00:00
|
|
|
use crate::dbs::Transaction;
|
2022-01-20 23:00:57 +00:00
|
|
|
use crate::err::Error;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::field::{Field, Fields};
|
2022-02-22 19:05:58 +00:00
|
|
|
use crate::sql::part::Next;
|
2022-01-13 17:36:41 +00:00
|
|
|
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;
|
2022-01-13 17:36:41 +00:00
|
|
|
|
|
|
|
impl Value {
|
2022-02-22 14:16:50 +00:00
|
|
|
#[cfg_attr(feature = "parallel", async_recursion)]
|
|
|
|
#[cfg_attr(not(feature = "parallel"), async_recursion(?Send))]
|
2022-01-14 08:12:56 +00:00
|
|
|
pub async fn get(
|
|
|
|
&self,
|
2022-05-14 12:35:08 +00:00
|
|
|
ctx: &Context<'_>,
|
2022-02-06 21:06:52 +00:00
|
|
|
opt: &Options,
|
2022-02-15 03:33:16 +00:00
|
|
|
txn: &Transaction,
|
2022-02-22 19:05:58 +00:00
|
|
|
path: &[Part],
|
2022-01-20 23:00:57 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-02-22 19:05:58 +00:00
|
|
|
match path.first() {
|
2022-01-13 17:36:41 +00:00
|
|
|
// Get the current path part
|
|
|
|
Some(p) => match self {
|
|
|
|
// Current path part is an object
|
|
|
|
Value::Object(v) => match p {
|
2022-05-05 00:27:57 +00:00
|
|
|
Part::Field(f) => match v.get(f as &str) {
|
2022-02-22 19:05:58 +00:00
|
|
|
Some(v) => v.get(ctx, opt, txn, path.next()).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(Value::None),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-03-25 18:45:14 +00:00
|
|
|
Part::All => self.get(ctx, opt, txn, path.next()).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
_ => Ok(Value::None),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
|
|
|
// Current path part is an array
|
|
|
|
Value::Array(v) => match p {
|
2022-01-14 08:12:56 +00:00
|
|
|
Part::All => {
|
2022-02-22 19:05:58 +00:00
|
|
|
let path = path.next();
|
2022-05-04 16:14:40 +00:00
|
|
|
let futs = v.iter().map(|v| v.get(ctx, opt, txn, path));
|
2022-02-22 19:05:58 +00:00
|
|
|
try_join_all(futs).await.map(|v| v.into())
|
2022-01-14 08:12:56 +00:00
|
|
|
}
|
2022-05-04 16:14:40 +00:00
|
|
|
Part::First => match v.first() {
|
2022-02-22 19:05:58 +00:00
|
|
|
Some(v) => v.get(ctx, opt, txn, path.next()).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(Value::None),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-05-04 16:14:40 +00:00
|
|
|
Part::Last => match v.last() {
|
2022-02-22 19:05:58 +00:00
|
|
|
Some(v) => v.get(ctx, opt, txn, path.next()).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(Value::None),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-05-04 16:14:40 +00:00
|
|
|
Part::Index(i) => match v.get(i.to_usize()) {
|
2022-02-22 19:05:58 +00:00
|
|
|
Some(v) => v.get(ctx, opt, txn, path.next()).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(Value::None),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-01-14 08:12:56 +00:00
|
|
|
Part::Where(w) => {
|
2022-02-22 19:05:58 +00:00
|
|
|
let path = path.next();
|
2022-01-14 08:12:56 +00:00
|
|
|
let mut a = Vec::new();
|
2022-05-04 16:14:40 +00:00
|
|
|
for v in v.iter() {
|
2022-03-04 16:01:32 +00:00
|
|
|
if w.compute(ctx, opt, txn, Some(v)).await?.is_truthy() {
|
2022-02-22 19:05:58 +00:00
|
|
|
a.push(v.get(ctx, opt, txn, path).await?)
|
2022-01-20 23:00:57 +00:00
|
|
|
}
|
2022-01-14 08:12:56 +00:00
|
|
|
}
|
2022-01-20 23:00:57 +00:00
|
|
|
Ok(a.into())
|
2022-01-14 08:12:56 +00:00
|
|
|
}
|
2022-02-22 16:33:43 +00:00
|
|
|
_ => {
|
2022-05-04 16:14:40 +00:00
|
|
|
let futs = v.iter().map(|v| v.get(ctx, opt, txn, path));
|
2022-02-22 19:05:58 +00:00
|
|
|
try_join_all(futs).await.map(|v| v.into())
|
2022-02-22 16:33:43 +00:00
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
|
|
|
// Current path part is a thing
|
2022-02-22 19:05:58 +00:00
|
|
|
Value::Thing(v) => match path.len() {
|
2022-01-13 17:36:41 +00:00
|
|
|
// No remote embedded fields, so just return this
|
2022-02-05 09:24:39 +00:00
|
|
|
0 => Ok(Value::Thing(v.clone())),
|
2022-01-13 17:36:41 +00:00
|
|
|
// Remote embedded field, so fetch the thing
|
|
|
|
_ => {
|
|
|
|
let stm = SelectStatement {
|
|
|
|
expr: Fields(vec![Field::All]),
|
|
|
|
what: Values(vec![Value::Thing(v.clone())]),
|
|
|
|
..SelectStatement::default()
|
|
|
|
};
|
2022-05-16 23:12:58 +00:00
|
|
|
stm.compute(ctx, opt, txn, None)
|
2022-02-05 09:24:39 +00:00
|
|
|
.await?
|
2022-03-25 21:14:48 +00:00
|
|
|
.first()
|
2022-02-22 19:05:58 +00:00
|
|
|
.get(ctx, opt, txn, path)
|
2022-02-05 09:24:39 +00:00
|
|
|
.await
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// Ignore everything else
|
2022-01-20 23:00:57 +00:00
|
|
|
_ => Ok(Value::None),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
|
|
|
// No more parts so get the value
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(self.clone()),
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::dbs::test::mock;
|
2022-03-18 07:24:36 +00:00
|
|
|
use crate::sql::id::Id;
|
2022-02-22 19:05:58 +00:00
|
|
|
use crate::sql::idiom::Idiom;
|
2022-01-13 17:36:41 +00:00
|
|
|
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-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-01-20 23:00:57 +00:00
|
|
|
let idi = Idiom::default();
|
2022-01-13 17:36:41 +00:00
|
|
|
let val = Value::parse("{ test: { other: null, something: 123 } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
let res = val.get(&ctx, &opt, &txn, &idi).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn get_basic() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-01-13 17:36:41 +00:00
|
|
|
let idi = Idiom::parse("test.something");
|
|
|
|
let val = Value::parse("{ test: { other: null, something: 123 } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
let res = val.get(&ctx, &opt, &txn, &idi).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(res, Value::from(123));
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn get_thing() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-01-13 17:36:41 +00:00
|
|
|
let idi = Idiom::parse("test.other");
|
|
|
|
let val = Value::parse("{ test: { other: test:tobie, something: 123 } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
let res = val.get(&ctx, &opt, &txn, &idi).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(
|
|
|
|
res,
|
|
|
|
Value::from(Thing {
|
|
|
|
tb: String::from("test"),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from("tobie")
|
2022-01-13 17:36:41 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn get_array() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-01-13 17:36:41 +00:00
|
|
|
let idi = Idiom::parse("test.something[1]");
|
|
|
|
let val = Value::parse("{ test: { something: [123, 456, 789] } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
let res = val.get(&ctx, &opt, &txn, &idi).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(res, Value::from(456));
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn get_array_thing() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-01-13 17:36:41 +00:00
|
|
|
let idi = Idiom::parse("test.something[1]");
|
|
|
|
let val = Value::parse("{ test: { something: [test:tobie, test:jaime] } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
let res = val.get(&ctx, &opt, &txn, &idi).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(
|
|
|
|
res,
|
|
|
|
Value::from(Thing {
|
|
|
|
tb: String::from("test"),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from("jaime")
|
2022-01-13 17:36:41 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn get_array_field() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-01-13 17:36:41 +00:00
|
|
|
let idi = Idiom::parse("test.something[1].age");
|
|
|
|
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
let res = val.get(&ctx, &opt, &txn, &idi).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(res, Value::from(36));
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn get_array_fields() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-01-13 17:36:41 +00:00
|
|
|
let idi = Idiom::parse("test.something[*].age");
|
|
|
|
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
let res = val.get(&ctx, &opt, &txn, &idi).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(res, Value::from(vec![34, 36]));
|
|
|
|
}
|
2022-01-14 08:12:56 +00:00
|
|
|
|
2022-02-22 16:33:43 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn get_array_fields_flat() {
|
|
|
|
let (ctx, opt, txn) = mock().await;
|
|
|
|
let idi = Idiom::parse("test.something.age");
|
|
|
|
let val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
|
|
|
|
let res = val.get(&ctx, &opt, &txn, &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-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
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-02-15 01:00:30 +00:00
|
|
|
let res = val.get(&ctx, &opt, &txn, &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-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
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-02-15 01:00:30 +00:00
|
|
|
let res = val.get(&ctx, &opt, &txn, &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),
|
|
|
|
})])
|
|
|
|
);
|
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|