Skip to content

Commit

Permalink
Bug
Browse files Browse the repository at this point in the history
getbylookupkey_always_got_first
  • Loading branch information
ghstahl committed Feb 28, 2024
1 parent bec69d0 commit db2999a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ func (c *container) createEngine() ContainerEngine {

func (c *container) createServiceLookupKeyAccessor(key string) (ServiceAccessor, error) {
descriptor := c.CallSiteFactory.descriptorKeyLookup[key]
callSite, err := c.CallSiteFactory.GetCallSiteByDescriptor(descriptor.item, newCallSiteChain())
itemDescriptor := descriptor.item
if len(descriptor.items) > 0 {
// get the last one
itemDescriptor = descriptor.items[len(descriptor.items)-1]
}
callSite, err := c.CallSiteFactory.GetCallSiteByDescriptor(itemDescriptor, newCallSiteChain())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/fluffy-bunny/fluffy-dozm-di

go 1.21
go 1.22

require github.com/stretchr/testify v1.8.4

Expand Down
12 changes: 11 additions & 1 deletion many_interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func AddSingletonEmployeesWithLookupKeys(b ContainerBuilder) {
return &employee{Name: "1"}
}, []string{"1"}, map[string]interface{}{"name": "1"},
reflect.TypeOf((*IEmployee)(nil)))
AddSingletonWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "2a"}
}, []string{"2"}, map[string]interface{}{"name": "2a"},
reflect.TypeOf((*IEmployee)(nil)))
AddSingletonWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "2"}
Expand Down Expand Up @@ -344,12 +349,17 @@ func TestManyWithSingletonWithLookupKeys(t *testing.T) {
scopeFactory := Get[ScopeFactory](c)
scope1 := scopeFactory.CreateScope()
employees := Get[[]IEmployee](scope1.Container())
require.Equal(t, 2, len(employees))
require.Equal(t, 3, len(employees))
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetName())
})
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "2")
require.NotNil(t, h)
require.Equal(t, "2", h.GetName())
})
}
func TestManyWithTransientWithLookupKeys(t *testing.T) {
b := Builder()
Expand Down

0 comments on commit db2999a

Please sign in to comment.