Process document fields correctly

This commit is contained in:
Tobie Morgan Hitchcock 2022-04-07 11:16:24 +01:00
parent e378105f11
commit 6b6d4f65f9

View file

@ -19,102 +19,105 @@ impl<'a> Document<'a> {
) -> Result<(), Error> { ) -> Result<(), Error> {
// Loop through all field statements // Loop through all field statements
for fd in self.fd(opt, txn).await?.iter() { for fd in self.fd(opt, txn).await?.iter() {
// Get the initial value // Loop over each field in document
let old = self.initial.get(ctx, opt, txn, &fd.name).await?; for k in self.current.each(&fd.name).into_iter() {
// Get the current value // Get the initial value
let mut val = self.current.get(ctx, opt, txn, &fd.name).await?; let old = self.initial.pick(&k);
// Check for a VALUE clause // Get the current value
if let Some(expr) = &fd.value { let mut val = self.current.pick(&k);
// Configure the context // Check for a VALUE clause
let mut ctx = Context::new(ctx); if let Some(expr) = &fd.value {
ctx.add_value("value".into(), val.clone()); // Configure the context
ctx.add_value("after".into(), val.clone()); let mut ctx = Context::new(ctx);
ctx.add_value("before".into(), old.clone()); ctx.add_value("value".into(), val.clone());
let ctx = ctx.freeze(); ctx.add_value("after".into(), val.clone());
// Process the VALUE clause ctx.add_value("before".into(), old.clone());
val = expr.compute(&ctx, opt, txn, Some(&self.current)).await?; let ctx = ctx.freeze();
} // Process the VALUE clause
// Check for a TYPE clause val = expr.compute(&ctx, opt, txn, Some(&self.current)).await?;
if let Some(kind) = &fd.kind {
val = match kind {
Kind::Any => val,
Kind::Bool => val.make_bool(),
Kind::Int => val.make_int(),
Kind::Float => val.make_float(),
Kind::Decimal => val.make_decimal(),
Kind::Number => val.make_number(),
Kind::String => val.make_strand(),
Kind::Datetime => val.make_datetime(),
Kind::Duration => val.make_duration(),
Kind::Array => match val {
Value::Array(_) => val,
_ => Value::None,
},
Kind::Object => match val {
Value::Object(_) => val,
_ => Value::None,
},
Kind::Record(t) => match val.is_type_record(t) {
true => val,
_ => Value::None,
},
Kind::Geometry(t) => match val.is_type_geometry(t) {
true => val,
_ => Value::None,
},
} }
} // Check for a TYPE clause
// Check for a ASSERT clause if let Some(kind) = &fd.kind {
if let Some(expr) = &fd.assert { val = match kind {
// Configure the context Kind::Any => val,
let mut ctx = Context::new(ctx); Kind::Bool => val.make_bool(),
ctx.add_value("value".into(), val.clone()); Kind::Int => val.make_int(),
ctx.add_value("after".into(), val.clone()); Kind::Float => val.make_float(),
ctx.add_value("before".into(), old.clone()); Kind::Decimal => val.make_decimal(),
let ctx = ctx.freeze(); Kind::Number => val.make_number(),
// Process the ASSERT clause Kind::String => val.make_strand(),
if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { Kind::Datetime => val.make_datetime(),
return Err(Error::FieldValue { Kind::Duration => val.make_duration(),
value: val.clone(), Kind::Array => match val {
field: fd.name.clone(), Value::Array(_) => val,
check: expr.clone(), _ => Value::None,
}); },
Kind::Object => match val {
Value::Object(_) => val,
_ => Value::None,
},
Kind::Record(t) => match val.is_type_record(t) {
true => val,
_ => Value::None,
},
Kind::Geometry(t) => match val.is_type_geometry(t) {
true => val,
_ => Value::None,
},
}
} }
} // Check for a ASSERT clause
// Check for a PERMISSIONS clause if let Some(expr) = &fd.assert {
if opt.perms && opt.auth.perms() { // Configure the context
// Get the permission clause let mut ctx = Context::new(ctx);
let perms = if self.initial.is_none() { ctx.add_value("value".into(), val.clone());
&fd.permissions.create ctx.add_value("after".into(), val.clone());
} else if self.current.is_none() { ctx.add_value("before".into(), old.clone());
&fd.permissions.delete let ctx = ctx.freeze();
} else { // Process the ASSERT clause
&fd.permissions.update if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
}; return Err(Error::FieldValue {
// Match the permission clause value: val.clone(),
match perms { field: fd.name.clone(),
Permission::Full => (), check: expr.clone(),
Permission::None => val = old, });
Permission::Specific(e) => { }
// Configure the context }
let mut ctx = Context::new(ctx); // Check for a PERMISSIONS clause
ctx.add_value("value".into(), val.clone()); if opt.perms && opt.auth.perms() {
ctx.add_value("after".into(), val.clone()); // Get the permission clause
ctx.add_value("before".into(), old.clone()); let perms = if self.initial.is_none() {
let ctx = ctx.freeze(); &fd.permissions.create
// Process the PERMISSION clause } else if self.current.is_none() {
if !e.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { &fd.permissions.delete
val = old } else {
&fd.permissions.update
};
// Match the permission clause
match perms {
Permission::Full => (),
Permission::None => val = old,
Permission::Specific(e) => {
// Configure the context
let mut ctx = Context::new(ctx);
ctx.add_value("value".into(), val.clone());
ctx.add_value("after".into(), val.clone());
ctx.add_value("before".into(), old.clone());
let ctx = ctx.freeze();
// Process the PERMISSION clause
if !e.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() {
val = old
}
} }
} }
} }
// Set the value of the field
match val {
Value::None => self.current.to_mut().del(ctx, opt, txn, &k).await?,
Value::Void => self.current.to_mut().del(ctx, opt, txn, &k).await?,
_ => self.current.to_mut().set(ctx, opt, txn, &k, val).await?,
};
} }
// Set the value of the field
match val {
Value::None => self.current.to_mut().del(ctx, opt, txn, &fd.name).await?,
Value::Void => self.current.to_mut().del(ctx, opt, txn, &fd.name).await?,
_ => self.current.to_mut().set(ctx, opt, txn, &fd.name, val).await?,
};
} }
// Carry on // Carry on
Ok(()) Ok(())