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

CLI generate enum with numeric like variants #588

Merged
merged 1 commit into from
Jun 30, 2022

Conversation

billy1624
Copy link
Member

PR Info

Fixes

  • When generating ActiveEnum with number like variants. Those variants can't be formatted as Rust identifier. This can be fixed by add an _ before the number like variants.

The SQL of Enum type.

create type tea as enum ('1', '2');

The generated ActiveEnum

//! SeaORM Entity. Generated by sea-orm-codegen 0.6.0

use sea_orm::entity::prelude::*;

#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "tea")]
pub enum Tea {
    #[sea_orm(string_value = "1")]
    _1,
    #[sea_orm(string_value = "2")]
    _2,
}

@billy1624 billy1624 self-assigned this Mar 8, 2022
@billy1624 billy1624 requested a review from tyt2y3 March 8, 2022 09:55
@billy1624
Copy link
Member Author

billy1624 commented Mar 8, 2022

Hey @yb3616, you can try it by cloning the branch. Then, cd into sea-orm/sea-orm-cli folder. Finally, run the sea-orm-cli with the following command

cargo run -- generate entity --database-url postgres://root:root@localhost/active_enum_tests -o output_folder

@billy1624 billy1624 marked this pull request as ready for review March 8, 2022 09:58
@tyt2y3
Copy link
Member

tyt2y3 commented Mar 22, 2022

@yb3616 what is your use case for this? Isn't it strange to use a enum with numeric variants?

.iter()
.map(|v| format_ident!("{}", v.to_camel_case()));
let variants = self.values.iter().map(|v| v.trim()).map(|v| {
if v.chars().all(|c| c.is_numeric()) {
Copy link
Member

Choose a reason for hiding this comment

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

#1172 may be 'starts_with numeric'?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed on #1211

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
2 participants