sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

Throw compile errors when unexpected proc_macros attributes are provided

Open billy1624 opened this issue 3 years ago • 5 comments

Related to #391

billy1624 avatar Dec 21 '21 04:12 billy1624

Create a whitelist for all supported #[sea_orm(xxx)] proc_macros attributes. Thrown compile errors when it parse unknown attributes.

billy1624 avatar Jul 12 '22 04:07 billy1624

I was researching this issue and I believe there should be something like

#[proc_macro_attribute]
pub fn sea_orm(attr: TokenStream, item: TokenStream) -> TokenStream {

where all the attributes would pass from like column_name and table_name but I was unable to find this code in repo.

Is it because we are using proc-macro2 and serde libraries? Please guide me.

Thanks.

Diwakar-Gupta avatar Jan 19 '23 12:01 Diwakar-Gupta

Hey @Diwakar-Gupta, thanks for the investigation!!

First, every procedural macros is self contained in a .rs file under the sea-orm-macros/src/derives folder.

For example, DeriveActiveEnum is defined in a file below:

https://github.com/SeaQL/sea-orm/blob/9f6b5664eba59c5eafeb6ce91e8946af47983fdd/sea-orm-macros/src/derives/active_enum.rs#L12-L186

Each procedural macros starts by parsing the inputting TokenStream manually. Yes, it's done manually and it's prone to mistake. And some old procedural macros don't even has a explicite parsing stage.

Ideally we want each procedural macro should be testable. With that in mind we better implement it in two stages:

  1. Parsing: take the input TokenStream into a struct that contains all necessary data for the next code generating stage.
  2. Code Generating: take a data struct from the parsing stage and from that we can use quote! macro to generate necessary implementations.

Then, we can test the parsing and code generating parts separately and thoroughly.

We already has the code generating inplace, we just need to rewrite all of them. I think https://github.com/TedDriggs/darling could helps us do the parsing.

Does that make sense to you?

billy1624 avatar Jan 19 '23 13:01 billy1624

Thanks for the explanation @billy1624

What I understood is each derive/* receives DeriveInput from which it extracts necessary attributes and values in enum/struct called Parsing then it is passed for code addon using quote! called code generation.

This parsing step can be replaced with use of darling as you mentioned.

We wanted to test both parsing and code generation step's separately for each derive/*.

Create a whitelist for all supported #[sea_orm(xxx)] proc_macros attributes. Thrown compile errors when it parse unknown attributes.

and this too right.

Diwakar-Gupta avatar Jan 19 '23 15:01 Diwakar-Gupta

Sounds like we have a plan now :)

billy1624 avatar Jan 19 '23 15:01 billy1624