fix #1563 due to latest DI source changes

Former-commit-id: 3e3f209ebc60edf6a8702979f56e1f0ed73e4189
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-07-17 12:59:39 +03:00
parent 0f79728c88
commit dab03102f3
3 changed files with 31 additions and 16 deletions

View File

@ -14,7 +14,7 @@ func main() {
app.Logger().SetLevel("debug") app.Logger().SetLevel("debug")
// load templates. // load templates.
app.RegisterView(iris.HTML("./views", ".html")) // app.RegisterView(iris.HTML("./views", ".html"))
// render the ./browser/index.html. // render the ./browser/index.html.
app.HandleDir("/", "./browser") app.HandleDir("/", "./browser")

View File

@ -265,7 +265,7 @@ func getBindingsForStruct(v reflect.Value, dependencies []*Dependency, paramsCou
}) })
} }
fields := lookupFields(elem, true, true, nil) fields, stateless := lookupFields(elem, true, true, nil)
n := len(fields) n := len(fields)
if n > 1 && sorter != nil { if n > 1 && sorter != nil {
@ -280,15 +280,19 @@ func getBindingsForStruct(v reflect.Value, dependencies []*Dependency, paramsCou
inputs[i] = fields[i].Type inputs[i] = fields[i].Type
} }
exportedBindings := getBindingsFor(inputs, dependencies, paramsCount) exportedBindings := getBindingsFor(inputs, dependencies, paramsCount)
// fmt.Printf("Controller [%s] | Inputs length: %d vs Bindings length: %d\n", typ, n, len(exportedBindings))
// fmt.Printf("Controller [%s] Inputs length: %d vs Bindings length: %d\n", typ, n, len(exportedBindings)) if stateless == 0 && len(nonZero) >= len(exportedBindings) {
if len(nonZero) >= len(exportedBindings) { // if all are fields are defined then just return. // if we have not a single stateless and fields are defined then just return.
// Note(@kataras): this can accept further improvements.
return return
} }
// get declared bindings from deps. // get declared bindings from deps.
bindings = append(bindings, exportedBindings...) bindings = append(bindings, exportedBindings...)
for _, binding := range bindings { for _, binding := range bindings {
// fmt.Printf(""Controller [%s] | Binding: %s\n", typ, binding.String())
if len(binding.Input.StructFieldIndex) == 0 { if len(binding.Input.StructFieldIndex) == 0 {
// set correctly the input's field index. // set correctly the input's field index.
structFieldIndex := fields[binding.Input.Index].Index structFieldIndex := fields[binding.Input.Index].Index
@ -296,8 +300,7 @@ func getBindingsForStruct(v reflect.Value, dependencies []*Dependency, paramsCou
} }
// fmt.Printf("Controller [%s] | binding Index: %v | binding Type: %s\n", typ, binding.Input.StructFieldIndex, binding.Input.Type) // fmt.Printf("Controller [%s] | binding Index: %v | binding Type: %s\n", typ, binding.Input.StructFieldIndex, binding.Input.Type)
// fmt.Printf("Controller [%s] Set [%s] to struct field index: %v\n", typ.String(), binding.Input.Type.String(), binding.Input.StructFieldIndex)
// fmt.Printf("Controller [%s] Set [%s] to struct field index: %v\n", typ.String(), binding.Input.Type.String(), structFieldIndex)
} }
return return

View File

@ -101,23 +101,35 @@ func structFieldIgnored(f reflect.StructField) bool {
return true // if not anonymous(embedded), ignore it. return true // if not anonymous(embedded), ignore it.
} }
s := f.Tag.Get("ignore") if s := f.Tag.Get("ignore"); s == "true" {
return s == "true" // if has an ignore tag then ignore it. return true
}
if s := f.Tag.Get("stateless"); s == "true" {
return true
}
return false
} }
// all except non-zero. // all except non-zero.
func lookupFields(elem reflect.Value, skipUnexported bool, onlyZeros bool, parentIndex []int) (fields []reflect.StructField) { func lookupFields(elem reflect.Value, skipUnexported bool, onlyZeros bool, parentIndex []int) (fields []reflect.StructField, stateless int) {
elemTyp := elem.Type() elemTyp := elem.Type()
for i, n := 0, elem.NumField(); i < n; i++ { for i, n := 0, elem.NumField(); i < n; i++ {
field := elemTyp.Field(i)
fieldValue := elem.Field(i) fieldValue := elem.Field(i)
field := elemTyp.Field(i)
// embed any fields from other structs. // embed any fields from other structs.
if indirectType(field.Type).Kind() == reflect.Struct && !structFieldIgnored(field) { if indirectType(field.Type).Kind() == reflect.Struct {
fields = append(fields, lookupFields(fieldValue, skipUnexported, onlyZeros, append(parentIndex, i))...) if structFieldIgnored(field) {
stateless++ // don't skip the loop yet, e.g. iris.Context.
} else {
structFields, statelessN := lookupFields(fieldValue, skipUnexported, onlyZeros, append(parentIndex, i))
stateless += statelessN
fields = append(fields, structFields...)
continue continue
} }
}
if onlyZeros && !isZero(fieldValue) { if onlyZeros && !isZero(fieldValue) {
continue continue
@ -144,7 +156,7 @@ func lookupFields(elem reflect.Value, skipUnexported bool, onlyZeros bool, paren
} }
func lookupNonZeroFieldValues(elem reflect.Value) (nonZeroFields []reflect.StructField) { func lookupNonZeroFieldValues(elem reflect.Value) (nonZeroFields []reflect.StructField) {
fields := lookupFields(elem, true, false, nil) fields, _ := lookupFields(elem, true, false, nil)
for _, f := range fields { for _, f := range fields {
if fieldVal := elem.FieldByIndex(f.Index); goodVal(fieldVal) && !isZero(fieldVal) { if fieldVal := elem.FieldByIndex(f.Index); goodVal(fieldVal) && !isZero(fieldVal) {
/* && f.Type.Kind() == reflect.Ptr &&*/ /* && f.Type.Kind() == reflect.Ptr &&*/