diff --git a/auth/auth.go b/auth/auth.go index b88b72bf..08583eb6 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -126,11 +126,17 @@ type accountKey struct{} // is not set, a nil account will be returned. The error is only returned // when there was a problem retrieving an account func AccountFromContext(ctx context.Context) (*Account, bool) { + if ctx == nil { + return nil, false + } acc, ok := ctx.Value(accountKey{}).(*Account) return acc, ok } // ContextWithAccount sets the account in the context func ContextWithAccount(ctx context.Context, account *Account) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, accountKey{}, account) } diff --git a/broker/context.go b/broker/context.go index 893b8188..90d453f9 100644 --- a/broker/context.go +++ b/broker/context.go @@ -8,12 +8,18 @@ type brokerKey struct{} // FromContext returns broker from passed context func FromContext(ctx context.Context) (Broker, bool) { + if ctx == nil { + return nil, false + } c, ok := ctx.Value(brokerKey{}).(Broker) return c, ok } // NewContext savess broker in context func NewContext(ctx context.Context, s Broker) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, brokerKey{}, s) } diff --git a/client/context.go b/client/context.go index a079ef11..37d2f34d 100644 --- a/client/context.go +++ b/client/context.go @@ -8,12 +8,18 @@ type clientKey struct{} // FromContext get client from context func FromContext(ctx context.Context) (Client, bool) { + if ctx == nil { + return nil, false + } c, ok := ctx.Value(clientKey{}).(Client) return c, ok } // NewContext put client in context func NewContext(ctx context.Context, c Client) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, clientKey{}, c) } diff --git a/config/context.go b/config/context.go index e3c251a2..04e262f0 100644 --- a/config/context.go +++ b/config/context.go @@ -7,11 +7,17 @@ import ( type configKey struct{} func FromContext(ctx context.Context) (Config, bool) { + if ctx == nil { + return nil, false + } c, ok := ctx.Value(configKey{}).(Config) return c, ok } func NewContext(ctx context.Context, c Config) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, configKey{}, c) } diff --git a/logger/context.go b/logger/context.go index 70056fa4..6cb6b65f 100644 --- a/logger/context.go +++ b/logger/context.go @@ -6,12 +6,18 @@ type loggerKey struct{} // FromContext returns logger from passed context func FromContext(ctx context.Context) (Logger, bool) { + if ctx == nil { + return nil, false + } l, ok := ctx.Value(loggerKey{}).(Logger) return l, ok } // NewContext stores logger into passed context func NewContext(ctx context.Context, l Logger) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, loggerKey{}, l) } diff --git a/micro.go b/micro.go index 87f2a6d4..8e5ed1d9 100644 --- a/micro.go +++ b/micro.go @@ -83,12 +83,18 @@ func NewService(opts ...Option) Service { // FromContext retrieves a Service from the Context. func FromContext(ctx context.Context) (Service, bool) { + if ctx == nil { + return nil, false + } s, ok := ctx.Value(serviceKey{}).(Service) return s, ok } // NewContext returns a new Context with the Service embedded within it. func NewContext(ctx context.Context, s Service) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, serviceKey{}, s) } diff --git a/network/transport/context.go b/network/transport/context.go index 86649f81..73e4681d 100644 --- a/network/transport/context.go +++ b/network/transport/context.go @@ -8,12 +8,18 @@ type transportKey struct{} // FromContext get transport from context func FromContext(ctx context.Context) (Transport, bool) { + if ctx == nil { + return nil, false + } c, ok := ctx.Value(transportKey{}).(Transport) return c, ok } // NewContext put transport in context func NewContext(ctx context.Context, c Transport) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, transportKey{}, c) } diff --git a/registry/context.go b/registry/context.go index 9ad7c8a6..f491cea9 100644 --- a/registry/context.go +++ b/registry/context.go @@ -8,12 +8,18 @@ type registryKey struct{} // FromContext get registry from context func FromContext(ctx context.Context) (Registry, bool) { + if ctx == nil { + return nil, false + } c, ok := ctx.Value(registryKey{}).(Registry) return c, ok } // NewContext put registry in context func NewContext(ctx context.Context, c Registry) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, registryKey{}, c) } diff --git a/server/context.go b/server/context.go index e57e57ca..7bd9287f 100644 --- a/server/context.go +++ b/server/context.go @@ -8,12 +8,18 @@ type serverKey struct{} // FromContext returns Server from context func FromContext(ctx context.Context) (Server, bool) { + if ctx == nil { + return nil, false + } c, ok := ctx.Value(serverKey{}).(Server) return c, ok } // NewContext stores Server to context func NewContext(ctx context.Context, s Server) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, serverKey{}, s) } diff --git a/store/context.go b/store/context.go index f0bdcb98..d048cad7 100644 --- a/store/context.go +++ b/store/context.go @@ -8,12 +8,18 @@ type storeKey struct{} // FromContext get store from context func FromContext(ctx context.Context) (Store, bool) { + if ctx == nil { + return nil, false + } c, ok := ctx.Value(storeKey{}).(Store) return c, ok } // NewContext put store in context func NewContext(ctx context.Context, c Store) context.Context { + if ctx == nil { + ctx = context.Background() + } return context.WithValue(ctx, storeKey{}, c) } diff --git a/tracer/context.go b/tracer/context.go index 271a6653..5617c098 100644 --- a/tracer/context.go +++ b/tracer/context.go @@ -14,6 +14,9 @@ const ( // FromContext returns a span from context func FromContext(ctx context.Context) (traceID string, parentSpanID string, isFound bool) { + if ctx == nil { + return "", "", false + } traceID, traceOk := metadata.Get(ctx, traceIDKey) microID, microOk := metadata.Get(ctx, "Micro-Id") 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 func NewContext(ctx context.Context, traceID, parentSpanID string) context.Context { + if ctx == nil { + ctx = context.Background() + } md := metadata.New(2) md.Set(traceIDKey, traceID) md.Set(spanIDKey, parentSpanID)