From 0132f670ce095bad5c1358025767f9c96bdf1acd Mon Sep 17 00:00:00 2001 From: peterdeme Date: Wed, 28 Feb 2024 15:25:53 +0100 Subject: [PATCH] Add `V1` postfix to callbacks --- callback_create.go | 8 ++++---- callback_delete.go | 4 ++-- callback_query.go | 2 +- callback_update.go | 8 ++++---- callbacks_test.go | 18 +++++++++--------- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/callback_create.go b/callback_create.go index 59840f863b..58f2296ead 100644 --- a/callback_create.go +++ b/callback_create.go @@ -21,10 +21,10 @@ func init() { // beforeCreateCallback will invoke `BeforeSave`, `BeforeCreate` method before creating func beforeCreateCallback(scope *Scope) { if !scope.HasError() { - scope.CallMethod("BeforeSave") + scope.CallMethod("BeforeSaveV1") } if !scope.HasError() { - scope.CallMethod("BeforeCreate") + scope.CallMethod("BeforeCreateV2") } } @@ -196,9 +196,9 @@ func forceReloadAfterCreateCallback(scope *Scope) { // afterCreateCallback will invoke `AfterCreate`, `AfterSave` method after creating func afterCreateCallback(scope *Scope) { if !scope.HasError() { - scope.CallMethod("AfterCreate") + scope.CallMethod("AfterCreateV1") } if !scope.HasError() { - scope.CallMethod("AfterSave") + scope.CallMethod("AfterSaveV1") } } diff --git a/callback_delete.go b/callback_delete.go index 48b97acbfb..ed488dea68 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -21,7 +21,7 @@ func beforeDeleteCallback(scope *Scope) { return } if !scope.HasError() { - scope.CallMethod("BeforeDelete") + scope.CallMethod("BeforeDeleteV1") } } @@ -58,6 +58,6 @@ func deleteCallback(scope *Scope) { // afterDeleteCallback will invoke `AfterDelete` method after deleting func afterDeleteCallback(scope *Scope) { if !scope.HasError() { - scope.CallMethod("AfterDelete") + scope.CallMethod("AfterDeleteV1") } } diff --git a/callback_query.go b/callback_query.go index f756271527..c1f1fde4c5 100644 --- a/callback_query.go +++ b/callback_query.go @@ -100,6 +100,6 @@ func queryCallback(scope *Scope) { // afterQueryCallback will invoke `AfterFind` method after querying func afterQueryCallback(scope *Scope) { if !scope.HasError() { - scope.CallMethod("AfterFind") + scope.CallMethod("AfterFindV1") } } diff --git a/callback_update.go b/callback_update.go index 699e534b96..67cba4e616 100644 --- a/callback_update.go +++ b/callback_update.go @@ -39,10 +39,10 @@ func beforeUpdateCallback(scope *Scope) { } if _, ok := scope.Get("gorm:update_column"); !ok { if !scope.HasError() { - scope.CallMethod("BeforeSave") + scope.CallMethod("BeforeSaveV1") } if !scope.HasError() { - scope.CallMethod("BeforeUpdate") + scope.CallMethod("BeforeUpdateV1") } } } @@ -112,10 +112,10 @@ func updateCallback(scope *Scope) { func afterUpdateCallback(scope *Scope) { if _, ok := scope.Get("gorm:update_column"); !ok { if !scope.HasError() { - scope.CallMethod("AfterUpdate") + scope.CallMethod("AfterUpdateV1") } if !scope.HasError() { - scope.CallMethod("AfterSave") + scope.CallMethod("AfterSaveV1") } } } diff --git a/callbacks_test.go b/callbacks_test.go index bebd0e38c6..3df18ed2a6 100644 --- a/callbacks_test.go +++ b/callbacks_test.go @@ -8,7 +8,7 @@ import ( "github.com/jinzhu/gorm" ) -func (s *Product) BeforeCreate() (err error) { +func (s *Product) BeforeCreateV1() (err error) { if s.Code == "Invalid" { err = errors.New("invalid product") } @@ -16,7 +16,7 @@ func (s *Product) BeforeCreate() (err error) { return } -func (s *Product) BeforeUpdate() (err error) { +func (s *Product) BeforeUpdateV1() (err error) { if s.Code == "dont_update" { err = errors.New("can't update") } @@ -24,7 +24,7 @@ func (s *Product) BeforeUpdate() (err error) { return } -func (s *Product) BeforeSave() (err error) { +func (s *Product) BeforeSaveV1() (err error) { if s.Code == "dont_save" { err = errors.New("can't save") } @@ -32,19 +32,19 @@ func (s *Product) BeforeSave() (err error) { return } -func (s *Product) AfterFind() { +func (s *Product) AfterFindV1() { s.AfterFindCallTimes = s.AfterFindCallTimes + 1 } -func (s *Product) AfterCreate(tx *gorm.DB) { +func (s *Product) AfterCreateV1(tx *gorm.DB) { tx.Model(s).UpdateColumn(Product{AfterCreateCallTimes: s.AfterCreateCallTimes + 1}) } -func (s *Product) AfterUpdate() { +func (s *Product) AfterUpdateV1() { s.AfterUpdateCallTimes = s.AfterUpdateCallTimes + 1 } -func (s *Product) AfterSave() (err error) { +func (s *Product) AfterSaveV1() (err error) { if s.Code == "after_save_error" { err = errors.New("can't save") } @@ -52,7 +52,7 @@ func (s *Product) AfterSave() (err error) { return } -func (s *Product) BeforeDelete() (err error) { +func (s *Product) BeforeDeleteV1() (err error) { if s.Code == "dont_delete" { err = errors.New("can't delete") } @@ -60,7 +60,7 @@ func (s *Product) BeforeDelete() (err error) { return } -func (s *Product) AfterDelete() (err error) { +func (s *Product) AfterDeleteV1() (err error) { if s.Code == "after_delete_error" { err = errors.New("can't delete") }