From 31366f388f5e625e911d141c4eb890b64aaf30b1 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 17 Jan 2016 16:14:14 +0800 Subject: [PATCH] Remove uncessary method CallMethodWithErrorCheck for Scope --- callback_create.go | 16 ++++++++++++---- callback_delete.go | 8 ++++++-- callback_query.go | 4 +++- callback_update.go | 16 ++++++++++++---- scope.go | 8 ++------ 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/callback_create.go b/callback_create.go index 3003b3a5..6054aa91 100644 --- a/callback_create.go +++ b/callback_create.go @@ -6,8 +6,12 @@ import ( ) func beforeCreateCallback(scope *Scope) { - scope.CallMethodWithErrorCheck("BeforeSave") - scope.CallMethodWithErrorCheck("BeforeCreate") + if !scope.HasError() { + scope.CallMethod("BeforeSave") + } + if !scope.HasError() { + scope.CallMethod("BeforeCreate") + } } func updateTimeStampForCreateCallback(scope *Scope) { @@ -109,8 +113,12 @@ func forceReloadAfterCreateCallback(scope *Scope) { } func afterCreateCallback(scope *Scope) { - scope.CallMethodWithErrorCheck("AfterCreate") - scope.CallMethodWithErrorCheck("AfterSave") + if !scope.HasError() { + scope.CallMethod("AfterCreate") + } + if !scope.HasError() { + scope.CallMethod("AfterSave") + } } func init() { diff --git a/callback_delete.go b/callback_delete.go index 7616cb87..804bdf71 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -3,7 +3,9 @@ package gorm import "fmt" func beforeDeleteCallback(scope *Scope) { - scope.CallMethodWithErrorCheck("BeforeDelete") + if !scope.HasError() { + scope.CallMethod("BeforeDelete") + } } func deleteCallback(scope *Scope) { @@ -24,7 +26,9 @@ func deleteCallback(scope *Scope) { } func afterDeleteCallback(scope *Scope) { - scope.CallMethodWithErrorCheck("AfterDelete") + if !scope.HasError() { + scope.CallMethod("AfterDelete") + } } func init() { diff --git a/callback_query.go b/callback_query.go index 707d83f5..9ebff6b7 100644 --- a/callback_query.go +++ b/callback_query.go @@ -79,7 +79,9 @@ func queryCallback(scope *Scope) { } func afterQueryCallback(scope *Scope) { - scope.CallMethodWithErrorCheck("AfterFind") + if !scope.HasError() { + scope.CallMethod("AfterFind") + } } func init() { diff --git a/callback_update.go b/callback_update.go index f8ded58b..9463c994 100644 --- a/callback_update.go +++ b/callback_update.go @@ -26,8 +26,12 @@ func assignUpdateAttributesCallback(scope *Scope) { func beforeUpdateCallback(scope *Scope) { if _, ok := scope.Get("gorm:update_column"); !ok { - scope.CallMethodWithErrorCheck("BeforeSave") - scope.CallMethodWithErrorCheck("BeforeUpdate") + if !scope.HasError() { + scope.CallMethod("BeforeSave") + } + if !scope.HasError() { + scope.CallMethod("BeforeUpdate") + } } } @@ -77,8 +81,12 @@ func updateCallback(scope *Scope) { func afterUpdateCallback(scope *Scope) { if _, ok := scope.Get("gorm:update_column"); !ok { - scope.CallMethodWithErrorCheck("AfterUpdate") - scope.CallMethodWithErrorCheck("AfterSave") + if !scope.HasError() { + scope.CallMethod("AfterUpdate") + } + if !scope.HasError() { + scope.CallMethod("AfterSave") + } } } diff --git a/scope.go b/scope.go index 0d6602b5..b2a2852b 100644 --- a/scope.go +++ b/scope.go @@ -192,8 +192,8 @@ func (scope *Scope) SetColumn(column interface{}, value interface{}) error { return errors.New("could not convert column to field") } -func (scope *Scope) CallMethod(name string, checkError bool) { - if scope.Value == nil || (checkError && scope.HasError()) { +func (scope *Scope) CallMethod(name string) { + if scope.Value == nil { return } @@ -239,10 +239,6 @@ func (scope *Scope) CallMethod(name string, checkError bool) { } } -func (scope *Scope) CallMethodWithErrorCheck(name string) { - scope.CallMethod(name, true) -} - // AddToVars add value as sql's vars, gorm will escape them func (scope *Scope) AddToVars(value interface{}) string { if expr, ok := value.(*expr); ok {