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

minor changes to MVC, see HISTORY.md#Next for more

This commit is contained in:
Gerasimos (Makis) Maropoulos
2023-08-21 20:11:14 +03:00
parent 79404f91c1
commit 9f53d15133
4 changed files with 43 additions and 4 deletions

View File

@@ -43,6 +43,18 @@ type Struct struct {
Singleton bool
}
type singletonStruct interface {
Singleton() bool
}
func isMarkedAsSingleton(structPtr any) bool {
if sing, ok := structPtr.(singletonStruct); ok && sing.Singleton() {
return true
}
return false
}
func makeStruct(structPtr interface{}, c *Container, partyParamsCount int) *Struct {
v := valueOf(structPtr)
typ := v.Type()
@@ -50,8 +62,19 @@ func makeStruct(structPtr interface{}, c *Container, partyParamsCount int) *Stru
panic("binder: struct: should be a pointer to a struct value")
}
isSingleton := isMarkedAsSingleton(structPtr)
disablePayloadAutoBinding := c.DisablePayloadAutoBinding
enableStructDependents := c.EnableStructDependents
disableStructDynamicBindings := c.DisableStructDynamicBindings
if isSingleton {
disablePayloadAutoBinding = true
enableStructDependents = false
disableStructDynamicBindings = true
}
// get struct's fields bindings.
bindings := getBindingsForStruct(v, c.Dependencies, c.MarkExportedFieldsAsRequired, c.DisablePayloadAutoBinding, c.EnableStructDependents, c.DependencyMatcher, partyParamsCount, c.Sorter)
bindings := getBindingsForStruct(v, c.Dependencies, c.MarkExportedFieldsAsRequired, disablePayloadAutoBinding, 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.
@@ -73,7 +96,7 @@ func makeStruct(structPtr interface{}, c *Container, partyParamsCount int) *Stru
elem.FieldByIndex(b.Input.StructFieldIndex).Set(input)
} else if !b.Dependency.Static {
if c.DisableStructDynamicBindings {
if disableStructDynamicBindings {
panic(fmt.Sprintf("binder: DisableStructDynamicBindings setting is set to true: dynamic binding found: %s", b.String()))
}
@@ -81,6 +104,10 @@ func makeStruct(structPtr interface{}, c *Container, partyParamsCount int) *Stru
}
}
if isSingleton && !singleton {
panic(fmt.Sprintf("binder: Singleton setting is set to true but struct has dynamic bindings: %s", typ))
}
s := &Struct{
ptrValue: v,
ptrType: typ,