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-01-20 23:00:57 +00:00
|
|
|
use crate::err::Error;
|
2023-04-18 21:32:29 +00:00
|
|
|
use crate::exe::try_join_all_buffered;
|
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::value::Value;
|
2022-01-14 08:12:56 +00:00
|
|
|
use async_recursion::async_recursion;
|
2022-01-13 17:36:41 +00:00
|
|
|
|
|
|
|
impl Value {
|
2023-05-06 00:15:03 +00:00
|
|
|
/// Asynchronous method for setting a field on a `Value`
|
2023-01-07 09:42:45 +00:00
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_recursion)]
|
|
|
|
#[cfg_attr(target_arch = "wasm32", async_recursion(?Send))]
|
2023-04-24 20:51:35 +00:00
|
|
|
pub(crate) async fn set(
|
2022-01-13 17:36:41 +00:00
|
|
|
&mut self,
|
2022-05-14 12:35:08 +00:00
|
|
|
ctx: &Context<'_>,
|
2022-02-06 21:06:52 +00:00
|
|
|
opt: &Options,
|
2022-02-22 19:05:58 +00:00
|
|
|
path: &[Part],
|
2022-01-13 17:36:41 +00:00
|
|
|
val: Value,
|
2022-01-20 23:00:57 +00:00
|
|
|
) -> Result<(), 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-06-09 08:18:08 +00:00
|
|
|
Part::Graph(g) => match v.get_mut(g.to_raw().as_str()) {
|
2023-06-19 18:41:13 +00:00
|
|
|
Some(v) if v.is_some() => v.set(ctx, opt, path.next(), val).await,
|
2022-06-09 08:18:08 +00:00
|
|
|
_ => {
|
|
|
|
let mut obj = Value::base();
|
2023-06-19 18:41:13 +00:00
|
|
|
obj.set(ctx, opt, path.next(), val).await?;
|
2022-06-09 08:18:08 +00:00
|
|
|
v.insert(g.to_raw(), obj);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
},
|
2022-09-30 20:40:56 +00:00
|
|
|
Part::Field(f) => match v.get_mut(f as &str) {
|
2023-06-19 18:41:13 +00:00
|
|
|
Some(v) if v.is_some() => v.set(ctx, opt, 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();
|
2023-06-19 18:41:13 +00:00
|
|
|
obj.set(ctx, opt, path.next(), val).await?;
|
2022-05-31 19:44:27 +00:00
|
|
|
v.insert(f.to_raw(), obj);
|
2022-01-20 23:00:57 +00:00
|
|
|
Ok(())
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
},
|
2023-05-15 20:22:13 +00:00
|
|
|
Part::Index(i) => match v.get_mut(&i.to_string()) {
|
2023-06-19 18:41:13 +00:00
|
|
|
Some(v) if v.is_some() => v.set(ctx, opt, path.next(), val).await,
|
2023-05-15 20:22:13 +00:00
|
|
|
_ => {
|
|
|
|
let mut obj = Value::base();
|
2023-06-19 18:41:13 +00:00
|
|
|
obj.set(ctx, opt, path.next(), val).await?;
|
2023-05-15 20:22:13 +00:00
|
|
|
v.insert(i.to_string(), obj);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
},
|
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-02-22 19:05:58 +00:00
|
|
|
let path = path.next();
|
2023-06-19 18:41:13 +00:00
|
|
|
let futs = v.iter_mut().map(|v| v.set(ctx, opt, path, val.clone()));
|
2023-04-18 21:32:29 +00:00
|
|
|
try_join_all_buffered(futs).await?;
|
2022-01-20 23:00:57 +00:00
|
|
|
Ok(())
|
2022-01-14 08:12:56 +00:00
|
|
|
}
|
2022-05-04 16:14:40 +00:00
|
|
|
Part::First => match v.first_mut() {
|
2023-06-19 18:41:13 +00:00
|
|
|
Some(v) => v.set(ctx, opt, path.next(), val).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(()),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-05-04 16:14:40 +00:00
|
|
|
Part::Last => match v.last_mut() {
|
2023-06-19 18:41:13 +00:00
|
|
|
Some(v) => v.set(ctx, opt, path.next(), val).await,
|
2022-01-20 23:00:57 +00:00
|
|
|
None => Ok(()),
|
2022-01-13 17:36:41 +00:00
|
|
|
},
|
2022-05-04 16:14:40 +00:00
|
|
|
Part::Index(i) => match v.get_mut(i.to_usize()) {
|
2023-06-19 18:41:13 +00:00
|
|
|
Some(v) => v.set(ctx, opt, 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-02-22 19:05:58 +00:00
|
|
|
let path = path.next();
|
2022-05-04 16:14:40 +00:00
|
|
|
for v in v.iter_mut() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let mut child_ctx = Context::new(ctx);
|
|
|
|
child_ctx.add_cursor_doc(v);
|
|
|
|
if w.compute(&child_ctx, opt).await?.is_truthy() {
|
|
|
|
v.set(ctx, opt, path, 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
|
|
|
_ => {
|
2023-06-19 18:41:13 +00:00
|
|
|
let futs = v.iter_mut().map(|v| v.set(ctx, opt, path, val.clone()));
|
2023-04-18 21:32:29 +00:00
|
|
|
try_join_all_buffered(futs).await?;
|
2022-02-22 16:33:43 +00:00
|
|
|
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();
|
2023-06-19 18:41:13 +00:00
|
|
|
self.set(ctx, opt, path, val).await
|
2022-02-09 08:49:08 +00:00
|
|
|
}
|
|
|
|
// Current path part is empty
|
|
|
|
Value::None => {
|
|
|
|
*self = Value::base();
|
2023-06-19 18:41:13 +00:00
|
|
|
self.set(ctx, opt, 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;
|
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;
|
|
|
|
|
2022-01-14 08:12:56 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn set_none() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &idi, Value::from(999)).await.unwrap();
|
2022-02-09 08:49:08 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn set_blank() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 } } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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] } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 }] } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 }] } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &idi, Value::from(21)).await.unwrap();
|
2022-02-22 16:33:43 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn set_array_fields_flat() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = mock().await;
|
2022-02-22 16:33:43 +00:00
|
|
|
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 }] } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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 }] } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &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() {
|
2023-06-19 18:41:13 +00:00
|
|
|
let (ctx, opt) = 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] } }");
|
2023-06-19 18:41:13 +00:00
|
|
|
val.set(&ctx, &opt, &idi, Value::from(21)).await.unwrap();
|
2022-01-13 17:36:41 +00:00
|
|
|
assert_eq!(res, val);
|
|
|
|
}
|
|
|
|
}
|