30 lines
534 B
Go
30 lines
534 B
Go
package utils
|
|
|
|
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 Get[T any](i *Injector, key string) T {
|
|
return i.Get(key).(T)
|
|
}
|
|
|
|
func (i *Injector) Set(key string, content any) {
|
|
if i.content == nil {
|
|
i.content = map[string]any{}
|
|
}
|
|
_, ok := i.content[key]
|
|
if ok {
|
|
panic(fmt.Sprintf("Key %s already have content", key))
|
|
}
|
|
i.content[key] = content
|
|
}
|