Strip MetadataKey global var

This commit is contained in:
Asim Aslam 2020-04-12 11:16:08 +01:00
parent cf67d460b7
commit 962588b649
4 changed files with 15 additions and 15 deletions

View File

@ -118,7 +118,7 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// create context
cx := ctx.FromRequest(r)
// get context from http handler wrappers
md, ok := r.Context().Value(metadata.MetadataKey{}).(metadata.Metadata)
md, ok := metadata.FromContext(r.Context())
if !ok {
md = make(metadata.Metadata)
}
@ -293,7 +293,7 @@ func requestPayload(r *http.Request) ([]byte, error) {
// otherwise as per usual
ctx := r.Context()
// dont user meadata.FromContext as it mangles names
md, ok := ctx.Value(metadata.MetadataKey{}).(metadata.Metadata)
md, ok := metadata.FromContext(ctx)
if !ok {
md = make(map[string]string)
}

View File

@ -271,7 +271,7 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) {
}
pMatch = true
ctx := req.Context()
md, ok := ctx.Value(metadata.MetadataKey{}).(metadata.Metadata)
md, ok := metadata.FromContext(ctx)
if !ok {
md = make(metadata.Metadata)
}
@ -279,7 +279,7 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) {
md[fmt.Sprintf("x-api-field-%s", k)] = v
}
md["x-api-body"] = ep.apiep.Body
*req = *req.Clone(context.WithValue(ctx, metadata.MetadataKey{}, md))
*req = *req.Clone(metadata.NewContext(ctx, md))
break pathLoop
}
if !pMatch {

View File

@ -6,7 +6,7 @@ import (
"strings"
)
type MetadataKey struct{}
type metadataKey struct{}
// Metadata is our way of representing request headers internally.
// They're used at the RPC level and translate back and forth
@ -57,7 +57,7 @@ func Set(ctx context.Context, k, v string) context.Context {
} else {
md[k] = v
}
return context.WithValue(ctx, MetadataKey{}, md)
return context.WithValue(ctx, metadataKey{}, md)
}
// Get returns a single value from metadata in the context
@ -80,7 +80,7 @@ func Get(ctx context.Context, key string) (string, bool) {
// FromContext returns metadata from the given context
func FromContext(ctx context.Context) (Metadata, bool) {
md, ok := ctx.Value(MetadataKey{}).(Metadata)
md, ok := ctx.Value(metadataKey{}).(Metadata)
if !ok {
return nil, ok
}
@ -96,7 +96,7 @@ func FromContext(ctx context.Context) (Metadata, bool) {
// NewContext creates a new context with the given metadata
func NewContext(ctx context.Context, md Metadata) context.Context {
return context.WithValue(ctx, MetadataKey{}, md)
return context.WithValue(ctx, metadataKey{}, md)
}
// MergeContext merges metadata to existing metadata, overwriting if specified
@ -104,7 +104,7 @@ func MergeContext(ctx context.Context, patchMd Metadata, overwrite bool) context
if ctx == nil {
ctx = context.Background()
}
md, _ := ctx.Value(MetadataKey{}).(Metadata)
md, _ := ctx.Value(metadataKey{}).(Metadata)
cmd := make(Metadata, len(md))
for k, v := range md {
cmd[k] = v
@ -118,5 +118,5 @@ func MergeContext(ctx context.Context, patchMd Metadata, overwrite bool) context
delete(cmd, k)
}
}
return context.WithValue(ctx, MetadataKey{}, cmd)
return context.WithValue(ctx, metadataKey{}, cmd)
}