From 5e0c625ac0aeb0644a33d24f585164ec73417f8c Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 27 Oct 2022 00:40:03 +0800 Subject: [PATCH] Fix - vector of float & double does not derive Eq (#1158) * Fix - vector of float & double does not derive Eq * clippy Co-authored-by: Chris Tsang --- sea-orm-codegen/src/entity/base_entity.rs | 14 ++-- sea-orm-codegen/src/entity/writer.rs | 43 +++++++++++-- .../tests/compact/collection_float.rs | 17 +++++ .../collection_float.rs | 17 +++++ .../tests/expanded/collection_float.rs | 60 +++++++++++++++++ .../collection_float.rs | 64 +++++++++++++++++++ 6 files changed, 205 insertions(+), 10 deletions(-) create mode 100644 sea-orm-codegen/tests/compact/collection_float.rs create mode 100644 sea-orm-codegen/tests/compact_with_schema_name/collection_float.rs create mode 100644 sea-orm-codegen/tests/expanded/collection_float.rs create mode 100644 sea-orm-codegen/tests/expanded_with_schema_name/collection_float.rs diff --git a/sea-orm-codegen/src/entity/base_entity.rs b/sea-orm-codegen/src/entity/base_entity.rs index d58a6529..85d19aa5 100644 --- a/sea-orm-codegen/src/entity/base_entity.rs +++ b/sea-orm-codegen/src/entity/base_entity.rs @@ -152,14 +152,16 @@ impl Entity { } pub fn get_eq_needed(&self) -> TokenStream { + fn is_floats(col_type: &sea_query::ColumnType) -> bool { + match col_type { + ColumnType::Float(_) | ColumnType::Double(_) => true, + ColumnType::Array(col_type) => is_floats(col_type), + _ => false, + } + } self.columns .iter() - .find(|column| { - matches!( - column.col_type, - ColumnType::Float(_) | ColumnType::Double(_) - ) - }) + .find(|column| is_floats(&column.col_type)) // check if float or double exist. // if exist, return nothing .map_or(quote! {, Eq}, |_| quote! {}) diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 006beeda..71cdb4e9 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -1158,6 +1158,37 @@ mod tests { name: "id".to_owned(), }], }, + Entity { + table_name: "collection_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: "floats".to_owned(), + col_type: ColumnType::Array(SeaRc::new(Box::new(ColumnType::Float(None)))), + auto_increment: false, + not_null: true, + unique: false, + }, + Column { + name: "doubles".to_owned(), + col_type: ColumnType::Array(SeaRc::new(Box::new(ColumnType::Double(None)))), + auto_increment: false, + not_null: true, + unique: false, + }, + ], + relations: vec![], + conjunct_relations: vec![], + primary_keys: vec![PrimaryKey { + name: "id".to_owned(), + }], + }, ] } @@ -1182,7 +1213,7 @@ mod tests { #[test] fn test_gen_expanded_code_blocks() -> io::Result<()> { let entities = setup(); - const ENTITY_FILES: [&str; 9] = [ + const ENTITY_FILES: [&str; 10] = [ include_str!("../../tests/expanded/cake.rs"), include_str!("../../tests/expanded/cake_filling.rs"), include_str!("../../tests/expanded/filling.rs"), @@ -1192,8 +1223,9 @@ mod tests { include_str!("../../tests/expanded/cake_with_float.rs"), include_str!("../../tests/expanded/cake_with_double.rs"), include_str!("../../tests/expanded/collection.rs"), + include_str!("../../tests/expanded/collection_float.rs"), ]; - const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 9] = [ + const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 10] = [ 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"), @@ -1203,6 +1235,7 @@ mod tests { include_str!("../../tests/expanded_with_schema_name/cake_with_float.rs"), include_str!("../../tests/expanded_with_schema_name/cake_with_double.rs"), include_str!("../../tests/expanded_with_schema_name/collection.rs"), + include_str!("../../tests/expanded_with_schema_name/collection_float.rs"), ]; assert_eq!(entities.len(), ENTITY_FILES.len()); @@ -1264,7 +1297,7 @@ mod tests { #[test] fn test_gen_compact_code_blocks() -> io::Result<()> { let entities = setup(); - const ENTITY_FILES: [&str; 9] = [ + const ENTITY_FILES: [&str; 10] = [ include_str!("../../tests/compact/cake.rs"), include_str!("../../tests/compact/cake_filling.rs"), include_str!("../../tests/compact/filling.rs"), @@ -1274,8 +1307,9 @@ mod tests { include_str!("../../tests/compact/cake_with_float.rs"), include_str!("../../tests/compact/cake_with_double.rs"), include_str!("../../tests/compact/collection.rs"), + include_str!("../../tests/compact/collection_float.rs"), ]; - const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 9] = [ + const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 10] = [ 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"), @@ -1285,6 +1319,7 @@ mod tests { include_str!("../../tests/compact_with_schema_name/cake_with_float.rs"), include_str!("../../tests/compact_with_schema_name/cake_with_double.rs"), include_str!("../../tests/compact_with_schema_name/collection.rs"), + include_str!("../../tests/compact_with_schema_name/collection_float.rs"), ]; assert_eq!(entities.len(), ENTITY_FILES.len()); diff --git a/sea-orm-codegen/tests/compact/collection_float.rs b/sea-orm-codegen/tests/compact/collection_float.rs new file mode 100644 index 00000000..08155fed --- /dev/null +++ b/sea-orm-codegen/tests/compact/collection_float.rs @@ -0,0 +1,17 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.10.0 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "collection_float")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub floats: Vec , + pub doubles: Vec , +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/compact_with_schema_name/collection_float.rs b/sea-orm-codegen/tests/compact_with_schema_name/collection_float.rs new file mode 100644 index 00000000..2fe35a67 --- /dev/null +++ b/sea-orm-codegen/tests/compact_with_schema_name/collection_float.rs @@ -0,0 +1,17 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.10.0 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(schema_name = "schema_name", table_name = "collection_float")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub floats: Vec , + pub doubles: Vec , +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/expanded/collection_float.rs b/sea-orm-codegen/tests/expanded/collection_float.rs new file mode 100644 index 00000000..b27d8f00 --- /dev/null +++ b/sea-orm-codegen/tests/expanded/collection_float.rs @@ -0,0 +1,60 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.10.0 + +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl EntityName for Entity { + fn table_name(&self) -> &str { + "collection_float" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] +pub struct Model { + pub id: i32, + pub floats: Vec , + pub doubles: Vec , +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id, + Floats, + Doubles, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id, +} + +impl PrimaryKeyTrait for PrimaryKey { + type ValueType = i32; + fn auto_increment() -> bool { + true + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation {} + +impl ColumnTrait for Column { + type EntityName = Entity; + fn def(&self) -> ColumnDef { + match self { + Self::Id => ColumnType::Integer.def(), + Self::Floats => ColumnType::Array(sea_orm::sea_query::SeaRc::new(ColumnType::Float)).def(), + Self::Doubles => ColumnType::Array(sea_orm::sea_query::SeaRc::new(ColumnType::Double)).def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + panic!("No RelationDef") + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/expanded_with_schema_name/collection_float.rs b/sea-orm-codegen/tests/expanded_with_schema_name/collection_float.rs new file mode 100644 index 00000000..2413f3e0 --- /dev/null +++ b/sea-orm-codegen/tests/expanded_with_schema_name/collection_float.rs @@ -0,0 +1,64 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.10.0 + +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl EntityName for Entity { + fn schema_name(&self) -> Option< &str > { + Some("schema_name") + } + + fn table_name(&self) -> &str { + "collection_float" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] +pub struct Model { + pub id: i32, + pub floats: Vec , + pub doubles: Vec , +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id, + Floats, + Doubles, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id, +} + +impl PrimaryKeyTrait for PrimaryKey { + type ValueType = i32; + fn auto_increment() -> bool { + true + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation {} + +impl ColumnTrait for Column { + type EntityName = Entity; + fn def(&self) -> ColumnDef { + match self { + Self::Id => ColumnType::Integer.def(), + Self::Floats => ColumnType::Array(sea_orm::sea_query::SeaRc::new(ColumnType::Float)).def(), + Self::Doubles => ColumnType::Array(sea_orm::sea_query::SeaRc::new(ColumnType::Double)).def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + panic!("No RelationDef") + } +} + +impl ActiveModelBehavior for ActiveModel {}