id_effect_proc_macro 0.9.0

Procedural `effect!` macro for the `effect` crate (proc-macro only; declarative macros live in `id_effect_macro`)
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
//! `#[derive(Entity)]` — generate an `id_effect_sql::repository::Entity` impl mapping a
//! `#[derive(FromRow)]` struct to a table.
//!
//! ```text
//! #[entity(table = "accounts",            // default: the type name in snake case
//!          tenant = "org_id",             // scope every statement to the repository's tenant
//!          soft_delete = "deleted_at",    // hide rows whose column is not NULL
//!          scope = active_only,           // fn() -> impl Into<Condition>; repeatable
//!          crate = path)]                 // all optional
//! struct Account {
//!   #[entity(id, generated)] id: i64,     // primary key; `generated`: assigned by the database
//!   #[from_row(rename = "title")] name: String, // columns are the FromRow columns
//!   #[entity(version)] version: i64,      // optimistic locking
//! }
//! ```
//!
//! The columns follow `FromRow`'s rules (`#[from_row(rename)]`, else `#[schema("..")]`, else
//! the field name) so the two derives always agree; `#[from_row(flatten)]` fields have no single
//! column and are rejected. Each value is `<FieldTy as ToSqlValue>::to_sql_value(&self.field)`,
//! spanned at the field type, so a type without `ToSqlValue` is reported there.

use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, quote_spanned};
use syn::ext::IdentExt;
use syn::spanned::Spanned;
use syn::{Data, DeriveInput, Field, Fields, Ident, LitStr, Path, parse_macro_input};

pub(crate) fn derive(input: TokenStream) -> TokenStream {
  let input = parse_macro_input!(input as DeriveInput);
  match expand(input) {
    Ok(ts) => ts.into(),
    Err(e) => e.to_compile_error().into(),
  }
}

#[derive(Default)]
struct ContainerAttrs {
  table: Option<LitStr>,
  tenant: Option<LitStr>,
  soft_delete: Option<LitStr>,
  scopes: Vec<Path>,
  krate: Option<Path>,
}

fn parse_container(input: &DeriveInput) -> syn::Result<ContainerAttrs> {
  let mut attrs = ContainerAttrs::default();
  for attr in input.attrs.iter().filter(|a| a.path().is_ident("entity")) {
    attr.parse_nested_meta(|meta| {
      if meta.path.is_ident("table") {
        attrs.table = Some(meta.value()?.parse()?);
      } else if meta.path.is_ident("tenant") {
        attrs.tenant = Some(meta.value()?.parse()?);
      } else if meta.path.is_ident("soft_delete") {
        attrs.soft_delete = Some(meta.value()?.parse()?);
      } else if meta.path.is_ident("scope") {
        attrs.scopes.push(meta.value()?.parse()?);
      } else if meta.path.is_ident("crate") {
        attrs.krate = Some(meta.value()?.parse()?);
      } else {
        return Err(meta.error(
          "expected `table = \"..\"`, `tenant = \"..\"`, `soft_delete = \"..\"`, `scope = path` \
           or `crate = path` on the struct",
        ));
      }
      Ok(())
    })?;
  }
  Ok(attrs)
}

#[derive(Default)]
struct FieldAttrs {
  id: bool,
  generated: bool,
  version: bool,
  /// The first `#[entity(..)]` attribute, for duplicate errors.
  span: Option<Span>,
}

fn parse_field(field: &Field) -> syn::Result<FieldAttrs> {
  let mut attrs = FieldAttrs::default();
  for attr in field.attrs.iter().filter(|a| a.path().is_ident("entity")) {
    attrs.span.get_or_insert_with(|| attr.span());
    attr.parse_nested_meta(|meta| {
      if meta.path.is_ident("id") {
        attrs.id = true;
      } else if meta.path.is_ident("generated") {
        attrs.generated = true;
      } else if meta.path.is_ident("version") {
        attrs.version = true;
      } else {
        return Err(meta.error("expected `id`, `generated` or `version`"));
      }
      Ok(())
    })?;
  }
  if attrs.generated && !attrs.id {
    return Err(syn::Error::new_spanned(
      field,
      "`generated` marks a database-assigned primary key: use it as `#[entity(id, generated)]`",
    ));
  }
  if attrs.id && attrs.version {
    return Err(syn::Error::new_spanned(
      field,
      "a field cannot be both the `id` and the `version` column",
    ));
  }
  Ok(attrs)
}

/// The field's `FromRow` column (`#[from_row(rename)]`, else `#[schema("..")]`, else its name)
/// and whether it is `#[from_row(flatten)]`. Other `from_row` keys are `FromRow`'s to check.
fn from_row_column(field: &Field) -> syn::Result<(LitStr, bool)> {
  let (mut rename, mut flatten) = (None::<LitStr>, false);
  for attr in field.attrs.iter().filter(|a| a.path().is_ident("from_row")) {
    attr.parse_nested_meta(|meta| {
      if meta.path.is_ident("rename") {
        rename = Some(meta.value()?.parse()?);
      } else if meta.path.is_ident("flatten") {
        flatten = true;
      }
      Ok(())
    })?;
  }
  let ident = field.ident.as_ref().expect("named field");
  let column = rename
    .or_else(|| {
      field
        .attrs
        .iter()
        .filter(|a| a.path().is_ident("schema"))
        .find_map(|a| a.parse_args::<LitStr>().ok())
    })
    .unwrap_or_else(|| LitStr::new(&ident.unraw().to_string(), ident.span()));
  Ok((column, flatten))
}

/// `LedgerEntry` → `ledger_entry`.
fn snake_case(ident: &Ident) -> String {
  let mut out = String::new();
  let mut prev_lower = false;
  for c in ident.unraw().to_string().chars() {
    if c.is_uppercase() {
      if prev_lower {
        out.push('_');
      }
      out.extend(c.to_lowercase());
      prev_lower = false;
    } else {
      out.push(c);
      prev_lower = c.is_lowercase() || c.is_ascii_digit();
    }
  }
  out
}

fn option(lit: Option<&LitStr>) -> TokenStream2 {
  match lit {
    Some(lit) => quote! { ::core::option::Option::Some(#lit) },
    None => quote! { ::core::option::Option::None },
  }
}

pub(crate) fn expand(input: DeriveInput) -> syn::Result<TokenStream2> {
  let name = &input.ident;
  let Data::Struct(data) = &input.data else {
    return Err(syn::Error::new(
      Span::call_site(),
      "#[derive(Entity)] supports structs with named fields only",
    ));
  };
  let Fields::Named(fields) = &data.fields else {
    return Err(syn::Error::new_spanned(
      &data.fields,
      "#[derive(Entity)] supports structs with named fields only",
    ));
  };
  let attrs = parse_container(&input)?;
  let krate = attrs
    .krate
    .unwrap_or_else(|| syn::parse_quote!(::id_effect_sql));
  let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

  let (mut id, mut version) = (None, None);
  let (mut columns, mut values) = (Vec::new(), Vec::new());
  for field in &fields.named {
    let fa = parse_field(field)?;
    let (column, flatten) = from_row_column(field)?;
    if flatten {
      return Err(syn::Error::new_spanned(
        field,
        "#[derive(Entity)] maps each field to one column: `#[from_row(flatten)]` fields are not \
         supported",
      ));
    }
    let ident = field.ident.as_ref().expect("named field");
    let ty = &field.ty;
    let span = fa.span.unwrap_or_else(|| field.span());
    if fa.id {
      if id.is_some() {
        return Err(syn::Error::new(
          span,
          "#[derive(Entity)] takes one `#[entity(id)]` field",
        ));
      }
      id = Some((ident, ty, column.clone(), fa.generated));
    }
    if fa.version {
      if version.is_some() {
        return Err(syn::Error::new(
          span,
          "#[derive(Entity)] takes one `#[entity(version)]` field",
        ));
      }
      version = Some((ident, ty, column.clone()));
    }
    values.push(quote_spanned! { ty.span()=>
      (#column, <#ty as #krate::ToSqlValue>::to_sql_value(&self.#ident))
    });
    columns.push(column);
  }
  let Some((id_ident, id_ty, id_column, generated)) = id else {
    return Err(syn::Error::new(
      name.span(),
      "#[derive(Entity)] needs an `#[entity(id)]` field (the primary key)",
    ));
  };

  let name_str = name.unraw().to_string();
  let table = attrs
    .table
    .unwrap_or_else(|| LitStr::new(&snake_case(name), name.span()));
  let version_column = option(version.as_ref().map(|(_, _, c)| c));
  let tenant = option(attrs.tenant.as_ref());
  let soft_delete = option(attrs.soft_delete.as_ref());
  let bump = version.map(|(ident, ty, _)| {
    let add = quote_spanned! { ty.span()=> self.#ident += 1; };
    quote! {
      fn bump_version(&mut self) {
        #add
      }
    }
  });
  let scope = (!attrs.scopes.is_empty()).then(|| {
    let scopes = &attrs.scopes;
    quote! {
      fn scope() -> #krate::query_builder::sea_query::Condition {
        #krate::query_builder::sea_query::Condition::all() #(.add(#scopes()))*
      }
    }
  });

  Ok(quote! {
    #[automatically_derived]
    impl #impl_generics #krate::repository::Entity for #name #ty_generics #where_clause {
      type Id = #id_ty;

      const NAME: &'static str = #name_str;
      const TABLE: &'static str = #table;
      const COLUMNS: &'static [&'static str] = &[#(#columns),*];
      const ID: &'static str = #id_column;
      const ID_GENERATED: bool = #generated;
      const VERSION: ::core::option::Option<&'static str> = #version_column;
      const SOFT_DELETE: ::core::option::Option<&'static str> = #soft_delete;
      const TENANT: ::core::option::Option<&'static str> = #tenant;

      fn id(&self) -> Self::Id {
        ::core::clone::Clone::clone(&self.#id_ident)
      }

      fn values(&self) -> ::std::vec::Vec<(&'static str, #krate::SqlValue)> {
        ::std::vec![#(#values),*]
      }

      #bump
      #scope
    }
  })
}

#[cfg(test)]
mod tests {
  use super::*;

  fn expand_str(src: &str) -> Result<String, String> {
    let input: DeriveInput = syn::parse_str(src).expect("valid item");
    expand(input)
      .map(|ts| ts.to_string())
      .map_err(|e| e.to_string())
  }

  #[test]
  fn maps_table_columns_id_version_and_filters() {
    let out = expand_str(
      "#[entity(table = \"accounts\", tenant = \"org_id\", soft_delete = \"deleted_at\")]
       #[entity(scope = active, scope = crate::visible)]
       struct Account {
         #[entity(id, generated)] id: i64,
         #[from_row(rename = \"title\", default)] name: String,
         #[schema(\"nick_name\")] nick: Option<String>,
         #[entity(version)] version: i32,
         r#type: String,
       }",
    )
    .unwrap();
    for part in [
      "impl :: id_effect_sql :: repository :: Entity for Account",
      "type Id = i64 ;",
      "const NAME : & 'static str = \"Account\" ;",
      "const TABLE : & 'static str = \"accounts\" ;",
      "const COLUMNS : & 'static [& 'static str] = & [\"id\" , \"title\" , \"nick_name\" , \"version\" , \"type\"] ;",
      "const ID : & 'static str = \"id\" ;",
      "const ID_GENERATED : bool = true ;",
      "const VERSION : :: core :: option :: Option < & 'static str > = :: core :: option :: Option :: Some (\"version\") ;",
      "const SOFT_DELETE : :: core :: option :: Option < & 'static str > = :: core :: option :: Option :: Some (\"deleted_at\") ;",
      "const TENANT : :: core :: option :: Option < & 'static str > = :: core :: option :: Option :: Some (\"org_id\") ;",
      "(\"title\" , < String as :: id_effect_sql :: ToSqlValue > :: to_sql_value (& self . name))",
      "(\"type\" , < String as :: id_effect_sql :: ToSqlValue > :: to_sql_value (& self . r#type))",
      "fn bump_version (& mut self) { self . version += 1 ; }",
      "Condition :: all () . add (active ()) . add (crate :: visible ())",
      ":: core :: clone :: Clone :: clone (& self . id)",
    ] {
      assert!(out.contains(part), "missing `{part}` in {out}");
    }
  }

  #[test]
  fn defaults_and_crate_override() {
    let out = expand_str(
      "#[entity(crate = my::sql)]
       struct LedgerEntry2Line<T> where T: Clone { #[entity(id)] key: String, t: T }",
    )
    .unwrap();
    for part in [
      "impl < T > my :: sql :: repository :: Entity for LedgerEntry2Line < T > where T : Clone",
      "const TABLE : & 'static str = \"ledger_entry2_line\" ;",
      "const ID : & 'static str = \"key\" ;",
      "const ID_GENERATED : bool = false ;",
      "const VERSION : :: core :: option :: Option < & 'static str > = :: core :: option :: Option :: None ;",
      "my :: sql :: SqlValue",
    ] {
      assert!(out.contains(part), "missing `{part}` in {out}");
    }
    assert!(!out.contains("bump_version"), "{out}");
    assert!(!out.contains("fn scope"), "{out}");
    assert_eq!(
      snake_case(&syn::parse_str("HTTPServer").unwrap()),
      "httpserver"
    );
  }

  #[test]
  fn rejects_shapes() {
    let err = expand_str("enum E { A }").unwrap_err();
    assert!(err.contains("named fields only"), "{err}");
    let err = expand_str("struct T(i64);").unwrap_err();
    assert!(err.contains("named fields only"), "{err}");
    let err = expand_str("struct S { a: i64 }").unwrap_err();
    assert!(err.contains("needs an `#[entity(id)]` field"), "{err}");
  }

  #[test]
  fn rejects_bad_attributes() {
    let cases = [
      (
        "#[entity(name = \"x\")] struct S { #[entity(id)] a: i64 }",
        "expected `table",
      ),
      (
        "#[entity(table = 1)] struct S { #[entity(id)] a: i64 }",
        "expected string literal",
      ),
      (
        "struct S { #[entity(key)] a: i64 }",
        "expected `id`, `generated` or `version`",
      ),
      (
        "struct S { #[entity(generated)] a: i64 }",
        "#[entity(id, generated)]",
      ),
      (
        "struct S { #[entity(id, version)] a: i64 }",
        "both the `id` and the `version`",
      ),
      (
        "struct S { #[entity(id)] a: i64, #[entity(id)] b: i64 }",
        "one `#[entity(id)]` field",
      ),
      (
        "struct S { #[entity(id)] a: i64, #[entity(version)] b: i64, #[entity(version)] c: i64 }",
        "one `#[entity(version)]` field",
      ),
      (
        "struct S { #[entity(id)] a: i64, #[from_row(flatten)] b: B }",
        "`#[from_row(flatten)]` fields are not supported",
      ),
      (
        "struct S { #[entity(id)] #[from_row(rename = 1)] a: i64 }",
        "expected string literal",
      ),
    ];
    for (src, expected) in cases {
      let err = expand_str(src).unwrap_err();
      assert!(err.contains(expected), "{src}: {err}");
    }
  }
}