1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use crate::ctx::{Context, MutableContext};
use crate::dbs::Options;
use crate::dbs::Statement;
use crate::doc::Document;
use crate::err::Error;
use crate::iam::Action;
use crate::sql::data::Data;
use crate::sql::idiom::Idiom;
use crate::sql::kind::Kind;
use crate::sql::permission::Permission;
use crate::sql::statements::DefineFieldStatement;
use crate::sql::thing::Thing;
use crate::sql::value::Value;
use reblessive::tree::Stk;
use std::sync::Arc;
impl Document {
/// Ensures that any remaining fields on a
/// SCHEMAFULL table are cleaned up and removed.
/// If a field is defined as FLEX, then any
/// nested fields or array values are untouched.
pub(super) async fn cleanup_table_fields(
&mut self,
ctx: &Context,
opt: &Options,
_stm: &Statement<'_>,
) -> Result<(), Error> {
// Get the table
let tb = self.tb(ctx, opt).await?;
// This table is schemafull
if tb.full {
// Create a vector to store the keys
let mut keys: Vec<Idiom> = vec![];
// Loop through all field statements
for fd in self.fd(ctx, opt).await?.iter() {
// Is this a schemaless field?
match fd.flex || fd.kind.as_ref().is_some_and(Kind::is_literal_nested) {
false => {
// Loop over this field in the document
for k in self.current.doc.each(&fd.name).into_iter() {
keys.push(k);
}
}
true => {
// Loop over every field under this field in the document
for k in self.current.doc.every(Some(&fd.name), true, true).into_iter() {
keys.push(k);
}
}
}
}
// Loop over every field in the document
for fd in self.current.doc.every(None, true, true).iter() {
if !keys.contains(fd) {
match fd {
// Built-in fields
fd if fd.is_id() => continue,
fd if fd.is_in() => continue,
fd if fd.is_out() => continue,
fd if fd.is_meta() => continue,
// Custom fields
fd => match opt.strict {
// If strict, then throw an error on an undefined field
true => {
return Err(Error::FieldUndefined {
table: tb.name.to_raw(),
field: fd.to_owned(),
})
}
// Otherwise, delete the field silently and don't error
false => self.current.doc.to_mut().cut(fd),
},
}
}
// NONE values should never be stored
if self.current.doc.pick(fd).is_none() {
self.current.doc.to_mut().cut(fd);
}
}
} else {
// Loop over every field in the document
for fd in self.current.doc.every(None, true, true).iter() {
// NONE values should never be stored
if self.current.doc.pick(fd).is_none() {
self.current.doc.to_mut().cut(fd);
}
}
}
// Carry on
Ok(())
}
/// Processes `DEFINE FIELD` statements which
/// have been defined on the table for this
/// record. These fields are executed for
/// every matching field in the input document.
pub(super) async fn process_table_fields(
&mut self,
stk: &mut Stk,
ctx: &Context,
opt: &Options,
stm: &Statement<'_>,
) -> Result<(), Error> {
// Check import
if opt.import {
return Ok(());
}
// Get the record id
let rid = self.id()?;
// Get the user applied input
let inp = self.initial.doc.as_ref().changed(self.current.doc.as_ref());
// When set, any matching embedded object fields
// which are prefixed with the specified idiom
// will be skipped, as the parent object is optional
let mut skip: Option<&Idiom> = None;
// Loop through all field statements
for fd in self.fd(ctx, opt).await?.iter() {
// Check if we should skip this field
let skipped = match skip {
// We are skipping a parent field
Some(inner) => {
// Check if this field is a child field
let skipped = fd.name.starts_with(inner);
// Let's stop skipping fields if not
if !skipped {
skip = None;
}
// Specify whether we should skip
skipped
}
None => false,
};
// Loop over each field in document
for (k, mut val) in self.current.doc.as_ref().walk(&fd.name).into_iter() {
// Get the initial value
let old = Arc::new(self.initial.doc.as_ref().pick(&k));
// Get the input value
let inp = Arc::new(inp.pick(&k));
// Check for the `id` field
if fd.name.is_id() {
if !self.is_new() && val.ne(&old) {
return Err(Error::FieldReadonly {
field: fd.name.clone(),
thing: rid.to_string(),
});
} else if !self.is_new() {
continue;
}
}
// If the field is READONLY then we
// will check that the field has not
// been modified. If it has just been
// omitted then we reset it, otherwise
// we throw a field readonly error.
if fd.readonly {
// Check if we are updating the
// document, and check if the new
// field value is now different to
// the old field value in any way.
if !self.is_new() && val.ne(&old) {
// Check the data clause type
match stm.data() {
// If the field is NONE, we assume
// that the field was ommitted when
// using a CONTENT clause, and we
// revert the value to the old value.
Some(Data::ContentExpression(_)) if val.is_none() => {
self.current
.doc
.to_mut()
.set(stk, ctx, opt, &k, old.as_ref().clone())
.await?;
continue;
}
// If the field has been modified
// and the user didn't use a CONTENT
// clause, then this should not be
// allowed, and we throw an error.
_ => {
return Err(Error::FieldReadonly {
field: fd.name.clone(),
thing: rid.to_string(),
});
}
}
}
// If this field was not modified then
// we can continue without needing to
// process the field in any other way.
else if !self.is_new() {
continue;
}
}
// Generate the field context
let mut field = FieldEditContext {
context: None,
doc: self,
rid: rid.clone(),
def: fd,
stk,
ctx,
opt,
old,
inp,
};
// Skip this field?
if !skipped {
// Process any DEFAULT clause
val = field.process_default_clause(val).await?;
// Process any TYPE clause
val = field.process_type_clause(val).await?;
// Process any VALUE clause
val = field.process_value_clause(val).await?;
// Process any TYPE clause
val = field.process_type_clause(val).await?;
// Process any ASSERT clause
val = field.process_assert_clause(val).await?;
}
// Process any PERMISSIONS clause
val = field.process_permissions_clause(val).await?;
// Skip this field?
if !skipped {
// If the field is empty, mark child fields as skippable
if val.is_none() && fd.kind.as_ref().is_some_and(Kind::can_be_none) {
skip = Some(&fd.name);
}
// Set the new value of the field, or delete it if empty
match val.is_none() {
false => self.current.doc.to_mut().put(&k, val),
true => self.current.doc.to_mut().cut(&k),
};
}
}
}
// Carry on
Ok(())
}
}
struct FieldEditContext<'a> {
/// The mutable request context
context: Option<MutableContext>,
/// The defined field statement
def: &'a DefineFieldStatement,
/// The current request stack
stk: &'a mut Stk,
/// The current request context
ctx: &'a Context,
/// The current request options
opt: &'a Options,
/// The current document record being processed
doc: &'a Document,
/// The record id of the document that we are processing
rid: Arc<Thing>,
/// The initial value of the field before being modified
old: Arc<Value>,
/// The user input value of the field edited by the user
inp: Arc<Value>,
}
impl FieldEditContext<'_> {
/// Process any TYPE clause for the field definition
async fn process_type_clause(&self, val: Value) -> Result<Value, Error> {
// Check for a TYPE clause
if let Some(kind) = &self.def.kind {
// Check if this is the `id` field
if self.def.name.is_id() {
// Ensure that the outer value is a record
if let Value::Thing(ref id) = val {
// See if we should check the inner type
if !kind.is_record() {
// Get the value of the ID only
let inner = Value::from(id.id.clone());
// Check the type of the ID part
inner.coerce_to(kind).map_err(|e| match e {
// There was a conversion error
Error::CoerceTo {
from,
..
} => Error::FieldCheck {
thing: self.rid.to_string(),
field: self.def.name.clone(),
check: kind.to_string(),
value: from.to_string(),
},
// There was a different error
e => e,
})?;
}
}
// The outer value should be a record
else {
// There was a field check error
return Err(Error::FieldCheck {
thing: self.rid.to_string(),
field: self.def.name.clone(),
check: kind.to_string(),
value: val.to_string(),
});
}
}
// This is not the `id` field
else {
// Check the type of the field value
let val = val.coerce_to(kind).map_err(|e| match e {
// There was a conversion error
Error::CoerceTo {
from,
..
} => Error::FieldCheck {
thing: self.rid.to_string(),
field: self.def.name.clone(),
check: kind.to_string(),
value: from.to_string(),
},
// There was a different error
e => e,
})?;
// Return the modified value
return Ok(val);
}
}
// Return the original value
Ok(val)
}
/// Process any DEFAULT clause for the field definition
async fn process_default_clause(&mut self, val: Value) -> Result<Value, Error> {
// This field has a value specified
if !val.is_none() {
return Ok(val);
}
// The document is not being created
if !self.doc.is_new() {
return Ok(val);
}
// Get the default value
let def = match &self.def.default {
Some(v) => Some(v),
_ => match &self.def.value {
// The VALUE clause doesn't
Some(v) if v.is_static() => Some(v),
_ => None,
},
};
// Check for a DEFAULT clause
if let Some(expr) = def {
// Arc the current value
let now = Arc::new(val);
// Get the current document
let doc = Some(&self.doc.current);
// Configure the context
let ctx = match self.context.take() {
Some(mut ctx) => {
ctx.add_value("after", now.clone());
ctx.add_value("value", now);
ctx
}
None => {
let mut ctx = MutableContext::new(self.ctx);
ctx.add_value("before", self.old.clone());
ctx.add_value("input", self.inp.clone());
ctx.add_value("after", now.clone());
ctx.add_value("value", now);
ctx
}
};
// Freeze the new context
let ctx = ctx.freeze();
// Process the VALUE clause
let val = expr.compute(self.stk, &ctx, self.opt, doc).await?;
// Unfreeze the new context
self.context = Some(MutableContext::unfreeze(ctx)?);
// Return the modified value
return Ok(val);
}
// Return the original value
Ok(val)
}
/// Process any VALUE clause for the field definition
async fn process_value_clause(&mut self, val: Value) -> Result<Value, Error> {
// Check for a VALUE clause
if let Some(expr) = &self.def.value {
// Arc the current value
let now = Arc::new(val);
// Get the current document
let doc = Some(&self.doc.current);
// Configure the context
let ctx = match self.context.take() {
Some(mut ctx) => {
ctx.add_value("after", now.clone());
ctx.add_value("value", now);
ctx
}
None => {
let mut ctx = MutableContext::new(self.ctx);
ctx.add_value("before", self.old.clone());
ctx.add_value("input", self.inp.clone());
ctx.add_value("after", now.clone());
ctx.add_value("value", now);
ctx
}
};
// Freeze the new context
let ctx = ctx.freeze();
// Process the VALUE clause
let val = expr.compute(self.stk, &ctx, self.opt, doc).await?;
// Unfreeze the new context
self.context = Some(MutableContext::unfreeze(ctx)?);
// Return the modified value
return Ok(val);
}
// Return the original value
Ok(val)
}
/// Process any ASSERT clause for the field definition
async fn process_assert_clause(&mut self, val: Value) -> Result<Value, Error> {
// If the field TYPE is optional, and the
// field value was not set or is NONE we
// ignore any defined ASSERT clause.
if val.is_none() && self.def.kind.as_ref().is_some_and(Kind::can_be_none) {
return Ok(val);
}
// Check for a ASSERT clause
if let Some(expr) = &self.def.assert {
// Arc the current value
let now = Arc::new(val.clone());
// Get the current document
let doc = Some(&self.doc.current);
// Configure the context
let ctx = match self.context.take() {
Some(mut ctx) => {
ctx.add_value("after", now.clone());
ctx.add_value("value", now.clone());
ctx
}
None => {
let mut ctx = MutableContext::new(self.ctx);
ctx.add_value("before", self.old.clone());
ctx.add_value("input", self.inp.clone());
ctx.add_value("after", now.clone());
ctx.add_value("value", now.clone());
ctx
}
};
// Freeze the new context
let ctx = ctx.freeze();
// Process the ASSERT clause
let res = expr.compute(self.stk, &ctx, self.opt, doc).await?;
// Unfreeze the new context
self.context = Some(MutableContext::unfreeze(ctx)?);
// Check the ASSERT clause result
if !res.is_truthy() {
return Err(Error::FieldValue {
thing: self.rid.to_string(),
field: self.def.name.clone(),
check: expr.to_string(),
value: now.to_string(),
});
}
}
// Return the original value
Ok(val)
}
/// Process any PERMISSIONS clause for the field definition
async fn process_permissions_clause(&mut self, val: Value) -> Result<Value, Error> {
// Check for a PERMISSIONS clause
if self.opt.check_perms(Action::Edit)? {
// Get the permission clause
let perms = if self.doc.is_new() {
&self.def.permissions.create
} else {
&self.def.permissions.update
};
// Match the permission clause
let val = match perms {
// The field PERMISSIONS clause
// is FULL, enabling this field
// to be updated without checks.
Permission::Full => val,
// The field PERMISSIONS clause
// is NONE, meaning that this
// change will be reverted.
Permission::None => match val.eq(&self.old) {
false => self.old.as_ref().clone(),
true => val,
},
// The field PERMISSIONS clause
// is a custom expression, so
// we check the expression and
// revert the field if denied.
Permission::Specific(expr) => {
// Arc the current value
let now = Arc::new(val.clone());
// Get the current document
let doc = Some(&self.doc.current);
// Disable permissions
let opt = &self.opt.new_with_perms(false);
// Configure the context
// Configure the context
let ctx = match self.context.take() {
Some(mut ctx) => {
ctx.add_value("after", now.clone());
ctx.add_value("value", now);
ctx
}
None => {
let mut ctx = MutableContext::new(self.ctx);
ctx.add_value("before", self.old.clone());
ctx.add_value("input", self.inp.clone());
ctx.add_value("after", now.clone());
ctx.add_value("value", now);
ctx
}
};
// Freeze the new context
let ctx = ctx.freeze();
// Process the PERMISSION clause
let res = expr.compute(self.stk, &ctx, opt, doc).await?;
// Unfreeze the new context
self.context = Some(MutableContext::unfreeze(ctx)?);
// If the specific permissions
// expression was not truthy,
// then this field could not be
// updated, meanint that this
// change will be reverted.
match res.is_truthy() {
false => match val.eq(&self.old) {
false => self.old.as_ref().clone(),
true => val,
},
true => val,
}
}
};
// Return the modified value
return Ok(val);
}
// Return the original value
Ok(val)
}
}