diff --git a/dingo.go b/dingo.go index 55ac33e..953fc6e 100644 --- a/dingo.go +++ b/dingo.go @@ -15,9 +15,13 @@ const ( DEFAULT ) -var ErrInvalidInjectReceiver = errors.New("usage of 'Inject' method with struct receiver is not allowed") -var traceCircular []circularTraceEntry -var errPointersToInterface = errors.New(" Do not use pointers to interface.") +var ( + ErrInvalidInjectReceiver = errors.New("usage of 'Inject' method with struct receiver is not allowed") + errPointersToInterface = errors.New(" Do not use pointers to interface.") + + traceCircular []circularTraceEntry + injectionTracing = false +) // EnableCircularTracing activates dingo's trace feature to find circular dependencies // this is super expensive (memory wise), so it should only be used for debugging purposes @@ -25,6 +29,10 @@ func EnableCircularTracing() { traceCircular = make([]circularTraceEntry, 0) } +func EnableInjectionTracing() { + injectionTracing = true +} + type ( // Injector defines bindings and multibindings // it is possible to have a parent-injector, which can be asked if no resolution is available @@ -386,6 +394,14 @@ func (injector *Injector) createInstanceOfAnnotatedType(t reflect.Type, annotati return n, injector.requestInjection(n.Interface(), subCircularTrace) } + if injectionTracing { + if t.PkgPath() == "" || t.Name() == "" { + log.Println("INJECTING: " + t.String()) + } else { + log.Println("INJECTING: " + t.PkgPath() + "#" + t.Name() + " \"" + annotation + "\"") + } + } + n := reflect.New(t) return n, injector.requestInjection(n.Interface(), nil) } @@ -742,11 +758,20 @@ func (injector *Injector) requestInjection(object interface{}, circularTrace []c } } if field.Kind() != reflect.Ptr && field.Kind() != reflect.Interface && instance.Kind() == reflect.Ptr { + if injectionTracing { + log.Println("SETTING FIELD: \"" + currentFieldName + "\" of type: \"" + ctype.Field(fieldIndex).Type.String() + "\"") + } + field.Set(instance.Elem()) } else { if field.Kind() == reflect.Ptr && field.Type().Kind() == reflect.Ptr && field.Type().Elem().Kind() == reflect.Interface { return wrapErr(fmt.Errorf("field %#v is pointer to interface. %w", currentFieldName, errPointersToInterface)) } + + if injectionTracing { + log.Println("SETTING FIELD: \"" + currentFieldName + "\" of type: \"" + ctype.Field(fieldIndex).Type.String() + "\"") + } + field.Set(instance) } }