surrealpatch/src/sql/value/set.rs

189 lines
5.7 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::idiom::Idiom;
use crate::sql::object::Object;
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;
impl Value {
2022-01-14 08:12:56 +00:00
#[async_recursion]
pub async fn set(
&mut self,
ctx: &Runtime,
opt: &Options,
exe: &Executor<'_>,
path: &Idiom,
val: Value,
) -> Result<(), 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_mut(&p.name) {
2022-01-14 08:12:56 +00:00
Some(v) if v.is_some() => v.set(ctx, opt, exe, &path.next(), val).await,
_ => {
let mut obj = Value::from(Object::default());
obj.set(ctx, opt, exe, &path.next(), val).await?;
v.insert(&p.name, obj);
Ok(())
}
},
_ => Ok(()),
},
// 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_mut().map(|v| v.set(ctx, opt, exe, &pth, val.clone()));
try_join_all(fut).await?;
Ok(())
2022-01-14 08:12:56 +00:00
}
Part::First => match v.value.first_mut() {
2022-01-14 08:12:56 +00:00
Some(v) => v.set(ctx, opt, exe, &path.next(), val).await,
None => Ok(()),
},
Part::Last => match v.value.last_mut() {
2022-01-14 08:12:56 +00:00
Some(v) => v.set(ctx, opt, exe, &path.next(), val).await,
None => Ok(()),
},
Part::Index(i) => match v.value.get_mut(i.to_usize()) {
2022-01-14 08:12:56 +00:00
Some(v) => v.set(ctx, opt, exe, &path.next(), val).await,
None => Ok(()),
},
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 {
if w.compute(ctx, opt, exe, Some(&v)).await?.is_truthy() {
2022-01-30 23:32:00 +00:00
v.set(ctx, opt, exe, &pth, val.clone()).await?;
}
2022-01-14 08:12:56 +00:00
}
Ok(())
}
_ => Ok(()),
},
// Ignore everything else
_ => Ok(()),
},
// No more parts so set the value
None => {
*self = val.clone();
Ok(())
}
}
}
}
#[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-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::default();
let mut val = Value::parse("{ test: { other: null, something: 123 } }");
let res = Value::parse("999");
2022-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &idi, Value::from(999)).await.unwrap();
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_reset() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::parse("test");
let mut val = Value::parse("{ test: { other: null, something: 123 } }");
let res = Value::parse("{ test: 999 }");
2022-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &idi, Value::from(999)).await.unwrap();
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_basic() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
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-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &idi, Value::from(999)).await.unwrap();
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_wrong() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
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-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &idi, Value::from(999)).await.unwrap();
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_other() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
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-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &idi, Value::from(999)).await.unwrap();
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_array() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
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-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &idi, Value::from(999)).await.unwrap();
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_array_field() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
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-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &idi, Value::from(21)).await.unwrap();
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_array_fields() {
2022-01-29 16:15:30 +00:00
let (ctx, opt, exe) = mock();
let idi = Idiom::parse("test.something[*].age");
let mut val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let res = Value::parse("{ test: { something: [{ age: 21 }, { age: 21 }] } }");
2022-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &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-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 mut val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let res = Value::parse("{ test: { something: [{ age: 34 }, { age: 21 }] } }");
2022-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &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-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 mut val = Value::parse("{ test: { something: [{ age: 34 }, { age: 36 }] } }");
let res = Value::parse("{ test: { something: [{ age: 34 }, 21] } }");
2022-01-29 16:15:30 +00:00
val.set(&ctx, &opt, &exe, &idi, Value::from(21)).await.unwrap();
assert_eq!(res, val);
}
}