Skip to content

Commit

Permalink
feat(dingo): introduce injection tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanMaidurov committed Nov 14, 2024
1 parent 8e15da8 commit 48cea9f
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions dingo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ 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
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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 48cea9f

Please sign in to comment.