geck-go/viperconfig/context.go
2024-08-29 12:53:31 +02:00

26 lines
803 B
Go

package viperconfig
import "context"
type contextKey struct{}
var (
ConfigurationKey = contextKey{}
)
// GetConfigFromContext[T any] returns the current configuration extracted from the passed context
func GetConfigFromContext[T any](ctx context.Context) T {
cm := GetConfigMgrFromContext[T](ctx)
return cm.Get()
}
// GetConfigMgrFromContext[T any] returns the configuration manager stored in the passed context
func GetConfigMgrFromContext[T any](ctx context.Context) ConfigurationManager[T] {
return ctx.Value(ConfigurationKey).(ConfigurationManager[T])
}
// SetConfigMgrOnContext[T any] stores the passed configuration manager on the passed context
func SetConfigMgrOnContext[T any](ctx context.Context, value T) context.Context {
return context.WithValue(ctx, ConfigurationKey, value)
}