1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-28 07:17:06 +00:00

Implement ResultHandler as requested at: https://github.com/kataras/iris/issues/1465

Former-commit-id: 9d76c2f00766afd53cf6e591c25f861f179dd817
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-04-18 22:40:47 +03:00
parent 68c5883bce
commit dcf02480b3
7 changed files with 94 additions and 46 deletions

View File

@@ -33,6 +33,10 @@ type Container struct {
// GetErrorHandler should return a valid `ErrorHandler` to handle bindings AND handler dispatch errors.
// Defaults to a functon which returns the `DefaultErrorHandler`.
GetErrorHandler func(context.Context) ErrorHandler // cannot be nil.
// resultHandlers is a list of functions that serve the return struct value of a function handler.
// Defaults to "defaultResultHandler" but it can be overridden.
resultHandlers []func(next ResultHandler) ResultHandler
}
// BuiltinDependencies is a list of builtin dependencies that are added on Container's initilization.
@@ -103,6 +107,7 @@ func (c *Container) Clone() *Container {
clonedDeps := make([]*Dependency, len(c.Dependencies))
copy(clonedDeps, c.Dependencies)
cloned.Dependencies = clonedDeps
cloned.resultHandlers = c.resultHandlers
return cloned
}
@@ -149,6 +154,14 @@ func (c *Container) Register(dependency interface{}) *Dependency {
return d
}
// 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.
func (c *Container) UseResultHandler(handler func(next ResultHandler) ResultHandler) *Container {
c.resultHandlers = append(c.resultHandlers, handler)
return c
}
// Handler accepts a "handler" function which can accept any input arguments that match
// with the Container's `Dependencies` and any output result; like string, int (string,int),
// custom structs, Result(View | Response) and anything you can imagine.