Add generic mustcomponent as in middleware

This commit is contained in:
mcrakhman 2023-01-02 13:16:49 +01:00
parent 1a2ceb6e2b
commit 8680c24329
No known key found for this signature in database
GPG Key ID: DED12CFEF5B8396B

View File

@ -128,6 +128,19 @@ func (app *App) MustComponent(name string) Component {
return s return s
} }
// MustComponent - generic version of app.MustComponent
func MustComponent[i any](app *App) i {
app.mu.RLock()
defer app.mu.RUnlock()
for _, s := range app.components {
if v, ok := s.(i); ok {
return v
}
}
empty := new(i)
panic(fmt.Errorf("component with interface %T is not found", empty))
}
// ComponentNames returns all registered names // ComponentNames returns all registered names
func (app *App) ComponentNames() (names []string) { func (app *App) ComponentNames() (names []string) {
app.mu.RLock() app.mu.RLock()