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

DerivePartialModel: Allow specification of Entity via a Path in addition to as an Ident #2111

Closed
Closed
Changes from all 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
65 changes: 45 additions & 20 deletions sea-orm-macros/src/derives/partial_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use self::util::GetAsKVMeta;
#[derive(Debug)]
enum Error {
InputNotStruct,
EntityNotSpecific,
EntityNotSpecified,
NotSupportGeneric(Span),
BothFromColAndFromExpr(Span),
Syn(syn::Error),
Expand All @@ -32,7 +32,7 @@ enum ColumnAs {
}

struct DerivePartialModel {
entity_ident: Option<syn::Ident>,
entity_path: Option<syn::Path>,
ident: syn::Ident,
fields: Vec<ColumnAs>,
}
Expand All @@ -54,7 +54,7 @@ impl DerivePartialModel {
return Err(Error::InputNotStruct);
};

let mut entity_ident = None;
let mut entity_path = None;

for attr in input.attrs.iter() {
if !attr.path().is_ident("sea_orm") {
Expand All @@ -63,9 +63,9 @@ impl DerivePartialModel {

if let Ok(list) = attr.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated) {
for meta in list {
entity_ident = meta
entity_path = meta
.get_as_kv("entity")
.map(|s| syn::parse_str::<syn::Ident>(&s).map_err(Error::Syn))
.map(|s| syn::parse_str::<syn::Path>(&s).map_err(Error::Syn))
.transpose()?;
}
}
Expand Down Expand Up @@ -102,8 +102,8 @@ impl DerivePartialModel {

let col_as = match (from_col, from_expr) {
(None, None) => {
if entity_ident.is_none() {
return Err(Error::EntityNotSpecific);
if entity_path.is_none() {
return Err(Error::EntityNotSpecified);
}
ColumnAs::Col(format_ident!(
"{}",
Expand All @@ -115,8 +115,8 @@ impl DerivePartialModel {
field_name: field_name.to_string(),
},
(Some(col), None) => {
if entity_ident.is_none() {
return Err(Error::EntityNotSpecific);
if entity_path.is_none() {
return Err(Error::EntityNotSpecified);
}

let field = field_name.to_string();
Expand All @@ -128,7 +128,7 @@ impl DerivePartialModel {
}

Ok(Self {
entity_ident,
entity_path,
ident: input.ident,
fields: column_as_list,
})
Expand All @@ -141,18 +141,18 @@ impl DerivePartialModel {
fn impl_partial_model_trait(&self) -> TokenStream {
let select_ident = format_ident!("select");
let DerivePartialModel {
entity_ident,
entity_path,
ident,
fields,
} = self;
let select_col_code_gen = fields.iter().map(|col_as| match col_as {
ColumnAs::Col(ident) => {
let entity = entity_ident.as_ref().unwrap();
let entity = entity_path.as_ref().unwrap();
let col_value = quote!( <#entity as sea_orm::EntityTrait>::Column:: #ident);
quote!(let #select_ident = sea_orm::SelectColumns::select_column(#select_ident, #col_value);)
},
ColumnAs::ColAlias { col, field } => {
let entity = entity_ident.as_ref().unwrap();
let entity = entity_path.as_ref().unwrap();
let col_value = quote!( <#entity as sea_orm::EntityTrait>::Column:: #col);
quote!(let #select_ident = sea_orm::SelectColumns::select_column_as(#select_ident, #col_value, #field);)
},
Expand Down Expand Up @@ -184,8 +184,8 @@ pub fn expand_derive_partial_model(input: syn::DeriveInput) -> syn::Result<Token
Err(Error::BothFromColAndFromExpr(span)) => Ok(quote_spanned! {
span => compile_error!("you can only use one of `from_col` or `from_expr`");
}),
Err(Error::EntityNotSpecific) => Ok(quote_spanned! {
ident_span => compile_error!("you need specific which entity you are using")
Err(Error::EntityNotSpecified) => Ok(quote_spanned! {
ident_span => compile_error!("you need to specify which entity you are using")
}),
Err(Error::InputNotStruct) => Ok(quote_spanned! {
ident_span => compile_error!("you can only derive `DerivePartialModel` on named struct");
Expand Down Expand Up @@ -234,11 +234,12 @@ mod test {

use super::DerivePartialModel;

#[cfg(test)]
type StdResult<T> = Result<T, Box<dyn std::error::Error>>;

#[cfg(test)]
const CODE_SNIPPET: &str = r#"
#[test]
fn test_load_macro_input() -> StdResult<()> {
const CODE_SNIPPET: &str = r#"
#[sea_orm(entity = "Entity")]
struct PartialModel{
default_field: i32,
Expand All @@ -248,13 +249,14 @@ struct PartialModel{
expr_field : i32
}
"#;
#[test]
fn test_load_macro_input() -> StdResult<()> {
let input = syn::parse_str::<DeriveInput>(CODE_SNIPPET)?;

let middle = DerivePartialModel::new(input).unwrap();

assert_eq!(middle.entity_ident, Some(format_ident!("Entity")));
let entity_ident = middle.entity_path.unwrap();
assert_eq!(entity_ident.segments.len(), 1);
assert_eq!(entity_ident.segments[0].ident, format_ident!("Entity"));

assert_eq!(middle.ident, format_ident!("PartialModel"));
assert_eq!(middle.fields.len(), 3);
assert_eq!(
Expand All @@ -278,4 +280,27 @@ struct PartialModel{

Ok(())
}

#[cfg(test)]
#[test]
fn test_load_macro_input_entity_path() -> StdResult<()> {
const CODE_SNIPPET: &str = r#"
#[sea_orm(entity = "test::Entity")]
struct PartialModel {}
"#;

let input = syn::parse_str::<DeriveInput>(CODE_SNIPPET)?;

let middle = DerivePartialModel::new(input).unwrap();

let entity_ident = middle.entity_path.unwrap();
assert_eq!(entity_ident.segments.len(), 2);
assert_eq!(entity_ident.segments[0].ident, format_ident!("test"));
assert_eq!(entity_ident.segments[1].ident, format_ident!("Entity"));

assert_eq!(middle.ident, format_ident!("PartialModel"));
assert!(middle.fields.is_empty());

Ok(())
}
}