fix context usage across codebase
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
70a17dc10a
commit
f6c0728a59
@ -126,11 +126,17 @@ type accountKey struct{}
|
|||||||
// is not set, a nil account will be returned. The error is only returned
|
// is not set, a nil account will be returned. The error is only returned
|
||||||
// when there was a problem retrieving an account
|
// when there was a problem retrieving an account
|
||||||
func AccountFromContext(ctx context.Context) (*Account, bool) {
|
func AccountFromContext(ctx context.Context) (*Account, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
acc, ok := ctx.Value(accountKey{}).(*Account)
|
acc, ok := ctx.Value(accountKey{}).(*Account)
|
||||||
return acc, ok
|
return acc, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContextWithAccount sets the account in the context
|
// ContextWithAccount sets the account in the context
|
||||||
func ContextWithAccount(ctx context.Context, account *Account) context.Context {
|
func ContextWithAccount(ctx context.Context, account *Account) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, accountKey{}, account)
|
return context.WithValue(ctx, accountKey{}, account)
|
||||||
}
|
}
|
||||||
|
@ -8,12 +8,18 @@ type brokerKey struct{}
|
|||||||
|
|
||||||
// FromContext returns broker from passed context
|
// FromContext returns broker from passed context
|
||||||
func FromContext(ctx context.Context) (Broker, bool) {
|
func FromContext(ctx context.Context) (Broker, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
c, ok := ctx.Value(brokerKey{}).(Broker)
|
c, ok := ctx.Value(brokerKey{}).(Broker)
|
||||||
return c, ok
|
return c, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext savess broker in context
|
// NewContext savess broker in context
|
||||||
func NewContext(ctx context.Context, s Broker) context.Context {
|
func NewContext(ctx context.Context, s Broker) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, brokerKey{}, s)
|
return context.WithValue(ctx, brokerKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,12 +8,18 @@ type clientKey struct{}
|
|||||||
|
|
||||||
// FromContext get client from context
|
// FromContext get client from context
|
||||||
func FromContext(ctx context.Context) (Client, bool) {
|
func FromContext(ctx context.Context) (Client, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
c, ok := ctx.Value(clientKey{}).(Client)
|
c, ok := ctx.Value(clientKey{}).(Client)
|
||||||
return c, ok
|
return c, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext put client in context
|
// NewContext put client in context
|
||||||
func NewContext(ctx context.Context, c Client) context.Context {
|
func NewContext(ctx context.Context, c Client) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, clientKey{}, c)
|
return context.WithValue(ctx, clientKey{}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,11 +7,17 @@ import (
|
|||||||
type configKey struct{}
|
type configKey struct{}
|
||||||
|
|
||||||
func FromContext(ctx context.Context) (Config, bool) {
|
func FromContext(ctx context.Context) (Config, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
c, ok := ctx.Value(configKey{}).(Config)
|
c, ok := ctx.Value(configKey{}).(Config)
|
||||||
return c, ok
|
return c, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContext(ctx context.Context, c Config) context.Context {
|
func NewContext(ctx context.Context, c Config) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, configKey{}, c)
|
return context.WithValue(ctx, configKey{}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,12 +6,18 @@ type loggerKey struct{}
|
|||||||
|
|
||||||
// FromContext returns logger from passed context
|
// FromContext returns logger from passed context
|
||||||
func FromContext(ctx context.Context) (Logger, bool) {
|
func FromContext(ctx context.Context) (Logger, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
l, ok := ctx.Value(loggerKey{}).(Logger)
|
l, ok := ctx.Value(loggerKey{}).(Logger)
|
||||||
return l, ok
|
return l, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext stores logger into passed context
|
// NewContext stores logger into passed context
|
||||||
func NewContext(ctx context.Context, l Logger) context.Context {
|
func NewContext(ctx context.Context, l Logger) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, loggerKey{}, l)
|
return context.WithValue(ctx, loggerKey{}, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
micro.go
6
micro.go
@ -83,12 +83,18 @@ func NewService(opts ...Option) Service {
|
|||||||
|
|
||||||
// FromContext retrieves a Service from the Context.
|
// FromContext retrieves a Service from the Context.
|
||||||
func FromContext(ctx context.Context) (Service, bool) {
|
func FromContext(ctx context.Context) (Service, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
s, ok := ctx.Value(serviceKey{}).(Service)
|
s, ok := ctx.Value(serviceKey{}).(Service)
|
||||||
return s, ok
|
return s, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext returns a new Context with the Service embedded within it.
|
// NewContext returns a new Context with the Service embedded within it.
|
||||||
func NewContext(ctx context.Context, s Service) context.Context {
|
func NewContext(ctx context.Context, s Service) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, serviceKey{}, s)
|
return context.WithValue(ctx, serviceKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,12 +8,18 @@ type transportKey struct{}
|
|||||||
|
|
||||||
// FromContext get transport from context
|
// FromContext get transport from context
|
||||||
func FromContext(ctx context.Context) (Transport, bool) {
|
func FromContext(ctx context.Context) (Transport, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
c, ok := ctx.Value(transportKey{}).(Transport)
|
c, ok := ctx.Value(transportKey{}).(Transport)
|
||||||
return c, ok
|
return c, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext put transport in context
|
// NewContext put transport in context
|
||||||
func NewContext(ctx context.Context, c Transport) context.Context {
|
func NewContext(ctx context.Context, c Transport) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, transportKey{}, c)
|
return context.WithValue(ctx, transportKey{}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,12 +8,18 @@ type registryKey struct{}
|
|||||||
|
|
||||||
// FromContext get registry from context
|
// FromContext get registry from context
|
||||||
func FromContext(ctx context.Context) (Registry, bool) {
|
func FromContext(ctx context.Context) (Registry, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
c, ok := ctx.Value(registryKey{}).(Registry)
|
c, ok := ctx.Value(registryKey{}).(Registry)
|
||||||
return c, ok
|
return c, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext put registry in context
|
// NewContext put registry in context
|
||||||
func NewContext(ctx context.Context, c Registry) context.Context {
|
func NewContext(ctx context.Context, c Registry) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, registryKey{}, c)
|
return context.WithValue(ctx, registryKey{}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,12 +8,18 @@ type serverKey struct{}
|
|||||||
|
|
||||||
// FromContext returns Server from context
|
// FromContext returns Server from context
|
||||||
func FromContext(ctx context.Context) (Server, bool) {
|
func FromContext(ctx context.Context) (Server, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
c, ok := ctx.Value(serverKey{}).(Server)
|
c, ok := ctx.Value(serverKey{}).(Server)
|
||||||
return c, ok
|
return c, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext stores Server to context
|
// NewContext stores Server to context
|
||||||
func NewContext(ctx context.Context, s Server) context.Context {
|
func NewContext(ctx context.Context, s Server) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, serverKey{}, s)
|
return context.WithValue(ctx, serverKey{}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,12 +8,18 @@ type storeKey struct{}
|
|||||||
|
|
||||||
// FromContext get store from context
|
// FromContext get store from context
|
||||||
func FromContext(ctx context.Context) (Store, bool) {
|
func FromContext(ctx context.Context) (Store, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
c, ok := ctx.Value(storeKey{}).(Store)
|
c, ok := ctx.Value(storeKey{}).(Store)
|
||||||
return c, ok
|
return c, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext put store in context
|
// NewContext put store in context
|
||||||
func NewContext(ctx context.Context, c Store) context.Context {
|
func NewContext(ctx context.Context, c Store) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
return context.WithValue(ctx, storeKey{}, c)
|
return context.WithValue(ctx, storeKey{}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,9 @@ const (
|
|||||||
|
|
||||||
// FromContext returns a span from context
|
// FromContext returns a span from context
|
||||||
func FromContext(ctx context.Context) (traceID string, parentSpanID string, isFound bool) {
|
func FromContext(ctx context.Context) (traceID string, parentSpanID string, isFound bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return "", "", false
|
||||||
|
}
|
||||||
traceID, traceOk := metadata.Get(ctx, traceIDKey)
|
traceID, traceOk := metadata.Get(ctx, traceIDKey)
|
||||||
microID, microOk := metadata.Get(ctx, "Micro-Id")
|
microID, microOk := metadata.Get(ctx, "Micro-Id")
|
||||||
if !traceOk && !microOk {
|
if !traceOk && !microOk {
|
||||||
@ -29,6 +32,9 @@ func FromContext(ctx context.Context) (traceID string, parentSpanID string, isFo
|
|||||||
|
|
||||||
// NewContext saves the trace and span ids in the context
|
// NewContext saves the trace and span ids in the context
|
||||||
func NewContext(ctx context.Context, traceID, parentSpanID string) context.Context {
|
func NewContext(ctx context.Context, traceID, parentSpanID string) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
md := metadata.New(2)
|
md := metadata.New(2)
|
||||||
md.Set(traceIDKey, traceID)
|
md.Set(traceIDKey, traceID)
|
||||||
md.Set(spanIDKey, parentSpanID)
|
md.Set(spanIDKey, parentSpanID)
|
||||||
|
Loading…
Reference in New Issue
Block a user