+
Skip to content

fix: include column_name in DeriveColumn FromStr impl #2603

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

Merged
Merged
Show file tree
Hide file tree
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
38 changes: 30 additions & 8 deletions sea-orm-macros/src/derives/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,36 @@ pub fn impl_col_from_str(ident: &Ident, data: &Data) -> syn::Result<TokenStream>
}
};

let columns = data_enum.variants.iter().map(|column| {
let column_iden = column.ident.clone();
let column_str_snake = column_iden.to_string().to_snake_case();
let column_str_mixed = column_iden.to_string().to_lower_camel_case();
quote!(
#column_str_snake | #column_str_mixed => Ok(#ident::#column_iden)
)
});
let columns = data_enum
.variants
.iter()
.map(|column| {
let column_iden = column.ident.clone();
let column_str_snake = column_iden.to_string().to_snake_case();
let column_str_mixed = column_iden.to_string().to_lower_camel_case();

let mut column_name = column_str_snake.clone();
for attr in column.attrs.iter() {
if !attr.path().is_ident("sea_orm") {
continue;
}
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("column_name") {
column_name = meta.value()?.parse::<LitStr>()?.value();
} else {
// Reads the value expression to advance the parse stream.
// Some parameters, such as `primary_key`, do not have any value,
// so ignoring an error occurred here.
let _: Option<Expr> = meta.value().and_then(|v| v.parse()).ok();
}
Ok(())
})?;
}
Ok::<TokenStream, syn::Error>(quote!(
#column_str_snake | #column_str_mixed | #column_name => Ok(#ident::#column_iden)
))
Comment on lines +81 to +100
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the attribute parsing from impl_default_as_str (above in column.rs).

In cases where #[sea_orm(column_name = "...")] is not found, this results in a redundant string match like:

"artist_id" | "artistId" | "artist_id" => Ok(Column::ArtistId),

I recognize that this is not very clean— it would be nicer to only include additional strings when they differ.
(I don't have the patience to think through that, so I invite help if we care about the redundancy. <3)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be fine as long as it doesnt trigger clippy warning

})
.collect::<Result<Vec<_>, _>>()?;

Ok(quote!(
#[automatically_derived]
Expand Down
6 changes: 6 additions & 0 deletions sea-orm-macros/tests/derive_entity_model_column_name_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use sea_orm::prelude::*;
use sea_orm::Iden;
use sea_orm::Iterable;
Expand Down Expand Up @@ -36,4 +38,8 @@ fn test_column_names() {
"ordersCount",
]
);

let col =
Column::from_str("lAsTnAmE").expect("column from str should recognize column_name attr");
assert!(matches!(col, Column::LastName))
}
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载