From ee8b96d75a6ba5471c37b18e1c5376b94ef2615e Mon Sep 17 00:00:00 2001 From: Mikhail Iudin Date: Wed, 11 Dec 2024 12:21:17 +0100 Subject: [PATCH 1/3] GO-4384 Add a non panicking method shouldcomponent --- app/app.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app/app.go b/app/app.go index ecdc5995..61295369 100644 --- a/app/app.go +++ b/app/app.go @@ -27,6 +27,7 @@ var ( StopDeadline = time.Minute StopWarningAfter = time.Second * 10 StartWarningAfter = time.Second * 10 + ComponentNotFound = errors.New("component not found") ) // Component is a minimal interface for a common app.Component @@ -179,22 +180,30 @@ func (app *App) MustComponent(name string) Component { return s } -// MustComponent - generic version of app.MustComponent -func MustComponent[i any](app *App) i { +func ShouldComponent[t any](app *App) (t, error) { app.mu.RLock() defer app.mu.RUnlock() + var empty t current := app for current != nil { for _, s := range current.components { - if v, ok := s.(i); ok { + if v, ok := s.(t); ok { app.onComponent(s) - return v + return v, nil } } current = current.parent } - empty := new(i) - panic(fmt.Errorf("component with interface %T is not found", empty)) + return empty, ComponentNotFound +} + +// MustComponent - generic version of app.MustComponent +func MustComponent[t any](app *App) t { + component, err := ShouldComponent[t](app) + if err != nil { + panic(fmt.Errorf("component with interface %T is not found", new(t))) + } + return component } // ComponentNames returns all registered names From 16aa0c4c2f2b6239151fc26eb62f79ad0d1a7a31 Mon Sep 17 00:00:00 2001 From: Mikhail Iudin Date: Wed, 11 Dec 2024 13:26:58 +0100 Subject: [PATCH 2/3] GO-4384 Add a non panicking method shouldcomponent --- app/app.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/app.go b/app/app.go index 61295369..730484f5 100644 --- a/app/app.go +++ b/app/app.go @@ -180,7 +180,7 @@ func (app *App) MustComponent(name string) Component { return s } -func ShouldComponent[t any](app *App) (t, error) { +func GetComponent[t any](app *App) (t, error) { app.mu.RLock() defer app.mu.RUnlock() var empty t @@ -199,7 +199,7 @@ func ShouldComponent[t any](app *App) (t, error) { // MustComponent - generic version of app.MustComponent func MustComponent[t any](app *App) t { - component, err := ShouldComponent[t](app) + component, err := GetComponent[t](app) if err != nil { panic(fmt.Errorf("component with interface %T is not found", new(t))) } From 7c5c5c1ed8d7160f708233f69ae3d73cbb8436a4 Mon Sep 17 00:00:00 2001 From: Mikhail Iudin Date: Wed, 11 Dec 2024 13:50:48 +0100 Subject: [PATCH 3/3] GO-4384 Add a non panicking method shouldcomponent --- app/app.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/app.go b/app/app.go index 730484f5..947000b9 100644 --- a/app/app.go +++ b/app/app.go @@ -23,11 +23,11 @@ var ( ) var ( - log = logger.NewNamed("app") - StopDeadline = time.Minute - StopWarningAfter = time.Second * 10 - StartWarningAfter = time.Second * 10 - ComponentNotFound = errors.New("component not found") + log = logger.NewNamed("app") + StopDeadline = time.Minute + StopWarningAfter = time.Second * 10 + StartWarningAfter = time.Second * 10 + ErrComponentNotFound = errors.New("component not found") ) // Component is a minimal interface for a common app.Component @@ -194,7 +194,7 @@ func GetComponent[t any](app *App) (t, error) { } current = current.parent } - return empty, ComponentNotFound + return empty, ErrComponentNotFound } // MustComponent - generic version of app.MustComponent