Files
mangezmieux/mangezmieux-backend/internal/injector/injector.go
2024-07-19 17:04:42 +02:00

43 lines
811 B
Go

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
}