Add generic mustcomponent as in middleware

This commit is contained in:
mcrakhman 2023-01-02 13:16:49 +01:00 committed by Mikhail Iudin
parent 9c1f0acf8b
commit 54813ccdec
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0

View File

@ -128,6 +128,19 @@ func (app *App) MustComponent(name string) Component {
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
func (app *App) ComponentNames() (names []string) {
app.mu.RLock()