Skip to content

Commit

Permalink
feat(code): Derive PartialEq for Update structs (Wulf#118)
Browse files Browse the repository at this point in the history
To allow easy checks for "empty" updates, this change adds the PartialEq
derive to the generated Update structs. This allows comparison with
`Default::default()` to check if an update is empty.

Fixes: Wulf#117
  • Loading branch information
longsleep authored Nov 10, 2023
1 parent cc78f35 commit c4697b2
Show file tree
Hide file tree
Showing 19 changed files with 20 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- list changes to files (unchanged, modified, deleted)
- generate doc-comments for generated structs, fields and functions
- add derive `QueryableByName` to read-structs
- add derive `PartialEq` to update-structs

## 0.0.17 (yanked)

Expand Down
2 changes: 2 additions & 0 deletions src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub mod derives {
pub const ASSOCIATIONS: &str = "Associations";
#[cfg(feature = "derive-queryablebyname")]
pub const QUERYABLEBYNAME: &str = "QueryableByName";
pub const PARTIALEQ: &str = "PartialEq";
}

impl<'a> Struct<'a> {
Expand Down Expand Up @@ -207,6 +208,7 @@ impl<'a> Struct<'a> {
.all(|f| self.table.primary_key_column_names().contains(&f.name))
{
derives_vec.push(derives::ASCHANGESET);
derives_vec.push(derives::PARTIALEQ);
}

derives_vec.push(derives::DEFAULT);
Expand Down
2 changes: 1 addition & 1 deletion test/autogenerated_all/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Todos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `created_at`
Expand Down
2 changes: 1 addition & 1 deletion test/autogenerated_attributes/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `created_at`
Expand Down
2 changes: 1 addition & 1 deletion test/autogenerated_primary_keys/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `text`
Expand Down
2 changes: 1 addition & 1 deletion test/cleanup_generated_content/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `text`
Expand Down
2 changes: 1 addition & 1 deletion test/create_update_str_cow/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct CreateTodos<'a> {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos<'a> {
/// Field representing column `text_nullable`
Expand Down
2 changes: 1 addition & 1 deletion test/create_update_str_str/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct CreateTodos<'a> {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos<'a> {
/// Field representing column `text_nullable`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct CreateTableB {
}

/// Update Struct for a row in table `tableB` for [`TableB`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=tableB)]
pub struct UpdateTableB {
/// Field representing column `link`
Expand Down
2 changes: 1 addition & 1 deletion test/custom_model_path/models/tableB/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct CreateTableB {
}

/// Update Struct for a row in table `tableB` for [`TableB`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=tableB)]
pub struct UpdateTableB {
/// Field representing column `link`
Expand Down
2 changes: 1 addition & 1 deletion test/multiple_primary_keys/models/users/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct CreateUsers {
}

/// Update Struct for a row in table `users` for [`Users`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=users)]
pub struct UpdateUsers {
/// Field representing column `secret`
Expand Down
2 changes: 1 addition & 1 deletion test/no_default_features/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `unsigned`
Expand Down
2 changes: 1 addition & 1 deletion test/readonly/models/normal/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct CreateNormal {
}

/// Update Struct for a row in table `normal` for [`Normal`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=normal)]
pub struct UpdateNormal {
/// Field representing column `testprop`
Expand Down
2 changes: 1 addition & 1 deletion test/simple_table/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `unsigned`
Expand Down
2 changes: 1 addition & 1 deletion test/simple_table_async/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `unsigned`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `unsigned`
Expand Down
2 changes: 1 addition & 1 deletion test/simple_table_no_crud/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `unsigned`
Expand Down
2 changes: 1 addition & 1 deletion test/simple_table_no_serde/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct CreateTodos {
}

/// Update Struct for a row in table `todos` for [`Todos`]
#[derive(Debug, Clone, AsChangeset, Default)]
#[derive(Debug, Clone, AsChangeset, PartialEq, Default)]
#[diesel(table_name=todos)]
pub struct UpdateTodos {
/// Field representing column `unsigned`
Expand Down
2 changes: 1 addition & 1 deletion test/use_statements/models/fang_tasks/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct CreateFangTasks {
}

/// Update Struct for a row in table `fang_tasks` for [`FangTasks`]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, Default)]
#[derive(Debug, Clone, Serialize, Deserialize, AsChangeset, PartialEq, Default)]
#[diesel(table_name=fang_tasks)]
pub struct UpdateFangTasks {
/// Field representing column `metadata`
Expand Down

0 comments on commit c4697b2

Please sign in to comment.