From 76c11520acd6141a2a8b3a79b911c1a170814e94 Mon Sep 17 00:00:00 2001 From: seekwe Date: Sun, 21 Jul 2024 16:05:53 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Structured=20routing=20supp?= =?UTF-8?q?orts=20PreInvoker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- znet/bind_router.go | 44 +++++++++++++++++----------------------- znet/bind_router_test.go | 9 +++----- znet/inject.go | 32 ++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/znet/bind_router.go b/znet/bind_router.go index b126b02..78868fc 100644 --- a/znet/bind_router.go +++ b/znet/bind_router.go @@ -11,6 +11,23 @@ import ( "github.com/sohaha/zlsgo/zutil" ) +var preInvokers = make([]reflect.Type, 0) + +// RegisterPreInvoker Register Pre Invoker +func RegisterPreInvoker(invoker ...zdi.PreInvoker) { +loop: + for i := range invoker { + typ := zreflect.TypeOf(invoker[i]) + for i := range preInvokers { + if preInvokers[i] == typ { + continue loop + } + } + + preInvokers = append(preInvokers, typ) + } +} + // BindStruct Bind Struct func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) error { g := e.Group(prefix) @@ -40,24 +57,10 @@ func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) err } } - preInvokerFn := of.MethodByName("PreInvoker") - var preInvokers []reflect.Type - if preInvokerFn.IsValid() { - before, ok := preInvokerFn.Interface().(func() []zdi.PreInvoker) - if ok { - pres := before() - for i := range pres { - preInvokers = append(preInvokers, reflect.TypeOf(pres[i])) - } - } else { - return fmt.Errorf("func: [%s] is not an effective routing method", "PreInvoker") - } - } - typeOf := reflect.Indirect(of).Type() return zutil.TryCatch(func() error { return zreflect.ForEachMethod(of, func(i int, m reflect.Method, value reflect.Value) error { - if m.Name == "Init" || m.Name == "PreInvoker" { + if m.Name == "Init" { return nil } path, method, key := "", "", "" @@ -83,16 +86,7 @@ func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) err method = strings.ToUpper(info[1]) } - var fn interface{} - for i := range preInvokers { - if value.Type().ConvertibleTo(preInvokers[i]) { - fn = value.Convert(preInvokers[i]).Interface() - break - } - } - if fn == nil { - fn = value.Interface() - } + fn := value.Interface() handleName := strings.Join([]string{typeOf.PkgPath(), typeOf.Name(), m.Name}, ".") if e.BindStructCase != nil { diff --git a/znet/bind_router_test.go b/znet/bind_router_test.go index c6c9860..f4268f1 100644 --- a/znet/bind_router_test.go +++ b/znet/bind_router_test.go @@ -8,7 +8,6 @@ import ( "testing" "github.com/sohaha/zlsgo" - "github.com/sohaha/zlsgo/zdi" "github.com/sohaha/zlsgo/ztype" ) @@ -22,16 +21,14 @@ type testController struct{} func (t *testController) Init(e *Engine) { e.Log.Debug("initialization") -} - -func (t *testController) PreInvoker() []zdi.PreInvoker { - return []zdi.PreInvoker{(*invokerCodeText)(nil)} + RegisterPreInvoker((invokerCodeText)(nil), (*CustomInvoker)(nil)) } func (t *testController) GETUser(_ *Context) { } -func (t *testController) GETGetUser(_ *Context) { +func (t *testController) GETGetUser(_ *Context) (b []byte) { + return []byte("GETUser") } func (t *testController) POSTUserInfo(_ *Context) { diff --git a/znet/inject.go b/znet/inject.go index d0e5520..9ca58b2 100644 --- a/znet/inject.go +++ b/znet/inject.go @@ -143,21 +143,33 @@ func (utils) ParseHandlerFunc(h Handler) (fn handlerFn) { } default: val := zreflect.ValueOf(v) - if val.Kind() != reflect.Func { - b := ztype.ToBytes(v) - isJSON := zjson.ValidBytes(b) - return func(c *Context) error { - c.Byte(http.StatusOK, b) - if isJSON { - c.SetContentType(ContentTypeJSON) + + var fn interface{} + for i := range preInvokers { + if val.Type().ConvertibleTo(preInvokers[i]) { + fn = val.Convert(preInvokers[i]).Interface() + break + } + } + + if fn == nil { + if val.Kind() != reflect.Func { + b := ztype.ToBytes(v) + isJSON := zjson.ValidBytes(b) + return func(c *Context) error { + c.Byte(http.StatusOK, b) + if isJSON { + c.SetContentType(ContentTypeJSON) + } + return nil } - return nil + // panic("znet Handler is not a function: " + val.Kind().String()) } - // panic("znet Handler is not a function: " + val.Kind().String()) + fn = v } return func(c *Context) error { - v, err := c.injector.Invoke(v) + v, err := c.injector.Invoke(fn) if c.stopHandle.Load() { return nil