surrealpatch/src/sql/value/set.rs

184 lines
5.6 KiB
Rust
Raw Normal View History

use crate::dbs::Executor;
use crate::dbs::Options;
use crate::dbs::Runtime;
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;
impl Value {
2022-01-14 08:12:56 +00:00
#[async_recursion]
pub async fn set(
&mut self,
ctx: &Runtime,
2022-01-14 08:12:56 +00:00
opt: &Options<'_>,
exe: &mut Executor,
path: &Idiom,
val: Value,
) {
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());
2022-01-14 08:12:56 +00:00
obj.set(ctx, opt, exe, &path.next(), val).await;
v.insert(&p.name, obj)
}
},
_ => (),
},
// Current path part is an array
Value::Array(v) => match p {
2022-01-14 08:12:56 +00:00
Part::All => {
for v in &mut v.value {
v.set(ctx, opt, exe, &path.next(), val.clone()).await
}
}
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 => (),
},
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 => (),
},
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 => (),
},
Part::Where(w) => {
2022-01-14 08:12:56 +00:00
for v in &mut v.value {
match w.compute(ctx, opt, exe, Some(&v)).await {
Ok(x) if x.is_truthy() => {
v.set(ctx, opt, exe, &path.next(), val.clone()).await
}
_ => (),
};
}
}
_ => (),
},
// Ignore everything else
_ => (),
},
// No more parts so set the value
None => *self = val.clone(),
}
}
}
#[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() {
let (ctx, opt, mut exe) = mock();
let idi = Idiom {
parts: vec![],
};
let mut val = Value::parse("{ test: { other: null, something: 123 } }");
let res = Value::parse("999");
2022-01-14 08:12:56 +00:00
val.set(&ctx, &opt, &mut exe, &idi, Value::from(999)).await;
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_reset() {
let (ctx, opt, mut 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-14 08:12:56 +00:00
val.set(&ctx, &opt, &mut exe, &idi, Value::from(999)).await;
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_basic() {
let (ctx, opt, mut 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-14 08:12:56 +00:00
val.set(&ctx, &opt, &mut exe, &idi, Value::from(999)).await;
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_wrong() {
let (ctx, opt, mut 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-14 08:12:56 +00:00
val.set(&ctx, &opt, &mut exe, &idi, Value::from(999)).await;
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_other() {
let (ctx, opt, mut 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-14 08:12:56 +00:00
val.set(&ctx, &opt, &mut exe, &idi, Value::from(999)).await;
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_array() {
let (ctx, opt, mut 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-14 08:12:56 +00:00
val.set(&ctx, &opt, &mut exe, &idi, Value::from(999)).await;
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_array_field() {
let (ctx, opt, mut 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-14 08:12:56 +00:00
val.set(&ctx, &opt, &mut exe, &idi, Value::from(21)).await;
assert_eq!(res, val);
}
2022-01-14 08:12:56 +00:00
#[tokio::test]
async fn set_array_fields() {
let (ctx, opt, mut 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-14 08:12:56 +00:00
val.set(&ctx, &opt, &mut exe, &idi, Value::from(21)).await;
assert_eq!(res, val);
}
#[tokio::test]
async fn set_array_where_field() {
let (ctx, opt, mut exe) = mock();
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 }] } }");
val.set(&ctx, &opt, &mut exe, &idi, Value::from(21)).await;
assert_eq!(res, val);
}
#[tokio::test]
async fn set_array_where_fields() {
let (ctx, opt, mut exe) = mock();
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] } }");
val.set(&ctx, &opt, &mut exe, &idi, Value::from(21)).await;
assert_eq!(res, val);
}
}