diff --git a/sea-orm-macros/src/derives/model.rs b/sea-orm-macros/src/derives/model.rs index cb56b7ac..fbb3d22c 100644 --- a/sea-orm-macros/src/derives/model.rs +++ b/sea-orm-macros/src/derives/model.rs @@ -1,7 +1,7 @@ use heck::CamelCase; use proc_macro2::{Ident, TokenStream}; use quote::{format_ident, quote, quote_spanned}; -use syn::{Data, DataStruct, Field, Fields}; +use syn::{Data, DataStruct, Field, Fields, Type}; pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result { let fields = match data { @@ -23,10 +23,16 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result .collect(); let name: Vec = fields + .clone() .into_iter() .map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string().to_camel_case())) .collect(); + let ty: Vec = fields + .into_iter() + .map(|Field { ty, .. }| ty) + .collect(); + Ok(quote!( impl sea_orm::ModelTrait for #ident { type Column = Column; @@ -49,5 +55,46 @@ pub fn expend_derive_model(ident: Ident, data: Data) -> syn::Result }) } } + + #[derive(Clone, Debug)] + pub struct ActiveModel { + #(pub #field: sea_orm::Action<#ty>),* + } + + impl sea_orm::ActiveModelOf<#ident> for ActiveModel { + fn from_model(m: #ident) -> Self { + Self::from(m) + } + } + + impl From<#ident> for ActiveModel { + fn from(m: #ident) -> Self { + Self { + #(#field: sea_orm::Action::Set(m.#field)),* + } + } + } + + impl sea_orm::ActiveModelTrait for ActiveModel { + type Column = Column; + + fn get(&self, c: Self::Column) -> sea_orm::Action { + match c { + #(Self::Column::#name => self.#field.clone().into_action_value()),* + } + } + + fn set(&mut self, c: Self::Column, v: sea_orm::Value) { + match c { + #(Self::Column::#name => self.#field = sea_orm::Action::Set(v.unwrap())),* + } + } + + fn unset(&mut self, c: Self::Column) { + match c { + #(Self::Column::#name => self.#field = sea_orm::Action::Unset),* + } + } + } )) } diff --git a/src/tests_cfg/cake.rs b/src/tests_cfg/cake.rs index 46a14765..d61c13ee 100644 --- a/src/tests_cfg/cake.rs +++ b/src/tests_cfg/cake.rs @@ -11,13 +11,6 @@ pub struct Model { pub name: String, } -// can we generate this? -#[derive(Clone, Debug)] -pub struct ActiveModel { - pub id: Action, - pub name: Action, -} - #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] pub enum Column { Id, @@ -81,46 +74,3 @@ impl Model { Entity::find_related().belongs_to::(self) } } - -// can we generate this? -impl ActiveModelOf for ActiveModel { - fn from_model(m: Model) -> Self { - Self::from(m) - } -} - -// can we generate this? -impl From for ActiveModel { - fn from(m: Model) -> Self { - Self { - id: Action::Set(m.id), - name: Action::Set(m.name), - } - } -} - -// can we generate this? -impl ActiveModelTrait for ActiveModel { - type Column = Column; - - fn get(&self, c: Self::Column) -> Action { - match c { - Column::Id => self.id.clone().into_action_value(), - Column::Name => self.name.clone().into_action_value(), - } - } - - fn set(&mut self, c: Self::Column, v: Value) { - match c { - Column::Id => self.id = Action::Set(v.unwrap()), - Column::Name => self.name = Action::Set(v.unwrap()), - } - } - - fn unset(&mut self, c: Self::Column) { - match c { - Column::Id => self.id = Action::Unset, - Column::Name => self.name = Action::Unset, - } - } -} \ No newline at end of file