From d10e7bba01f440a024633a43174b4d710600c598 Mon Sep 17 00:00:00 2001 From: Snider Date: Tue, 24 Mar 2026 20:10:29 +0000 Subject: [PATCH] feat: MustServiceFor[T] + fix service names test for auto-registered cli Co-Authored-By: Virgil --- service.go | 12 ++++++++++++ service_test.go | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/service.go b/service.go index 3424769..1a420d9 100644 --- a/service.go +++ b/service.go @@ -140,6 +140,18 @@ func ServiceFor[T any](c *Core, name string) (T, bool) { return typed, ok } +// MustServiceFor retrieves a registered service by name and asserts its type. +// Panics if the service is not found or the type assertion fails. +// +// cli := core.MustServiceFor[*Cli](c, "cli") +func MustServiceFor[T any](c *Core, name string) T { + v, ok := ServiceFor[T](c, name) + if !ok { + panic(E("core.MustServiceFor", Sprintf("service %q not found or wrong type", name), nil)) + } + return v +} + // Services returns all registered service names. // // names := c.Services() diff --git a/service_test.go b/service_test.go index ddd32fd..63b49b8 100644 --- a/service_test.go +++ b/service_test.go @@ -47,9 +47,9 @@ func TestService_Names_Good(t *testing.T) { c.Service("a", Service{}) c.Service("b", Service{}) names := c.Services() - assert.Len(t, names, 2) assert.Contains(t, names, "a") assert.Contains(t, names, "b") + assert.Contains(t, names, "cli") // auto-registered by CliRegister in New() } // --- Service Lifecycle ---