Skip to content

Commit

Permalink
[i-love-flamingo#7]: failing on usage of struct receiver in 'Inject' …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
Tristan Baumbusch committed Nov 2, 2022
1 parent 72b6052 commit ba221a5
Show file tree
Hide file tree
Showing 2 changed files with 28 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
23 changes: 23 additions & 0 deletions dingo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,26 @@ 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(_ string) {}

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

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

injector.Bind(new(TestInjectStructRecInterface)).To(new(testInjectStructRecStruct))
injector.Bind(new(string)).ToInstance("test")

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

0 comments on commit ba221a5

Please sign in to comment.