From bfe3f18bb6aa356c6f40d9c70db2489e5979619b Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 11 Jan 2023 19:53:08 +0800 Subject: [PATCH] Rename methods --- sea-orm-macros/src/derives/active_model.rs | 4 ++-- src/entity/active_model.rs | 27 ++++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/sea-orm-macros/src/derives/active_model.rs b/sea-orm-macros/src/derives/active_model.rs index a30e42768..5dbba6692 100644 --- a/sea-orm-macros/src/derives/active_model.rs +++ b/sea-orm-macros/src/derives/active_model.rs @@ -155,9 +155,9 @@ fn derive_active_model(all_fields: IntoIter) -> syn::Result } } - fn set_dirty(&mut self, c: ::Column) { + fn reset(&mut self, c: ::Column) { match c { - #(::Column::#name => self.#field.set_dirty(),)* + #(::Column::#name => self.#field.reset(),)* _ => panic!("This ActiveModel does not have this field"), } } diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index e7637a085..5a6ebda1f 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -105,13 +105,15 @@ pub trait ActiveModelTrait: Clone + Debug { /// The default implementation of the ActiveModel fn default() -> Self; - /// Set fields of value [ActiveValue::Unchanged] as [ActiveValue::Set] - fn set_dirty(&mut self, c: ::Column); + /// Reset the value from [ActiveValue::Unchanged] to [ActiveValue::Set], + /// leaving [ActiveValue::NotSet] untouched. + fn reset(&mut self, c: ::Column); - /// Set all fields of value [ActiveValue::Unchanged] as [ActiveValue::Set] - fn set_all_dirty(mut self) -> Self { + /// Reset all values from [ActiveValue::Unchanged] to [ActiveValue::Set], + /// leaving [ActiveValue::NotSet] untouched. + fn reset_all(mut self) -> Self { for col in ::Column::iter() { - self.set_dirty(col); + self.reset(col); } self } @@ -831,8 +833,9 @@ where } } - /// Convert [ActiveValue::Unchanged] into [ActiveValue::Set] - pub fn set_dirty(&mut self) { + /// Reset the value from [ActiveValue::Unchanged] to [ActiveValue::Set], + /// leaving [ActiveValue::NotSet] untouched. + pub fn reset(&mut self) { *self = match self.take() { Some(value) => ActiveValue::Set(value), None => ActiveValue::NotSet, @@ -1293,7 +1296,7 @@ mod tests { } #[test] - fn test_set_dirty_1() { + fn test_reset_1() { assert_eq!( fruit::Model { id: 1, @@ -1315,7 +1318,7 @@ mod tests { cake_id: None, } .into_active_model() - .set_all_dirty(), + .reset_all(), fruit::ActiveModel { id: Set(1), name: Set("Apple".into()), @@ -1344,7 +1347,7 @@ mod tests { cake_id: Some(2), } .into_active_model() - .set_all_dirty(), + .reset_all(), fruit::ActiveModel { id: Set(1), name: Set("Apple".into()), @@ -1354,7 +1357,7 @@ mod tests { } #[smol_potat::test] - async fn test_set_dirty_2() -> Result<(), DbErr> { + async fn test_reset_2() -> Result<(), DbErr> { use crate::*; let db = MockDatabase::new(DbBackend::Postgres) @@ -1397,7 +1400,7 @@ mod tests { cake_id: None, } .into_active_model() - .set_all_dirty() + .reset_all() .update(&db) .await?;