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
|
||||
// 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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
6
micro.go
6
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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user