diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f73d738..11397534 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,30 @@ fn get_arity_of() -> usize { } ``` * Associate `ActiveModel` to `EntityTrait` https://github.com/SeaQL/sea-orm/pull/2186 +* [sea-orm-macros] Added `rename_all` attribute to `DeriveEntityModel` & `DeriveActiveEnum` https://github.com/SeaQL/sea-orm/pull/2170 +```rust +#[derive(DeriveEntityModel)] +#[sea_orm(table_name = "user", rename_all = "camelCase")] +pub struct Model { + #[sea_orm(primary_key)] + id: i32, + first_name: String, // firstName + #[sea_orm(column_name = "lAsTnAmE")] + last_name: String, // lAsTnAmE +} +#[derive(EnumIter, DeriveActiveEnum)] +#[sea_orm(rs_type = "String", db_type = "String(StringLen::None)", rename_all = "camelCase")] +pub enum TestEnum { + DefaultVariant, // defaultVariant + #[sea_orm(rename = "kebab-case")] + VariantKebabCase, // variant-kebab-case + #[sea_orm(rename = "snake_case")] + VariantSnakeCase, // variant_snake_case + #[sea_orm(string_value = "CuStOmStRiNgVaLuE")] + CustomStringValue, // CuStOmStRiNgVaLuE +} +``` ### Enhancements * Added `ActiveValue::set_if_not_equals()` https://github.com/SeaQL/sea-orm/pull/2194 @@ -562,7 +585,7 @@ assert!(matches!(res, Ok(TryInsertResult::Conflicted))); * Fixed `DeriveActiveEnum` throwing errors because `string_value` consists non-UAX#31 compliant characters https://github.com/SeaQL/sea-orm/pull/1374 ```rust -#[derive(DeriveActiveEnum)] +#[derive(EnumIter, DeriveActiveEnum)] #[sea_orm(rs_type = "String", db_type = "String(None)")] pub enum StringValue { #[sea_orm(string_value = "")] diff --git a/sea-orm-macros/src/derives/active_enum.rs b/sea-orm-macros/src/derives/active_enum.rs index 6cca1c95..0e2698c6 100644 --- a/sea-orm-macros/src/derives/active_enum.rs +++ b/sea-orm-macros/src/derives/active_enum.rs @@ -115,6 +115,7 @@ impl ActiveEnum { // to be considered unknown attribute parameter meta.value()?.parse::()?; } else if meta.path.is_ident("rename") { + is_string = true; rename_rule = Some((&meta).try_into()?); } else { return Err(meta.error(format!( @@ -228,7 +229,6 @@ impl ActiveEnum { quote! { #num_value } } else if let Some(rename_rule) = variant.rename.or(*rename_all) { let variant_ident = variant.ident.convert_case(Some(rename_rule)); - quote! { #variant_ident } } else { quote_spanned! { diff --git a/sea-orm-macros/tests/derive_active_enum_test.rs b/sea-orm-macros/tests/derive_active_enum_test.rs index 806d0cc8..bfa69495 100644 --- a/sea-orm-macros/tests/derive_active_enum_test.rs +++ b/sea-orm-macros/tests/derive_active_enum_test.rs @@ -1,4 +1,4 @@ -use sea_orm::ActiveEnum; +use sea_orm::{entity::prelude::StringLen, ActiveEnum}; use sea_orm_macros::{DeriveActiveEnum, EnumIter}; #[derive(Debug, EnumIter, DeriveActiveEnum, Eq, PartialEq)] @@ -34,6 +34,18 @@ enum TestEnum { CustomStringValue, } +#[derive(Debug, EnumIter, DeriveActiveEnum, Eq, PartialEq)] +#[sea_orm( + rs_type = "String", + db_type = "String(StringLen::None)", + rename_all = "snake_case" +)] +pub enum TestEnum2 { + HelloWorld, + #[sea_orm(rename = "camelCase")] + HelloWorldTwo, +} + #[test] fn derive_active_enum_value() { assert_eq!(TestEnum::DefaultVariant.to_value(), "defaultVariant"); @@ -107,3 +119,9 @@ fn derive_active_enum_from_value() { Ok(TestEnum::CustomStringValue) ); } + +#[test] +fn derive_active_enum_value_2() { + assert_eq!(TestEnum2::HelloWorld.to_value(), "hello_world"); + assert_eq!(TestEnum2::HelloWorldTwo.to_value(), "helloWorldTwo"); +}