Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ type (
func (b *Binding) To(what interface{}) *Binding {
to := reflect.TypeOf(what)

if to.Kind() == reflect.Func {
if !reflect.ValueOf(what).IsZero() {
return b.ToInstance(what)
}
}

for to.Kind() == reflect.Ptr {
to = to.Elem()
}
Expand Down
8 changes: 4 additions & 4 deletions module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestTryModule(t *testing.T) {
}

func TestResolveDependenciesWithModuleFunc(t *testing.T) {
var countInline, countExtern int
var markIntern, countExtern int

ext := ModuleFunc(func(injector *Injector) {
countExtern++
Expand All @@ -43,18 +43,18 @@ func TestResolveDependenciesWithModuleFunc(t *testing.T) {
injector, err := NewInjector(
new(tryModuleOk),
ModuleFunc(func(injector *Injector) {
countInline++
markIntern |= 1 << 1
}),
ModuleFunc(func(injector *Injector) {
countInline++
markIntern |= 1 << 2
}),
ext,
ext,
)

assert.NoError(t, err)
assert.NotNil(t, injector)
assert.Equal(t, 2, countInline, "inline modules should be called once (eventually twice for this test)")
assert.Equal(t, (1<<2)|(1<<1), markIntern, "inline modules should be called once (eventually twice for this test)")
assert.Equal(t, 1, countExtern, "variable defined modules should only be called once")
}

Expand Down