wip
This commit is contained in:
42
mangezmieux-backend/internal/injector/injector.go
Normal file
42
mangezmieux-backend/internal/injector/injector.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package injector
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Injector struct {
|
||||
content map[string]any
|
||||
}
|
||||
|
||||
func (i *Injector) Get(key string) any {
|
||||
val, ok := i.content[key]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("Can't get key %s from injector", key))
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (i *Injector) GetWithDefault(key string, defaultValue any) any {
|
||||
val, ok := i.content[key]
|
||||
if !ok {
|
||||
return defaultValue
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func Get[T any](i *Injector, key string) T {
|
||||
return i.Get(key).(T)
|
||||
}
|
||||
|
||||
func GetWithDefault[T any](i *Injector, key string, defaultValue any) T {
|
||||
return i.GetWithDefault(key, defaultValue).(T)
|
||||
}
|
||||
|
||||
func (i *Injector) Set(key string, content any) {
|
||||
if i.content == nil {
|
||||
i.content = map[string]any{}
|
||||
}
|
||||
|
||||
if _, ok := i.content[key]; ok {
|
||||
panic(fmt.Sprintf("Key %s already have content", key))
|
||||
}
|
||||
i.content[key] = content
|
||||
}
|
||||
Reference in New Issue
Block a user