Skip to content

Commit

Permalink
[#7]: failing on usage of struct receiver in 'Inject' method
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanessquare authored and bastianccm committed Nov 4, 2022
1 parent 72b6052 commit bea6ce0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dingo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
DEFAULT
)

var ErrInvalidInjectReceiver = errors.New("usage of 'Inject' method with struct receiver is not allowed")
var traceCircular []circularTraceEntry

// EnableCircularTracing activates dingo's trace feature to find circular dependencies
Expand Down Expand Up @@ -693,6 +694,10 @@ func (injector *Injector) requestInjection(object interface{}, circularTrace []c

i++

if ctype.Kind() != reflect.Ptr && current.MethodByName("Inject").IsValid() {
return fmt.Errorf("invalid inject receiver %s: %w", current, ErrInvalidInjectReceiver)
}

switch ctype.Kind() {
// dereference pointer
case reflect.Ptr:
Expand Down
22 changes: 22 additions & 0 deletions dingo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,25 @@ func TestInjector_InitModules(t *testing.T) {
assert.NoError(t, err)
assert.Error(t, injector.InitModules(new(testInjectInvalid)))
}

type TestInjectStructRecInterface interface {
TestXyz()
}

type testInjectStructRecStruct struct{}

func (t testInjectStructRecStruct) TestXyz() {}

func (t testInjectStructRecStruct) Inject() {}

func TestInjectStructRec(t *testing.T) {
t.Parallel()

injector, err := NewInjector()
assert.NoError(t, err)

injector.Bind(new(TestInjectStructRecInterface)).To(new(testInjectStructRecStruct))

_, err = injector.GetInstance(new(TestInjectStructRecInterface))
assert.Error(t, err)
}

0 comments on commit bea6ce0

Please sign in to comment.