Add service.ContextWithDefaultRegistry

This commit is contained in:
世界 2023-04-23 10:02:15 +08:00
parent a39dcdba79
commit 81e2f7d664
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -10,6 +10,13 @@ func ContextWithRegistry(ctx context.Context, registry Registry) context.Context
return context.WithValue(ctx, common.DefaultValue[*Registry](), registry)
}
func ContextWithDefaultRegistry(ctx context.Context) context.Context {
if RegistryFromContext(ctx) != nil {
return ctx
}
return context.WithValue(ctx, common.DefaultValue[*Registry](), NewRegistry())
}
func RegistryFromContext(ctx context.Context) Registry {
registry := ctx.Value(common.DefaultValue[*Registry]())
if registry == nil {