From 54813ccdec6c33dbc70cfb20106f4936415651bb Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Mon, 2 Jan 2023 13:16:49 +0100 Subject: [PATCH] Add generic mustcomponent as in middleware --- common/app/app.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/common/app/app.go b/common/app/app.go index 9e2e3fa0..d945db87 100644 --- a/common/app/app.go +++ b/common/app/app.go @@ -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()