This commit is contained in:
xing 2024-04-18 11:26:58 +08:00
parent 166bb08ed0
commit da858d55e0

View File

@ -18,27 +18,26 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type GinHook func(*gin.Engine) type GinSetter func(*gin.Engine)
var hookers mockmap.Map[string, GinHook] var setters mockmap.Map[string, GinSetter]
var setterHooks []func(item mockmap.Item[string, GinSetter]) (mockmap.Item[string, GinSetter], bool)
// SetGinAction 方便插件在init时使用 // SetGinAction 方便插件在init时使用
func SetGinAction(name string, hook GinHook, orders ...float64) { func SetGinAction(name string, hook GinSetter, orders ...float64) {
hookers.Set(name, hook, orders...) setters.Set(name, hook, orders...)
} }
func HookGinAction(name string, fn func(item mockmap.Item[string, GinHook]) mockmap.Item[string, GinHook]) { func HookGinSetter(fn func(item mockmap.Item[string, GinSetter]) (mockmap.Item[string, GinSetter], bool)) {
item := hookers.Get(name) setterHooks = append(setterHooks, fn)
if item.Name == "" {
return
}
t := fn(item)
SetGinAction(name, t.Value, t.Order)
} }
// DelGinAction 方便插件在init时使用 // DelGinSetter 方便插件在init时使用
func DelGinAction(name string) { func DelGinSetter(name string) {
hookers.Del(name) setterHooks = append(setterHooks, func(item mockmap.Item[string, GinSetter]) (mockmap.Item[string, GinSetter], bool) {
return item, item.Name != name
})
} }
func SetupRouter() *gin.Engine { func SetupRouter() *gin.Engine {
@ -132,11 +131,15 @@ func SetupRouter() *gin.Engine {
} }
}, 80.8) }, 80.8)
slice.SimpleSort(hookers, slice.DESC, func(t mockmap.Item[string, GinHook]) float64 { for _, hook := range setterHooks {
setters = slice.FilterAndMap(setters, hook)
}
slice.SimpleSort(setters, slice.DESC, func(t mockmap.Item[string, GinSetter]) float64 {
return t.Order return t.Order
}) })
for _, fn := range hookers { for _, fn := range setters {
fn.Value(r) fn.Value(r)
} }