geck-go/viperconfig/context.go

27 lines
813 B
Go
Raw Normal View History

2024-08-29 10:36:48 +00:00
package configuration_manager
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)
}