Skip to content

Commit

Permalink
Fix - vector of float & double does not derive Eq (#1158)
Browse files Browse the repository at this point in the history
* Fix - vector of float & double does not derive Eq

* clippy

Co-authored-by: Chris Tsang <[email protected]>
  • Loading branch information
billy1624 and tyt2y3 authored Oct 26, 2022
1 parent 1497c2c commit 5e0c625
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 10 deletions.
14 changes: 8 additions & 6 deletions sea-orm-codegen/src/entity/base_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {})
Expand Down
43 changes: 39 additions & 4 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}],
},
]
}

Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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());
Expand Down Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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());
Expand Down
17 changes: 17 additions & 0 deletions sea-orm-codegen/tests/compact/collection_float.rs
Original file line number Diff line number Diff line change
@@ -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<f32> ,
pub doubles: Vec<f64> ,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
17 changes: 17 additions & 0 deletions sea-orm-codegen/tests/compact_with_schema_name/collection_float.rs
Original file line number Diff line number Diff line change
@@ -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<f32> ,
pub doubles: Vec<f64> ,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
60 changes: 60 additions & 0 deletions sea-orm-codegen/tests/expanded/collection_float.rs
Original file line number Diff line number Diff line change
@@ -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<f32> ,
pub doubles: Vec<f64> ,
}

#[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 {}
Original file line number Diff line number Diff line change
@@ -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<f32> ,
pub doubles: Vec<f64> ,
}

#[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 {}

0 comments on commit 5e0c625

Please sign in to comment.