2021-01-29 13:17:32 +03:00
|
|
|
package register
|
2020-12-07 16:10:20 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
type registerKey struct{}
|
2020-12-07 16:10:20 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// FromContext get register from context
|
|
|
|
func FromContext(ctx context.Context) (Register, bool) {
|
2020-12-17 22:52:00 +03:00
|
|
|
if ctx == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
c, ok := ctx.Value(registerKey{}).(Register)
|
2020-12-07 16:10:20 +03:00
|
|
|
return c, ok
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// NewContext put register in context
|
|
|
|
func NewContext(ctx context.Context, c Register) context.Context {
|
2020-12-17 22:52:00 +03:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
return context.WithValue(ctx, registerKey{}, c)
|
2020-12-07 16:10:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|