Skip to content

Commit

Permalink
✨ feat: Structured routing supports PreInvoker
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Jul 21, 2024
1 parent 92bccaf commit b7373a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
32 changes: 30 additions & 2 deletions znet/bind_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"strings"

"github.com/sohaha/zlsgo/zdi"
"github.com/sohaha/zlsgo/zreflect"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/zutil"
Expand All @@ -22,6 +23,7 @@ func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) err
if !of.IsValid() {
return nil
}

initFn := of.MethodByName("Init")
if initFn.IsValid() {
before, ok := initFn.Interface().(func(e *Engine))
Expand All @@ -36,12 +38,26 @@ 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" {
if m.Name == "Init" || m.Name == "PreInvoker" {
return nil
}
path, method, key := "", "", ""
Expand All @@ -67,7 +83,17 @@ func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) err
method = strings.ToUpper(info[1])
}

fn := value.Interface()
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()
}

handleName := strings.Join([]string{typeOf.PkgPath(), typeOf.Name(), m.Name}, ".")
if e.BindStructCase != nil {
path = e.BindStructCase(path)
Expand All @@ -88,6 +114,8 @@ func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) err
}
if path == "/" {
path = ""
} else if path == "s" {
path = "/"
}

var (
Expand Down
22 changes: 7 additions & 15 deletions znet/bind_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,48 @@ import (
"testing"

"github.com/sohaha/zlsgo"
"github.com/sohaha/zlsgo/zdi"
"github.com/sohaha/zlsgo/ztype"
)

type testErrController struct {
}
type testErrController struct{}

func (t *testErrController) Init(e *Engine) error {
return errors.New("test error")
}

type testController struct {
}
type testController struct{}

func (t *testController) Init(e *Engine) {
e.Log.Debug("initialization")
}

func (t *testController) GETUser(_ *Context) {
func (t *testController) PreInvoker() []zdi.PreInvoker {
return []zdi.PreInvoker{(*invokerCodeText)(nil)}
}

func (t *testController) GETUser(_ *Context) {
}

func (t *testController) GETGetUser(_ *Context) {

}

func (t *testController) POSTUserInfo(_ *Context) {

}

func (t *testController) PUTUserInfo(_ *Context) {

}

func (t *testController) DELETEUserInfo(_ *Context) {

}

func (t *testController) PATCHUserInfo(_ *Context) {

}

func (t *testController) HEADUserInfo(_ *Context) {

}

func (t *testController) OPTIONSUserInfo(_ *Context) {

}

func (t *testController) AnyOk(c *Context) error {
Expand All @@ -63,15 +58,12 @@ func (t *testController) AnyOk(c *Context) error {
}

func (t *testController) IDGET(_ *Context) {

}

func (t *testController) IDGETUser(_ *Context) {

}

func (t *testController) FullGETFile(_ *Context) {

}

func TestBindStruct(t *testing.T) {
Expand Down

0 comments on commit b7373a4

Please sign in to comment.