Refactor if-blocks in GetBearerToken method

This commit is contained in:
Pavel Korotkov 2020-07-23 14:29:36 +03:00
parent ae66e4de61
commit 04dbe30f49

View file

@ -13,15 +13,16 @@ const bearerTokenContextKey contextKey = "bearer-token"
// GetBearerToken returns a bearer token embedded into a context or error, if any. // GetBearerToken returns a bearer token embedded into a context or error, if any.
func GetBearerToken(ctx context.Context) (*service.BearerTokenMsg, error) { func GetBearerToken(ctx context.Context) (*service.BearerTokenMsg, error) {
if bt := ctx.Value(bearerTokenContextKey); bt != nil { bt := ctx.Value(bearerTokenContextKey)
if bt == nil {
return nil, errors.New("got nil bearer token")
}
v, ok := bt.(*service.BearerTokenMsg) v, ok := bt.(*service.BearerTokenMsg)
if !ok { if !ok {
return nil, errors.Errorf("extracted unexpected type other than bearer token's: %T", v) return nil, errors.Errorf("extracted unexpected type other than bearer token's: %T", v)
} }
return v, nil return v, nil
} }
return nil, errors.New("got nil bearer token")
}
// SetBearerToken return a context with embedded bearer token. // SetBearerToken return a context with embedded bearer token.
func SetBearerToken(ctx context.Context, bearerToken *service.BearerTokenMsg) context.Context { func SetBearerToken(ctx context.Context, bearerToken *service.BearerTokenMsg) context.Context {