Allow any value in CONTENT / REPLACE / MERGE clauses

This commit is contained in:
Tobie Morgan Hitchcock 2022-02-13 23:39:10 +00:00
parent 1ca5f085c5
commit 6715ac251b
3 changed files with 20 additions and 17 deletions

View file

@ -18,9 +18,9 @@ pub enum Data {
EmptyExpression, EmptyExpression,
SetExpression(Vec<(Idiom, Operator, Value)>), SetExpression(Vec<(Idiom, Operator, Value)>),
PatchExpression(Array), PatchExpression(Array),
MergeExpression(Object), MergeExpression(Value),
ReplaceExpression(Object), ReplaceExpression(Value),
ContentExpression(Object), ContentExpression(Value),
SingleExpression(Value), SingleExpression(Value),
ValuesExpression(Vec<Vec<(Idiom, Value)>>), ValuesExpression(Vec<Vec<(Idiom, Value)>>),
UpdateExpression(Vec<(Idiom, Operator, Value)>), UpdateExpression(Vec<(Idiom, Operator, Value)>),
@ -106,21 +106,21 @@ fn patch(i: &str) -> IResult<&str, Data> {
fn merge(i: &str) -> IResult<&str, Data> { fn merge(i: &str) -> IResult<&str, Data> {
let (i, _) = tag_no_case("MERGE")(i)?; let (i, _) = tag_no_case("MERGE")(i)?;
let (i, _) = shouldbespace(i)?; let (i, _) = shouldbespace(i)?;
let (i, v) = object(i)?; let (i, v) = value(i)?;
Ok((i, Data::MergeExpression(v))) Ok((i, Data::MergeExpression(v)))
} }
fn replace(i: &str) -> IResult<&str, Data> { fn replace(i: &str) -> IResult<&str, Data> {
let (i, _) = tag_no_case("REPLACE")(i)?; let (i, _) = tag_no_case("REPLACE")(i)?;
let (i, _) = shouldbespace(i)?; let (i, _) = shouldbespace(i)?;
let (i, v) = object(i)?; let (i, v) = value(i)?;
Ok((i, Data::ReplaceExpression(v))) Ok((i, Data::ReplaceExpression(v)))
} }
fn content(i: &str) -> IResult<&str, Data> { fn content(i: &str) -> IResult<&str, Data> {
let (i, _) = tag_no_case("CONTENT")(i)?; let (i, _) = tag_no_case("CONTENT")(i)?;
let (i, _) = shouldbespace(i)?; let (i, _) = shouldbespace(i)?;
let (i, v) = object(i)?; let (i, v) = value(i)?;
Ok((i, Data::ContentExpression(v))) Ok((i, Data::ContentExpression(v)))
} }

View file

@ -2,7 +2,6 @@ use crate::dbs::Executor;
use crate::dbs::Options; use crate::dbs::Options;
use crate::dbs::Runtime; use crate::dbs::Runtime;
use crate::err::Error; use crate::err::Error;
use crate::sql::object::Object;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
@ -11,7 +10,7 @@ impl Value {
ctx: &Runtime, ctx: &Runtime,
opt: &Options, opt: &Options,
exe: &Executor<'_>, exe: &Executor<'_>,
val: &Object, val: &Value,
) -> Result<(), Error> { ) -> Result<(), Error> {
match val.compute(ctx, opt, exe, Some(self)).await? { match val.compute(ctx, opt, exe, Some(self)).await? {
Value::Object(v) => { Value::Object(v) => {
@ -20,7 +19,7 @@ impl Value {
} }
Ok(()) Ok(())
} }
_ => unreachable!(), _ => Ok(()),
} }
} }
} }

View file

@ -2,21 +2,25 @@ use crate::dbs::Executor;
use crate::dbs::Options; use crate::dbs::Options;
use crate::dbs::Runtime; use crate::dbs::Runtime;
use crate::err::Error; use crate::err::Error;
use crate::sql::object::Object;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub async fn replace( pub async fn replace(
&mut self, &mut self,
_ctx: &Runtime, ctx: &Runtime,
_opt: &Options, opt: &Options,
_exe: &Executor<'_>, exe: &Executor<'_>,
val: &Object, val: &Value,
) -> Result<(), Error> { ) -> Result<(), Error> {
// Clear all entries // Clear all entries
*self = Value::from(val.clone()); match val.compute(ctx, opt, exe, Some(self)).await? {
Value::Object(v) => {
*self = Value::from(v);
Ok(()) Ok(())
} }
_ => Ok(()),
}
}
} }
#[cfg(test)] #[cfg(test)]
@ -31,7 +35,7 @@ mod tests {
let (ctx, opt, exe) = mock(); let (ctx, opt, exe) = mock();
let mut val = Value::parse("{ test: { other: null, something: 123 } }"); let mut val = Value::parse("{ test: { other: null, something: 123 } }");
let res = Value::parse("{ other: true }"); let res = Value::parse("{ other: true }");
let obj = Object::from(map! {String::from("other") => Value::from(true) }); let obj = Value::parse("{ other: true }");
val.replace(&ctx, &opt, &exe, &obj).await.unwrap(); val.replace(&ctx, &opt, &exe, &obj).await.unwrap();
assert_eq!(res, val); assert_eq!(res, val);
} }