sl: add Invoke function
This commit is contained in:
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user