add beforeAfter muilt

This commit is contained in:
xxj
2023-03-10 16:40:30 +08:00
parent 4f7ef0ab68
commit c4ce5f8f42
4 changed files with 19 additions and 8 deletions

View File

@@ -98,8 +98,13 @@ func (b *_Base) beforCall(c *gin.Context, tvl, obj reflect.Value, req interface{
if bfobj, ok := obj.Interface().(GinBeforeAfter); ok { // 本类型
is = bfobj.GinBefore(info)
}
if is && b.beforeAfter != nil {
is = b.beforeAfter.GinBefore(info)
if is && len(b.beforeAfter) > 0 {
for _, call := range b.beforeAfter {
is = call.GinBefore(info)
if !is {
break
}
}
}
return info, is
}
@@ -109,8 +114,13 @@ func (b *_Base) afterCall(info *GinBeforeAfterInfo, obj reflect.Value) bool {
if bfobj, ok := obj.Interface().(GinBeforeAfter); ok { // 本类型
is = bfobj.GinAfter(info)
}
if is && b.beforeAfter != nil {
is = b.beforeAfter.GinAfter(info)
if is && len(b.beforeAfter) > 0 {
for _, call := range b.beforeAfter {
is = call.GinAfter(info)
if !is {
break
}
}
}
return is
}

Binary file not shown.

5
gen.go
View File

@@ -58,8 +58,9 @@ func AddGenOne(handFunName, routerPath string, methods []string, thirdParty []Ge
}
func GetThirdParty(routerPath, thirdParty string) (*GenThirdParty, error) {
if _, ok := _genMap[fmt.Sprintf("%v-%v", routerPath, thirdParty)]; ok {
tmp := _genMap[routerPath]
key := fmt.Sprintf("%v-%v", routerPath, thirdParty)
if _, ok := _genMap[key]; ok {
tmp := _genMap[key]
return &GenThirdParty{
Name: tmp.Name,
Note: tmp.Note,

View File

@@ -20,7 +20,7 @@ type _Base struct {
apiFun NewAPIFunc
apiType reflect.Type
outPath string // output path.输出目录
beforeAfter GinBeforeAfter
beforeAfter []GinBeforeAfter
isOutDoc bool
recoverErrorFunc RecoverErrorFunc
}
@@ -84,7 +84,7 @@ func WithOutDoc(b bool) Option {
// WithBeforeAfter set before and after call.设置对象调用前后执行中间件
func WithBeforeAfter(beforeAfter GinBeforeAfter) Option {
return optionFunc(func(o *_Base) {
o.beforeAfter = beforeAfter
o.beforeAfter = append(o.beforeAfter, beforeAfter)
})
}