gorm/callback.go

193 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)
2015-02-17 03:34:01 +03:00
processors []*callbackProcessor
2014-01-24 17:52:26 +04:00
}
2015-02-17 03:34:01 +03:00
type callbackProcessor struct {
2014-01-24 17:52:26 +04:00
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
}
2015-02-17 03:34:01 +03:00
func (c *callback) addProcessor(typ string) *callbackProcessor {
cp := &callbackProcessor{typ: typ, callback: c}
2014-01-24 17:52:26 +04:00
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 {
2015-02-26 09:47:56 +03:00
return &callback{
creates: c.creates,
updates: c.updates,
deletes: c.deletes,
queries: c.queries,
processors: c.processors,
}
2014-01-29 06:25:58 +04:00
}
2015-02-17 03:34:01 +03:00
func (c *callback) Create() *callbackProcessor {
2014-01-24 17:52:26 +04:00
return c.addProcessor("create")
2014-01-23 12:43:39 +04:00
}
2015-02-17 03:34:01 +03:00
func (c *callback) Update() *callbackProcessor {
2014-01-24 17:52:26 +04:00
return c.addProcessor("update")
2014-01-23 12:43:39 +04:00
}
2015-02-17 03:34:01 +03:00
func (c *callback) Delete() *callbackProcessor {
2014-01-24 17:52:26 +04:00
return c.addProcessor("delete")
2014-01-23 12:43:39 +04:00
}
2015-02-17 03:34:01 +03:00
func (c *callback) Query() *callbackProcessor {
2014-01-24 17:52:26 +04:00
return c.addProcessor("query")
2014-01-23 12:43:39 +04:00
}
2015-02-17 03:34:01 +03:00
func (cp *callbackProcessor) Before(name string) *callbackProcessor {
2014-01-24 17:52:26 +04:00
cp.before = name
return cp
}
2015-02-17 03:34:01 +03:00
func (cp *callbackProcessor) After(name string) *callbackProcessor {
2014-01-24 17:52:26 +04:00
cp.after = name
return cp
}
2015-02-17 03:34:01 +03:00
func (cp *callbackProcessor) 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
}
2015-02-17 03:34:01 +03:00
func (cp *callbackProcessor) Remove(name string) {
fmt.Printf("[info] removing callback `%v` from %v\n", name, fileWithLineNum())
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
2015-02-17 03:34:01 +03:00
func (cp *callbackProcessor) Replace(name string, fc func(scope *Scope)) {
fmt.Printf("[info] replacing callback `%v` from %v\n", name, fileWithLineNum())
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
}
2015-02-17 03:34:01 +03:00
func sortProcessors(cps []*callbackProcessor) []*func(scope *Scope) {
var sortCallbackProcessor func(c *callbackProcessor)
2014-01-25 15:35:21 +04:00
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 && !cp.remove {
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)
}
2015-02-17 03:34:01 +03:00
sortCallbackProcessor = func(c *callbackProcessor) {
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)
2014-08-27 12:10:33 +04:00
sortCallbackProcessor(cps[index])
2014-01-25 15:35:21 +04:00
} 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
}
2014-08-27 12:10:33 +04:00
sortCallbackProcessor(cp)
2014-01-25 15:35:21 +04:00
} else {
sortedNames = append(sortedNames, c.name)
}
}
2014-08-27 12:10:33 +04:00
if getRIndex(sortedNames, c.name) == -1 {
2014-01-25 15:35:21 +04:00
sortedNames = append(sortedNames, c.name)
}
}
for _, cp := range cps {
2014-08-27 12:10:33 +04:00
sortCallbackProcessor(cp)
2014-01-25 15:35:21 +04:00
}
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() {
2015-02-17 03:34:01 +03:00
creates, updates, deletes, queries := []*callbackProcessor{}, []*callbackProcessor{}, []*callbackProcessor{}, []*callbackProcessor{}
2014-01-25 15:35:21 +04:00
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
}
2015-02-17 03:34:01 +03:00
var DefaultCallback = &callback{processors: []*callbackProcessor{}}