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

Struct / enum derive PartialEq should also derive Eq #988

Merged
merged 4 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions sea-orm-codegen/src/entity/active_enum.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::WithSerde;
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::WithSerde;

#[derive(Clone, Debug)]
pub struct ActiveEnum {
pub(crate) enum_name: String,
Expand Down Expand Up @@ -30,7 +31,7 @@ impl ActiveEnum {
};

quote! {
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum #copy_derive #extra_derive)]
#[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 {
#(
Expand Down
29 changes: 27 additions & 2 deletions sea-orm-codegen/src/entity/base_entity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{Column, ConjunctRelation, DateTimeCrate, PrimaryKey, Relation};
use heck::{CamelCase, SnakeCase};
use proc_macro2::{Ident, TokenStream};
use quote::format_ident;
use quote::quote;
use sea_query::ColumnType;

use crate::{Column, ConjunctRelation, DateTimeCrate, PrimaryKey, Relation};

#[derive(Clone, Debug)]
pub struct Entity {
Expand Down Expand Up @@ -145,14 +148,29 @@ impl Entity {
.map(|con_rel| con_rel.get_to_camel_case())
.collect()
}

pub fn get_eq_needed(&self) -> TokenStream {
self.columns
.iter()
.find(|column| {
matches!(
column.col_type,
ColumnType::Float(_) | ColumnType::Double(_)
)
})
// check if float or double exist.
// if exist, return nothing
.map_or(quote! {, Eq}, |_| quote! {})
}
}

#[cfg(test)]
mod tests {
use crate::{Column, DateTimeCrate, Entity, PrimaryKey, Relation, RelationType};
use quote::format_ident;
use sea_query::{ColumnType, ForeignKeyAction};

use crate::{Column, DateTimeCrate, Entity, PrimaryKey, Relation, RelationType};

fn setup() -> Entity {
Entity {
table_name: "special_cake".to_owned(),
Expand Down Expand Up @@ -416,4 +434,11 @@ mod tests {
assert_eq!(elem, entity.conjunct_relations[i].get_to_camel_case());
}
}

#[test]
fn test_get_eq_needed() {
let entity = setup();

println!("entity: {:?}", entity.get_eq_needed());
}
}
109 changes: 102 additions & 7 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,11 @@ impl EntityWriter {
) -> TokenStream {
let column_names_snake_case = entity.get_column_names_snake_case();
let column_rs_types = entity.get_column_rs_types(date_time_crate);

let if_eq_needed = entity.get_eq_needed();
let extra_derive = with_serde.extra_derive();

quote! {
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel #extra_derive)]
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel #if_eq_needed #extra_derive)]
pub struct Model {
#(pub #column_names_snake_case: #column_rs_types,)*
}
Expand Down Expand Up @@ -566,6 +566,7 @@ impl EntityWriter {
let table_name = entity.table_name.as_str();
let column_names_snake_case = entity.get_column_names_snake_case();
let column_rs_types = entity.get_column_rs_types(date_time_crate);
let if_eq_needed = entity.get_eq_needed();
let primary_keys: Vec<String> = entity
.primary_keys
.iter()
Expand Down Expand Up @@ -620,7 +621,7 @@ impl EntityWriter {
let extra_derive = with_serde.extra_derive();

quote! {
#[derive(Clone, Debug, PartialEq, DeriveEntityModel #extra_derive)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel #if_eq_needed #extra_derive)]
#[sea_orm(
#schema_name
table_name = #table_name
Expand Down Expand Up @@ -1032,6 +1033,92 @@ mod tests {
name: "id".to_owned(),
}],
},
Entity {
table_name: "cake_with_float".to_owned(),
columns: vec![
Column {
name: "id".to_owned(),
col_type: ColumnType::Integer(Some(11)),
auto_increment: true,
not_null: true,
unique: false,
},
Column {
name: "name".to_owned(),
col_type: ColumnType::Text,
auto_increment: false,
not_null: false,
unique: false,
},
Column {
name: "price".to_owned(),
col_type: ColumnType::Float(Some(2)),
auto_increment: false,
not_null: false,
unique: false,
},
],
relations: vec![Relation {
ref_table: "fruit".to_owned(),
columns: vec![],
ref_columns: vec![],
rel_type: RelationType::HasMany,
on_delete: None,
on_update: None,
self_referencing: false,
num_suffix: 0,
}],
conjunct_relations: vec![ConjunctRelation {
via: "cake_filling".to_owned(),
to: "filling".to_owned(),
}],
primary_keys: vec![PrimaryKey {
name: "id".to_owned(),
}],
},
Entity {
table_name: "cake_with_double".to_owned(),
columns: vec![
Column {
name: "id".to_owned(),
col_type: ColumnType::Integer(Some(11)),
auto_increment: true,
not_null: true,
unique: false,
},
Column {
name: "name".to_owned(),
col_type: ColumnType::Text,
auto_increment: false,
not_null: false,
unique: false,
},
Column {
name: "price".to_owned(),
col_type: ColumnType::Double(Some(2)),
auto_increment: false,
not_null: false,
unique: false,
},
],
relations: vec![Relation {
ref_table: "fruit".to_owned(),
columns: vec![],
ref_columns: vec![],
rel_type: RelationType::HasMany,
on_delete: None,
on_update: None,
self_referencing: false,
num_suffix: 0,
}],
conjunct_relations: vec![ConjunctRelation {
via: "cake_filling".to_owned(),
to: "filling".to_owned(),
}],
primary_keys: vec![PrimaryKey {
name: "id".to_owned(),
}],
},
]
}

Expand All @@ -1056,21 +1143,25 @@ mod tests {
#[test]
fn test_gen_expanded_code_blocks() -> io::Result<()> {
let entities = setup();
const ENTITY_FILES: [&str; 6] = [
const ENTITY_FILES: [&str; 8] = [
include_str!("../../tests/expanded/cake.rs"),
include_str!("../../tests/expanded/cake_filling.rs"),
include_str!("../../tests/expanded/filling.rs"),
include_str!("../../tests/expanded/fruit.rs"),
include_str!("../../tests/expanded/vendor.rs"),
include_str!("../../tests/expanded/rust_keyword.rs"),
include_str!("../../tests/expanded/cake_with_float.rs"),
include_str!("../../tests/expanded/cake_with_double.rs"),
];
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 6] = [
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 8] = [
include_str!("../../tests/expanded_with_schema_name/cake.rs"),
include_str!("../../tests/expanded_with_schema_name/cake_filling.rs"),
include_str!("../../tests/expanded_with_schema_name/filling.rs"),
include_str!("../../tests/expanded_with_schema_name/fruit.rs"),
include_str!("../../tests/expanded_with_schema_name/vendor.rs"),
include_str!("../../tests/expanded_with_schema_name/rust_keyword.rs"),
include_str!("../../tests/expanded_with_schema_name/cake_with_float.rs"),
include_str!("../../tests/expanded_with_schema_name/cake_with_double.rs"),
];

assert_eq!(entities.len(), ENTITY_FILES.len());
Expand Down Expand Up @@ -1132,21 +1223,25 @@ mod tests {
#[test]
fn test_gen_compact_code_blocks() -> io::Result<()> {
let entities = setup();
const ENTITY_FILES: [&str; 6] = [
const ENTITY_FILES: [&str; 8] = [
include_str!("../../tests/compact/cake.rs"),
include_str!("../../tests/compact/cake_filling.rs"),
include_str!("../../tests/compact/filling.rs"),
include_str!("../../tests/compact/fruit.rs"),
include_str!("../../tests/compact/vendor.rs"),
include_str!("../../tests/compact/rust_keyword.rs"),
include_str!("../../tests/compact/cake_with_float.rs"),
include_str!("../../tests/compact/cake_with_double.rs"),
];
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 6] = [
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 8] = [
include_str!("../../tests/compact_with_schema_name/cake.rs"),
include_str!("../../tests/compact_with_schema_name/cake_filling.rs"),
include_str!("../../tests/compact_with_schema_name/filling.rs"),
include_str!("../../tests/compact_with_schema_name/fruit.rs"),
include_str!("../../tests/compact_with_schema_name/vendor.rs"),
include_str!("../../tests/compact_with_schema_name/rust_keyword.rs"),
include_str!("../../tests/compact_with_schema_name/cake_with_float.rs"),
include_str!("../../tests/compact_with_schema_name/cake_with_double.rs"),
];

assert_eq!(entities.len(), ENTITY_FILES.len());
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact/cake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "cake")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact/cake_filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "_cake_filling_")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
Expand Down
37 changes: 37 additions & 0 deletions sea-orm-codegen/tests/compact/cake_with_double.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "cake_with_double")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(column_type = "Text", nullable)]
pub name: Option<String> ,
#[sea_orm(column_type = "Double(Some(2))", nullable)]
pub price: Option<f64> ,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::fruit::Entity")]
Fruit,
}

impl Related<super::fruit::Entity> for Entity {
fn to() -> RelationDef {
Relation::Fruit.def()
}
}

impl Related<super::filling::Entity> for Entity {
fn to() -> RelationDef {
super::cake_filling::Relation::Filling.def()
}
fn via() -> Option<RelationDef> {
Some(super::cake_filling::Relation::CakeWithDouble.def().rev())
}
}

impl ActiveModelBehavior for ActiveModel {}
37 changes: 37 additions & 0 deletions sea-orm-codegen/tests/compact/cake_with_float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "cake_with_float")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(column_type = "Text", nullable)]
pub name: Option<String> ,
#[sea_orm(column_type = "Float(Some(2))", nullable)]
pub price: Option<f32> ,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::fruit::Entity")]
Fruit,
}

impl Related<super::fruit::Entity> for Entity {
fn to() -> RelationDef {
Relation::Fruit.def()
}
}

impl Related<super::filling::Entity> for Entity {
fn to() -> RelationDef {
super::cake_filling::Relation::Filling.def()
}
fn via() -> Option<RelationDef> {
Some(super::cake_filling::Relation::CakeWithFloat.def().rev())
}
}

impl ActiveModelBehavior for ActiveModel {}
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact/filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "filling")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "fruit")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact/rust_keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "rust_keyword")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "vendor")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact_with_schema_name/cake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(schema_name = "schema_name", table_name = "cake")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
Loading