mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
PartyConfigure: try to bind the struct's exported zero fields based on the registered dependencies (if any)
If the PartyConfigurator value accepts only static dependencies then we have zero performance penalty, exactly like a Controller (structure) works
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -323,6 +324,20 @@ func (api *APIBuilder) ConfigureContainer(builder ...func(*APIContainer)) *APICo
|
||||
return api.apiBuilderDI
|
||||
}
|
||||
|
||||
// RegisterDependency calls the `ConfigureContainer.RegisterDependency` method
|
||||
// with the provided value(s). See `HandleFunc` and `PartyConfigure` methods too.
|
||||
func (api *APIBuilder) RegisterDependency(dependencies ...interface{}) {
|
||||
diContainer := api.ConfigureContainer()
|
||||
for i, dependency := range dependencies {
|
||||
if dependency == nil {
|
||||
api.logger.Warnf("Party: %s: nil dependency on position: %d", api.relativePath, i)
|
||||
continue
|
||||
}
|
||||
|
||||
diContainer.RegisterDependency(dependency)
|
||||
}
|
||||
}
|
||||
|
||||
// HandleFunc registers a route on HTTP verb "method" and relative, to this Party, path.
|
||||
// It is like the `Handle` method but it accepts one or more "handlersFn" functions
|
||||
// that each one of them can accept any input arguments as the HTTP request and
|
||||
@@ -862,6 +877,11 @@ type PartyConfigurator interface {
|
||||
// It initializes a new children Party and executes the PartyConfigurator's Configure.
|
||||
// Useful when the api's dependencies amount are too much to pass on a function.
|
||||
//
|
||||
// As an exception, if the end-developer registered one or more dependencies upfront through
|
||||
// RegisterDependencies or ConfigureContainer.RegisterDependency methods
|
||||
// and "p" is a pointer to a struct then try to bind the unset/zero exported fields
|
||||
// to the registered dependencies, just like we do with Controllers.
|
||||
//
|
||||
// Usage:
|
||||
// app.PartyConfigure("/users", &api.UsersAPI{UserRepository: ..., ...})
|
||||
// Where UsersAPI looks like:
|
||||
@@ -870,12 +890,23 @@ type PartyConfigurator interface {
|
||||
// router.Get("/{id:uuid}", api.getUser)
|
||||
// [...]
|
||||
// }
|
||||
// Usage with (static) dependencies:
|
||||
// app.RegisterDependency(userRepo, ...)
|
||||
// app.PartyConfigure("/users", &api.UsersAPI{})
|
||||
func (api *APIBuilder) PartyConfigure(relativePath string, partyReg ...PartyConfigurator) Party {
|
||||
child := api.Party(relativePath)
|
||||
for _, p := range partyReg {
|
||||
if p != nil {
|
||||
p.Configure(child)
|
||||
if p == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(api.apiBuilderDI.Container.Dependencies) > 0 {
|
||||
if typ := reflect.TypeOf(p); typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct {
|
||||
api.apiBuilderDI.Container.Struct(p, -1)
|
||||
}
|
||||
}
|
||||
|
||||
p.Configure(child)
|
||||
}
|
||||
return child
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user