sl: add Invoke function

This commit is contained in:
2023-06-13 23:34:15 +02:00
parent 064f42fb9f
commit e34f91fce6
+18
View File
@@ -158,6 +158,24 @@ func Use[T any](l *ServiceLocator, slotKey slot[T]) (T, error) {
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
// interface type)
func getTypeName[T any]() string {