From 4f84c0971c8001e67e7f859fa5c88d21936b86b6 Mon Sep 17 00:00:00 2001 From: ForzenString <964413011@qq.com> Date: Sun, 3 Mar 2024 16:23:45 +0800 Subject: [PATCH 1/3] :bug: fix macro attr `entity` only accept `Ident` --- sea-orm-macros/src/derives/partial_model.rs | 24 ++++++++++----------- tests/partial_model_tests.rs | 11 +++++++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/sea-orm-macros/src/derives/partial_model.rs b/sea-orm-macros/src/derives/partial_model.rs index 79ad9ea698..1f95c40f5d 100644 --- a/sea-orm-macros/src/derives/partial_model.rs +++ b/sea-orm-macros/src/derives/partial_model.rs @@ -27,12 +27,12 @@ enum ColumnAs { Col(syn::Ident), /// alias from a column in model ColAlias { col: syn::Ident, field: String }, - /// from a expr + /// from an expr Expr { expr: syn::Expr, field_name: String }, } struct DerivePartialModel { - entity_ident: Option, + entity: Option, ident: syn::Ident, fields: Vec, } @@ -54,7 +54,7 @@ impl DerivePartialModel { return Err(Error::InputNotStruct); }; - let mut entity_ident = None; + let mut entity = None; for attr in input.attrs.iter() { if !attr.path().is_ident("sea_orm") { @@ -63,9 +63,9 @@ impl DerivePartialModel { if let Ok(list) = attr.parse_args_with(Punctuated::::parse_terminated) { for meta in list { - entity_ident = meta + entity = meta .get_as_kv("entity") - .map(|s| syn::parse_str::(&s).map_err(Error::Syn)) + .map(|s| syn::parse_str::(&s).map_err(Error::Syn)) .transpose()?; } } @@ -102,7 +102,7 @@ impl DerivePartialModel { let col_as = match (from_col, from_expr) { (None, None) => { - if entity_ident.is_none() { + if entity.is_none() { return Err(Error::EntityNotSpecific); } ColumnAs::Col(format_ident!( @@ -115,7 +115,7 @@ impl DerivePartialModel { field_name: field_name.to_string(), }, (Some(col), None) => { - if entity_ident.is_none() { + if entity.is_none() { return Err(Error::EntityNotSpecific); } @@ -128,7 +128,7 @@ impl DerivePartialModel { } Ok(Self { - entity_ident, + entity, ident: input.ident, fields: column_as_list, }) @@ -141,18 +141,18 @@ impl DerivePartialModel { fn impl_partial_model_trait(&self) -> TokenStream { let select_ident = format_ident!("select"); let DerivePartialModel { - entity_ident, + entity, 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.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.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);) }, @@ -254,7 +254,7 @@ struct PartialModel{ let middle = DerivePartialModel::new(input).unwrap(); - assert_eq!(middle.entity_ident, Some(format_ident!("Entity"))); + assert_eq!(middle.entity, Some(format_ident!("Entity"))); assert_eq!(middle.ident, format_ident!("PartialModel")); assert_eq!(middle.fields.len(), 3); assert_eq!( diff --git a/tests/partial_model_tests.rs b/tests/partial_model_tests.rs index a9350045d1..7a83a01c61 100644 --- a/tests/partial_model_tests.rs +++ b/tests/partial_model_tests.rs @@ -1,5 +1,5 @@ use entity::{Column, Entity}; -use sea_orm::{ColumnTrait, DerivePartialModel, FromQueryResult}; +use sea_orm::{ColumnTrait, DerivePartialModel, EntityTrait, FromQueryResult, ModelTrait}; use sea_query::Expr; mod entity { @@ -29,6 +29,15 @@ struct SimpleTest { _bar: String, } + +#[derive(FromQueryResult, DerivePartialModel)] +#[sea_orm(entity = "::Entity")] +struct EntityNameNotAIdent { + #[sea_orm(from_col = "foo2")] + _foo: i32, + #[sea_orm(from_col = "bar2")] + _bar: String, +} #[derive(FromQueryResult, DerivePartialModel)] #[sea_orm(entity = "Entity")] struct FieldFromDiffNameColumnTest { From 89fbb63a7ccd84accf712050cae2e1d56a1b9060 Mon Sep 17 00:00:00 2001 From: ForzenString <964413011@qq.com> Date: Sun, 3 Mar 2024 16:25:03 +0800 Subject: [PATCH 2/3] :art: cargo fmt --- tests/partial_model_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/partial_model_tests.rs b/tests/partial_model_tests.rs index 7a83a01c61..10407c22eb 100644 --- a/tests/partial_model_tests.rs +++ b/tests/partial_model_tests.rs @@ -29,7 +29,6 @@ struct SimpleTest { _bar: String, } - #[derive(FromQueryResult, DerivePartialModel)] #[sea_orm(entity = "::Entity")] struct EntityNameNotAIdent { @@ -38,6 +37,7 @@ struct EntityNameNotAIdent { #[sea_orm(from_col = "bar2")] _bar: String, } + #[derive(FromQueryResult, DerivePartialModel)] #[sea_orm(entity = "Entity")] struct FieldFromDiffNameColumnTest { From 620381cf23b8244f3fde732b2c7cc5df96b06e4d Mon Sep 17 00:00:00 2001 From: ForzenString <964413011@qq.com> Date: Sun, 3 Mar 2024 16:41:12 +0800 Subject: [PATCH 3/3] :white_check_mark: fix test --- sea-orm-macros/src/derives/partial_model.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sea-orm-macros/src/derives/partial_model.rs b/sea-orm-macros/src/derives/partial_model.rs index 1f95c40f5d..0413fc86bb 100644 --- a/sea-orm-macros/src/derives/partial_model.rs +++ b/sea-orm-macros/src/derives/partial_model.rs @@ -228,7 +228,7 @@ mod util { #[cfg(test)] mod test { use quote::format_ident; - use syn::DeriveInput; + use syn::{parse_str, DeriveInput, Type}; use crate::derives::partial_model::ColumnAs; @@ -250,11 +250,10 @@ struct PartialModel{ "#; #[test] fn test_load_macro_input() -> StdResult<()> { - let input = syn::parse_str::(CODE_SNIPPET)?; + let input = parse_str::(CODE_SNIPPET)?; let middle = DerivePartialModel::new(input).unwrap(); - - assert_eq!(middle.entity, Some(format_ident!("Entity"))); + assert_eq!(middle.entity, Some(parse_str::("Entity").unwrap())); assert_eq!(middle.ident, format_ident!("PartialModel")); assert_eq!(middle.fields.len(), 3); assert_eq!(