2020-12-07 16:10:20 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type configKey struct{}
|
|
|
|
|
2021-02-13 15:35:56 +03:00
|
|
|
// FromContext returns store from context
|
2020-12-07 16:10:20 +03:00
|
|
|
func FromContext(ctx context.Context) (Config, bool) {
|
2020-12-17 22:52:00 +03:00
|
|
|
if ctx == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2020-12-07 16:10:20 +03:00
|
|
|
c, ok := ctx.Value(configKey{}).(Config)
|
|
|
|
return c, ok
|
|
|
|
}
|
|
|
|
|
2021-02-13 15:35:56 +03:00
|
|
|
// NewContext put store in context
|
2020-12-07 16:10:20 +03:00
|
|
|
func NewContext(ctx context.Context, c Config) context.Context {
|
2020-12-17 22:52:00 +03:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2020-12-07 16:10:20 +03:00
|
|
|
return context.WithValue(ctx, configKey{}, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOption returns a function to setup a context with given value
|
|
|
|
func SetOption(k, v interface{}) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, k, v)
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 15:47:56 +03:00
|
|
|
|
|
|
|
// SetSaveOption returns a function to setup a context with given value
|
|
|
|
func SetSaveOption(k, v interface{}) SaveOption {
|
|
|
|
return func(o *SaveOptions) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLoadOption returns a function to setup a context with given value
|
|
|
|
func SetLoadOption(k, v interface{}) LoadOption {
|
|
|
|
return func(o *LoadOptions) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, k, v)
|
|
|
|
}
|
|
|
|
}
|