[WJ-1168] Add proc macro for generating relation structs and methods - #3039
[WJ-1168] Add proc macro for generating relation structs and methods#3039emmiegit wants to merge 104 commits into
Conversation
a397e57 to
503186b
Compare
503186b to
2911363
Compare
First fully-working version, emits compiling code for all relations.
Now using our new procedural macro! Very exciting.
2911363 to
7fbe212
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #3039 +/- ##
===========================================
- Coverage 26.62% 26.54% -0.09%
===========================================
Files 198 201 +3
Lines 13451 13470 +19
===========================================
- Hits 3582 3576 -6
- Misses 9869 9894 +25
🚀 New features to boost your workflow:
|
| dest => bot_user: User, | ||
| from => owner_user: User, | ||
| data => UserBotMetadata, | ||
| create_fn => false, |
There was a problem hiding this comment.
Should this be create_fn => extern rather than false? The previous invocation used NO_CREATE_IMPL_OR_STRUCT and the parser test also models UserBotOwner using extern . My understanding is that false still generates the public creation struct, whereas extern generates neither the struct or method.
There was a problem hiding this comment.
When it's false, it creates a private creation method and struct, which is used in the custom public creation method. But you're right, there isn't a way to disable public struct creation except with extern; we need an option for "create private/inner, but nothing public".
There was a problem hiding this comment.
I've updated the PR to add separate settings for "create private/inner struct+method only" and "create private/inner struct+method, and a public struct for convenience".
| use crate::types::{GenerateMethod, RelationObjectType}; | ||
| use syn::Type; | ||
|
|
||
| #[test] |
There was a problem hiding this comment.
Non-blocking: would it be useful to add compile tests for the generated output, perhaps with trybuild?
There was a problem hiding this comment.
Hmm, that's worth considering. I originally thoguht about doing tests (in the proc macro crate) for parsing and expansion, but then I thought it would be easier to just ensure deepwell itself built rather than setting up a duplicate system for testing. But maybe trybuild is closer to what I want here.
If I do implement this, it would be in a follow-up PR.
0f86650 to
4fbd1a8
Compare
More readable.
4fbd1a8 to
c2be791
Compare
| } else { | ||
| quote! {} | ||
| }; |
There was a problem hiding this comment.
Does remove_fn => private_only currently leave the generated method without its input struct?
For PrivateWithoutStruct, generate_pub_struct is false, so this branch emits no Remove{Relation} struct. However, the generated remove_*_inner() method below still destructures Remove{Relation}. Should this mode generate a private removal struct, or should private_only not be supported for remove_fn?
| } | ||
|
|
||
| // Designate how the remove method should be generated | ||
| // This key is optional, default is "true". |
There was a problem hiding this comment.
should this say the default is public, with the example using remove_fn => public? The parser doesn't accept true anymore
Up to this point, we used a normal
macro_rules!macro to generate the code for relations. This had a few problems, such as it being cumbersome to disable generation of creation structs or methods, and the inability to excludemetadataas a field if it wasn't used for that relation (e.g. is()).With the new requirement to permit disabling
remove_*()method generation as well, I decided that the code was getting too complicated and it was time to use a procedural macro to generate the code instead. This allows for specifying a relation in a more readable syntax, as well as handling some niceties for us (such as doing thePascalCasetosnake_caseconversion for fields automatically).The arguments accepted are as follows:
With this done, I changed the site member and ban relations to add the audit logging, and did some refactoring to make the struct handling in
endpoints/consistent.Naturally, I also deleted the old macro definition.