Skip to content

Commit

Permalink
Codegen column_name proc_macro attribute (#433)
Browse files Browse the repository at this point in the history
* feat: codegen `column_name` proc_macro attribute

* test: codegen `column_name`
  • Loading branch information
billy1624 authored Feb 5, 2022
1 parent 3cde517 commit 21216f3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ impl Column {
format_ident!("{}", escape_rust_keyword(self.name.to_camel_case()))
}

pub fn is_snake_case_name(&self) -> bool {
self.name.to_snake_case() == self.name
}

pub fn get_rs_type(&self) -> TokenStream {
#[allow(unreachable_patterns)]
let ident: TokenStream = match &self.col_type {
Expand Down
25 changes: 20 additions & 5 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,22 @@ impl EntityWriter {
}

pub fn gen_column_enum(entity: &Entity) -> TokenStream {
let column_names_camel_case = entity.get_column_names_camel_case();
let column_variants = entity.columns.iter().map(|col| {
let variant = col.get_name_camel_case();
let mut variant = quote! { #variant };
if !col.is_snake_case_name() {
let column_name = &col.name;
variant = quote! {
#[sea_orm(column_name = #column_name)]
#variant
};
}
variant
});
quote! {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
#(#column_names_camel_case,)*
#(#column_variants,)*
}
}
}
Expand Down Expand Up @@ -474,6 +485,10 @@ impl EntityWriter {
.iter()
.map(|col| {
let mut attrs: Punctuated<_, Comma> = Punctuated::new();
if !col.is_snake_case_name() {
let column_name = &col.name;
attrs.push(quote! { column_name = #column_name });
}
if primary_keys.contains(&col.name) {
attrs.push(quote! { primary_key });
if !col.auto_increment {
Expand Down Expand Up @@ -724,14 +739,14 @@ mod tests {
unique: false,
},
Column {
name: "name".to_owned(),
name: "_name_".to_owned(),
col_type: ColumnType::String(Some(255)),
auto_increment: false,
not_null: true,
unique: false,
},
Column {
name: "fruit_id".to_owned(),
name: "fruitId".to_owned(),
col_type: ColumnType::Integer(Some(11)),
auto_increment: false,
not_null: false,
Expand All @@ -740,7 +755,7 @@ mod tests {
],
relations: vec![Relation {
ref_table: "fruit".to_owned(),
columns: vec!["fruit_id".to_owned()],
columns: vec!["fruitId".to_owned()],
ref_columns: vec!["id".to_owned()],
rel_type: RelationType::BelongsTo,
on_delete: None,
Expand Down
2 changes: 2 additions & 0 deletions sea-orm-codegen/tests/compact/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(column_name = "_name_")]
pub name: String,
#[sea_orm(column_name = "fruitId")]
pub fruit_id: Option<i32> ,
}

Expand Down
2 changes: 2 additions & 0 deletions sea-orm-codegen/tests/expanded/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
#[sea_orm(column_name = "_name_")]
Name,
#[sea_orm(column_name = "fruitId")]
FruitId,
}

Expand Down

0 comments on commit 21216f3

Please sign in to comment.