Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable convert from ActiveModel to Model #725

Merged
merged 5 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions sea-orm-macros/src/derives/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use syn::{punctuated::Punctuated, token::Comma, Data, DataStruct, Field, Fields,

/// Method to derive an [ActiveModel](sea_orm::ActiveModel)
pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<TokenStream> {
let fields = match data {
// including ignored fields
let all_fields = match data {
Data::Struct(DataStruct {
fields: Fields::Named(named),
..
Expand All @@ -17,8 +18,15 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
})
}
}
.into_iter()
.filter(field_not_ignored);
.into_iter();

let fields = all_fields.clone().filter(field_not_ignored);

let all_field: Vec<Ident> = all_fields
.clone()
.into_iter()
.map(|Field { ident, .. }| format_ident!("{}", ident.unwrap().to_string()))
.collect();

let field: Vec<Ident> = fields
.clone()
Expand Down Expand Up @@ -63,6 +71,27 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token

let ty: Vec<Type> = fields.into_iter().map(|Field { ty, .. }| ty).collect();

let ignore_attr: Vec<bool> = all_fields
.clone()
.map(|field| !field_not_ignored(&field))
.collect();

let field_value: Vec<TokenStream> = all_field
.iter()
.zip(ignore_attr)
.map(|(field, ignore)| {
if ignore {
quote! {
Default::default()
}
} else {
quote! {
a.#field.into_value().unwrap().unwrap()
}
}
})
.collect();

Ok(quote!(
#[derive(Clone, Debug, PartialEq)]
pub struct ActiveModel {
Expand Down Expand Up @@ -92,6 +121,28 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
}
}

#[automatically_derived]
impl std::convert::TryFrom<ActiveModel> for <Entity as EntityTrait>::Model {
type Error = DbErr;
fn try_from(a: ActiveModel) -> Result<Self, DbErr> {
#(if matches!(a.#field, sea_orm::ActiveValue::NotSet) {
return Err(DbErr::Custom(format!("field {} is NotSet", stringify!(#field))));
})*
Ok(
Self {
#(#all_field: #field_value),*
}
)
}
}

#[automatically_derived]
impl sea_orm::TryIntoModel<<Entity as EntityTrait>::Model> for ActiveModel {
fn try_into_model(self) -> Result<<Entity as EntityTrait>::Model, DbErr> {
self.try_into()
}
}

#[automatically_derived]
impl sea_orm::ActiveModelTrait for ActiveModel {
type Entity = Entity;
Expand Down
Loading