gorm/callback.go

189 lines
4.6 KiB
Go
Raw Normal View History

2014-01-23 12:43:39 +04:00
package gorm
2014-01-26 08:41:37 +04:00
import (
"fmt"
)
2014-01-25 16:04:01 +04:00
2014-01-23 12:43:39 +04:00
type callback struct {
2014-01-26 08:41:37 +04:00
creates []*func(scope *Scope)
updates []*func(scope *Scope)
deletes []*func(scope *Scope)
queries []*func(scope *Scope)
2014-01-24 17:52:26 +04:00
processors []*callback_processor
}
type callback_processor struct {
name string
before string
after string
replace bool
2014-01-25 16:04:01 +04:00
remove bool
2014-01-24 17:52:26 +04:00
typ string
2014-01-26 08:41:37 +04:00
processor *func(scope *Scope)
2014-01-24 17:52:26 +04:00
callback *callback
2014-01-23 12:43:39 +04:00
}
2014-01-24 17:52:26 +04:00
func (c *callback) addProcessor(typ string) *callback_processor {
cp := &callback_processor{typ: typ, callback: c}
c.processors = append(c.processors, cp)
return cp
2014-01-23 12:43:39 +04:00
}
2014-01-29 06:25:58 +04:00
func (c *callback) clone() *callback {
return &callback{processors: c.processors}
}
2014-01-24 17:52:26 +04:00
func (c *callback) Create() *callback_processor {
return c.addProcessor("create")
2014-01-23 12:43:39 +04:00
}
2014-01-24 17:52:26 +04:00
func (c *callback) Update() *callback_processor {
return c.addProcessor("update")
2014-01-23 12:43:39 +04:00
}
2014-01-24 17:52:26 +04:00
func (c *callback) Delete() *callback_processor {
return c.addProcessor("delete")
2014-01-23 12:43:39 +04:00
}
2014-01-24 17:52:26 +04:00
func (c *callback) Query() *callback_processor {
return c.addProcessor("query")
2014-01-23 12:43:39 +04:00
}
2014-01-24 17:52:26 +04:00
func (cp *callback_processor) Before(name string) *callback_processor {
cp.before = name
return cp
}
func (cp *callback_processor) After(name string) *callback_processor {
cp.after = name
return cp
}
2014-01-26 08:41:37 +04:00
func (cp *callback_processor) Register(name string, fc func(scope *Scope)) {
2014-01-24 17:52:26 +04:00
cp.name = name
2014-01-25 15:35:21 +04:00
cp.processor = &fc
cp.callback.sort()
2014-01-24 17:52:26 +04:00
}
func (cp *callback_processor) Remove(name string) {
2014-01-25 16:04:01 +04:00
cp.name = name
cp.remove = true
cp.callback.sort()
2014-01-24 17:52:26 +04:00
}
2014-01-23 12:43:39 +04:00
2014-01-26 08:41:37 +04:00
func (cp *callback_processor) Replace(name string, fc func(scope *Scope)) {
2014-01-24 17:52:26 +04:00
cp.name = name
2014-01-25 15:35:21 +04:00
cp.processor = &fc
2014-01-24 17:52:26 +04:00
cp.replace = true
2014-01-25 15:35:21 +04:00
cp.callback.sort()
}
2014-01-25 16:04:01 +04:00
func getRIndex(strs []string, str string) int {
for i := len(strs) - 1; i >= 0; i-- {
if strs[i] == str {
return i
2014-01-25 15:35:21 +04:00
}
}
return -1
}
2014-01-26 08:41:37 +04:00
func sortProcessors(cps []*callback_processor) []*func(scope *Scope) {
2014-01-25 15:35:21 +04:00
var sortCallbackProcessor func(c *callback_processor, force bool)
var names, sortedNames = []string{}, []string{}
for _, cp := range cps {
2014-01-25 16:04:01 +04:00
if index := getRIndex(names, cp.name); index > -1 {
if cp.replace {
fmt.Printf("[info] replacing callback `%v` from %v\n", cp.name, fileWithLineNum())
} else if cp.remove {
fmt.Printf("[info] removing callback `%v` from %v\n", cp.name, fileWithLineNum())
} else {
2014-07-15 07:48:30 +04:00
fmt.Printf("[warning] duplicated callback `%v` from %v\n", cp.name, fileWithLineNum())
2014-01-25 16:04:01 +04:00
}
}
2014-01-25 15:35:21 +04:00
names = append(names, cp.name)
}
sortCallbackProcessor = func(c *callback_processor, force bool) {
2014-01-25 16:04:01 +04:00
if getRIndex(sortedNames, c.name) > -1 {
2014-01-25 15:35:21 +04:00
return
}
if len(c.before) > 0 {
2014-01-25 16:04:01 +04:00
if index := getRIndex(sortedNames, c.before); index > -1 {
2014-01-25 15:35:21 +04:00
sortedNames = append(sortedNames[:index], append([]string{c.name}, sortedNames[index:]...)...)
2014-01-25 16:04:01 +04:00
} else if index := getRIndex(names, c.before); index > -1 {
2014-01-25 15:35:21 +04:00
sortedNames = append(sortedNames, c.name)
sortCallbackProcessor(cps[index], true)
} else {
sortedNames = append(sortedNames, c.name)
}
}
if len(c.after) > 0 {
2014-01-25 16:04:01 +04:00
if index := getRIndex(sortedNames, c.after); index > -1 {
2014-01-25 15:35:21 +04:00
sortedNames = append(sortedNames[:index+1], append([]string{c.name}, sortedNames[index+1:]...)...)
2014-01-25 16:04:01 +04:00
} else if index := getRIndex(names, c.after); index > -1 {
2014-01-25 15:35:21 +04:00
cp := cps[index]
if len(cp.before) == 0 {
cp.before = c.name
}
sortCallbackProcessor(cp, true)
} else {
sortedNames = append(sortedNames, c.name)
}
}
2014-01-25 16:04:01 +04:00
if getRIndex(sortedNames, c.name) == -1 && force {
2014-01-25 15:35:21 +04:00
sortedNames = append(sortedNames, c.name)
}
}
for _, cp := range cps {
sortCallbackProcessor(cp, false)
}
2014-01-26 08:41:37 +04:00
var funcs = []*func(scope *Scope){}
var sortedFuncs = []*func(scope *Scope){}
2014-01-25 15:35:21 +04:00
for _, name := range sortedNames {
2014-01-25 16:04:01 +04:00
index := getRIndex(names, name)
if !cps[index].remove {
sortedFuncs = append(sortedFuncs, cps[index].processor)
}
2014-01-25 15:35:21 +04:00
}
for _, cp := range cps {
2014-01-25 16:04:01 +04:00
if sindex := getRIndex(sortedNames, cp.name); sindex == -1 {
if !cp.remove {
funcs = append(funcs, cp.processor)
}
2014-01-25 15:35:21 +04:00
}
}
return append(sortedFuncs, funcs...)
}
func (c *callback) sort() {
creates, updates, deletes, queries := []*callback_processor{}, []*callback_processor{}, []*callback_processor{}, []*callback_processor{}
for _, processor := range c.processors {
switch processor.typ {
case "create":
creates = append(creates, processor)
case "update":
updates = append(updates, processor)
case "delete":
deletes = append(deletes, processor)
case "query":
queries = append(queries, processor)
}
}
c.creates = sortProcessors(creates)
c.updates = sortProcessors(updates)
c.deletes = sortProcessors(deletes)
c.queries = sortProcessors(queries)
2014-01-23 12:43:39 +04:00
}
2014-01-24 17:52:26 +04:00
var DefaultCallback = &callback{processors: []*callback_processor{}}