Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify context key type #10375

Merged
merged 2 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions logger/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ import (
"go.uber.org/zap"
)

type key int

const (
loggerKey key = iota
)
type loggerContextKey struct{}

// NewContextWithLogger returns a new context with log added.
func NewContextWithLogger(ctx context.Context, log *zap.Logger) context.Context {
return context.WithValue(ctx, loggerKey, log)
return context.WithValue(ctx, loggerContextKey{}, log)
}

// FromContext returns the zap.Logger associated with ctx or nil if no logger has been assigned.
// LoggerFromContext returns the zap.Logger associated with ctx or nil if no logger has been assigned.
func FromContext(ctx context.Context) *zap.Logger {
l, _ := ctx.Value(loggerKey).(*zap.Logger)
l, _ := ctx.Value(loggerContextKey{}).(*zap.Logger)
return l
}
10 changes: 3 additions & 7 deletions pkg/metrics/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ package metrics

import "context"

type key int

const (
groupKey key = iota
)
type groupContextKey struct{}

// NewContextWithGroup returns a new context with the given Group added.
func NewContextWithGroup(ctx context.Context, c *Group) context.Context {
return context.WithValue(ctx, groupKey, c)
return context.WithValue(ctx, groupContextKey{}, c)
}

// GroupFromContext returns the Group associated with ctx or nil if no Group has been assigned.
func GroupFromContext(ctx context.Context) *Group {
c, _ := ctx.Value(groupKey).(*Group)
c, _ := ctx.Value(groupContextKey{}).(*Group)
return c
}
16 changes: 7 additions & 9 deletions pkg/tracing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@ package tracing

import "context"

type key int

const (
spanKey key = iota
traceKey
type (
spanContextKey struct{}
traceContextKey struct{}
)

// NewContextWithSpan returns a new context with the given Span added.
func NewContextWithSpan(ctx context.Context, c *Span) context.Context {
return context.WithValue(ctx, spanKey, c)
return context.WithValue(ctx, spanContextKey{}, c)
}

// SpanFromContext returns the Span associated with ctx or nil if no Span has been assigned.
func SpanFromContext(ctx context.Context) *Span {
c, _ := ctx.Value(spanKey).(*Span)
c, _ := ctx.Value(spanContextKey{}).(*Span)
return c
}

// NewContextWithTrace returns a new context with the given Trace added.
func NewContextWithTrace(ctx context.Context, t *Trace) context.Context {
return context.WithValue(ctx, traceKey, t)
return context.WithValue(ctx, traceContextKey{}, t)
}

// TraceFromContext returns the Trace associated with ctx or nil if no Trace has been assigned.
func TraceFromContext(ctx context.Context) *Trace {
c, _ := ctx.Value(traceKey).(*Trace)
c, _ := ctx.Value(traceContextKey{}).(*Trace)
return c
}
2 changes: 1 addition & 1 deletion query/execution_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (ctx *ExecutionContext) Err() error {

func (ctx *ExecutionContext) Value(key interface{}) interface{} {
switch key {
case monitorContextKey:
case monitorContextKey{}:
return ctx.task
}
return ctx.Context.Value(key)
Expand Down
10 changes: 4 additions & 6 deletions query/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,15 @@ type ExecutionOptions struct {
AbortCh <-chan struct{}
}

type contextKey int

const (
iteratorsContextKey contextKey = iota
monitorContextKey
type (
iteratorsContextKey struct{}
monitorContextKey struct{}
)

// NewContextWithIterators returns a new context.Context with the *Iterators slice added.
// The query planner will add instances of AuxIterator to the Iterators slice.
func NewContextWithIterators(ctx context.Context, itr *Iterators) context.Context {
return context.WithValue(ctx, iteratorsContextKey, itr)
return context.WithValue(ctx, iteratorsContextKey{}, itr)
}

// StatementExecutor executes a statement within the Executor.
Expand Down
2 changes: 1 addition & 1 deletion query/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Monitor interface {
// MonitorFromContext returns a Monitor embedded within the Context
// if one exists.
func MonitorFromContext(ctx context.Context) Monitor {
v, _ := ctx.Value(monitorContextKey).(Monitor)
v, _ := ctx.Value(monitorContextKey{}).(Monitor)
return v
}

Expand Down