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 1 commit
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
27 changes: 25 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,27 @@ impl Entity {
.map(|con_rel| con_rel.get_to_camel_case())
.collect()
}

pub fn get_eq_needed(&self) -> TokenStream {
self.columns
.iter()
.find(|column| match column.col_type {
ColumnType::Float(_) | ColumnType::Double(_) => true,
_ => false,
})
// 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 +432,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());
}
}
101 changes: 96 additions & 5 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
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
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
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_filling_")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact_with_schema_name/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(schema_name = "schema_name", table_name = "filling")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact_with_schema_name/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(schema_name = "schema_name", table_name = "fruit")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
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 = "rust_keyword")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact_with_schema_name/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(schema_name = "schema_name", table_name = "vendor")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/compact_with_serde/cake_both.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use sea_orm::entity::prelude:: * ;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "cake")]
pub struct Model {
#[sea_orm(primary_key)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use sea_orm::entity::prelude:: * ;
use serde::Deserialize;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize)]
#[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_with_serde/cake_none.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_with_serde/cake_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use sea_orm::entity::prelude:: * ;
use serde::Serialize;

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

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)]
pub struct Model {
pub id: i32,
pub name: Option<String> ,
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded/cake_filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl EntityName for Entity {
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)]
pub struct Model {
pub cake_id: i32,
pub filling_id: i32,
Expand Down
Loading