-
Notifications
You must be signed in to change notification settings - Fork 213
/
types.rs
390 lines (345 loc) Β· 14.8 KB
/
types.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
use super::{attributes, context::Context};
use crate::{ast, diagnostics::DatamodelError};
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use std::{
collections::{BTreeMap, HashMap},
str::FromStr,
};
pub(super) fn resolve_types(ctx: &mut Context<'_>) {
for (top_id, top) in ctx.db.ast.iter_tops() {
match (top_id, top) {
(ast::TopId::Alias(alias_id), ast::Top::Type(type_alias)) => visit_type_alias(alias_id, type_alias, ctx),
(ast::TopId::Model(model_id), ast::Top::Model(model)) => visit_model(model_id, model, ctx),
(ast::TopId::Enum(enum_id), ast::Top::Enum(enm)) => visit_enum(enum_id, enm, ctx),
(_, ast::Top::Source(_)) | (_, ast::Top::Generator(_)) => (),
_ => unreachable!(),
}
}
detect_alias_cycles(ctx);
}
#[derive(Debug, Default)]
pub(super) struct Types<'ast> {
pub(super) type_aliases: HashMap<ast::AliasId, ScalarFieldType>,
pub(super) scalar_fields: BTreeMap<(ast::ModelId, ast::FieldId), ScalarField<'ast>>,
pub(super) models: HashMap<ast::ModelId, ModelData<'ast>>,
/// This contains only the relation fields actually present in the schema
/// source text.
pub(super) relation_fields: BTreeMap<(ast::ModelId, ast::FieldId), RelationField<'ast>>,
pub(super) enums: HashMap<ast::EnumId, EnumData<'ast>>,
}
impl<'ast> Types<'ast> {
pub(super) fn take_model_data(&mut self, model_id: &ast::ModelId) -> Option<ModelData<'ast>> {
self.models.remove(model_id)
}
pub(super) fn take_scalar_field(
&mut self,
model_id: ast::ModelId,
field_id: ast::FieldId,
) -> Option<ScalarField<'ast>> {
self.scalar_fields.remove(&(model_id, field_id))
}
pub(super) fn take_relation_field(
&mut self,
model_id: ast::ModelId,
field_id: ast::FieldId,
) -> Option<RelationField<'ast>> {
self.relation_fields.remove(&(model_id, field_id))
}
}
#[derive(Debug)]
enum FieldType {
Model(ast::ModelId),
Scalar(ScalarFieldType),
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum ScalarFieldType {
Enum(ast::EnumId),
BuiltInScalar(dml::scalars::ScalarType),
Alias(ast::AliasId),
Unsupported,
}
#[derive(Debug)]
pub(crate) struct ScalarField<'ast> {
pub(crate) r#type: ScalarFieldType,
pub(crate) is_ignored: bool,
pub(crate) is_updated_at: bool,
pub(crate) default: Option<dml::default_value::DefaultValue>,
/// @map
pub(super) mapped_name: Option<&'ast str>,
// Native type name and arguments
pub(crate) native_type: Option<(&'ast str, Vec<String>)>,
}
#[derive(Debug)]
pub(crate) struct RelationField<'ast> {
pub(crate) referenced_model: ast::ModelId,
pub(crate) on_delete: Option<dml::relation_info::ReferentialAction>,
pub(crate) on_update: Option<dml::relation_info::ReferentialAction>,
/// The fields _explicitly present_ in the AST.
pub(crate) fields: Option<Vec<ast::FieldId>>,
/// The `references` fields _explicitly present_ in the AST.
pub(crate) references: Option<Vec<ast::FieldId>>,
/// The name _explicitly present_ in the AST.
pub(crate) name: Option<&'ast str>,
pub(crate) is_ignored: bool,
}
impl RelationField<'_> {
fn new(referenced_model: ast::ModelId) -> Self {
RelationField {
referenced_model,
on_delete: None,
on_update: None,
fields: None,
references: None,
name: None,
is_ignored: false,
}
}
}
#[derive(Default, Debug)]
pub(crate) struct ModelData<'ast> {
pub(crate) primary_key: Option<PrimaryKeyData<'ast>>,
pub(crate) is_ignored: bool,
/// @(@) index and @(@)unique.
pub(crate) indexes: Vec<IndexData<'ast>>,
/// @@map
pub(super) mapped_name: Option<&'ast str>,
}
#[derive(Debug, Default)]
pub(crate) struct IndexData<'ast> {
pub(crate) is_unique: bool,
pub(crate) fields: Vec<ast::FieldId>,
pub(crate) source_field: Option<ast::FieldId>,
pub(crate) name: Option<&'ast str>,
pub(crate) db_name: Option<&'ast str>,
}
#[derive(Debug, Default)]
pub(crate) struct PrimaryKeyData<'ast> {
pub(crate) fields: Vec<ast::FieldId>,
pub(crate) source_field: Option<ast::FieldId>,
pub(crate) name: Option<&'ast str>,
pub(crate) db_name: Option<&'ast str>,
}
#[derive(Debug, Default)]
pub(super) struct EnumData<'ast> {
pub(super) mapped_name: Option<&'ast str>,
/// @map on enum values.
pub(super) mapped_values: HashMap<u32, &'ast str>,
}
fn visit_model<'ast>(model_id: ast::ModelId, ast_model: &'ast ast::Model, ctx: &mut Context<'ast>) {
let model_data = ModelData {
// This needs to be looked up first, because we want to skip some field
// validations when the model is ignored.
is_ignored: ast_model.attributes.iter().any(|attr| attr.name.name == "ignore"),
..Default::default()
};
for (field_id, ast_field) in ast_model.iter_fields() {
match field_type(ast_field, ctx) {
Ok(FieldType::Model(referenced_model)) => {
let rf = RelationField::new(referenced_model);
ctx.db.types.relation_fields.insert((model_id, field_id), rf);
}
Ok(FieldType::Scalar(scalar_field_type)) => {
let field_data = ScalarField {
r#type: scalar_field_type,
is_ignored: false,
is_updated_at: false,
default: None,
mapped_name: None,
native_type: None,
};
if matches!(scalar_field_type, ScalarFieldType::BuiltInScalar(t) if t.is_json())
&& !ctx.db.active_connector().supports_json()
{
ctx.push_error(DatamodelError::new_field_validation_error(
&format!("Field `{}` in model `{}` can't be of type Json. The current connector does not support the Json type.", &ast_field.name.name, &ast_model.name.name),
&ast_model.name.name,
&ast_field.name.name,
ast_field.span,
));
}
if ast_field.arity.is_list() && !ctx.db.active_connector().supports_scalar_lists() {
ctx.push_error(DatamodelError::new_scalar_list_fields_are_not_supported(
&ast_model.name.name,
&ast_field.name.name,
ast_field.span,
));
}
if matches!(scalar_field_type, ScalarFieldType::Unsupported) {
validate_unsupported_field_type(ast_field, ast_field.field_type.as_unsupported().unwrap().0, ctx);
}
ctx.db.types.scalar_fields.insert((model_id, field_id), field_data);
}
Err(supported) => ctx.push_error(DatamodelError::new_type_not_found_error(
supported,
ast_field.field_type.span(),
)),
}
}
ctx.db.types.models.insert(model_id, model_data);
}
/// Detect self-referencing type aliases, possibly indirectly. We loop
/// through each type alias in the schema. If it references another type
/// alias β which may in turn reference another type alias β, we check that
/// it is not self-referencing. If a type alias ends up transitively
/// referencing itself, we create an error diagnostic.
fn detect_alias_cycles(ctx: &mut Context<'_>) {
// The IDs of the type aliases we traversed to get to the current type alias.
let mut path = Vec::new();
// We accumulate the errors here because we want to sort them at the end.
let mut errors: Vec<(ast::AliasId, DatamodelError)> = Vec::new();
for (alias_id, ty) in &ctx.db.types.type_aliases {
// Loop variable. This is the "tip" of the sequence of type aliases.
let mut current = (*alias_id, ty);
path.clear();
// Follow the chain of type aliases referencing other type aliases.
while let ScalarFieldType::Alias(next_alias_id) = current.1 {
path.push(current.0);
let next_alias = &ctx.db.ast[*next_alias_id];
// Detect a cycle where next type is also the root. In that
// case, we want to report an error.
if path.len() > 1 && &path[0] == next_alias_id {
errors.push((
*alias_id,
DatamodelError::new_validation_error(
&format!(
"Recursive type definitions are not allowed. Recursive path was: {} -> {}.",
path.iter().map(|id| &ctx.db.ast[*id].name.name).join(" -> "),
&next_alias.name.name,
),
next_alias.field_type.span(),
),
));
break;
}
// We detect a cycle anywhere else in the chain of type aliases.
// In that case, the error will be reported somewhere else, and
// we can just move on from this alias.
if path.contains(next_alias_id) {
break;
}
match ctx.db.types.type_aliases.get(next_alias_id) {
Some(next_alias_type) => {
current = (*next_alias_id, next_alias_type);
}
// A missing alias at this point means that there was an
// error resolving the type of the next alias. We stop
// validation here.
None => break,
}
}
}
errors.sort_by_key(|(id, _err)| *id);
for (_, error) in errors {
ctx.push_error(error);
}
}
fn visit_enum<'ast>(enum_id: ast::EnumId, enm: &'ast ast::Enum, ctx: &mut Context<'ast>) {
let mut enum_data = EnumData::default();
if !ctx.db.active_connector().supports_enums() {
ctx.push_error(DatamodelError::new_validation_error(
&format!(
"You defined the enum `{}`. But the current connector does not support enums.",
&enm.name.name
),
enm.span,
));
}
if enm.values.is_empty() {
ctx.push_error(DatamodelError::new_validation_error(
"An enum must have at least one value.",
enm.span,
))
}
for (field_idx, field) in enm.values.iter().enumerate() {
ctx.visit_attributes(&field.attributes, |attributes, ctx| {
// @map
attributes.visit_optional_single("map", ctx, |map_args, ctx| {
if let Some(mapped_name) = attributes::visit_map(map_args, ctx) {
enum_data.mapped_values.insert(field_idx as u32, mapped_name);
ctx.mapped_enum_value_names
.insert((enum_id, mapped_name), field_idx as u32);
}
})
});
}
ctx.visit_attributes(&enm.attributes, |attributes, ctx| {
// @@map
attributes.visit_optional_single("map", ctx, |map_args, ctx| {
if let Some(mapped_name) = attributes::visit_map(map_args, ctx) {
enum_data.mapped_name = Some(mapped_name);
ctx.mapped_enum_names.insert(mapped_name, enum_id);
}
})
});
ctx.db.types.enums.insert(enum_id, enum_data);
}
fn visit_type_alias<'ast>(alias_id: ast::AliasId, alias: &'ast ast::Field, ctx: &mut Context<'ast>) {
match field_type(alias, ctx) {
Ok(FieldType::Scalar(scalar_field_type)) => {
ctx.db.types.type_aliases.insert(alias_id, scalar_field_type);
}
Ok(FieldType::Model(_)) => ctx.push_error(DatamodelError::new_validation_error(
"Only scalar types can be used for defining custom types.",
alias.field_type.span(),
)),
Err(supported) => ctx.push_error(DatamodelError::new_type_not_found_error(
supported,
alias.field_type.span(),
)),
};
}
fn field_type<'ast>(field: &'ast ast::Field, ctx: &mut Context<'ast>) -> Result<FieldType, &'ast str> {
let supported = match &field.field_type {
ast::FieldType::Supported(ident) => &ident.name,
ast::FieldType::Unsupported(_, _) => return Ok(FieldType::Scalar(ScalarFieldType::Unsupported)),
};
if let Ok(tpe) = dml::scalars::ScalarType::from_str(supported) {
return Ok(FieldType::Scalar(ScalarFieldType::BuiltInScalar(tpe)));
}
match ctx
.db
.names
.tops
.get(supported.as_str())
.map(|id| (*id, &ctx.db.ast[*id]))
{
Some((ast::TopId::Model(model_id), ast::Top::Model(_))) => Ok(FieldType::Model(model_id)),
Some((ast::TopId::Enum(enum_id), ast::Top::Enum(_))) => Ok(FieldType::Scalar(ScalarFieldType::Enum(enum_id))),
Some((ast::TopId::Alias(id), ast::Top::Type(_))) => Ok(FieldType::Scalar(ScalarFieldType::Alias(id))),
Some((_, ast::Top::Generator(_))) | Some((_, ast::Top::Source(_))) => unreachable!(),
None => Err(supported),
_ => unreachable!(),
}
}
fn validate_unsupported_field_type(ast_field: &ast::Field, unsupported_lit: &str, ctx: &mut Context<'_>) {
static TYPE_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"(?x)
^ # beginning of the string
(?P<prefix>[^(]+) # a required prefix that is any character until the first opening brace
(?:\((?P<params>.*?)\))? # (optional) an opening parenthesis, a closing parenthesis and captured params in-between
(?P<suffix>.+)? # (optional) captured suffix after the params until the end of the string
$ # end of the string
"#).unwrap()
});
if let Some(source) = ctx.db.datasource() {
let connector = &source.active_connector;
if let Some(captures) = TYPE_REGEX.captures(unsupported_lit) {
let prefix = captures.name("prefix").unwrap().as_str().trim();
let params = captures.name("params");
let args = match params {
None => vec![],
Some(params) => params.as_str().split(',').map(|s| s.trim().to_string()).collect(),
};
if let Ok(native_type) = connector.parse_native_type(prefix, args) {
let prisma_type = connector.scalar_type_for_native_type(native_type.serialized_native_type.clone());
let msg = format!(
"The type `Unsupported(\"{}\")` you specified in the type definition for the field `{}` is supported as a native type by Prisma. Please use the native type notation `{} @{}.{}` for full support.",
unsupported_lit, ast_field.name.name, prisma_type.to_string(), &source.name, native_type.render()
);
ctx.push_error(DatamodelError::new_validation_error(&msg, ast_field.span));
}
}
}
}