From cd232fffc2f281be5b9df535237931ad84a7b7c0 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 8 Nov 2022 16:11:38 +0800 Subject: [PATCH] [CLI] Fix generating Enum with variant starts with number --- sea-orm-codegen/src/entity/active_enum.rs | 65 ++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/sea-orm-codegen/src/entity/active_enum.rs b/sea-orm-codegen/src/entity/active_enum.rs index 8b4219824..de791752e 100644 --- a/sea-orm-codegen/src/entity/active_enum.rs +++ b/sea-orm-codegen/src/entity/active_enum.rs @@ -17,7 +17,7 @@ impl ActiveEnum { let enum_iden = format_ident!("{}", enum_name.to_camel_case()); let values: Vec = self.values.iter().map(|v| v.to_string()).collect(); let variants = values.iter().map(|v| v.trim()).map(|v| { - if v.chars().all(|c| c.is_numeric()) { + if v.chars().next().map(char::is_numeric).unwrap_or(false) { format_ident!("_{}", v) } else { format_ident!("{}", v.to_camel_case()) @@ -43,3 +43,66 @@ impl ActiveEnum { } } } + +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + use sea_query::{Alias, IntoIden}; + + #[test] + fn test_enum_variant_starts_with_number() { + assert_eq!( + ActiveEnum { + enum_name: Alias::new("media_type").into_iden(), + values: vec![ + "UNKNOWN", + "BITMAP", + "DRAWING", + "AUDIO", + "VIDEO", + "MULTIMEDIA", + "OFFICE", + "TEXT", + "EXECUTABLE", + "ARCHIVE", + "3D", + ] + .into_iter() + .map(|variant| Alias::new(variant).into_iden()) + .collect(), + } + .impl_active_enum(&WithSerde::None, true) + .to_string(), + quote!( + #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Copy)] + #[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "media_type")] + pub enum MediaType { + #[sea_orm(string_value = "UNKNOWN")] + Unknown, + #[sea_orm(string_value = "BITMAP")] + Bitmap, + #[sea_orm(string_value = "DRAWING")] + Drawing, + #[sea_orm(string_value = "AUDIO")] + Audio, + #[sea_orm(string_value = "VIDEO")] + Video, + #[sea_orm(string_value = "MULTIMEDIA")] + Multimedia, + #[sea_orm(string_value = "OFFICE")] + Office, + #[sea_orm(string_value = "TEXT")] + Text, + #[sea_orm(string_value = "EXECUTABLE")] + Executable, + #[sea_orm(string_value = "ARCHIVE")] + Archive, + #[sea_orm(string_value = "3D")] + _3D, + } + ) + .to_string() + ) + } +}