DeriveEntityModel automatically derive DeriveModel and DeriveActiveModel

This commit is contained in:
Billy Chan 2021-09-08 22:02:24 +08:00
parent c0a77c1298
commit 54bb358cca
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
4 changed files with 12 additions and 7 deletions

View File

@ -1,6 +1,6 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "cake")] #[sea_orm(table_name = "cake")]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]

View File

@ -16,6 +16,7 @@ pub fn derive_entity(input: TokenStream) -> TokenStream {
#[proc_macro_derive(DeriveEntityModel, attributes(sea_orm))] #[proc_macro_derive(DeriveEntityModel, attributes(sea_orm))]
pub fn derive_entity_model(input: TokenStream) -> TokenStream { pub fn derive_entity_model(input: TokenStream) -> TokenStream {
let input_ts = input.clone();
let DeriveInput { let DeriveInput {
ident, data, attrs, .. ident, data, attrs, ..
} = parse_macro_input!(input as DeriveInput); } = parse_macro_input!(input as DeriveInput);
@ -24,10 +25,14 @@ pub fn derive_entity_model(input: TokenStream) -> TokenStream {
panic!("Struct name must be Model"); panic!("Struct name must be Model");
} }
match derives::expand_derive_entity_model(data, attrs) { let mut ts: TokenStream = derives::expand_derive_entity_model(data, attrs)
Ok(ts) => ts.into(), .unwrap_or_else(Error::into_compile_error)
Err(e) => e.to_compile_error().into(), .into();
} ts.extend(vec![
derive_model(input_ts.clone()),
derive_active_model(input_ts),
]);
ts
} }
#[proc_macro_derive(DerivePrimaryKey)] #[proc_macro_derive(DerivePrimaryKey)]

View File

@ -1,7 +1,7 @@
use crate as sea_orm; use crate as sea_orm;
use crate::entity::prelude::*; use crate::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "cake")] #[sea_orm(table_name = "cake")]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]

View File

@ -1,7 +1,7 @@
use crate as sea_orm; use crate as sea_orm;
use crate::entity::prelude::*; use crate::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "fruit")] #[sea_orm(table_name = "fruit")]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]