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

Casting columns as a different data type on select, insert and update #1304

Merged
merged 10 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
53 changes: 53 additions & 0 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
// generate Column enum and it's ColumnTrait impl
let mut columns_enum: Punctuated<_, Comma> = Punctuated::new();
let mut columns_trait: Punctuated<_, Comma> = Punctuated::new();
let mut cast_selects: Punctuated<_, Comma> = Punctuated::new();
let mut cast_values: Punctuated<_, Comma> = Punctuated::new();
let mut primary_keys: Punctuated<_, Comma> = Punctuated::new();
let mut primary_key_types: Punctuated<_, Comma> = Punctuated::new();
let mut auto_increment = true;
Expand Down Expand Up @@ -90,6 +92,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
let mut nullable = false;
let mut default_value = None;
let mut default_expr = None;
let mut cast_select = None;
let mut cast_value = None;
let mut indexed = false;
let mut ignore = false;
let mut unique = false;
Expand Down Expand Up @@ -169,6 +173,24 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
format!("Invalid enum_name {:?}", nv.lit),
));
}
} else if name == "cast_select" {
if let Lit::Str(litstr) = &nv.lit {
cast_select = Some(litstr.value());
} else {
return Err(Error::new(
field.span(),
format!("Invalid cast_select {:?}", nv.lit),
));
}
} else if name == "cast_value" {
if let Lit::Str(litstr) = &nv.lit {
cast_value = Some(litstr.value());
} else {
return Err(Error::new(
field.span(),
format!("Invalid cast_value {:?}", nv.lit),
));
}
}
}
}
Expand Down Expand Up @@ -224,6 +246,23 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
});
}

if let Some(cast_select) = cast_select {
cast_selects.push(quote! {
Self::#field_name => sea_orm::sea_query::SimpleExpr::cast_as(
Into::<sea_orm::sea_query::SimpleExpr>::into(expr),
sea_orm::sea_query::Alias::new(&#cast_select),
),
});
}
if let Some(cast_value) = cast_value {
cast_values.push(quote! {
Self::#field_name => sea_orm::sea_query::SimpleExpr::cast_as(
Into::<sea_orm::sea_query::SimpleExpr>::into(val),
sea_orm::sea_query::Alias::new(&#cast_value),
),
});
}

let field_type = &field.ty;
let field_type = quote! { #field_type }
.to_string() //E.g.: "Option < String >"
Expand Down Expand Up @@ -347,6 +386,20 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
#columns_trait
}
}

fn cast_select(&self, expr: sea_orm::sea_query::Expr) -> sea_orm::sea_query::SimpleExpr {
match self {
#cast_selects
_ => sea_orm::prelude::ColumnTrait::cast_select_enum(self, expr),
}
}

fn cast_value(&self, val: sea_orm::sea_query::Expr) -> sea_orm::sea_query::SimpleExpr {
match self {
#cast_values
_ => sea_orm::prelude::ColumnTrait::cast_value_enum(self, val),
}
}
}

#entity_def
Expand Down
Loading