diff --git a/HISTORY.md b/HISTORY.md index 5741925f..a3559d7c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -82,7 +82,7 @@ Container *hero.Container // OnError adds an error handler for this Party's DI Hero Container and its handlers (or controllers). // The "errorHandler" handles any error may occurred and returned // during dependencies injection of the Party's hero handlers or from the handlers themselves. -OnError(errorHandler func(context.Context, error)) +OnError(errorHandler func(iris.Context, error)) ``` ```go @@ -92,7 +92,6 @@ OnError(errorHandler func(context.Context, error)) // * {structValue} // * func(accepts ) returns or (, error) // * func(accepts iris.Context) returns or (, error) -// * func(accepts1 iris.Context, accepts2 *hero.Input) returns or (, error) // // A Dependency can accept a previous registered dependency and return a new one or the same updated. // * func(accepts1 , accepts2 ) returns or (, error) or error @@ -104,8 +103,20 @@ OnError(errorHandler func(context.Context, error)) // - RegisterDependency(func(ctx iris.Context) User {...}) // - RegisterDependency(func(User) OtherResponse {...}) RegisterDependency(dependency interface{}) + +// UseResultHandler adds a result handler to the Container. +// A result handler can be used to inject the returned struct value +// from a request handler or to replace the default renderer. +UseResultHandler(handler func(next iris.ResultHandler) iris.ResultHandler) ``` +
ResultHandler + +```go +type ResultHandler func(ctx iris.Context, v interface{}) error +``` +
+ ```go // Use same as a common Party's "Use" but it accepts dynamic functions as its "handlersFn" input. Use(handlersFn ...interface{}) @@ -169,7 +180,7 @@ Other Improvements: - A result of can implement the new `hero.PreflightResult` interface which contains a single method of `Preflight(iris.Context) error`. If this method exists on a custom struct value which is returned from a handler then it will fire that `Preflight` first and if not errored then it will cotninue by sending the struct value as JSON(by-default) response body. -- `ctx.JSON, JSONP, XML`: if `iris.WithOptimizations` is NOT passed on `app.Run/Listen` then the indentation defaults to `" "` (two spaces) otherwise it is empty or the provided value. +- `ctx.JSON, JSONP, XML`: if `iris.WithOptimizations` is NOT passed on `app.Run/Listen` then the indentation defaults to `" "` (four spaces) and `" "` respectfully otherwise it is empty or the provided value. - Hero Handlers (and `app.ConfigureContainer().Handle`) do not have to require `iris.Context` just to call `ctx.Next()` anymore, this is done automatically now. diff --git a/core/router/api_container.go b/core/router/api_container.go index b3def2d1..0e11da53 100644 --- a/core/router/api_container.go +++ b/core/router/api_container.go @@ -47,7 +47,6 @@ func (api *APIContainer) OnError(errorHandler func(context.Context, error)) { // * {structValue} // * func(accepts ) returns or (, error) // * func(accepts iris.Context) returns or (, error) -// * func(accepts1 iris.Context, accepts2 *hero.Input) returns or (, error) // // A Dependency can accept a previous registered dependency and return a new one or the same updated. // * func(accepts1 , accepts2 ) returns or (, error) or error @@ -65,8 +64,8 @@ func (api *APIContainer) RegisterDependency(dependency interface{}) *hero.Depend } // UseResultHandler adds a result handler to the Container. -// A result handler can be used to inject the struct value -// or to replace the default renderer. +// A result handler can be used to inject the returned struct value +// from a request handler or to replace the default renderer. func (api *APIContainer) UseResultHandler(handler func(next hero.ResultHandler) hero.ResultHandler) *APIContainer { api.Container.UseResultHandler(handler) return api diff --git a/hero/container.go b/hero/container.go index 2f4c01a0..ab08b1a0 100644 --- a/hero/container.go +++ b/hero/container.go @@ -155,8 +155,8 @@ func (c *Container) Register(dependency interface{}) *Dependency { } // UseResultHandler adds a result handler to the Container. -// A result handler can be used to inject the struct value -// or to replace the default renderer. +// A result handler can be used to inject the returned struct value +// from a request handler or to replace the default renderer. func (c *Container) UseResultHandler(handler func(next ResultHandler) ResultHandler) *Container { c.resultHandlers = append(c.resultHandlers, handler) return c diff --git a/hero/container_test.go b/hero/container_test.go index 1159f0a2..8399ebcf 100644 --- a/hero/container_test.go +++ b/hero/container_test.go @@ -97,3 +97,35 @@ func TestContainerInject(t *testing.T) { t.Fatalf("[service] expected: %#+v but got: %#+v", expected3, got3) } } + +func TestContainerUseResultHandler(t *testing.T) { + c := New() + resultLogger := func(next ResultHandler) ResultHandler { + return func(ctx iris.Context, v interface{}) error { + t.Logf("%#+v", v) + return next(ctx, v) + } + } + + c.UseResultHandler(resultLogger) + expectedResponse := map[string]interface{}{"injected": true} + c.UseResultHandler(func(next ResultHandler) ResultHandler { + return func(ctx iris.Context, v interface{}) error { + return next(ctx, expectedResponse) + } + }) + c.UseResultHandler(resultLogger) + + handler := c.Handler(func(id int) testOutput { + return testOutput{ + ID: id, + Name: "kataras", + } + }) + + app := iris.New() + app.Get("/{id:int}", handler) + + e := httptest.New(t, app) + e.GET("/42").Expect().Status(httptest.StatusOK).JSON().Equal(expectedResponse) +}