feat: MustServiceFor[T] + fix service names test for auto-registered cli
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
6efa1591d6
commit
d10e7bba01
2 changed files with 13 additions and 1 deletions
12
service.go
12
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()
|
||||
|
|
|
|||
|
|
@ -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 ---
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue