2020-09-22 13:15:05 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type brokerKey struct{}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// FromContext returns broker from passed context
|
2020-09-22 13:15:05 +03:00
|
|
|
func FromContext(ctx context.Context) (Broker, bool) {
|
2020-12-17 22:52:00 +03:00
|
|
|
if ctx == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2020-09-22 13:15:05 +03:00
|
|
|
c, ok := ctx.Value(brokerKey{}).(Broker)
|
|
|
|
return c, ok
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// NewContext savess broker in context
|
2020-09-22 13:15:05 +03:00
|
|
|
func NewContext(ctx context.Context, s Broker) context.Context {
|
2020-12-17 22:52:00 +03:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2020-09-22 13:15:05 +03:00
|
|
|
return context.WithValue(ctx, brokerKey{}, s)
|
|
|
|
}
|