Skip to content

Commit

Permalink
Add must register func for service
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Sep 3, 2023
1 parent 30bf19f commit 0eec7bb
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions service/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ func ContextWithPtr[T any](ctx context.Context, servicePtr *T) context.Context {
registry.Register(common.DefaultValue[*T](), servicePtr)
return ctx
}

func MustRegister[T any](ctx context.Context, service T) {
registry := RegistryFromContext(ctx)
if registry == nil {
panic("missing service registry in context")
}
registry.Register(common.DefaultValue[*T](), service)
}

func MustRegisterPtr[T any](ctx context.Context, servicePtr *T) {
registry := RegistryFromContext(ctx)
if registry == nil {
panic("missing service registry in context")
}
registry.Register(common.DefaultValue[*T](), servicePtr)
}

0 comments on commit 0eec7bb

Please sign in to comment.