|
|
@ -5,69 +5,146 @@ import (
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type provider struct {
|
|
|
|
type SlotKey[T any] *struct{}
|
|
|
|
initialized bool
|
|
|
|
|
|
|
|
service Service
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ServiceLocator struct {
|
|
|
|
func NewSlot[T any]() SlotKey[T] {
|
|
|
|
providers map[string]*provider
|
|
|
|
return SlotKey[T](new(struct{}))
|
|
|
|
hooks map[string][]Service
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Service interface {
|
|
|
|
type slot struct {
|
|
|
|
Initialize(l *ServiceLocator) error
|
|
|
|
createFunc func(*ServiceLocator) (any, error)
|
|
|
|
|
|
|
|
created bool
|
|
|
|
|
|
|
|
value any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typ string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func New() *ServiceLocator {
|
|
|
|
func (s *slot) checkInitialized(l *ServiceLocator) error {
|
|
|
|
return &ServiceLocator{
|
|
|
|
if !s.created {
|
|
|
|
providers: map[string]*provider{},
|
|
|
|
v, err := s.createFunc(l)
|
|
|
|
hooks: map[string][]Service{},
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf(`initialized lazy value of type %T for slot of type %s`, v, s.typ)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s.value = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getTypeName[T any]() string {
|
|
|
|
return nil
|
|
|
|
var v T
|
|
|
|
|
|
|
|
return fmt.Sprintf(`%T`, &v)[1:]
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Inject will set the implementation for "S" to "value" (the service will be initialized when needed after all of its dependencies)
|
|
|
|
type ServiceLocator struct {
|
|
|
|
func Inject[S Service](l *ServiceLocator, value S) {
|
|
|
|
providers map[any]*slot
|
|
|
|
key := getTypeName[S]()
|
|
|
|
}
|
|
|
|
log.Printf(`injecting value of type %T for interface %s`, value, key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
l.providers[key] = &provider{false, value}
|
|
|
|
func New() *ServiceLocator {
|
|
|
|
|
|
|
|
return &ServiceLocator{
|
|
|
|
|
|
|
|
providers: map[any]*slot{},
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// InjectValue will set the implementation for "S" to "value" and mark this service as already initialized (as this is just a constant)
|
|
|
|
func InjectValue[T any](l *ServiceLocator, slotKey SlotKey[T], value T) T {
|
|
|
|
func InjectValue[S Service](l *ServiceLocator, value S) S {
|
|
|
|
log.Printf(`injected value of type %T for slot of type %s`, value, getTypeName[T]())
|
|
|
|
key := getTypeName[S]()
|
|
|
|
|
|
|
|
log.Printf(`injecting value of type %T for interface %s`, value, key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
l.providers[key] = &provider{true, value}
|
|
|
|
l.providers[slotKey] = &slot{nil, true, value, getTypeName[T]()}
|
|
|
|
return value
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Use will retrive from the service locator the implementation set for the type "T" and initialize the service if yet to be initialized
|
|
|
|
func InjectLazy[T any](l *ServiceLocator, slotKey SlotKey[T], createFunc func(*ServiceLocator) (T, error)) {
|
|
|
|
func Use[T Service](l *ServiceLocator) (T, error) {
|
|
|
|
log.Printf(`injected lazy for slot of type %s`, getTypeName[T]())
|
|
|
|
provider, ok := l.providers[getTypeName[T]()]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
var zero T
|
|
|
|
|
|
|
|
return zero, fmt.Errorf(`no injected value for type "%T"`, zero)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if provider.initialized {
|
|
|
|
l.providers[slotKey] = &slot{
|
|
|
|
service := provider.service.(T)
|
|
|
|
createFunc: func(l *ServiceLocator) (any, error) {
|
|
|
|
return service, nil
|
|
|
|
return createFunc(l)
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
created: false,
|
|
|
|
|
|
|
|
value: nil,
|
|
|
|
|
|
|
|
typ: getTypeName[T](),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf(`initializing %T`, provider.service)
|
|
|
|
func Use[T any](l *ServiceLocator, slotKey SlotKey[T]) (T, error) {
|
|
|
|
if err := provider.service.Initialize(l); err != nil {
|
|
|
|
|
|
|
|
var zero T
|
|
|
|
var zero T
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
slot, ok := l.providers[slotKey]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
return zero, fmt.Errorf(`no injected value for type %s`, getTypeName[T]())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := slot.checkInitialized(l)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return zero, err
|
|
|
|
return zero, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
provider.initialized = true
|
|
|
|
v := slot.value.(T)
|
|
|
|
service := provider.service.(T)
|
|
|
|
|
|
|
|
return service, nil
|
|
|
|
log.Printf(`using slot of type %s with value of type %T`, getTypeName[T](), v)
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Require forces the initialization of a slot
|
|
|
|
|
|
|
|
// func Require[T any](l *ServiceLocator, slotKey SlotKey[T]) error {
|
|
|
|
|
|
|
|
// var zero T
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// slot, ok := l.providers[slotKey]
|
|
|
|
|
|
|
|
// if !ok {
|
|
|
|
|
|
|
|
// return fmt.Errorf(`no injected value for type %T`, zero)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return slot.checkInitialized(l)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // MustRequire forces the initialization of a slot or panics if the slot is missing
|
|
|
|
|
|
|
|
// func MustRequire[T any](l *ServiceLocator, slotKey SlotKey[T]) {
|
|
|
|
|
|
|
|
// err := Require(l, slotKey)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// panic(err)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Inject will set the implementation for "S" to "value" (the service will be initialized when needed after all of its dependencies)
|
|
|
|
|
|
|
|
// func Inject[S Service](l *Context, value S) {
|
|
|
|
|
|
|
|
// key := getTypeName[S]()
|
|
|
|
|
|
|
|
// log.Printf(`injecting value of type %T for interface %s`, value, key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// l.providers[key] = &provider{false, nil, value}
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // InjectValue will set the implementation for "S" to "value" and mark this service as already initialized (as this is just a constant)
|
|
|
|
|
|
|
|
// func InjectValue[S Service](l *Context, value S) S {
|
|
|
|
|
|
|
|
// key := getTypeName[S]()
|
|
|
|
|
|
|
|
// log.Printf(`injecting value of type %T for interface %s`, value, key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// l.providers[key] = &provider{true, nil, value}
|
|
|
|
|
|
|
|
// return value
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Use will retrive from the service locator the implementation set for the type "T" and initialize the service if yet to be initialized
|
|
|
|
|
|
|
|
// func Use[T Service](l *Context) (T, error) {
|
|
|
|
|
|
|
|
// provider, ok := l.providers[getTypeName[T]()]
|
|
|
|
|
|
|
|
// if !ok {
|
|
|
|
|
|
|
|
// var zero T
|
|
|
|
|
|
|
|
// return zero, fmt.Errorf(`no injected value for type "%T"`, zero)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if provider.initialized {
|
|
|
|
|
|
|
|
// service := provider.service.(T)
|
|
|
|
|
|
|
|
// return service, nil
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// log.Printf(`initializing %T`, provider.service)
|
|
|
|
|
|
|
|
// if err := provider.service.Initialize(l); err != nil {
|
|
|
|
|
|
|
|
// var zero T
|
|
|
|
|
|
|
|
// return zero, err
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// provider.initialized = true
|
|
|
|
|
|
|
|
// service := provider.service.(T)
|
|
|
|
|
|
|
|
// return service, nil
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getTypeName[T any]() string {
|
|
|
|
|
|
|
|
var zero T
|
|
|
|
|
|
|
|
return fmt.Sprintf(`%T`, &zero)[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|