@@ -57,6 +57,13 @@ type Container struct {
5757 // if at least one input binding depends on the request and not in a static structure.
5858 DisableStructDynamicBindings bool
5959
60+ // StructDependents if true then the Container will try to resolve
61+ // the fields of a struct value, if any, when it's a dependent struct value
62+ // based on the previous registered dependencies.
63+ //
64+ // Defaults to false.
65+ EnableStructDependents bool // this can be renamed to IndirectDependencies?.
66+
6067 // DependencyMatcher holds the function that compares equality between
6168 // a dependency with an input. Defaults to DefaultMatchDependencyFunc.
6269 DependencyMatcher DependencyMatcher
@@ -146,11 +153,11 @@ func (c *Container) fillReport(fullName string, bindings []*binding) {
146153// Contains the iris context, standard context, iris sessions and time dependencies.
147154var BuiltinDependencies = []* Dependency {
148155 // iris context dependency.
149- newDependency (func (ctx * context.Context ) * context.Context { return ctx }, true , nil ).Explicitly (),
156+ newDependency (func (ctx * context.Context ) * context.Context { return ctx }, true , false , nil ).Explicitly (),
150157 // standard context dependency.
151158 newDependency (func (ctx * context.Context ) stdContext.Context {
152159 return ctx .Request ().Context ()
153- }, true , nil ).Explicitly (),
160+ }, true , false , nil ).Explicitly (),
154161 // iris session dependency.
155162 newDependency (func (ctx * context.Context ) * sessions.Session {
156163 session := sessions .Get (ctx )
@@ -163,43 +170,43 @@ var BuiltinDependencies = []*Dependency{
163170 }
164171
165172 return session
166- }, true , nil ).Explicitly (),
173+ }, true , false , nil ).Explicitly (),
167174 // application's logger.
168175 newDependency (func (ctx * context.Context ) * golog.Logger {
169176 return ctx .Application ().Logger ()
170- }, true , nil ).Explicitly (),
177+ }, true , false , nil ).Explicitly (),
171178 // time.Time to time.Now dependency.
172179 newDependency (func (ctx * context.Context ) time.Time {
173180 return time .Now ()
174- }, true , nil ).Explicitly (),
181+ }, true , false , nil ).Explicitly (),
175182 // standard http Request dependency.
176183 newDependency (func (ctx * context.Context ) * http.Request {
177184 return ctx .Request ()
178- }, true , nil ).Explicitly (),
185+ }, true , false , nil ).Explicitly (),
179186 // standard http ResponseWriter dependency.
180187 newDependency (func (ctx * context.Context ) http.ResponseWriter {
181188 return ctx .ResponseWriter ()
182- }, true , nil ).Explicitly (),
189+ }, true , false , nil ).Explicitly (),
183190 // http headers dependency.
184191 newDependency (func (ctx * context.Context ) http.Header {
185192 return ctx .Request ().Header
186- }, true , nil ).Explicitly (),
193+ }, true , false , nil ).Explicitly (),
187194 // Client IP.
188195 newDependency (func (ctx * context.Context ) net.IP {
189196 return net .ParseIP (ctx .RemoteAddr ())
190- }, true , nil ).Explicitly (),
197+ }, true , false , nil ).Explicitly (),
191198 // Status Code (special type for MVC HTTP Error handler to not conflict with path parameters)
192199 newDependency (func (ctx * context.Context ) Code {
193200 return Code (ctx .GetStatusCode ())
194- }, true , nil ).Explicitly (),
201+ }, true , false , nil ).Explicitly (),
195202 // Context Error. May be nil
196203 newDependency (func (ctx * context.Context ) Err {
197204 err := ctx .GetErr ()
198205 if err == nil {
199206 return nil
200207 }
201208 return err
202- }, true , nil ).Explicitly (),
209+ }, true , false , nil ).Explicitly (),
203210 // Context User, e.g. from basic authentication.
204211 newDependency (func (ctx * context.Context ) context.User {
205212 u := ctx .User ()
@@ -208,7 +215,7 @@ var BuiltinDependencies = []*Dependency{
208215 }
209216
210217 return u
211- }, true , nil ),
218+ }, true , false , nil ),
212219 // payload and param bindings are dynamically allocated and declared at the end of the `binding` source file.
213220}
214221
@@ -254,6 +261,7 @@ func (c *Container) Clone() *Container {
254261 cloned .Dependencies = clonedDeps
255262 cloned .DisablePayloadAutoBinding = c .DisablePayloadAutoBinding
256263 cloned .DisableStructDynamicBindings = c .DisableStructDynamicBindings
264+ cloned .EnableStructDependents = c .EnableStructDependents
257265 cloned .MarkExportedFieldsAsRequired = c .MarkExportedFieldsAsRequired
258266 cloned .resultHandlers = c .resultHandlers
259267 // Reports are not cloned.
@@ -291,7 +299,7 @@ func Register(dependency interface{}) *Dependency {
291299// - Register(func(ctx iris.Context) User {...})
292300// - Register(func(User) OtherResponse {...})
293301func (c * Container ) Register (dependency interface {}) * Dependency {
294- d := newDependency (dependency , c .DisablePayloadAutoBinding , c .DependencyMatcher , c .Dependencies ... )
302+ d := newDependency (dependency , c .DisablePayloadAutoBinding , c .EnableStructDependents , c . DependencyMatcher , c .Dependencies ... )
295303 if d .DestType == nil {
296304 // prepend the dynamic dependency so it will be tried at the end
297305 // (we don't care about performance here, design-time)
0 commit comments