es-entity-macros 0.10.36

Proc macros for es-entity
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
494
495
496
497
498
use darling::ToTokens;
use proc_macro2::{Span, TokenStream};
use quote::{TokenStreamExt, quote};

use super::{list_by_fn::CursorStruct, options::*};

pub struct ListForFn<'a> {
    ignore_prefix: Option<&'a syn::LitStr>,
    pub for_column: &'a Column,
    pub by_column: &'a Column,
    entity: &'a syn::Ident,
    id: &'a syn::Ident,
    table_name: &'a str,
    query_error: syn::Ident,
    delete: DeleteOption,
    cursor_mod: syn::Ident,
    any_nested: bool,
    post_hydrate_error: Option<&'a syn::Type>,
    #[cfg(feature = "instrument")]
    repo_name_snake: String,
}

impl<'a> ListForFn<'a> {
    pub fn new(for_column: &'a Column, by_column: &'a Column, opts: &'a RepositoryOptions) -> Self {
        Self {
            ignore_prefix: opts.table_prefix(),
            for_column,
            by_column,
            id: opts.id(),
            entity: opts.entity(),
            table_name: opts.table_name(),
            query_error: opts.query_error(),
            delete: opts.delete,
            cursor_mod: opts.cursor_mod(),
            any_nested: opts.any_nested(),
            post_hydrate_error: opts.post_hydrate_hook.as_ref().map(|h| &h.error),
            #[cfg(feature = "instrument")]
            repo_name_snake: opts.repo_name_snake_case(),
        }
    }

    pub fn cursor(&'a self) -> CursorStruct<'a> {
        CursorStruct {
            column: self.by_column,
            id: self.id,
            entity: self.entity,
            cursor_mod: &self.cursor_mod,
        }
    }
}

impl ToTokens for ListForFn<'_> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let entity = self.entity;
        let cursor = self.cursor();
        let cursor_ident = cursor.ident();
        let cursor_mod = cursor.cursor_mod();
        let error = &self.query_error;
        let query_fn_generics = RepositoryOptions::query_fn_generics(self.any_nested);
        let query_fn_op_arg = RepositoryOptions::query_fn_op_arg(self.any_nested);
        let query_fn_op_traits = RepositoryOptions::query_fn_op_traits(self.any_nested);
        let query_fn_get_op = RepositoryOptions::query_fn_get_op(self.any_nested);

        let by_column_name = self.by_column.name();

        let for_column_name = self.for_column.name();
        let filter_arg_name = syn::Ident::new(
            &format!("filter_{}", self.for_column.name()),
            Span::call_site(),
        );
        let (for_column_type, for_impl_expr, for_access_expr) = self.for_column.ty_for_find_by();

        let destructure_tokens = self.cursor().destructure_tokens();
        let select_columns = cursor.select_columns(Some(for_column_name));
        let arg_tokens = cursor.query_arg_tokens();

        for delete in [DeleteOption::No, DeleteOption::Soft] {
            let fn_name = syn::Ident::new(
                &format!(
                    "list_for_{}_by_{}{}",
                    for_column_name,
                    by_column_name,
                    delete.include_deletion_fn_postfix()
                ),
                Span::call_site(),
            );
            let fn_in_op = syn::Ident::new(
                &format!(
                    "list_for_{}_by_{}{}_in_op",
                    for_column_name,
                    by_column_name,
                    delete.include_deletion_fn_postfix()
                ),
                Span::call_site(),
            );

            let filter_op = if self.for_column.is_optional() {
                "IS NOT DISTINCT FROM"
            } else {
                "="
            };
            let asc_query = format!(
                r#"SELECT {} FROM {} WHERE (({} {} $1) AND ({})){} ORDER BY {} LIMIT $2"#,
                select_columns,
                self.table_name,
                for_column_name,
                filter_op,
                cursor.condition(1, true),
                if delete == DeleteOption::No {
                    self.delete.not_deleted_condition()
                } else {
                    ""
                },
                cursor.order_by(true)
            );
            let desc_query = format!(
                r#"SELECT {} FROM {} WHERE (({} {} $1) AND ({})){} ORDER BY {} LIMIT $2"#,
                select_columns,
                self.table_name,
                for_column_name,
                filter_op,
                cursor.condition(1, false),
                if delete == DeleteOption::No {
                    self.delete.not_deleted_condition()
                } else {
                    ""
                },
                cursor.order_by(false)
            );

            let es_query_asc_call = if let Some(prefix) = self.ignore_prefix {
                quote! {
                    es_entity::es_query!(
                        tbl_prefix = #prefix,
                        #asc_query,
                        #filter_arg_name as &#for_column_type,
                        #arg_tokens
                    )
                }
            } else {
                quote! {
                    es_entity::es_query!(
                        entity = #entity,
                        #asc_query,
                        #filter_arg_name as &#for_column_type,
                        #arg_tokens
                    )
                }
            };

            let es_query_desc_call = if let Some(prefix) = self.ignore_prefix {
                quote! {
                    es_entity::es_query!(
                        tbl_prefix = #prefix,
                        #desc_query,
                        #filter_arg_name as &#for_column_type,
                        #arg_tokens
                    )
                }
            } else {
                quote! {
                    es_entity::es_query!(
                        entity = #entity,
                        #desc_query,
                        #filter_arg_name as &#for_column_type,
                        #arg_tokens
                    )
                }
            };

            #[cfg(feature = "instrument")]
            let (
                instrument_attr,
                extract_has_cursor,
                record_fields,
                record_results,
                error_recording,
            ) = {
                let entity_name = entity.to_string();
                let repo_name = &self.repo_name_snake;
                let span_name = format!(
                    "{}.list_for_{}_by_{}",
                    repo_name, for_column_name, by_column_name
                );
                let filter_field_name = format!("query_{}", filter_arg_name);
                let filter_field_ident =
                    syn::Ident::new(&filter_field_name, proc_macro2::Span::call_site());
                (
                    quote! {
                        #[tracing::instrument(name = #span_name, skip_all, fields(entity = #entity_name, #filter_field_ident = tracing::field::Empty, first, has_cursor, direction = tracing::field::debug(&direction), count = tracing::field::Empty, has_next_page = tracing::field::Empty, ids = tracing::field::Empty, error = tracing::field::Empty, exception.message = tracing::field::Empty, exception.type = tracing::field::Empty))]
                    },
                    quote! {
                        let has_cursor = cursor.after.is_some();
                    },
                    quote! {
                        tracing::Span::current().record(#filter_field_name, tracing::field::debug(&#filter_arg_name));
                        tracing::Span::current().record("first", first);
                        tracing::Span::current().record("has_cursor", has_cursor);
                    },
                    quote! {
                        let result_ids: Vec<_> = entities.iter().map(|e| &e.id).collect();
                        tracing::Span::current().record("count", result_ids.len());
                        tracing::Span::current().record("has_next_page", has_next_page);
                        tracing::Span::current().record("ids", tracing::field::debug(&result_ids));
                    },
                    quote! {
                        if let Err(ref e) = __result {
                            tracing::Span::current().record("error", true);
                            tracing::Span::current().record("exception.message", tracing::field::display(e));
                            tracing::Span::current().record("exception.type", std::any::type_name_of_val(e));
                        }
                    },
                )
            };
            #[cfg(not(feature = "instrument"))]
            let (
                instrument_attr,
                extract_has_cursor,
                record_fields,
                record_results,
                error_recording,
            ) = (quote! {}, quote! {}, quote! {}, quote! {}, quote! {});

            let post_hydrate_check = if self.post_hydrate_error.is_some() {
                quote! {
                    for __entity in &entities {
                        self.execute_post_hydrate_hook(__entity).map_err(#error::PostHydrateError)?;
                    }
                }
            } else {
                quote! {}
            };

            tokens.append_all(quote! {
                pub async fn #fn_name(
                    &self,
                    #filter_arg_name: #for_impl_expr,
                    cursor: es_entity::PaginatedQueryArgs<#cursor_mod::#cursor_ident>,
                    direction: es_entity::ListDirection,
                ) -> Result<es_entity::PaginatedQueryRet<#entity, #cursor_mod::#cursor_ident>, #error> {
                    self.#fn_in_op(#query_fn_get_op, #filter_arg_name, cursor, direction).await
                }

                #instrument_attr
                pub async fn #fn_in_op #query_fn_generics(
                    &self,
                    #query_fn_op_arg,
                    #filter_arg_name: #for_impl_expr,
                    cursor: es_entity::PaginatedQueryArgs<#cursor_mod::#cursor_ident>,
                    direction: es_entity::ListDirection,
                ) -> Result<es_entity::PaginatedQueryRet<#entity, #cursor_mod::#cursor_ident>, #error>
                    where
                        OP: #query_fn_op_traits
                {
                    let __result: Result<es_entity::PaginatedQueryRet<#entity, #cursor_mod::#cursor_ident>, #error> = async {
                        #extract_has_cursor
                        let #filter_arg_name = #filter_arg_name.#for_access_expr;
                        #destructure_tokens
                        #record_fields

                        let (entities, has_next_page) = match direction {
                            es_entity::ListDirection::Ascending => {
                                #es_query_asc_call.fetch_n(op, first).await?
                            },
                            es_entity::ListDirection::Descending => {
                                #es_query_desc_call.fetch_n(op, first).await?
                            }
                        };

                        #post_hydrate_check
                        #record_results

                        let end_cursor = entities.last().map(#cursor_mod::#cursor_ident::from);

                        Ok(es_entity::PaginatedQueryRet {
                            entities,
                            has_next_page,
                            end_cursor,
                        })
                    }.await;

                    #error_recording
                    __result
                }
            });

            if delete == self.delete || self.delete == DeleteOption::SoftWithoutQueries {
                break;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use proc_macro2::Span;
    use syn::Ident;

    #[test]
    fn list_for_fn() {
        let entity = Ident::new("Entity", Span::call_site());
        let query_error = syn::Ident::new("EntityQueryError", Span::call_site());
        let id = syn::Ident::new("EntityId", proc_macro2::Span::call_site());
        let by_column = Column::for_id(syn::parse_str("EntityId").unwrap());
        let for_column = Column::new(
            syn::Ident::new("customer_id", proc_macro2::Span::call_site()),
            syn::parse_str("Uuid").unwrap(),
        );
        let cursor_mod = Ident::new("cursor_mod", Span::call_site());

        let persist_fn = ListForFn {
            ignore_prefix: None,
            entity: &entity,
            id: &id,
            for_column: &for_column,
            by_column: &by_column,
            table_name: "entities",
            query_error,
            delete: DeleteOption::No,
            cursor_mod,
            any_nested: false,
            post_hydrate_error: None,
            #[cfg(feature = "instrument")]
            repo_name_snake: "test_repo".to_string(),
        };

        let mut tokens = TokenStream::new();
        persist_fn.to_tokens(&mut tokens);

        let expected = quote! {
            pub async fn list_for_customer_id_by_id(
                &self,
                filter_customer_id: impl std::borrow::Borrow<Uuid>,
                cursor: es_entity::PaginatedQueryArgs<cursor_mod::EntityByIdCursor>,
                direction: es_entity::ListDirection,
            ) -> Result<es_entity::PaginatedQueryRet<Entity, cursor_mod::EntityByIdCursor>, EntityQueryError> {
                self.list_for_customer_id_by_id_in_op(self.pool(), filter_customer_id, cursor, direction).await
            }

            pub async fn list_for_customer_id_by_id_in_op<'a, OP>(
                &self,
                op: OP,
                filter_customer_id: impl std::borrow::Borrow<Uuid>,
                cursor: es_entity::PaginatedQueryArgs<cursor_mod::EntityByIdCursor>,
                direction: es_entity::ListDirection,
            ) -> Result<es_entity::PaginatedQueryRet<Entity, cursor_mod::EntityByIdCursor>, EntityQueryError>
                where
                    OP: es_entity::IntoOneTimeExecutor<'a>
            {
                let __result: Result<es_entity::PaginatedQueryRet<Entity, cursor_mod::EntityByIdCursor>, EntityQueryError> = async {
                    let filter_customer_id = filter_customer_id.borrow();
                    let es_entity::PaginatedQueryArgs { first, after } = cursor;
                    let id = if let Some(after) = after {
                        Some(after.id)
                    } else {
                        None
                    };
                    let (entities, has_next_page) = match direction {
                        es_entity::ListDirection::Ascending => {
                            es_entity::es_query!(
                                entity = Entity,
                                "SELECT customer_id, id FROM entities WHERE ((customer_id = $1) AND (COALESCE(id > $3, true))) ORDER BY id ASC LIMIT $2",
                                filter_customer_id as &Uuid,
                                (first + 1) as i64,
                                id as Option<EntityId>,
                            )
                                .fetch_n(op, first)
                                .await?
                        },
                        es_entity::ListDirection::Descending => {
                            es_entity::es_query!(
                                entity = Entity,
                                "SELECT customer_id, id FROM entities WHERE ((customer_id = $1) AND (COALESCE(id < $3, true))) ORDER BY id DESC LIMIT $2",
                                filter_customer_id as &Uuid,
                                (first + 1) as i64,
                                id as Option<EntityId>,
                            )
                                .fetch_n(op, first)
                                .await?
                        }
                    };

                        let end_cursor = entities.last().map(cursor_mod::EntityByIdCursor::from);
                        Ok(es_entity::PaginatedQueryRet {
                            entities,
                            has_next_page,
                            end_cursor,
                        })
                }.await;

                __result
            }
        };

        assert_eq!(tokens.to_string(), expected.to_string());
    }

    #[test]
    fn list_same_column() {
        let entity = Ident::new("Entity", Span::call_site());
        let query_error = syn::Ident::new("EntityQueryError", Span::call_site());
        let id = syn::Ident::new("EntityId", proc_macro2::Span::call_site());
        let column = Column::new(
            syn::Ident::new("email", proc_macro2::Span::call_site()),
            syn::parse_str("String").unwrap(),
        );
        let cursor_mod = Ident::new("cursor_mod", Span::call_site());

        let persist_fn = ListForFn {
            ignore_prefix: None,
            entity: &entity,
            id: &id,
            for_column: &column,
            by_column: &column,
            table_name: "entities",
            query_error,
            delete: DeleteOption::No,
            cursor_mod,
            any_nested: false,
            post_hydrate_error: None,
            #[cfg(feature = "instrument")]
            repo_name_snake: "test_repo".to_string(),
        };

        let mut tokens = TokenStream::new();
        persist_fn.to_tokens(&mut tokens);

        let expected = quote! {
            pub async fn list_for_email_by_email(
                &self,
                filter_email: impl std::convert::AsRef<str>,
                cursor: es_entity::PaginatedQueryArgs<cursor_mod::EntityByEmailCursor>,
                direction: es_entity::ListDirection,
            ) -> Result<es_entity::PaginatedQueryRet<Entity, cursor_mod::EntityByEmailCursor>, EntityQueryError> {
                self.list_for_email_by_email_in_op(self.pool(), filter_email, cursor, direction).await
            }

            pub async fn list_for_email_by_email_in_op<'a, OP>(
                &self,
                op: OP,
                filter_email: impl std::convert::AsRef<str>,
                cursor: es_entity::PaginatedQueryArgs<cursor_mod::EntityByEmailCursor>,
                direction: es_entity::ListDirection,
            ) -> Result<es_entity::PaginatedQueryRet<Entity, cursor_mod::EntityByEmailCursor>, EntityQueryError>
                where
                    OP: es_entity::IntoOneTimeExecutor<'a>
            {
                let __result: Result<es_entity::PaginatedQueryRet<Entity, cursor_mod::EntityByEmailCursor>, EntityQueryError> = async {
                    let filter_email = filter_email.as_ref();
                    let es_entity::PaginatedQueryArgs { first, after } = cursor;
                    let (id, email) = if let Some(after) = after {
                        (Some(after.id), Some(after.email))
                    } else {
                        (None, None)
                    };
                    let (entities, has_next_page) = match direction {
                        es_entity::ListDirection::Ascending => {
                            es_entity::es_query!(
                                entity = Entity,
                                "SELECT email, id FROM entities WHERE ((email = $1) AND (COALESCE((email, id) > ($4, $3), $3 IS NULL))) ORDER BY email ASC, id ASC LIMIT $2",
                                filter_email as &str,
                                (first + 1) as i64,
                                id as Option<EntityId>,
                                email as Option<String>,
                            )
                                .fetch_n(op, first)
                                .await?
                        },
                        es_entity::ListDirection::Descending => {
                            es_entity::es_query!(
                                entity = Entity,
                                "SELECT email, id FROM entities WHERE ((email = $1) AND (COALESCE((email, id) < ($4, $3), $3 IS NULL))) ORDER BY email DESC, id DESC LIMIT $2",
                                filter_email as &str,
                                (first + 1) as i64,
                                id as Option<EntityId>,
                                email as Option<String>,
                            )
                                .fetch_n(op, first)
                                .await?
                        }
                    };

                    let end_cursor = entities.last().map(cursor_mod::EntityByEmailCursor::from);
                    Ok(es_entity::PaginatedQueryRet {
                        entities,
                        has_next_page,
                        end_cursor,
                    })
                }.await;

                __result
            }
        };

        assert_eq!(tokens.to_string(), expected.to_string());
    }
}