Skip to content

Commit

Permalink
removed Lazy keyword from funcs signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Firas Darwish committed Nov 15, 2024
1 parent 9336c47 commit 461c532
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 268 deletions.
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ c.AddOne()

```go
// register
ore.RegisterLazyCreator[Counter](ore.Scoped, &models.SimpleCounter{})
ore.RegisterCreator[Counter](ore.Scoped, &models.SimpleCounter{})

// OR
//ore.RegisterLazyCreator[Counter](ore.Transient, &models.SimpleCounter{})
//ore.RegisterLazyCreator[Counter](ore.Singleton, &models.SimpleCounter{})
//ore.RegisterCreator[Counter](ore.Transient, &models.SimpleCounter{})
//ore.RegisterCreator[Counter](ore.Singleton, &models.SimpleCounter{})

ctx := context.Background()

Expand All @@ -142,17 +142,17 @@ fmt.Println("TOTAL: ", c.GetCount())

```go
// register
ore.RegisterLazyFunc[Counter](ore.Scoped, func(ctx context.Context) (Counter, context.Context) {
ore.RegisterFunc[Counter](ore.Scoped, func(ctx context.Context) (Counter, context.Context) {
return &models.SimpleCounter{}, ctx
})

// OR
//ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
//ore.RegisterFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
// return &models.SimpleCounter{}, ctx
//})

// Keyed service registration
//ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
//ore.RegisterFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
// return &models.SimpleCounter{}, ctx
//}, "name here", 1234)

Expand Down Expand Up @@ -180,15 +180,15 @@ fmt.Println("TOTAL: ", c.GetCount())

```go
// register
ore.RegisterLazyCreator[Counter](ore.Scoped, &models.SimpleCounter{})
ore.RegisterCreator[Counter](ore.Scoped, &models.SimpleCounter{})

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

ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
ore.RegisterFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
return &models.SimpleCounter{}, ctx
})

ore.RegisterLazyCreator[Counter](ore.Singleton, &yetAnotherCounter{})
ore.RegisterCreator[Counter](ore.Singleton, &yetAnotherCounter{})

ctx := context.Background()

Expand Down Expand Up @@ -217,11 +217,11 @@ The last registered implementation takes precedence, so you can register a mock

```go
// register
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
ore.RegisterFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
return &models.SimpleCounter{}, ctx
}, "name here", 1234)

//ore.RegisterLazyCreator[Counter](ore.Scoped, &models.SimpleCounter{}, "name here", 1234)
//ore.RegisterCreator[Counter](ore.Scoped, &models.SimpleCounter{}, "name here", 1234)

//ore.RegisterEagerSingleton[Counter](&models.SimpleCounter{}, "name here", 1234)

Expand Down Expand Up @@ -249,13 +249,13 @@ type Trader struct {
} //implements IPerson

func TestGetInterfaceAlias(t *testing.T) {
ore.RegisterLazyFunc(ore.Scoped, func(ctx context.Context) (*Broker, context.Context) {
ore.RegisterFunc(ore.Scoped, func(ctx context.Context) (*Broker, context.Context) {
return &Broker{Name: "Peter"}, ctx
})
ore.RegisterLazyFunc(ore.Scoped, func(ctx context.Context) (*Broker, context.Context) {
ore.RegisterFunc(ore.Scoped, func(ctx context.Context) (*Broker, context.Context) {
return &Broker{Name: "John"}, ctx
})
ore.RegisterLazyFunc(ore.Scoped, func(ctx context.Context) (*Trader, context.Context) {
ore.RegisterFunc(ore.Scoped, func(ctx context.Context) (*Trader, context.Context) {
return &Trader{Name: "Mary"}, ctx
})

Expand Down Expand Up @@ -360,7 +360,7 @@ Here how Ore can help you:
type Disposer interface {
Dispose()
}
ore.RegisterLazyCreator(ore.Scoped, &SomeDisposableService{}) //*SomeDisposableService implements Disposer
ore.RegisterCreator(ore.Scoped, &SomeDisposableService{}) //*SomeDisposableService implements Disposer

//a new request arrive
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -399,8 +399,8 @@ The `ore.GetResolvedScopedInstances[TInterface](context)` function returns a lis
| GetResolvedSingletons | GetResolvedSingletonsFromContainer |
| RegisterAlias | RegisterAliasToContainer |
| RegisterEagerSingleton | RegisterEagerSingletonToContainer |
| RegisterLazyCreator | RegisterLazyCreatorToContainer |
| RegisterLazyFunc | RegisterLazyFuncToContainer |
| RegisterCreator | RegisterCreatorToContainer |
| RegisterFunc | RegisterFuncToContainer |
| RegisterPlaceHolder | RegisterPlaceHolderToContainer |
| ProvideScopedValue | ProvideScopedValueToContainer |

Expand All @@ -409,7 +409,7 @@ Most of time you only need the Default Container. In rare use case such as the M
```go
//broker module
brokerContainer := ore.NewContainer()
ore.RegisterLazyFuncToContainer(brokerContainer, ore.Singleton, func(ctx context.Context) (*Broker, context.Context) {
ore.RegisterFuncToContainer(brokerContainer, ore.Singleton, func(ctx context.Context) (*Broker, context.Context) {
brs, ctx = ore.GetFromContainer[*BrokerageSystem](brokerContainer, ctx)
return &Broker{brs}, ctx
})
Expand All @@ -420,7 +420,7 @@ broker, _ := ore.GetFromContainer[*Broker](brokerContainer, context.Background()

//trader module
traderContainer := ore.NewContainer()
ore.RegisterLazyFuncToContainer(traderContainer, ore.Singleton, func(ctx context.Context) (*Trader, context.Context) {
ore.RegisterFuncToContainer(traderContainer, ore.Singleton, func(ctx context.Context) (*Trader, context.Context) {
mkp, ctx = ore.GetFromContainer[*MarketPlace](traderContainer, ctx)
return &Trader{mkp}, ctx
})
Expand All @@ -438,7 +438,7 @@ A common scenario is that your "Service" depends on something which you couldn't

```go
//register SomeService which depends on "someConfig"
ore.RegisterLazyFunc[*SomeService](ore.Scoped, func(ctx context.Context) (*SomeService, context.Context) {
ore.RegisterFunc[*SomeService](ore.Scoped, func(ctx context.Context) (*SomeService, context.Context) {
someConfig, ctx := ore.Get[string](ctx, "someConfig")
return &SomeService{someConfig}, ctx
})
Expand Down Expand Up @@ -480,7 +480,7 @@ fmt.Println(service.someConfig) //"Admin config"
- `ore.ProvideScopedValue[T](context, value T, key...)` injects a concrete value into the given context
- `ore` can access (`Get()` or `GetList()`) to this value only if the corresponding placeholder (which matches the type and keys) is registered.

- A value provided to a placeholder would never replace value returned by other resolvers. It's the opposite, if a type (and key) could be resolved by a real resolver (such as `RegisterLazyFunc`, `RegisterLazyCreator`...), then the later would take precedent.
- A value provided to a placeholder would never replace value returned by other resolvers. It's the opposite, if a type (and key) could be resolved by a real resolver (such as `RegisterFunc`, `RegisterCreator`...), then the later would take precedent.

<br/>

Expand Down Expand Up @@ -513,7 +513,7 @@ func (gc *genericCounter[T]) GetCount(ctx context.Context) T {
```go

// register
ore.RegisterLazyFunc[GenericCounter[int]](ore.Scoped, func(ctx context.Context) (GenericCounter[int], context.Context) {
ore.RegisterFunc[GenericCounter[int]](ore.Scoped, func(ctx context.Context) (GenericCounter[int], context.Context) {
return &genericCounter[int]{}, ctx
})

Expand All @@ -530,8 +530,8 @@ goos: windows
goarch: amd64
pkg: github.com/firasdarwish/ore
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
BenchmarkRegisterLazyFunc-20 5706694 196.9 ns/op
BenchmarkRegisterLazyCreator-20 6283534 184.5 ns/op
BenchmarkRegisterFunc-20 5706694 196.9 ns/op
BenchmarkRegisterCreator-20 6283534 184.5 ns/op
BenchmarkRegisterEagerSingleton-20 5146953 211.5 ns/op
BenchmarkInitialGet-20 3440072 352.1 ns/op
BenchmarkGet-20 9806043 121.8 ns/op
Expand Down
30 changes: 15 additions & 15 deletions alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

func TestAliasResolverConflict(t *testing.T) {
clearAll()
RegisterLazyFunc(Singleton, func(ctx context.Context) (m.IPerson, context.Context) {
RegisterFunc(Singleton, func(ctx context.Context) (m.IPerson, context.Context) {
return &m.Trader{Name: "Peter Singleton"}, ctx
})
RegisterLazyFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) {
RegisterFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Mary Transient"}, ctx
})

Expand All @@ -36,10 +36,10 @@ func TestAliasResolverConflict(t *testing.T) {

func TestAliasOfAliasIsNotAllow(t *testing.T) {
clearAll()
RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.Trader, context.Context) {
RegisterFunc(Singleton, func(ctx context.Context) (*m.Trader, context.Context) {
return &m.Trader{Name: "Peter Singleton"}, ctx
})
RegisterLazyFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) {
RegisterFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Mary Transient"}, ctx
})

Expand All @@ -58,13 +58,13 @@ func TestAliasOfAliasIsNotAllow(t *testing.T) {
func TestAliasWithDifferentScope(t *testing.T) {
clearAll()
module := "TestGetInterfaceAliasWithDifferentScope"
RegisterLazyFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) {
RegisterFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Transient"}, ctx
}, module)
RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.Broker, context.Context) {
RegisterFunc(Singleton, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Singleton"}, ctx
}, module)
RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
RegisterFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Scoped"}, ctx
}, module)
RegisterAlias[m.IPerson, *m.Broker]() //link m.IPerson to *m.Broker
Expand All @@ -80,24 +80,24 @@ func TestAliasWithDifferentScope(t *testing.T) {

func TestAliasIsScopedByKeys(t *testing.T) {
clearAll()
RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
RegisterFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Peter1"}, ctx
}, "module1")
RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
RegisterFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "John1"}, ctx
}, "module1")
RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) {
RegisterFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) {
return &m.Trader{Name: "Mary1"}, ctx
}, "module1")

RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
RegisterFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "John2"}, ctx
}, "module2")
RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) {
RegisterFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) {
return &m.Trader{Name: "Mary2"}, ctx
}, "module2")

RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) {
RegisterFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) {
return &m.Trader{Name: "Mary3"}, ctx
}, "module3")

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestGetGenericAlias(t *testing.T) {
for _, registrationType := range types {
container := NewContainer()

RegisterLazyFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) {
RegisterFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) {
return &m.SimpleCounterUint{}, ctx
})
RegisterAliasToContainer[interfaces.SomeCounterGeneric[uint], *m.SimpleCounterUint](container)
Expand All @@ -157,7 +157,7 @@ func TestGetListGenericAlias(t *testing.T) {
container := NewContainer()

for i := 0; i < 3; i++ {
RegisterLazyFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) {
RegisterFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) {
return &m.SimpleCounterUint{}, ctx
})
}
Expand Down
34 changes: 17 additions & 17 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import (
"testing"
)

func BenchmarkRegisterLazyFunc(b *testing.B) {
func BenchmarkRegisterFunc(b *testing.B) {
clearAll()

b.ResetTimer()
for i := 0; i < b.N; i++ {
RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
return &models.SimpleCounter{}, ctx
})
}
}

func BenchmarkRegisterLazyCreator(b *testing.B) {
func BenchmarkRegisterCreator(b *testing.B) {
clearAll()

b.ResetTimer()
for i := 0; i < b.N; i++ {
RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})
RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})
}
}

Expand All @@ -32,20 +32,20 @@ func BenchmarkRegisterEagerSingleton(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
}
}

func BenchmarkInitialGet(b *testing.B) {
clearAll()

RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
return &models.SimpleCounter{}, ctx
})

RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})

RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})
RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})

Seal()
Validate()
Expand All @@ -61,13 +61,13 @@ func BenchmarkInitialGet(b *testing.B) {
func BenchmarkGet(b *testing.B) {
clearAll()

RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
return &models.SimpleCounter{}, ctx
})

RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})

RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})
RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})
Seal()
Validate()
ctx := context.Background()
Expand All @@ -81,13 +81,13 @@ func BenchmarkGet(b *testing.B) {
func BenchmarkInitialGetList(b *testing.B) {
clearAll()

RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
return &models.SimpleCounter{}, ctx
})

RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})

RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})
RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})
Seal()
Validate()

Expand All @@ -102,13 +102,13 @@ func BenchmarkInitialGetList(b *testing.B) {
func BenchmarkGetList(b *testing.B) {
clearAll()

RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) {
return &models.SimpleCounter{}, ctx
})

RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{})
RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{})

RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})
RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{})
Seal()
Validate()
ctx := context.Background()
Expand Down
Loading

0 comments on commit 461c532

Please sign in to comment.