diff --git a/mvc2/handler.go b/mvc2/handler.go index 1b15f105..a0ea70cc 100644 --- a/mvc2/handler.go +++ b/mvc2/handler.go @@ -33,15 +33,30 @@ var ( emptyIn = []reflect.Value{} ) -func makeHandler(handler interface{}, binders []*InputBinder) context.Handler { +// MustMakeHandler calls the `MakeHandler` and returns its first resultthe low-level handler), see its docs. +// It panics on error. +func MustMakeHandler(handler interface{}, binders []*InputBinder) context.Handler { + h, err := MakeHandler(handler, binders) + if err != nil { + panic(err) + } + return h +} + +// MakeHandler accepts a "handler" function which can accept any input that matches +// with the "binders" and any output, that matches the mvc types, like string, int (string,int), +// custom structs, Result(View | Response) and anything that you already know that mvc implementation supports, +// and returns a low-level `context/iris.Handler` which can be used anywhere in the Iris Application, +// as middleware or as simple route handler or party handler or subdomain handler-router. +func MakeHandler(handler interface{}, binders []*InputBinder) (context.Handler, error) { if err := validateHandler(handler); err != nil { golog.Errorf("mvc handler: %v", err) - return nil + return nil, err } if h, is := isContextHandler(handler); is { golog.Warnf("mvc handler: you could just use the low-level API to register a context handler instead") - return h + return h, nil } typ := indirectTyp(reflect.TypeOf(handler)) @@ -53,14 +68,15 @@ func makeHandler(handler interface{}, binders []*InputBinder) context.Handler { m := getBindersForInput(binders, typIn...) if len(m) != n { - golog.Errorf("mvc handler: input arguments length(%d) and valid binders length(%d) are not equal", n, len(m)) - return nil + err := fmt.Errorf("input arguments length(%d) of types(%s) and valid binders length(%d) are not equal", n, typIn, len(m)) + golog.Errorf("mvc handler: %v", err) + return nil, err } hasIn := len(m) > 0 fn := reflect.ValueOf(handler) - return func(ctx context.Context) { + resultHandler := func(ctx context.Context) { if !hasIn { methodfunc.DispatchFuncResult(ctx, fn.Call(emptyIn)) return @@ -81,4 +97,6 @@ func makeHandler(handler interface{}, binders []*InputBinder) context.Handler { } methodfunc.DispatchFuncResult(ctx, fn.Call(in)) } + + return resultHandler, nil } diff --git a/mvc2/handler_test.go b/mvc2/handler_test.go index 3564426c..046c0754 100644 --- a/mvc2/handler_test.go +++ b/mvc2/handler_test.go @@ -1,11 +1,96 @@ -package mvc2 +package mvc2_test -/* -TODO: +// black-box -Test that as we test the rest, with -a fake context, and after move again to -the mvc_test.go which will contain -the overall high-level (black-box) tests. +import ( + "fmt" + "testing" -*/ + "github.com/kataras/iris" + "github.com/kataras/iris/httptest" + . "github.com/kataras/iris/mvc2" +) + +// dynamic func +type testUserStruct struct { + ID int64 + Username string +} + +func testBinderFunc(ctx iris.Context) testUserStruct { + id, _ := ctx.Params().GetInt64("id") + username := ctx.Params().Get("username") + return testUserStruct{ + ID: id, + Username: username, + } +} + +// service +type ( + testService interface { + Say(string) string + } + testServiceImpl struct { + prefix string + } +) + +func (s *testServiceImpl) Say(message string) string { + return s.prefix + " " + message +} + +func TestMakeHandler(t *testing.T) { + binders := []*InputBinder{ + // #1 + MustMakeFuncInputBinder(testBinderFunc), + // #2 + MustMakeServiceInputBinder(&testServiceImpl{prefix: "say"}), + // #3 + MustMakeFuncInputBinder(func(ctx iris.Context) string { + return ctx.Params().Get("param") + }), + } + + var ( + // a context as first input arg, which is not needed to be binded manually, + // and a user struct which is binded to the input arg by the #1 func(ctx) any binder. + consumeUserHandler = func(ctx iris.Context, user testUserStruct) { + ctx.JSON(user) + } + h1 = MustMakeHandler(consumeUserHandler, binders) + + // just one input arg, the service which is binded by the #2 service binder. + consumeServiceHandler = func(service testService) string { + return service.Say("something") + } + h2 = MustMakeHandler(consumeServiceHandler, binders) + + // just one input arg, a standar string which is binded by the #3 func(ctx) any binder. + consumeParamHandler = func(myParam string) string { + return "param is: " + myParam + } + h3 = MustMakeHandler(consumeParamHandler, binders) + ) + + app := iris.New() + app.Get("/{id:long}/{username:string}", h1) + app.Get("/service", h2) + app.Get("/param/{param:string}", h3) + + expectedUser := testUserStruct{ + ID: 42, + Username: "kataras", + } + + e := httptest.New(t, app) + // 1 + e.GET(fmt.Sprintf("/%d/%s", expectedUser.ID, expectedUser.Username)).Expect().Status(httptest.StatusOK). + JSON().Equal(expectedUser) + // 2 + e.GET("/service").Expect().Status(httptest.StatusOK). + Body().Equal("say something") + // 3 + e.GET("/param/the_param_value").Expect().Status(httptest.StatusOK). + Body().Equal("param is: the_param_value") +} diff --git a/mvc2/mvc.go b/mvc2/mvc.go index 814d6a7d..a737aa34 100644 --- a/mvc2/mvc.go +++ b/mvc2/mvc.go @@ -45,5 +45,6 @@ func (m *Mvc) RegisterService(services ...interface{}) error { } func (m *Mvc) Handler(handler interface{}) context.Handler { - return makeHandler(handler, m.binders) + h, _ := MakeHandler(handler, m.binders) // it logs errors already, so on any error the "h" will be nil. + return h } diff --git a/mvc2/mvc_test.go b/mvc2/mvc_test.go deleted file mode 100644 index 2a611370..00000000 --- a/mvc2/mvc_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package mvc2_test - -import ( - "testing" - - "github.com/kataras/iris" - "github.com/kataras/iris/httptest" - "github.com/kataras/iris/mvc2" -) - -var ( - lowLevelHandler = func(ctx iris.Context) { - ctx.Writef("low-level handler") - } -) - -func TestHandler(t *testing.T) { - app := iris.New() - m := mvc2.New() - - // should just return a context.Handler - // without performance penalties. - app.Get("/", m.Handler(lowLevelHandler)) - - e := httptest.New(t, app, httptest.LogLevel("debug")) - // 1 - e.GET("/").Expect().Status(httptest.StatusOK). - Body().Equal("low-level handler") - -} diff --git a/mvc2/reflect.go b/mvc2/reflect.go index 0661464d..3e45cc1a 100644 --- a/mvc2/reflect.go +++ b/mvc2/reflect.go @@ -33,14 +33,15 @@ func isFunc(typ reflect.Type) bool { return typ.Kind() == reflect.Func } -func equalTypes(in reflect.Type, v reflect.Type) bool { - if in == v { +func equalTypes(got reflect.Type, expected reflect.Type) bool { + if got == expected { return true } - // if accepts an interface, check if the given "v" type does - // implement this. - if in.Kind() == reflect.Interface { - return v.Implements(in) + // if accepts an interface, check if the given "got" type does + // implement this "expected" user handler's input argument. + if expected.Kind() == reflect.Interface { + // fmt.Printf("expected interface = %s and got to set on the arg is: %s\n", expected.String(), got.String()) + return got.Implements(expected) } return false }