Skip to content

Commit

Permalink
Remove uncessary method CallMethodWithErrorCheck for Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jan 17, 2016
1 parent de73d30 commit 31366f3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
16 changes: 12 additions & 4 deletions callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 6 additions & 2 deletions callback_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -24,7 +26,9 @@ func deleteCallback(scope *Scope) {
}

func afterDeleteCallback(scope *Scope) {
scope.CallMethodWithErrorCheck("AfterDelete")
if !scope.HasError() {
scope.CallMethod("AfterDelete")
}
}

func init() {
Expand Down
4 changes: 3 additions & 1 deletion callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func queryCallback(scope *Scope) {
}

func afterQueryCallback(scope *Scope) {
scope.CallMethodWithErrorCheck("AfterFind")
if !scope.HasError() {
scope.CallMethod("AfterFind")
}
}

func init() {
Expand Down
16 changes: 12 additions & 4 deletions callback_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

Expand Down Expand Up @@ -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")
}
}
}

Expand Down
8 changes: 2 additions & 6 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 31366f3

Please sign in to comment.