forked from mirror/gorm
fix: callbcak sort when using multiple plugin (#5304)
This commit is contained in:
parent
d3488ae6bc
commit
b0104943ed
|
@ -246,7 +246,13 @@ func sortCallbacks(cs []*callback) (fns []func(*DB), err error) {
|
||||||
sortCallback func(*callback) error
|
sortCallback func(*callback) error
|
||||||
)
|
)
|
||||||
sort.Slice(cs, func(i, j int) bool {
|
sort.Slice(cs, func(i, j int) bool {
|
||||||
return cs[j].before == "*" || cs[j].after == "*"
|
if cs[j].before == "*" && cs[i].before != "*" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if cs[j].after == "*" && cs[i].after != "*" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
for _, c := range cs {
|
for _, c := range cs {
|
||||||
|
|
|
@ -38,6 +38,7 @@ func c2(*gorm.DB) {}
|
||||||
func c3(*gorm.DB) {}
|
func c3(*gorm.DB) {}
|
||||||
func c4(*gorm.DB) {}
|
func c4(*gorm.DB) {}
|
||||||
func c5(*gorm.DB) {}
|
func c5(*gorm.DB) {}
|
||||||
|
func c6(*gorm.DB) {}
|
||||||
|
|
||||||
func TestCallbacks(t *testing.T) {
|
func TestCallbacks(t *testing.T) {
|
||||||
type callback struct {
|
type callback struct {
|
||||||
|
@ -168,3 +169,37 @@ func TestCallbacks(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPluginCallbacks(t *testing.T) {
|
||||||
|
db, _ := gorm.Open(nil, nil)
|
||||||
|
createCallback := db.Callback().Create()
|
||||||
|
|
||||||
|
createCallback.Before("*").Register("plugin_1_fn1", c1)
|
||||||
|
createCallback.After("*").Register("plugin_1_fn2", c2)
|
||||||
|
|
||||||
|
if ok, msg := assertCallbacks(createCallback, []string{"c1", "c2"}); !ok {
|
||||||
|
t.Errorf("callbacks tests failed, got %v", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// plugin 2
|
||||||
|
createCallback.Before("*").Register("plugin_2_fn1", c3)
|
||||||
|
if ok, msg := assertCallbacks(createCallback, []string{"c3", "c1", "c2"}); !ok {
|
||||||
|
t.Errorf("callbacks tests failed, got %v", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
createCallback.After("*").Register("plugin_2_fn2", c4)
|
||||||
|
if ok, msg := assertCallbacks(createCallback, []string{"c3", "c1", "c2", "c4"}); !ok {
|
||||||
|
t.Errorf("callbacks tests failed, got %v", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// plugin 3
|
||||||
|
createCallback.Before("*").Register("plugin_3_fn1", c5)
|
||||||
|
if ok, msg := assertCallbacks(createCallback, []string{"c5", "c3", "c1", "c2", "c4"}); !ok {
|
||||||
|
t.Errorf("callbacks tests failed, got %v", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
createCallback.After("*").Register("plugin_3_fn2", c6)
|
||||||
|
if ok, msg := assertCallbacks(createCallback, []string{"c5", "c3", "c1", "c2", "c4", "c6"}); !ok {
|
||||||
|
t.Errorf("callbacks tests failed, got %v", msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue