Skip to content
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

Closed
Cinea4678 opened this issue Aug 22, 2023 · 2 comments · Fixed by #1821

Comments

@Cinea4678
Copy link
Contributor

Cinea4678 commented Aug 22, 2023

Description

When I use this enum type in MySQL:

ENUM('Question','QuestionsAdditional','Answer','Other','/')

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 the sea-orm-cli didn't give a clear explanation for this panic. It just shows this:

thread 'main' panicked at 'Ident is not allowed to be empty; use Option<Ident>', <HomePath>\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\proc-macro2-1.0.66\src\fallback.rs:752:9

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

  1. Setup a MySQL server, and run this:
CREATE TABLE `test` (
 `enum_column` enum('A','B','/') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  1. Try to use sea-orm-cli to generate entity

Expected 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 :)

CREATE TABLE `test` (
 `enum_column` enum('A','B','/') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

Reproducible Example

Versions

image

@Cinea4678 Cinea4678 changed the title sea-orm-cli entities generator panics when an item in EnumType consist entirely of symbols sea-orm-cli entities generator panics when an item in EnumType consist entirely without any letters or numbers Aug 22, 2023
@Cinea4678
Copy link
Contributor Author

What's more, I changed the source code of sea-orm-codegen to solve my problem, and it works fine. If you think this is ok, I can run the test and make a pull request.

sea-orm-codegen/src/entity/active_enum.rs

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,
                )*
            }
        }
    }
}

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 22, 2023

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
@Cinea4678 Cinea4678 mentioned this issue Aug 22, 2023
4 tasks
tyt2y3 pushed a commit that referenced this issue Jan 19, 2024
* Fix problems in #1819

* Add test cases

---------

Co-authored-by: Billy Chan <ccw.billy.123@gmail.com>
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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants