mirror of
https://github.com/kataras/iris.git
synced 2026-01-10 21:45:57 +00:00
Add mvc.Application.EnableStructDependents() and app.ConfigureContainer().EnableStructDependents()
relative to: #2158
This commit is contained in:
@@ -291,7 +291,7 @@ func getBindingsForFunc(fn reflect.Value, dependencies []*Dependency, disablePay
|
||||
return bindings
|
||||
}
|
||||
|
||||
func getBindingsForStruct(v reflect.Value, dependencies []*Dependency, markExportedFieldsAsRequired bool, disablePayloadAutoBinding bool, matchDependency DependencyMatcher, paramsCount int, sorter Sorter) (bindings []*binding) {
|
||||
func getBindingsForStruct(v reflect.Value, dependencies []*Dependency, markExportedFieldsAsRequired bool, disablePayloadAutoBinding, enableStructDependents bool, matchDependency DependencyMatcher, paramsCount int, sorter Sorter) (bindings []*binding) {
|
||||
typ := indirectType(v.Type())
|
||||
if typ.Kind() != reflect.Struct {
|
||||
panic(fmt.Sprintf("bindings: unresolved: not a struct type: %#+v", v))
|
||||
@@ -303,7 +303,7 @@ func getBindingsForStruct(v reflect.Value, dependencies []*Dependency, markExpor
|
||||
for _, f := range nonZero {
|
||||
// fmt.Printf("Controller [%s] | NonZero | Field Index: %v | Field Type: %s\n", typ, f.Index, f.Type)
|
||||
bindings = append(bindings, &binding{
|
||||
Dependency: newDependency(elem.FieldByIndex(f.Index).Interface(), disablePayloadAutoBinding, nil),
|
||||
Dependency: newDependency(elem.FieldByIndex(f.Index).Interface(), disablePayloadAutoBinding, enableStructDependents, nil),
|
||||
Input: newStructFieldInput(f),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -524,7 +524,7 @@ func TestBindingsForStruct(t *testing.T) {
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
bindings := getBindingsForStruct(reflect.ValueOf(tt.Value), tt.Registered, false, false, DefaultDependencyMatcher, 0, nil)
|
||||
bindings := getBindingsForStruct(reflect.ValueOf(tt.Value), tt.Registered, false, false, false, DefaultDependencyMatcher, 0, nil)
|
||||
|
||||
if expected, got := len(tt.Expected), len(bindings); expected != got {
|
||||
t.Logf("[%d] expected bindings length to be: %d but got: %d:\n", i, expected, got)
|
||||
@@ -565,5 +565,5 @@ func TestBindingsForStructMarkExportedFieldsAsRequred(t *testing.T) {
|
||||
}
|
||||
|
||||
// should panic if fail.
|
||||
_ = getBindingsForStruct(reflect.ValueOf(new(controller)), dependencies, true, true, DefaultDependencyMatcher, 0, nil)
|
||||
_ = getBindingsForStruct(reflect.ValueOf(new(controller)), dependencies, true, true, false, DefaultDependencyMatcher, 0, nil)
|
||||
}
|
||||
|
||||
@@ -57,6 +57,13 @@ type Container struct {
|
||||
// if at least one input binding depends on the request and not in a static structure.
|
||||
DisableStructDynamicBindings bool
|
||||
|
||||
// StructDependents if true then the Container will try to resolve
|
||||
// the fields of a struct value, if any, when it's a dependent struct value
|
||||
// based on the previous registered dependencies.
|
||||
//
|
||||
// Defaults to false.
|
||||
EnableStructDependents bool // this can be renamed to IndirectDependencies?.
|
||||
|
||||
// DependencyMatcher holds the function that compares equality between
|
||||
// a dependency with an input. Defaults to DefaultMatchDependencyFunc.
|
||||
DependencyMatcher DependencyMatcher
|
||||
@@ -146,11 +153,11 @@ func (c *Container) fillReport(fullName string, bindings []*binding) {
|
||||
// Contains the iris context, standard context, iris sessions and time dependencies.
|
||||
var BuiltinDependencies = []*Dependency{
|
||||
// iris context dependency.
|
||||
newDependency(func(ctx *context.Context) *context.Context { return ctx }, true, nil).Explicitly(),
|
||||
newDependency(func(ctx *context.Context) *context.Context { return ctx }, true, false, nil).Explicitly(),
|
||||
// standard context dependency.
|
||||
newDependency(func(ctx *context.Context) stdContext.Context {
|
||||
return ctx.Request().Context()
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// iris session dependency.
|
||||
newDependency(func(ctx *context.Context) *sessions.Session {
|
||||
session := sessions.Get(ctx)
|
||||
@@ -163,35 +170,35 @@ var BuiltinDependencies = []*Dependency{
|
||||
}
|
||||
|
||||
return session
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// application's logger.
|
||||
newDependency(func(ctx *context.Context) *golog.Logger {
|
||||
return ctx.Application().Logger()
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// time.Time to time.Now dependency.
|
||||
newDependency(func(ctx *context.Context) time.Time {
|
||||
return time.Now()
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// standard http Request dependency.
|
||||
newDependency(func(ctx *context.Context) *http.Request {
|
||||
return ctx.Request()
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// standard http ResponseWriter dependency.
|
||||
newDependency(func(ctx *context.Context) http.ResponseWriter {
|
||||
return ctx.ResponseWriter()
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// http headers dependency.
|
||||
newDependency(func(ctx *context.Context) http.Header {
|
||||
return ctx.Request().Header
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// Client IP.
|
||||
newDependency(func(ctx *context.Context) net.IP {
|
||||
return net.ParseIP(ctx.RemoteAddr())
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// Status Code (special type for MVC HTTP Error handler to not conflict with path parameters)
|
||||
newDependency(func(ctx *context.Context) Code {
|
||||
return Code(ctx.GetStatusCode())
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// Context Error. May be nil
|
||||
newDependency(func(ctx *context.Context) Err {
|
||||
err := ctx.GetErr()
|
||||
@@ -199,7 +206,7 @@ var BuiltinDependencies = []*Dependency{
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}, true, nil).Explicitly(),
|
||||
}, true, false, nil).Explicitly(),
|
||||
// Context User, e.g. from basic authentication.
|
||||
newDependency(func(ctx *context.Context) context.User {
|
||||
u := ctx.User()
|
||||
@@ -208,7 +215,7 @@ var BuiltinDependencies = []*Dependency{
|
||||
}
|
||||
|
||||
return u
|
||||
}, true, nil),
|
||||
}, true, false, nil),
|
||||
// payload and param bindings are dynamically allocated and declared at the end of the `binding` source file.
|
||||
}
|
||||
|
||||
@@ -254,6 +261,7 @@ func (c *Container) Clone() *Container {
|
||||
cloned.Dependencies = clonedDeps
|
||||
cloned.DisablePayloadAutoBinding = c.DisablePayloadAutoBinding
|
||||
cloned.DisableStructDynamicBindings = c.DisableStructDynamicBindings
|
||||
cloned.EnableStructDependents = c.EnableStructDependents
|
||||
cloned.MarkExportedFieldsAsRequired = c.MarkExportedFieldsAsRequired
|
||||
cloned.resultHandlers = c.resultHandlers
|
||||
// Reports are not cloned.
|
||||
@@ -291,7 +299,7 @@ func Register(dependency interface{}) *Dependency {
|
||||
// - Register(func(ctx iris.Context) User {...})
|
||||
// - Register(func(User) OtherResponse {...})
|
||||
func (c *Container) Register(dependency interface{}) *Dependency {
|
||||
d := newDependency(dependency, c.DisablePayloadAutoBinding, c.DependencyMatcher, c.Dependencies...)
|
||||
d := newDependency(dependency, c.DisablePayloadAutoBinding, c.EnableStructDependents, c.DependencyMatcher, c.Dependencies...)
|
||||
if d.DestType == nil {
|
||||
// prepend the dynamic dependency so it will be tried at the end
|
||||
// (we don't care about performance here, design-time)
|
||||
|
||||
@@ -38,6 +38,13 @@ type (
|
||||
|
||||
// Match holds the matcher. Defaults to the Container's one.
|
||||
Match DependencyMatchFunc
|
||||
|
||||
// StructDependents if true then the Container will try to resolve
|
||||
// the fields of a struct value, if any, when it's a dependent struct value
|
||||
// based on the previous registered dependencies.
|
||||
//
|
||||
// Defaults to false.
|
||||
StructDependents bool
|
||||
}
|
||||
)
|
||||
|
||||
@@ -50,6 +57,12 @@ func (d *Dependency) Explicitly() *Dependency {
|
||||
return d
|
||||
}
|
||||
|
||||
// EnableStructDependents sets StructDependents to true.
|
||||
func (d *Dependency) EnableStructDependents() *Dependency {
|
||||
d.StructDependents = true
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *Dependency) String() string {
|
||||
sourceLine := d.Source.String()
|
||||
val := d.OriginalValue
|
||||
@@ -63,10 +76,16 @@ func (d *Dependency) String() string {
|
||||
//
|
||||
// See `Container.Handler` for more.
|
||||
func NewDependency(dependency interface{}, funcDependencies ...*Dependency) *Dependency { // used only on tests.
|
||||
return newDependency(dependency, false, nil, funcDependencies...)
|
||||
return newDependency(dependency, false, false, nil, funcDependencies...)
|
||||
}
|
||||
|
||||
func newDependency(dependency interface{}, disablePayloadAutoBinding bool, matchDependency DependencyMatcher, funcDependencies ...*Dependency) *Dependency {
|
||||
func newDependency(
|
||||
dependency interface{},
|
||||
disablePayloadAutoBinding bool,
|
||||
enableStructDependents bool,
|
||||
matchDependency DependencyMatcher,
|
||||
funcDependencies ...*Dependency,
|
||||
) *Dependency {
|
||||
if dependency == nil {
|
||||
panic(fmt.Sprintf("bad value: nil: %T", dependency))
|
||||
}
|
||||
@@ -86,8 +105,9 @@ func newDependency(dependency interface{}, disablePayloadAutoBinding bool, match
|
||||
}
|
||||
|
||||
dest := &Dependency{
|
||||
Source: newSource(v),
|
||||
OriginalValue: dependency,
|
||||
Source: newSource(v),
|
||||
OriginalValue: dependency,
|
||||
StructDependents: enableStructDependents,
|
||||
}
|
||||
dest.Match = ToDependencyMatchFunc(dest, matchDependency)
|
||||
|
||||
@@ -171,7 +191,7 @@ func fromStructValueOrDependentStructValue(v reflect.Value, disablePayloadAutoBi
|
||||
return false
|
||||
}
|
||||
|
||||
if len(prevDependencies) == 0 { // As a non depedent struct.
|
||||
if len(prevDependencies) == 0 || !dest.StructDependents { // As a non depedent struct.
|
||||
// We must make this check so we can avoid the auto-filling of
|
||||
// the dependencies from Iris builtin dependencies.
|
||||
return fromStructValue(v, dest)
|
||||
@@ -180,11 +200,13 @@ func fromStructValueOrDependentStructValue(v reflect.Value, disablePayloadAutoBi
|
||||
// Check if it's a builtin dependency (e.g an MVC Application (see mvc.go#newApp)),
|
||||
// if it's and registered without a Dependency wrapper, like the rest builtin dependencies,
|
||||
// then do NOT try to resolve its fields.
|
||||
//
|
||||
// Although EnableStructDependents is false by default, we must check if it's a builtin dependency for any case.
|
||||
if strings.HasPrefix(indirectType(v.Type()).PkgPath(), "github.com/kataras/iris/v12") {
|
||||
return fromStructValue(v, dest)
|
||||
}
|
||||
|
||||
bindings := getBindingsForStruct(v, prevDependencies, false, disablePayloadAutoBinding, DefaultDependencyMatcher, -1, nil)
|
||||
bindings := getBindingsForStruct(v, prevDependencies, false, disablePayloadAutoBinding, dest.StructDependents, DefaultDependencyMatcher, -1, nil)
|
||||
if len(bindings) == 0 {
|
||||
return fromStructValue(v, dest) // same as above.
|
||||
}
|
||||
|
||||
@@ -154,6 +154,10 @@ func lookupFields(elem reflect.Value, skipUnexported bool, onlyZeros bool, paren
|
||||
// Note: embedded pointers are not supported.
|
||||
// elem = reflect.Indirect(elem)
|
||||
elemTyp := elem.Type()
|
||||
if elemTyp.Kind() == reflect.Pointer {
|
||||
return
|
||||
}
|
||||
|
||||
for i, n := 0, elem.NumField(); i < n; i++ {
|
||||
field := elemTyp.Field(i)
|
||||
fieldValue := elem.Field(i)
|
||||
|
||||
@@ -51,7 +51,7 @@ func makeStruct(structPtr interface{}, c *Container, partyParamsCount int) *Stru
|
||||
}
|
||||
|
||||
// get struct's fields bindings.
|
||||
bindings := getBindingsForStruct(v, c.Dependencies, c.MarkExportedFieldsAsRequired, c.DisablePayloadAutoBinding, c.DependencyMatcher, partyParamsCount, c.Sorter)
|
||||
bindings := getBindingsForStruct(v, c.Dependencies, c.MarkExportedFieldsAsRequired, c.DisablePayloadAutoBinding, c.EnableStructDependents, c.DependencyMatcher, partyParamsCount, c.Sorter)
|
||||
|
||||
// length bindings of 0, means that it has no fields or all mapped deps are static.
|
||||
// If static then Struct.Acquire will return the same "value" instance, otherwise it will create a new one.
|
||||
|
||||
Reference in New Issue
Block a user