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 ---