cido-macros 0.2.0

Macros for generating code that enables easier interfacing with cido
Documentation
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
use crate::parse::util::Dbg;
use deluxe::{ExtractAttributes, ParseAttributes};
use proc_macro2::TokenStream;
use quote::{ToTokens, quote};
use std::collections::HashSet;
use syn::{Attribute, Expr, Ident, Type, TypePath};

mod field;
mod parse;

pub const DEFAULT_DATALOADER_CACHE_SIZE: usize = 100_000;
pub const DEFAULT_DATALOADER_BATCH_SIZE: usize = 1_000;
pub const DEFAULT_DATALOADER_DELAY_MS: u64 = 10;
pub const DEFAULT_MAX_LOAD_ITEMS: u64 = 100_000;
pub const DEFAULT_MAX_SKIP_ITEMS: u64 = 10_000;
pub const DEFAULT_PART_MAX_ROWS: u64 = u64::MAX;
pub const DEFAULT_PART_MAX_BYTES: u64 = 256 * 1024 * 1024;

use crate::parse::util::{Attributes, SqlSafeName};

#[derive(Debug, Clone)]
pub struct TransformerStruct {
  pub name: Ident,
  pub attrs: Vec<Attribute>,
  pub vis: syn::Visibility,
  pub struct_opts: TransformerOptions,
  pub struct_type: StructType,
  pub id_offset: usize,
  pub fields: Vec<TransformerField>,
  pub derived: Vec<DerivedField>,
}

impl TransformerStruct {
  #[allow(clippy::too_many_lines)]
  pub fn parse(
    args: TokenStream,
    input: TokenStream,
    struct_type: StructType,
  ) -> deluxe::Result<Self> {
    let transformer_options = deluxe::parse2::<parse::TransformerParseOptions>(args)?;
    let mut t = syn::parse2::<syn::ItemStruct>(input)?;
    super::ensure_generics_zero(&t.generics)?;

    let name = t.ident.clone();

    let id_name: Ident = syn::parse_quote!(id);

    let mut fields = Vec::with_capacity(t.fields.len());
    let mut derived_fields = Vec::new();
    let mut id_offset = usize::MAX;

    for (idx, field) in t.fields.iter_mut().enumerate() {
      let name = field
        .ident
        .clone()
        .ok_or_else(|| crate::err(field, "All fields require names"))?;
      let mut aliases = HashSet::new();
      let mut ty = field.ty.clone();
      let mut id = false;
      let mut entity_or_event = None;

      {
        let id_field = field::Flag::<field::IdField>::extract_attributes(field)?;
        if id_field.set || name == id_name {
          if id {
            return Err(crate::parse::err_span(
              id_field.span,
              "#[id] can only be specified once for now. A field named `id` is treated as having the annotation.",
            ));
          }
          id_offset = idx;
          id = true;
        }
      }

      let alias = field::Alias::extract_attributes(field)?;
      for alias in alias.0 {
        aliases.insert(alias);
      }

      let static_field = field::Flag::<field::StaticField>::extract_attributes(field)?;

      if static_field.set && id {
        return Err(crate::parse::err_span(
          static_field.span,
          "#[id] implies #[static_field]",
        ));
      }

      let mut indexed = field::Flag::<field::Indexed>::extract_attributes(field)?;
      if indexed.set && id {
        return Err(crate::parse::err_span(
          indexed.span,
          "#[id] implies #[indexed]",
        ));
      }

      {
        let entity = field::Flag::<field::Entity>::extract_attributes(field)?;
        if entity.set {
          let field_ty = &field.ty;
          ty =
            syn::parse_quote! { <#field_ty as ::cido::__internal::ToIdentifiable>::Identifiable };
          if id {
            return Err(crate::parse::err_span(
              entity.span,
              "#[entity] cannot be specified with #[id]",
            ));
          }
          if indexed.set {
            return Err(crate::parse::err_span(
              entity.span,
              "#[entity] implies #[indexed]",
            ));
          }
          indexed.set = true;
          entity_or_event = Some(StructType::Entity);
        }
      }

      {
        let event = field::Flag::<field::Event>::extract_attributes(field)?;
        if event.set {
          let field_ty = &field.ty;
          ty =
            syn::parse_quote! { <#field_ty as ::cido::__internal::ToIdentifiable>::Identifiable };

          if id {
            return Err(crate::parse::err_span(
              event.span,
              "#[event] cannot be specified with #[id]",
            ));
          }
          if entity_or_event.is_some() {
            return Err(crate::parse::err_span(
              event.span,
              "Only one of #[entity] or #[event] can be specified",
            ));
          }
          if indexed.set {
            return Err(crate::parse::err_span(
              event.span,
              "#[event] implies #[indexed]",
            ));
          }
          indexed.set = true;
          entity_or_event = Some(StructType::Event);
        }
      }

      let sql_exists =
        field::ActuallyExists::<_, field::TransformerFieldSqlParseConfig>::parse_attributes(field)?
          .exists;
      let sql = field::TransformerFieldSqlParseConfig::extract_attributes(field)?;

      let sql_config = TransformerFieldSqlConfig {
        name: sql
          .name
          .unwrap_or_else(|| SqlSafeName::new(name.to_string())),
        ty: sql.ty,
        nullable: sql.nullable.is_set()
          || field
            .ty
            .to_token_stream()
            .to_string()
            .starts_with("Option<"),
      };

      let db_exists =
        field::ActuallyExists::<_, field::TransformerFieldDbParseConfig>::parse_attributes(field)?
          .exists;
      let db = field::TransformerFieldDbParseConfig::extract_attributes(field)?;
      let field_ty = &field.ty;

      if entity_or_event.is_some() && db.ty.is_some() {
        return Err(crate::err(
          db.ty.as_ref().unwrap(),
          "Cannot specify db field type for entities or events",
        ));
      }

      let db_config = TransformerFieldDbConfig {
        name: db.name.unwrap_or_else(|| name.clone()),
        ty: Dbg(db.ty.unwrap_or_else(|| {
          if entity_or_event.is_some() {
            syn::parse_quote!(
              <#field_ty as ::cido::__internal::ToIdentifiable>::Identifiable
            )
          } else {
            ty.clone()
          }
        })),
        field_to_db: db.field_to_db,
        db_to_field: db.db_to_field,
      };

      let gql_exists =
        field::ActuallyExists::<_, field::TransformerFieldGraphqlParseConfig>::parse_attributes(
          field,
        )?
        .exists;
      let gql = field::TransformerFieldGraphqlParseConfig::extract_attributes(field)?;
      let graphql_config = TransformerFieldGraphqlConfig {
        name: gql.name.unwrap_or_else(|| name.clone()),
        attrs: gql.attrs,
        hidden: gql.hidden.is_set(),
        comparison: None,
        complexity: gql.complexity,
        input_type: gql.input.ty,
        output_type: gql.output.ty,
        field_to_output: gql.output.field_to_gql,
        input_to_field: gql.input.gql_to_field,
      };

      let derived_from_exists =
        field::ActuallyExists::<_, field::DerivedField>::parse_attributes(field)?;
      if derived_from_exists.exists {
        if id
          || static_field.set
          || indexed.set
          || entity_or_event.is_some()
          || sql_exists
          || db_exists
          || gql_exists
        {
          return Err(crate::parse::err_span(
            derived_from_exists.span,
            "#[derived_from] is incompatible with all other annotations",
          ));
        }
        let derived = field::DerivedField::extract_attributes(field)?;
        derived_fields.push(DerivedField {
          name,
          ty,
          alias: aliases.into_iter().collect(),
          field_lookup: derived.field,
          complexity: derived.complexity,
          multiple: derived.multiple.is_set(),
          is_entity: false,
          is_event: false,
        });
        continue;
      }

      fields.push(TransformerField {
        field: Dbg(field.clone()),
        ty: Dbg(ty),
        name,
        alias: aliases,
        id,
        is_static: static_field.set,
        sql_config,
        db_config,
        graphql_config,
        entity_or_event,
        indexed: indexed.set,
      });
    }

    if id_offset == usize::MAX {
      return Err(crate::err(
        &t,
        "#[id] must be specified on a single field. A field named `id` is treated as having the annotation.",
      ));
    };

    let this = TransformerStruct {
      attrs: t.attrs,
      vis: t.vis,
      struct_opts: transformer_options.into_opts(&name, struct_type),
      struct_type,
      id_offset,
      fields,
      derived: derived_fields,
      name,
    };

    Ok(this)
  }
}

#[derive(Debug, Clone)]
#[must_use]
pub struct TransformerOptions {
  pub db_opts: TransformerDbOptions,
  pub sql_name: SqlSafeName,
  pub graphql_opts: TransformerGraphqlOptions,
  pub cache_policy: CachePolicy,
  pub cidomap: TypePath,
  pub embed_generated_code: bool,
}

#[derive(Debug, Clone)]
pub struct TransformerDbOptions {
  pub name: Ident,
  pub attrs: Attributes,
  pub partition_max_rows: u64,
  pub partition_max_bytes: u64,
}

#[derive(Debug, Clone)]
pub struct TransformerGraphqlOptions {
  pub name: Ident,
  pub gql_name: Ident,
  pub loader_cache_size: usize,
  pub loader_delay_ms: u64,
  pub loader_max_batch_size: usize,
  pub max_return_items: u64,
  pub max_skip_items: u64,
  pub attrs: Attributes,
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum StructType {
  Entity,
  Event,
}

impl StructType {
  #[allow(unused)]
  pub fn is_entity(self) -> bool {
    matches!(self, Self::Entity)
  }

  #[allow(unused)]
  pub fn is_event(self) -> bool {
    matches!(self, Self::Event)
  }

  pub fn default_cache_policy(self) -> CachePolicy {
    match self {
      Self::Entity => CachePolicy::Always,
      Self::Event => CachePolicy::Block,
    }
  }
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Default, Clone)]
pub enum Conversion {
  #[default]
  Identity,
  From(Dbg<Expr>),
}

impl deluxe::ParseMetaItem for Conversion {
  fn parse_meta_item(
    input: syn::parse::ParseStream,
    _mode: deluxe::ParseMode,
  ) -> syn::Result<Self> {
    if let Ok(s) = input.parse::<syn::LitStr>() {
      s.parse::<syn::Expr>()
    } else {
      input.parse::<syn::Expr>()
    }
    .map(|e| Self::From(Dbg(e)))
  }
}

impl Conversion {
  pub fn conversion(&self, ident: &Ident) -> TokenStream {
    match self {
      Self::Identity => {
        quote!(#ident.try_into()?)
      }
      Self::From(e) => quote!(#e),
    }
  }
}

#[derive(Debug, Clone)]
pub struct TransformerField {
  pub field: Dbg<syn::Field>,
  pub ty: Dbg<syn::Type>,
  pub name: Ident,
  pub alias: HashSet<Ident>,
  pub id: bool,
  pub is_static: bool,
  pub sql_config: TransformerFieldSqlConfig,
  pub db_config: TransformerFieldDbConfig,
  pub graphql_config: TransformerFieldGraphqlConfig,
  pub entity_or_event: Option<StructType>,
  pub indexed: bool,
}

impl TransformerField {
  pub fn self_type(&self) -> syn::Type {
    if self.entity_or_event.is_some() {
      let field_ty = &self.field.ty;
      syn::parse_quote! { <#field_ty as ::cido::__internal::ToIdentifiable>::Identifiable }
    } else {
      self.field.ty.clone()
    }
  }

  pub fn db_type(&self) -> syn::Type {
    (*self.db_config.ty).clone()
  }

  pub fn graphql_input_type(&self) -> syn::Type {
    self
      .graphql_config
      .input_type
      .clone()
      .unwrap_or_else(|| self.db_type())
  }

  pub fn graphql_input_name(&self) -> &Ident {
    &self.graphql_config.name
  }

  pub fn graphql_output_name(&self) -> &Ident {
    &self.graphql_config.name
  }

  pub fn graphql_output_type(&self) -> syn::Type {
    // self
    //   .graphql_config
    //   .output_type
    //   .clone()
    //   .unwrap_or_else(|| self.self_type())
    self.self_type()
  }

  pub fn graphql_comparison_sdl_name_string(&self) -> String {
    self.graphql_config.name.to_string()
  }

  pub fn graphql_sdl_output_name(&self) -> String {
    self.graphql_config.name.to_string()
  }

  pub fn graphql_sdl_output_type(&self) -> syn::Type {
    self
      .graphql_config
      .output_type
      .clone()
      .unwrap_or_else(|| self.graphql_output_type())
  }

  pub fn graphql_order_by_name(&self) -> String {
    self.graphql_sdl_output_name()
  }
}

#[derive(Debug, Clone)]
pub struct DerivedField {
  pub name: Ident,
  pub ty: syn::Type,
  pub alias: Vec<Ident>,
  pub field_lookup: Ident,
  pub complexity: Option<Expr>,
  pub multiple: bool,
  pub is_entity: bool,
  pub is_event: bool,
}

#[derive(Debug, Clone)]
pub struct TransformerFieldSqlConfig {
  pub name: SqlSafeName,
  pub ty: Option<Expr>,
  pub nullable: bool,
}

#[derive(Debug, Clone)]
pub struct TransformerFieldDbConfig {
  pub name: Ident,
  pub ty: Dbg<syn::Type>,
  pub field_to_db: Conversion,
  pub db_to_field: Conversion,
}

#[derive(Debug, Clone)]
pub struct TransformerFieldGraphqlConfig {
  pub name: Ident,
  pub attrs: Attributes,
  pub comparison: Option<syn::Type>,
  pub complexity: Option<Expr>,
  pub hidden: bool,
  pub input_type: Option<syn::Type>,
  pub output_type: Option<syn::Type>,
  pub field_to_output: Conversion,
  pub input_to_field: Conversion,
}

#[derive(Copy, Clone, Debug)]
pub enum CachePolicy {
  Always,
  Lru { size: usize, max_size: usize },
  Block,
  Never,
  Custom,
}