-
-
Notifications
You must be signed in to change notification settings - Fork 552
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
sea-orm-cli
entities generator panics when an item in EnumType consist entirely without any letters or numbers
#1819
Comments
sea-orm-cli
entities generator panics when an item in EnumType consist entirely of symbolssea-orm-cli
entities generator panics when an item in EnumType consist entirely without any letters or numbers
What's more, I changed the source code of
impl ActiveEnum {
pub fn impl_active_enum(&self, with_serde: &WithSerde, with_copy_enums: bool) -> TokenStream {
let enum_name = &self.enum_name.to_string();
let enum_iden = format_ident!("{}", enum_name.to_upper_camel_case());
let values: Vec<String> = self.values.iter().map(|v| v.to_string()).collect();
let variants = values.iter().map(|v| v.trim()).map(|v| {
if v.chars().next().map(char::is_numeric).unwrap_or(false) {
format_ident!("_{}", v)
} else {
let new_name = v.to_upper_camel_case();
if new_name.is_empty() {
// We can't find numbers or letters in the variant name.
let mut utf_string = String::new();
for c in v.chars() {
utf_string.push('U');
utf_string.push_str(&format!("{:04X}", c as u32));
}
format_ident!("{}", utf_string)
} else {
format_ident!("{}", new_name)
}
}
});
let extra_derive = with_serde.extra_derive();
let copy_derive = if with_copy_enums {
quote! { , Copy }
} else {
quote! {}
};
quote! {
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum #copy_derive #extra_derive)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = #enum_name)]
pub enum #enum_iden {
#(
#[sea_orm(string_value = #values)]
#variants,
)*
}
}
}
} |
For sure! A PR on better error messages and enum handling would be appreciated. |
Cinea4678
added a commit
to Cinea4678/sea-orm
that referenced
this issue
Aug 22, 2023
tyt2y3
pushed a commit
that referenced
this issue
Jan 19, 2024
anshap1719
pushed a commit
to anshap1719/sea-orm
that referenced
this issue
Feb 7, 2024
* Fix problems in SeaQL#1819 * Add test cases --------- Co-authored-by: Billy Chan <ccw.billy.123@gmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
When I use this enum type in MySQL:
The
sea-orm-cli
entities generator will panic because it cannot convert '/' into an enum item name in Rust. That's just fine, but what troubles me is thesea-orm-cli
didn't give a clear explanation for this panic. It just shows this:this is not really a bug itself, but we could probably provide users with a clearer error message or do some extra handling to accommodate these values. And if you'd be willing to first introduce a friendly error message as a transitional step before proposing other better approaches later, I'd be happy to provide a pull request for that.
Code related are mainly in this file:
sea-orm-codegen/src/entity/active_enum.rs:23
Steps to Reproduce
sea-orm-cli
to generate entityExpected Behavior
A friendly panic message is given, maybe saying 'We currently do not support enum items consisting entirely without any letters or numbers.', or just process this situation fine.
Actual Behavior
A confusing error message unrelated to the specific cause was given.
Reproduces How Often
Always
Workarounds
this is fine :)
Reproducible Example
Versions
The text was updated successfully, but these errors were encountered: