From c4ce5f8f4272cc61299e36b362ecaea6e19dfe10 Mon Sep 17 00:00:00 2001 From: xxj <346944475@qq.com> Date: Fri, 10 Mar 2023 16:40:30 +0800 Subject: [PATCH] add beforeAfter muilt --- common.go | 18 ++++++++++++++---- conf/gen_router.data | Bin 509 -> 557 bytes gen.go | 5 +++-- ginrpc.go | 4 ++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/common.go b/common.go index 7399a6d..e31ca7b 100644 --- a/common.go +++ b/common.go @@ -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 } diff --git a/conf/gen_router.data b/conf/gen_router.data index d391c754e65f144b3a4a0723176ac97723aa2240..a55b6dcb786a226ddb1b978171d7fcf716531625 100644 GIT binary patch delta 140 zcmey%yq0By1f!+d|4wE`Mqc;SypW8{qLhHdqLNC+|6L4>%#194iMgqaJPeF1E{P?H zV1{3Q2}p#2;s2e<9*oKojEszIMfs&AsYQ(3iHV6iNl8gM$;rtKld~D+*ch4h^z;}f ZcQI@|Sso1fzlO|4wE`Mqc;SypW8{qLhHdqLNC+|6L4>OpGjkiMgqaJPeF1E{P?H rKn4TD|E-e)8I>pZFv>A9P2Rw$4J02jYJf>mPGbhfKPQAO?=S!W+<_Ve diff --git a/gen.go b/gen.go index 351622d..2a736d6 100644 --- a/gen.go +++ b/gen.go @@ -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, diff --git a/ginrpc.go b/ginrpc.go index 25ef579..6b1a7e6 100644 --- a/ginrpc.go +++ b/ginrpc.go @@ -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) }) }