From e34f91fce671d09f93736d099767b0025e97dd0a Mon Sep 17 00:00:00 2001 From: Francesco Minnocci Date: Tue, 13 Jun 2023 23:34:15 +0200 Subject: [PATCH] sl: add Invoke function --- libs/sl/sl.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/libs/sl/sl.go b/libs/sl/sl.go index db85927..fb21076 100644 --- a/libs/sl/sl.go +++ b/libs/sl/sl.go @@ -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 {