mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-22 16:50:38 +08:00
31 lines
575 B
Go
31 lines
575 B
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
|
|
)
|
|
|
|
type contextKey struct{}
|
|
|
|
var storageKey = contextKey{}
|
|
|
|
func WithContext(ctx context.Context, storage Storage) context.Context {
|
|
if storage == nil {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, storageKey, storage)
|
|
}
|
|
|
|
func FromContext(ctx context.Context) Storage {
|
|
storage, ok := ctx.Value(storageKey).(Storage)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return storage
|
|
}
|
|
|
|
func WithOverwrite(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, ctxkey.OverwriteExisting, true)
|
|
}
|