Skip to content

Commit 3dc99d2

Browse files
authored
Merge pull request #3 from firasdarwish/fixes/1-nested-scoped
fix(2) fixed bug with nested scoped resolving
2 parents 07ae628 + dc0cba4 commit 3dc99d2

File tree

10 files changed

+168
-98
lines changed

10 files changed

+168
-98
lines changed

.github/workflows/go.yml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ jobs:
1414
build:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v3
1818

19-
- name: Set up Go
20-
uses: actions/setup-go@v4
21-
with:
22-
go-version: '1.22'
19+
- name: Set up Go
20+
uses: actions/setup-go@v4
21+
with:
22+
go-version: '1.22'
2323

24-
- name: Build
25-
run: go build -v ./...
24+
- name: Build
25+
run: go build -v ./...
2626

27-
- name: Test
28-
run: go test -v ./...
27+
- name: Test
28+
run: go test -v ./...
2929

30-
- name: Run coverage
31-
run: go test -race -coverprofile=coverage.out -covermode=atomic
30+
- name: Run coverage
31+
run: go test -race -coverprofile=coverage.out -covermode=atomic
3232

33-
- name: Upload coverage reports to Codecov
34-
uses: codecov/[email protected]
35-
with:
36-
token: ${{ secrets.CODECOV_TOKEN }}
37-
slug: firasdarwish/ore
33+
- name: Upload coverage reports to Codecov
34+
uses: codecov/[email protected]
35+
with:
36+
token: ${{ secrets.CODECOV_TOKEN }}
37+
slug: firasdarwish/ore

README.md

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ func (c *simpleCounter) GetCount() int {
8484
return c.counter
8585
}
8686

87-
func (c *simpleCounter) New(ctx context.Context) Counter {
88-
return &simpleCounter{}
87+
func (c *simpleCounter) New(ctx context.Context) (Counter, context.Context) {
88+
return &simpleCounter{}, ctx
8989
}
9090
```
9191

@@ -169,18 +169,18 @@ import (
169169

170170
func main() {
171171
// register
172-
ore.RegisterLazyFunc[Counter](ore.Scoped, func(ctx context.Context) Counter {
173-
return &simpleCounter{}
172+
ore.RegisterLazyFunc[Counter](ore.Scoped, func(ctx context.Context) (Counter, context.Context) {
173+
return &simpleCounter{}, ctx
174174
})
175175

176176
// OR
177-
//ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) Counter {
178-
// return &simpleCounter{}
177+
//ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
178+
// return &simpleCounter{}, ctx
179179
//})
180180

181181
// Keyed service registration
182-
//ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) Counter {
183-
// return &simpleCounter{}
182+
//ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
183+
// return &simpleCounter{}, ctx
184184
//}, "name here", 1234)
185185

186186
ctx := context.Background()
@@ -220,8 +220,8 @@ func main() {
220220

221221
ore.RegisterLazyCreator[Counter](ore.Scoped, &yetAnotherCounter{})
222222

223-
ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) Counter {
224-
return &simpleCounter{}
223+
ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
224+
return &simpleCounter{}, ctx
225225
})
226226

227227
ore.RegisterLazyCreator[Counter](ore.Singleton, &yetAnotherCounter{})
@@ -260,8 +260,8 @@ import (
260260

261261
func main() {
262262
// register
263-
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) Counter {
264-
return &simpleCounter{}
263+
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
264+
return &simpleCounter{}, ctx
265265
}, "name here", 1234)
266266

267267
//ore.RegisterLazyCreator[Counter](ore.Scoped, &simpleCounter{}, "name here", 1234)
@@ -281,10 +281,11 @@ func main() {
281281
```
282282

283283
## More Complex Example
284+
284285
```go
285286

286287
type Numeric interface {
287-
int
288+
int
288289
}
289290

290291
type GenericCounter[T Numeric] interface {
@@ -316,8 +317,8 @@ import (
316317
func main() {
317318

318319
// register
319-
ore.RegisterLazyFunc[GenericCounter[int]](ore.Scoped, func(ctx context.Context) GenericCounter[int] {
320-
return &genericCounter[int]{}
320+
ore.RegisterLazyFunc[GenericCounter[int]](ore.Scoped, func(ctx context.Context) (GenericCounter[int], context.Context) {
321+
return &genericCounter[int]{}, ctx
321322
})
322323

323324
// retrieve
@@ -336,25 +337,22 @@ goarch: amd64
336337
pkg: github.com/firasdarwish/ore
337338
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
338339
BenchmarkRegisterLazyFunc
339-
BenchmarkRegisterLazyFunc-20 5404572 209.6 ns/op
340+
BenchmarkRegisterLazyFunc-20 4953412 233.5 ns/op
340341
BenchmarkRegisterLazyCreator
341-
BenchmarkRegisterLazyCreator-20 5683119 195.5 ns/op
342+
BenchmarkRegisterLazyCreator-20 5468863 231.3 ns/op
342343
BenchmarkRegisterEagerSingleton
343-
BenchmarkRegisterEagerSingleton-20 5335443 218.8 ns/op
344+
BenchmarkRegisterEagerSingleton-20 4634733 267.4 ns/op
344345
BenchmarkGet
345-
BenchmarkGet-20 4231207 279.8 ns/op
346+
BenchmarkGet-20 3766730 321.9 ns/op
346347
BenchmarkGetList
347-
BenchmarkGetList-20 2098818 544.6 ns/op
348+
BenchmarkGetList-20 1852132 637.0 ns/op
348349
```
349350

350-
351351
# Contributing
352352

353-
354-
Feel free to contribute by opening issues, suggesting features, or submitting pull requests. We welcome your feedback and contributions.
355-
353+
Feel free to contribute by opening issues, suggesting features, or submitting pull requests. We welcome your feedback
354+
and contributions.
356355

357356
# License
358357

359-
360358
This project is licensed under the MIT License - see the LICENSE file for details.

benchmarks_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ func BenchmarkRegisterLazyFunc(b *testing.B) {
1010

1111
b.ResetTimer()
1212
for i := 0; i < b.N; i++ {
13-
RegisterLazyFunc[Counter](Scoped, func(ctx context.Context) Counter {
14-
return &simpleCounter{}
13+
RegisterLazyFunc[Counter](Scoped, func(ctx context.Context) (Counter, context.Context) {
14+
return &simpleCounter{}, ctx
1515
})
1616
}
1717
}
@@ -37,8 +37,8 @@ func BenchmarkRegisterEagerSingleton(b *testing.B) {
3737
func BenchmarkGet(b *testing.B) {
3838
clearAll()
3939

40-
RegisterLazyFunc[Counter](Scoped, func(ctx context.Context) Counter {
41-
return &simpleCounter{}
40+
RegisterLazyFunc[Counter](Scoped, func(ctx context.Context) (Counter, context.Context) {
41+
return &simpleCounter{}, ctx
4242
})
4343

4444
RegisterEagerSingleton[Counter](&simpleCounter{})
@@ -56,8 +56,8 @@ func BenchmarkGet(b *testing.B) {
5656
func BenchmarkGetList(b *testing.B) {
5757
clearAll()
5858

59-
RegisterLazyFunc[Counter](Scoped, func(ctx context.Context) Counter {
60-
return &simpleCounter{}
59+
RegisterLazyFunc[Counter](Scoped, func(ctx context.Context) (Counter, context.Context) {
60+
return &simpleCounter{}, ctx
6161
})
6262

6363
RegisterEagerSingleton[Counter](&simpleCounter{})

creator_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,26 @@ func TestRegisterLazyCreatorMultipleGenericImplementations(t *testing.T) {
200200
}
201201
}
202202
}
203+
204+
func TestRegisterLazyCreatorScopedNested(t *testing.T) {
205+
clearAll()
206+
207+
RegisterLazyCreator[*a](Transient, &a{})
208+
209+
RegisterLazyCreator[*c](Scoped, &c{})
210+
211+
ctx := context.Background()
212+
213+
a1, ctx := Get[*a](ctx)
214+
a1.C.Counter += 1
215+
216+
a2, ctx := Get[*a](ctx)
217+
a2.C.Counter += 1
218+
219+
a3, ctx := Get[*a](ctx)
220+
a3.C.Counter += 1
221+
222+
if got := a2.C.Counter; got != 3 {
223+
t.Errorf("got %v, expected %v", got, 3)
224+
}
225+
}

entry.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ore
33
import "context"
44

55
type (
6-
Initializer[T any] func(ctx context.Context) T
6+
Initializer[T any] func(ctx context.Context) (T, context.Context)
77
)
88

99
type entry[T any] struct {
@@ -14,7 +14,6 @@ type entry[T any] struct {
1414
}
1515

1616
func (i *entry[T]) load(ctx context.Context, ctxTidVal string) (T, context.Context, bool) {
17-
1817
// try get concrete implementation
1918
if i.lifetime == Singleton && i.concrete != nil {
2019
return *i.concrete, ctx, false
@@ -33,9 +32,9 @@ func (i *entry[T]) load(ctx context.Context, ctxTidVal string) (T, context.Conte
3332
// first, try make concrete implementation from `anonymousInitializer`
3433
// if nil, try the concrete implementation `Creator`
3534
if i.anonymousInitializer != nil {
36-
con = (*i.anonymousInitializer)(ctx)
35+
con, ctx = (*i.anonymousInitializer)(ctx)
3736
} else {
38-
con = i.creatorInstance.New(ctx)
37+
con, ctx = i.creatorInstance.New(ctx)
3938
}
4039

4140
// if scoped, attach to the current context

examples/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88

99
func main() {
1010

11-
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) Counter {
11+
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
1212
fmt.Println("NEWLY INITIALIZED FROM FUNC")
13-
return &counter{}
13+
return &counter{}, ctx
1414
}, "firas")
1515

16-
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) Counter {
16+
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
1717
fmt.Println("NEWLY INITIALIZED FROM FUNC")
18-
return &counter{}
18+
return &counter{}, ctx
1919
}, "darwish")
2020

2121
ore.RegisterLazyCreator[Counter](ore.Singleton, &counter{})

examples/service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ func (c *counter) Total() int {
1717
return c.count
1818
}
1919

20-
func (*counter) New(ctx context.Context) Counter {
20+
func (*counter) New(ctx context.Context) (Counter, context.Context) {
2121
fmt.Println("NEWLY INITIALIZED")
22-
return &counter{}
22+
return &counter{}, ctx
2323
}
2424

2525
type numeric interface {
@@ -38,6 +38,6 @@ func (t *genCounter[T]) Total() T {
3838
return t.count
3939
}
4040

41-
func (*genCounter[T]) New(ctx context.Context) GenericCounter[T] {
42-
return &genCounter[T]{}
41+
func (*genCounter[T]) New(ctx context.Context) (GenericCounter[T], context.Context) {
42+
return &genCounter[T]{}, ctx
4343
}

0 commit comments

Comments
 (0)