-
Notifications
You must be signed in to change notification settings - Fork 60
/
prototype.rs
620 lines (573 loc) · 23.4 KB
/
prototype.rs
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! An [`AttributePrototype`] represents, for a specific attribute:
//!
//! * Which context the following applies to ([`AttributeContext`](crate::AttributeContext))
//! * The function that should be run to find its value.
//! * In the case that the [`Prop`](crate::Prop) is the child of an
//! [`Array`](crate::prop::PropKind::Array): Which index in the `Array` the value
//! is for.
//! * In the case that the [`Prop`](crate::Prop) is the child of a
//! [`Map`](crate::prop::PropKind::Map): Which key of the `Map` the value is
//! for.
use async_recursion::async_recursion;
use serde::{Deserialize, Serialize};
use si_data::{NatsError, PgError};
use telemetry::prelude::*;
use thiserror::Error;
use crate::func::binding_return_value::FuncBindingReturnValueError;
use crate::{
attribute::{
context::{AttributeContext, AttributeContextError},
value::{AttributeValue, AttributeValueError, AttributeValueId},
},
func::FuncId,
func::{
binding::{FuncBindingError, FuncBindingId},
binding_return_value::FuncBindingReturnValueId,
},
impl_standard_model, pk, standard_model, standard_model_accessor, standard_model_has_many,
AttributePrototypeArgument, AttributePrototypeArgumentError, AttributeReadContext, ComponentId,
DalContext, ExternalProviderId, HistoryEventError, InternalProviderId, PropError, PropKind,
ReadTenancy, ReadTenancyError, StandardModel, StandardModelError, Timestamp, Visibility,
WriteTenancy,
};
pub mod argument;
const ATTRIBUTE_VALUES_IN_CONTEXT_OR_GREATER: &str =
include_str!("../queries/attribute_prototype_attribute_values_in_context_or_greater.sql");
const LIST_BY_HEAD_FROM_EXTERNAL_PROVIDER_USE_WITH_TAIL: &str = include_str!(
"../queries/attribute_prototype_list_by_head_from_external_provider_use_with_tail.sql"
);
const LIST_FROM_INTERNAL_PROVIDER_USE: &str =
include_str!("../queries/attribute_prototype_list_from_internal_provider_use.sql");
const LIST_FOR_CONTEXT: &str = include_str!("../queries/attribute_prototype_list_for_context.sql");
const FIND_WITH_PARENT_VALUE_AND_KEY_FOR_CONTEXT: &str =
include_str!("../queries/attribute_prototype_find_with_parent_value_and_key_for_context.sql");
#[derive(Error, Debug)]
pub enum AttributePrototypeError {
#[error("attribute resolver context builder error: {0}")]
AttributeContextBuilder(#[from] AttributeContextError),
#[error("attribute prototype argument error: {0}")]
AttributePrototypeArgument(#[from] AttributePrototypeArgumentError),
#[error("attribute value error: {0}")]
AttributeValue(#[from] AttributeValueError),
#[error("func binding error: {0}")]
FuncBinding(#[from] FuncBindingError),
#[error("func binding return value error: {0}")]
FuncBindingReturnValue(#[from] FuncBindingReturnValueError),
#[error("history event error: {0}")]
HistoryEvent(#[from] HistoryEventError),
#[error("invalid prop value; expected {0} but got {1}")]
InvalidPropValue(String, serde_json::Value),
#[error("AttributePrototype is missing")]
Missing,
#[error("func not found: {0}")]
MissingFunc(String),
#[error("attribute prototypes must have an associated prop, and this one does not. bug!")]
MissingProp,
#[error("missing attribute value for tenancy {0:?}, visibility {1:?}, prototype {2:?}, with parent attribute value {3:?}")]
MissingValue(
ReadTenancy,
Visibility,
AttributePrototypeId,
Option<AttributeValueId>,
),
#[error("nats txn error: {0}")]
Nats(#[from] NatsError),
#[error("attribute prototype not found: {0} ({1:?})")]
NotFound(AttributePrototypeId, Visibility),
#[error(
"parent must be for an array, map, or object prop: attribute prototype id {0} is for a {1}"
)]
ParentNotAllowed(AttributePrototypeId, PropKind),
#[error("pg error: {0}")]
Pg(#[from] PgError),
#[error("prop error: {0}")]
Prop(#[from] PropError),
#[error("error serializing/deserializing json: {0}")]
SerdeJson(#[from] serde_json::Error),
#[error("standard model error: {0}")]
StandardModelError(#[from] StandardModelError),
#[error("read tenancy error: {0}")]
ReadTenancy(#[from] ReadTenancyError),
#[error("cannot remove prototype with a least-specific context: {0}")]
LeastSpecificContextPrototypeRemovalNotAllowed(AttributePrototypeId),
#[error("cannot remove value with a least-specific context: {0}")]
LeastSpecificContextValueRemovalNotAllowed(AttributeValueId),
}
pub type AttributePrototypeResult<T> = Result<T, AttributePrototypeError>;
pk!(AttributePrototypePk);
pk!(AttributePrototypeId);
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct AttributePrototype {
pk: AttributePrototypePk,
id: AttributePrototypeId,
#[serde(flatten)]
tenancy: WriteTenancy,
#[serde(flatten)]
visibility: Visibility,
#[serde(flatten)]
timestamp: Timestamp,
/// The [`AttributeContext`] corresponding to the prototype.
#[serde(flatten)]
pub context: AttributeContext,
/// The [`Func`](crate::Func) corresponding to the prototype.
func_id: FuncId,
/// An optional key used for tracking parentage.
pub key: Option<String>,
}
/// This object is used for
/// [`AttributePrototype::list_by_head_from_external_provider_use_with_tail()`].
#[derive(Serialize, Deserialize, Debug)]
pub struct AttributePrototypeGroupByHeadComponentId {
pub head_component_id: ComponentId,
pub attribute_prototype: AttributePrototype,
}
impl_standard_model! {
model: AttributePrototype,
pk: AttributePrototypePk,
id: AttributePrototypeId,
table_name: "attribute_prototypes",
history_event_label_base: "attribute_prototype",
history_event_message_name: "Attribute Prototype"
}
impl AttributePrototype {
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
pub async fn new(
ctx: &DalContext<'_, '_, '_>,
func_id: FuncId,
func_binding_id: FuncBindingId,
func_binding_return_value_id: FuncBindingReturnValueId,
context: AttributeContext,
key: Option<String>,
parent_attribute_value_id: Option<AttributeValueId>,
) -> AttributePrototypeResult<Self> {
let row = ctx
.txns()
.pg()
.query_one(
"SELECT object FROM attribute_prototype_create_v1($1, $2, $3, $4, $5)",
&[
ctx.write_tenancy(),
ctx.visibility(),
&context,
&func_id,
&key,
],
)
.await?;
let object: AttributePrototype = standard_model::finish_create_from_row(ctx, row).await?;
let value = AttributeValue::new(
ctx,
func_binding_id,
func_binding_return_value_id,
context,
key.clone(),
)
.await?;
value.set_attribute_prototype(ctx, object.id()).await?;
if let Some(parent_attribute_value_id) = parent_attribute_value_id {
value
.set_parent_attribute_value(ctx, &parent_attribute_value_id)
.await?;
}
if !context.is_least_specific() {
let maybe_existing_attribute_value_proxy =
AttributeValue::find_with_parent_and_key_for_context(
ctx,
parent_attribute_value_id,
key.clone(),
AttributeReadContext::from(context),
)
.await?;
let maybe_existing_attribute_value_proxy = if let Some(existing_attribute_value_proxy) =
maybe_existing_attribute_value_proxy
{
if existing_attribute_value_proxy.context == context {
Some(existing_attribute_value_proxy)
} else {
None
}
} else {
None
};
if maybe_existing_attribute_value_proxy.is_none() {
let original_prototype = Self::find_with_parent_value_and_key_for_context(
ctx,
parent_attribute_value_id,
key,
context.less_specific()?,
)
.await?;
if let Some(original_prototype) = original_prototype {
Self::create_intermediate_proxy_values(
ctx,
parent_attribute_value_id,
*original_prototype.id(),
context.less_specific()?,
)
.await?;
}
}
}
Ok(object)
}
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
pub async fn new_with_existing_value(
ctx: &DalContext<'_, '_, '_>,
func_id: FuncId,
context: AttributeContext,
key: Option<String>,
parent_attribute_value_id: Option<AttributeValueId>,
attribute_value_id: AttributeValueId,
) -> AttributePrototypeResult<Self> {
let row = ctx
.txns()
.pg()
.query_one(
"SELECT object FROM attribute_prototype_create_v1($1, $2, $3, $4, $5)",
&[
ctx.write_tenancy(),
ctx.visibility(),
&context,
&func_id,
&key,
],
)
.await?;
let object: AttributePrototype = standard_model::finish_create_from_row(ctx, row).await?;
let value = AttributeValue::get_by_id(ctx, &attribute_value_id)
.await?
.ok_or(AttributeValueError::Missing)?;
value.unset_attribute_prototype(ctx).await?;
value.set_attribute_prototype(ctx, object.id()).await?;
value.unset_parent_attribute_value(ctx).await?;
if let Some(parent_attribute_value_id) = parent_attribute_value_id {
value
.set_parent_attribute_value(ctx, &parent_attribute_value_id)
.await?;
}
Ok(object)
}
standard_model_accessor!(func_id, Pk(FuncId), AttributePrototypeResult);
standard_model_accessor!(key, Option<String>, AttributePrototypeResult);
standard_model_has_many!(
lookup_fn: attribute_values,
table: "attribute_value_belongs_to_attribute_prototype",
model_table: "attribute_values",
returns: AttributeValue,
result: AttributePrototypeResult,
);
/// Deletes the [`AttributePrototype`] corresponding to a provided ID. Before deletion occurs,
/// its corresponding [`AttributeValue`](crate::AttributeValue), all of its child values
/// (and their children, recursively) and those children's prototypes are deleted. Any value or
/// prototype that could not be found or does not exist is assumed to have already been deleted
/// or never existed. Moreover, before deletion of the [`AttributePrototype`] occurs, we delete
/// all [`AttributePrototypeArguments`](crate::AttributePrototypeArgument) that belong to the
/// prototype.
///
/// Caution: this should be used rather than [`StandardModel::delete()`] when deleting an
/// [`AttributePrototype`]. That method should never be called directly.
pub async fn remove(
ctx: &DalContext<'_, '_, '_>,
attribute_prototype_id: &AttributePrototypeId,
) -> AttributePrototypeResult<()> {
// Get the prototype for the given id. Once we get its corresponding value, we can delete
// the prototype.
let attribute_prototype =
match AttributePrototype::get_by_id(ctx, attribute_prototype_id).await? {
Some(v) => v,
None => return Ok(()),
};
if attribute_prototype.context.is_least_specific() {
return Err(
AttributePrototypeError::LeastSpecificContextPrototypeRemovalNotAllowed(
*attribute_prototype_id,
),
);
}
// Delete all values and arguments found for a prototype before deleting the prototype.
let attribute_values = attribute_prototype.attribute_values(ctx).await?;
for argument in
AttributePrototypeArgument::list_for_attribute_prototype(ctx, *attribute_prototype_id)
.await?
{
argument.delete(ctx).await?;
}
attribute_prototype.delete(ctx).await?;
// Start with the initial value(s) from the prototype and build a work queue based on the
// value's children (and their children, recursively). Once we find the child values,
// we can delete the current value in the queue and its prototype.
let mut work_queue = attribute_values;
while let Some(current_value) = work_queue.pop() {
let child_attribute_values = current_value.child_attribute_values(ctx).await?;
if !child_attribute_values.is_empty() {
work_queue.extend(child_attribute_values);
}
// Delete the prototype if we find one and if its context is not "least-specific".
if let Some(current_prototype) = current_value.attribute_prototype(ctx).await? {
if current_prototype.context.is_least_specific() {
return Err(
AttributePrototypeError::LeastSpecificContextPrototypeRemovalNotAllowed(
*current_prototype.id(),
),
);
}
// Delete all arguments found for a prototype before deleting the prototype.
for argument in AttributePrototypeArgument::list_for_attribute_prototype(
ctx,
*current_prototype.id(),
)
.await?
{
argument.delete(ctx).await?;
}
current_prototype.delete(ctx).await?;
}
// Delete the value if its context is not "least-specific".
if current_value.context.is_least_specific() {
return Err(
AttributePrototypeError::LeastSpecificContextValueRemovalNotAllowed(
*current_value.id(),
),
);
}
current_value.delete(ctx).await?;
}
Ok(())
}
#[instrument(skip_all)]
pub async fn list_for_context(
ctx: &DalContext<'_, '_, '_>,
context: AttributeContext,
) -> AttributePrototypeResult<Vec<Self>> {
let rows = ctx
.txns()
.pg()
.query(
LIST_FOR_CONTEXT,
&[
ctx.read_tenancy(),
ctx.visibility(),
&context,
&context.prop_id(),
],
)
.await?;
let object = standard_model::objects_from_rows(rows)?;
Ok(object)
}
#[tracing::instrument(skip_all)]
pub async fn find_with_parent_value_and_key_for_context(
ctx: &DalContext<'_, '_, '_>,
parent_attribute_value_id: Option<AttributeValueId>,
key: Option<String>,
context: AttributeContext,
) -> AttributePrototypeResult<Option<Self>> {
let row = ctx
.txns()
.pg()
.query_opt(
FIND_WITH_PARENT_VALUE_AND_KEY_FOR_CONTEXT,
&[
ctx.read_tenancy(),
ctx.visibility(),
&context,
&parent_attribute_value_id,
&key,
],
)
.await?;
Ok(standard_model::option_object_from_row(row)?)
}
/// List [`Vec<Self>`] that depend on a provided [`InternalProviderId`](crate::InternalProvider).
pub async fn list_from_internal_provider_use(
ctx: &DalContext<'_, '_, '_>,
internal_provider_id: InternalProviderId,
) -> AttributePrototypeResult<Vec<Self>> {
let rows = ctx
.pg_txn()
.query(
LIST_FROM_INTERNAL_PROVIDER_USE,
&[ctx.read_tenancy(), ctx.visibility(), &internal_provider_id],
)
.await?;
Ok(standard_model::objects_from_rows(rows)?)
}
/// List [`Vec<Self>`] that depend on a provided [`ExternalProviderId`](crate::ExternalProvider)
/// and _tail_ [`ComponentId`](crate::Component).
pub async fn list_by_head_from_external_provider_use_with_tail(
ctx: &DalContext<'_, '_, '_>,
external_provider_id: ExternalProviderId,
tail_component_id: ComponentId,
) -> AttributePrototypeResult<Vec<AttributePrototypeGroupByHeadComponentId>> {
let rows = ctx
.pg_txn()
.query(
LIST_BY_HEAD_FROM_EXTERNAL_PROVIDER_USE_WITH_TAIL,
&[
ctx.read_tenancy(),
ctx.visibility(),
&external_provider_id,
&tail_component_id,
],
)
.await?;
let mut result = Vec::new();
for row in rows.into_iter() {
let head_component_id: ComponentId = row.try_get("head_component_id")?;
let attribute_prototype_json: serde_json::Value = row.try_get("object")?;
let attribute_prototype = serde_json::from_value(attribute_prototype_json)?;
result.push(AttributePrototypeGroupByHeadComponentId {
head_component_id,
attribute_prototype,
});
}
Ok(result)
}
/// List [`AttributeValues`](crate::AttributeValue) that belong to a provided [`AttributePrototypeId`](Self)
/// and whose context contains the provided [`AttributeReadContext`](crate::AttributeReadContext)
/// or are "more-specific" than the provided [`AttributeReadContext`](crate::AttributeReadContext).
pub async fn attribute_values_in_context_or_greater(
ctx: &DalContext<'_, '_, '_>,
attribute_prototype_id: AttributePrototypeId,
context: AttributeReadContext,
) -> AttributePrototypeResult<Vec<AttributeValue>> {
let rows = ctx
.pg_txn()
.query(
ATTRIBUTE_VALUES_IN_CONTEXT_OR_GREATER,
&[
ctx.read_tenancy(),
ctx.visibility(),
&attribute_prototype_id,
&context,
],
)
.await?;
Ok(standard_model::objects_from_rows(rows)?)
}
#[instrument(skip_all)]
#[allow(clippy::too_many_arguments)]
#[async_recursion]
async fn create_intermediate_proxy_values(
ctx: &DalContext<'_, '_, '_>,
parent_attribute_value_id: Option<AttributeValueId>,
prototype_id: AttributePrototypeId,
context: AttributeContext,
) -> AttributePrototypeResult<()> {
if context.is_least_specific() {
return Ok(());
}
if (AttributeValue::find_with_parent_and_prototype_for_context(
ctx,
parent_attribute_value_id,
prototype_id,
context,
)
.await?)
.is_none()
{
// Need to create a proxy to the next lowest level
Self::create_intermediate_proxy_values(
ctx,
parent_attribute_value_id,
prototype_id,
context.less_specific()?,
)
.await?;
if let Some(proxy_target) = AttributeValue::find_with_parent_and_prototype_for_context(
ctx,
parent_attribute_value_id,
prototype_id,
context.less_specific()?,
)
.await?
{
// Create the proxy at this level
let mut proxy_attribute_value = AttributeValue::new(
ctx,
proxy_target.func_binding_id(),
proxy_target.func_binding_return_value_id(),
context,
proxy_target.key().map(|k| k.to_string()),
)
.await?;
proxy_attribute_value
.set_proxy_for_attribute_value_id(ctx, Some(*proxy_target.id()))
.await?;
proxy_attribute_value
.set_attribute_prototype(ctx, &prototype_id)
.await?
} else {
return Err(AttributePrototypeError::MissingValue(
ctx.read_tenancy().clone(),
*ctx.visibility(),
prototype_id,
parent_attribute_value_id,
));
}
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub async fn update_for_context(
ctx: &DalContext<'_, '_, '_>,
attribute_prototype_id: AttributePrototypeId,
context: AttributeContext,
func_id: FuncId,
func_binding_id: FuncBindingId,
func_binding_return_value_id: FuncBindingReturnValueId,
parent_attribute_value_id: Option<AttributeValueId>,
existing_attribute_value_id: Option<AttributeValueId>,
) -> AttributePrototypeResult<AttributePrototypeId> {
let given_attribute_prototype = Self::get_by_id(ctx, &attribute_prototype_id)
.await?
.ok_or_else(|| {
AttributePrototypeError::NotFound(attribute_prototype_id, *ctx.visibility())
})?;
// If the AttributePrototype we were given isn't for the _specific_ context that we're
// trying to update, make a new one. This is necessary so that we don't end up changing the
// prototype for a context less specific than the one that we're trying to update.
let mut attribute_prototype = if given_attribute_prototype.context == context {
given_attribute_prototype
} else if let Some(attribute_value_id) = existing_attribute_value_id {
// Create new prototype with an existing value and clone the arguments of the given prototype into the new one.
let prototype = Self::new_with_existing_value(
ctx,
func_id,
context,
given_attribute_prototype.key().map(|k| k.to_string()),
parent_attribute_value_id,
attribute_value_id,
)
.await?;
let mut value = AttributeValue::get_by_id(ctx, &attribute_value_id)
.await?
.ok_or_else(|| {
AttributePrototypeError::MissingValue(
ctx.read_tenancy().clone(),
*ctx.visibility(),
*prototype.id(),
Some(attribute_value_id),
)
})?;
value.set_func_binding_id(ctx, func_binding_id).await?;
prototype
} else {
// Create new prototype and clone the arguments of the given prototype into the new one.
Self::new(
ctx,
func_id,
func_binding_id,
func_binding_return_value_id,
context,
given_attribute_prototype.key().map(|k| k.to_string()),
parent_attribute_value_id,
)
.await?
};
attribute_prototype.set_func_id(ctx, func_id).await?;
Ok(*attribute_prototype.id())
}
}