swarm: modify context key (#17925)

* swarm: modify context key

* gofmt sctx.go
This commit is contained in:
KimMachineGun 2018-11-06 17:54:19 +09:00 committed by Viktor Trón
parent 126dfde6c9
commit baee850471
2 changed files with 10 additions and 16 deletions

View File

@ -7,14 +7,10 @@ import (
"github.com/ethereum/go-ethereum/swarm/sctx" "github.com/ethereum/go-ethereum/swarm/sctx"
) )
type contextKey int type uriKey struct{}
const (
uriKey contextKey = iota
)
func GetRUID(ctx context.Context) string { func GetRUID(ctx context.Context) string {
v, ok := ctx.Value(sctx.HTTPRequestIDKey).(string) v, ok := ctx.Value(sctx.HTTPRequestIDKey{}).(string)
if ok { if ok {
return v return v
} }
@ -22,11 +18,11 @@ func GetRUID(ctx context.Context) string {
} }
func SetRUID(ctx context.Context, ruid string) context.Context { func SetRUID(ctx context.Context, ruid string) context.Context {
return context.WithValue(ctx, sctx.HTTPRequestIDKey, ruid) return context.WithValue(ctx, sctx.HTTPRequestIDKey{}, ruid)
} }
func GetURI(ctx context.Context) *api.URI { func GetURI(ctx context.Context) *api.URI {
v, ok := ctx.Value(uriKey).(*api.URI) v, ok := ctx.Value(uriKey{}).(*api.URI)
if ok { if ok {
return v return v
} }
@ -34,5 +30,5 @@ func GetURI(ctx context.Context) *api.URI {
} }
func SetURI(ctx context.Context, uri *api.URI) context.Context { func SetURI(ctx context.Context, uri *api.URI) context.Context {
return context.WithValue(ctx, uriKey, uri) return context.WithValue(ctx, uriKey{}, uri)
} }

View File

@ -2,19 +2,17 @@ package sctx
import "context" import "context"
type ContextKey int type (
HTTPRequestIDKey struct{}
const ( requestHostKey struct{}
HTTPRequestIDKey ContextKey = iota
requestHostKey
) )
func SetHost(ctx context.Context, domain string) context.Context { func SetHost(ctx context.Context, domain string) context.Context {
return context.WithValue(ctx, requestHostKey, domain) return context.WithValue(ctx, requestHostKey{}, domain)
} }
func GetHost(ctx context.Context) string { func GetHost(ctx context.Context) string {
v, ok := ctx.Value(requestHostKey).(string) v, ok := ctx.Value(requestHostKey{}).(string)
if ok { if ok {
return v return v
} }