sl: add Invoke function

next-astro
Francesco Minnocci 1 year ago
parent 064f42fb9f
commit e34f91fce6
Signed by: BachoSeven
GPG Key ID: 2BE4AB7FDAD828A4

@ -158,6 +158,24 @@ func Use[T any](l *ServiceLocator, slotKey slot[T]) (T, error) {
return v, nil return v, nil
} }
func Invoke[T any](l *ServiceLocator, slotKey slot[T]) error {
slot, ok := l.providers[slotKey]
if !ok {
return fmt.Errorf(`no injected value for type %s`, getTypeName[T]())
}
err := slot.checkInitialized(l)
if err != nil {
return err
}
v := slot.value.(T)
Logger.Printf(`[slot: %s] using slot with value of type %T`, getTypeName[T](), v)
return nil
}
// getTypeName is a trick to get the name of a type (even if it is an // getTypeName is a trick to get the name of a type (even if it is an
// interface type) // interface type)
func getTypeName[T any]() string { func getTypeName[T any]() string {

Loading…
Cancel
Save