2022-04-04 22:26:54 +00:00
|
|
|
use crate::ctx::Context;
|
|
|
|
use crate::dbs::Statement;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Options, Transaction};
|
2022-04-04 22:26:54 +00:00
|
|
|
use crate::doc::Document;
|
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::permission::Permission;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
|
|
|
impl<'a> Document<'a> {
|
|
|
|
pub async fn field(
|
|
|
|
&mut self,
|
2022-05-14 12:35:08 +00:00
|
|
|
ctx: &Context<'_>,
|
2022-04-04 22:26:54 +00:00
|
|
|
opt: &Options,
|
2023-07-06 14:57:42 +00:00
|
|
|
txn: &Transaction,
|
2023-05-07 12:33:32 +00:00
|
|
|
_stm: &Statement<'_>,
|
2022-04-04 22:26:54 +00:00
|
|
|
) -> Result<(), Error> {
|
2022-08-25 13:49:33 +00:00
|
|
|
// Check fields
|
|
|
|
if !opt.fields {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-09-10 04:59:08 +00:00
|
|
|
// Get the record id
|
|
|
|
let rid = self.id.as_ref().unwrap();
|
2023-05-07 12:33:32 +00:00
|
|
|
// Get the user applied input
|
2023-07-06 14:57:42 +00:00
|
|
|
let inp = self.initial.doc.changed(self.current.doc.as_ref());
|
2022-04-04 22:26:54 +00:00
|
|
|
// Loop through all field statements
|
2023-07-06 14:57:42 +00:00
|
|
|
for fd in self.fd(opt, txn).await?.iter() {
|
2022-04-07 10:16:24 +00:00
|
|
|
// Loop over each field in document
|
2023-07-06 14:57:42 +00:00
|
|
|
for (k, mut val) in self.current.doc.walk(&fd.name).into_iter() {
|
2022-04-07 10:16:24 +00:00
|
|
|
// Get the initial value
|
2023-07-06 14:57:42 +00:00
|
|
|
let old = self.initial.doc.pick(&k);
|
2023-03-31 18:46:13 +00:00
|
|
|
// Get the input value
|
2023-05-07 12:33:32 +00:00
|
|
|
let inp = inp.pick(&k);
|
2023-04-25 10:13:04 +00:00
|
|
|
// Check for a TYPE clause
|
|
|
|
if let Some(kind) = &fd.kind {
|
|
|
|
if !val.is_none() {
|
2023-06-06 06:12:59 +00:00
|
|
|
val = val.coerce_to(kind).map_err(|e| match e {
|
2023-04-25 10:13:04 +00:00
|
|
|
// There was a conversion error
|
2023-06-06 06:12:59 +00:00
|
|
|
Error::CoerceTo {
|
2023-04-25 10:13:04 +00:00
|
|
|
from,
|
|
|
|
..
|
|
|
|
} => Error::FieldCheck {
|
|
|
|
thing: rid.to_string(),
|
|
|
|
field: fd.name.clone(),
|
|
|
|
value: from.to_string(),
|
|
|
|
check: kind.to_string(),
|
|
|
|
},
|
|
|
|
// There was a different error
|
|
|
|
e => e,
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 10:16:24 +00:00
|
|
|
// Check for a VALUE clause
|
|
|
|
if let Some(expr) = &fd.value {
|
|
|
|
// Configure the context
|
|
|
|
let mut ctx = Context::new(ctx);
|
2023-05-06 20:49:34 +00:00
|
|
|
ctx.add_value("input", &inp);
|
|
|
|
ctx.add_value("value", &val);
|
|
|
|
ctx.add_value("after", &val);
|
|
|
|
ctx.add_value("before", &old);
|
2022-04-07 10:16:24 +00:00
|
|
|
// Process the VALUE clause
|
2023-07-06 14:57:42 +00:00
|
|
|
val = expr.compute(&ctx, opt, txn, Some(&self.current)).await?;
|
2022-04-04 22:26:54 +00:00
|
|
|
}
|
2022-04-07 10:16:24 +00:00
|
|
|
// Check for a TYPE clause
|
|
|
|
if let Some(kind) = &fd.kind {
|
2023-06-06 06:12:59 +00:00
|
|
|
val = val.coerce_to(kind).map_err(|e| match e {
|
2023-04-25 10:13:04 +00:00
|
|
|
// There was a conversion error
|
2023-06-06 06:12:59 +00:00
|
|
|
Error::CoerceTo {
|
2023-04-25 10:13:04 +00:00
|
|
|
from,
|
|
|
|
..
|
|
|
|
} => Error::FieldCheck {
|
|
|
|
thing: rid.to_string(),
|
|
|
|
field: fd.name.clone(),
|
|
|
|
value: from.to_string(),
|
|
|
|
check: kind.to_string(),
|
|
|
|
},
|
|
|
|
// There was a different error
|
|
|
|
e => e,
|
|
|
|
})?;
|
2022-04-04 22:26:54 +00:00
|
|
|
}
|
2022-04-07 10:16:24 +00:00
|
|
|
// Check for a ASSERT clause
|
|
|
|
if let Some(expr) = &fd.assert {
|
|
|
|
// Configure the context
|
|
|
|
let mut ctx = Context::new(ctx);
|
2023-05-06 20:49:34 +00:00
|
|
|
ctx.add_value("input", &inp);
|
|
|
|
ctx.add_value("value", &val);
|
|
|
|
ctx.add_value("after", &val);
|
|
|
|
ctx.add_value("before", &old);
|
2022-04-07 10:16:24 +00:00
|
|
|
// Process the ASSERT clause
|
2023-07-06 14:57:42 +00:00
|
|
|
if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
|
2022-04-07 10:16:24 +00:00
|
|
|
return Err(Error::FieldValue {
|
2022-09-10 04:59:08 +00:00
|
|
|
thing: rid.to_string(),
|
2022-04-07 10:16:24 +00:00
|
|
|
field: fd.name.clone(),
|
2023-04-25 10:13:04 +00:00
|
|
|
value: val.to_string(),
|
2022-05-06 22:09:08 +00:00
|
|
|
check: expr.to_string(),
|
2022-04-07 10:16:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for a PERMISSIONS clause
|
|
|
|
if opt.perms && opt.auth.perms() {
|
|
|
|
// Get the permission clause
|
2023-04-25 10:13:04 +00:00
|
|
|
let perms = if self.is_new() {
|
2022-06-26 13:28:55 +00:00
|
|
|
&fd.permissions.create
|
2022-04-07 10:16:24 +00:00
|
|
|
} else {
|
|
|
|
&fd.permissions.update
|
|
|
|
};
|
|
|
|
// Match the permission clause
|
|
|
|
match perms {
|
|
|
|
Permission::Full => (),
|
|
|
|
Permission::None => val = old,
|
|
|
|
Permission::Specific(e) => {
|
2023-02-12 11:48:11 +00:00
|
|
|
// Disable permissions
|
2023-07-05 21:26:13 +00:00
|
|
|
let opt = &opt.new_with_perms(false);
|
2022-04-07 10:16:24 +00:00
|
|
|
// Configure the context
|
|
|
|
let mut ctx = Context::new(ctx);
|
2023-05-06 20:49:34 +00:00
|
|
|
ctx.add_value("input", &inp);
|
|
|
|
ctx.add_value("value", &val);
|
|
|
|
ctx.add_value("after", &val);
|
|
|
|
ctx.add_value("before", &old);
|
2022-04-07 10:16:24 +00:00
|
|
|
// Process the PERMISSION clause
|
2023-07-06 14:57:42 +00:00
|
|
|
if !e.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
|
2022-04-07 10:16:24 +00:00
|
|
|
val = old
|
|
|
|
}
|
2022-04-04 22:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 10:16:24 +00:00
|
|
|
// Set the value of the field
|
|
|
|
match val {
|
2023-07-06 14:57:42 +00:00
|
|
|
Value::None => self.current.doc.to_mut().del(ctx, opt, txn, &k).await?,
|
|
|
|
_ => self.current.doc.to_mut().set(ctx, opt, txn, &k, val).await?,
|
2022-04-07 10:16:24 +00:00
|
|
|
};
|
2022-04-04 22:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Carry on
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|