2022-01-13 17:36:41 +00:00
|
|
|
use crate::dbs::Options;
|
|
|
|
use crate::dbs::Runtime;
|
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::idiom::Idiom;
|
|
|
|
use crate::sql::part::Part;
|
|
|
|
use crate::sql::value::Value;
|
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 set(
|
2022-01-13 17:36:41 +00:00
|
|
|
&mut self,
|
|
|
|
ctx: &Runtime,
|
2022-02-06 21:06:52 +00:00
|
|
|
opt: &Options,
|
2022-02-15 03:33:16 +00:00
|
|
|
txn: &Transaction,
|
2022-01-13 17:36:41 +00:00
|
|
|
path: &Idiom,
|
|
|
|
val: Value,
|
2022-01-20 23:00:57 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-01-13 17:36:41 +00:00
|
|
|
match path.parts.first() {
|
|
|
|
// Get the current path part
|
|
|
|
Some(p) => match self {
|
|
|
|
// Current path part is an object
|
|
|
|
Value::Object(v) => match p {
|
2022-02-22 16:33:43 +00:00
|
|
|
Part::Field(f) => match v.value.get_mut(&f.name) {
|
2022-02-15 01:00:30 +00:00
|
|
|
Some(v) if v.is_some() => v.set(ctx, opt, txn, &path.next(), val).await,
|
2022-01-13 17:36:41 +00:00
|
|
|
_ => {
|
2022-02-09 08:23:14 +00:00
|
|
|
let mut obj = Value::base();
|
2022-02-15 01:00:30 +00:00
|
|
|
obj.set(ctx, opt, txn, &path.next(), val).await?;
|
2022-02-22 16:33:43 +00:00
|
|
|
v.insert(&f.name, obj);
|
2022-01-20 23:00:57 +00:00
|
|
|
Ok(())
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
},
|
2022-01-20 23:00:57 +00:00
|
|
|
_ => Ok(()),
|
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-01-30 23:32:00 +00:00
|
|
|
let pth = path.next();
|
|
|
|
let fut =
|
2022-02-15 01:00:30 +00:00
|
|
|
v.value.iter_mut().map(|v| v.set(ctx, opt, txn, &pth, val.clone()));
|
2022-01-30 23:32:00 +00:00
|
|
|
try_join_all(fut).await?;
|
2022-01-20 23:00:57 +00:00
|
|
|
Ok(())
|
2022-01-14 08:12:56 +00:00
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
Part::First => match v.value.first_mut() {
|
2022-02-15 01:00:30 +00:00
|
|
|
Some(v) => v.set(ctx, opt, txn, &path.next(), val).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(()),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
|
|
|
Part::Last => match v.value.last_mut() {
|
2022-02-15 01:00:30 +00:00
|
|
|
Some(v) => v.set(ctx, opt, txn, &path.next(), val).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(()),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
|
|
|
Part::Index(i) => match v.value.get_mut(i.to_usize()) {
|
2022-02-15 01:00:30 +00:00
|
|
|
Some(v) => v.set(ctx, opt, txn, &path.next(), val).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(()),
|
2022-01-13 17:36:41 +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
|
|
|
for v in &mut v.value {
|
2022-02-15 01:00:30 +00:00
|
|
|
if w.compute(ctx, opt, txn, Some(&v)).await?.is_truthy() {
|
|
|
|
v.set(ctx, opt, txn, &pth, val.clone()).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(())
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
2022-02-22 16:33:43 +00:00
|
|
|
_ => {
|
|
|
|
let fut =
|
|
|
|
v.value.iter_mut().map(|v| v.set(ctx, opt, txn, &path, val.clone()));
|
|
|
|
try_join_all(fut).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-02-09 08:49:08 +00:00
|
|
|
// Current path part is empty
|
|
|
|
Value::Null => {
|
|
|
|
*self = Value::base();
|
2022-02-15 01:00:30 +00:00
|
|
|
self.set(ctx, opt, txn, path, val).await
|
2022-02-09 08:49:08 +00:00
|
|
|
}
|
|
|
|
// Current path part is empty
|
|
|
|
Value::None => {
|
|
|
|
*self = Value::base();
|
2022-02-15 01:00:30 +00:00
|
|
|
self.set(ctx, opt, txn, path, val).await
|
2022-02-09 08:49:08 +00:00
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
// Ignore everything else
|
2022-01-20 23:00:57 +00:00
|
|
|
_ => Ok(()),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
|
|
|
// No more parts so set the value
|
2022-01-20 23:00:57 +00:00
|
|
|
None => {
|
2022-02-09 08:49:22 +00:00
|
|
|
*self = val;
|
2022-01-20 23:00:57 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::dbs::test::mock;
|
|
|
|
use crate::sql::test::Parse;
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn set_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 mut val = Value::parse("{ test: { other: null, something: 123 } }");
|
|
|
|
let res = Value::parse("999");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(999)).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
2022-02-09 08:49:08 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn set_empty() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-02-09 08:49:08 +00:00
|
|
|
let idi = Idiom::parse("test");
|
|
|
|
let mut val = Value::None;
|
|
|
|
let res = Value::parse("{ test: 999 }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(999)).await.unwrap();
|
2022-02-09 08:49:08 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn set_blank() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-02-09 08:49:08 +00:00
|
|
|
let idi = Idiom::parse("test.something");
|
|
|
|
let mut val = Value::None;
|
|
|
|
let res = Value::parse("{ test: { something: 999 } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(999)).await.unwrap();
|
2022-02-09 08:49:08 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn set_reset() {
|
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");
|
|
|
|
let mut val = Value::parse("{ test: { other: null, something: 123 } }");
|
|
|
|
let res = Value::parse("{ test: 999 }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(999)).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 set_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 mut val = Value::parse("{ test: { other: null, something: 123 } }");
|
|
|
|
let res = Value::parse("{ test: { other: null, something: 999 } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(999)).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
2022-02-09 08:49:08 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn set_allow() {
|
2022-02-15 01:00:30 +00:00
|
|
|
let (ctx, opt, txn) = mock().await;
|
2022-02-09 08:49:08 +00:00
|
|
|
let idi = Idiom::parse("test.something.allow");
|
|
|
|
let mut val = Value::parse("{ test: { other: null } }");
|
|
|
|
let res = Value::parse("{ test: { other: null, something: { allow: 999 } } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(999)).await.unwrap();
|
2022-02-09 08:49:08 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn set_wrong() {
|
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.wrong");
|
|
|
|
let mut val = Value::parse("{ test: { other: null, something: 123 } }");
|
|
|
|
let res = Value::parse("{ test: { other: null, something: 123 } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(999)).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 set_other() {
|
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.something");
|
|
|
|
let mut val = Value::parse("{ test: { other: null, something: 123 } }");
|
|
|
|
let res = Value::parse("{ test: { other: { something: 999 }, something: 123 } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(999)).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 set_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 mut val = Value::parse("{ test: { something: [123, 456, 789] } }");
|
|
|
|
let res = Value::parse("{ test: { something: [123, 999, 789] } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(999)).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 set_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 mut val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
|
|
|
|
let res = Value::parse("{ test: { something: [{ age: 34 }, { age: 21 }] } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(21)).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 set_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 mut val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
|
2022-02-22 16:33:43 +00:00
|
|
|
let res = Value::parse("{ test: { something: [{ age: 21 }, { age: 21 }] } }");
|
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(21)).await.unwrap();
|
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn set_array_fields_flat() {
|
|
|
|
let (ctx, opt, txn) = mock().await;
|
|
|
|
let idi = Idiom::parse("test.something.age");
|
|
|
|
let mut val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
|
2022-01-13 17:36:41 +00:00
|
|
|
let res = Value::parse("{ test: { something: [{ age: 21 }, { age: 21 }] } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(21)).await.unwrap();
|
2022-01-14 08:12:56 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn set_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 mut val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
|
|
|
|
let res = Value::parse("{ test: { something: [{ age: 34 }, { age: 21 }] } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(21)).await.unwrap();
|
2022-01-14 08:12:56 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn set_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 mut val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
|
|
|
|
let res = Value::parse("{ test: { something: [{ age: 34 }, 21] } }");
|
2022-02-15 01:00:30 +00:00
|
|
|
val.set(&ctx, &opt, &txn, &idi, Value::from(21)).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
}
|