Meta key change
This commit is contained in:
parent
9ae0956cea
commit
b9f4e17d4c
@ -4,26 +4,22 @@ import (
|
|||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
type contextKeyT string
|
type metaKey struct{}
|
||||||
|
|
||||||
const (
|
|
||||||
contextKey = contextKeyT("github.com/micro/go-micro/metadata")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Metadata map[string]string
|
type Metadata map[string]string
|
||||||
|
|
||||||
func FromContext(ctx context.Context) (Metadata, bool) {
|
func FromContext(ctx context.Context) (Metadata, bool) {
|
||||||
md, ok := ctx.Value(contextKey).(Metadata)
|
md, ok := ctx.Value(metaKey{}).(Metadata)
|
||||||
return md, ok
|
return md, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContext(ctx context.Context, md Metadata) context.Context {
|
func NewContext(ctx context.Context, md Metadata) context.Context {
|
||||||
if emd, ok := ctx.Value(contextKey).(Metadata); ok {
|
if emd, ok := ctx.Value(metaKey{}).(Metadata); ok {
|
||||||
for k, v := range emd {
|
for k, v := range emd {
|
||||||
if _, ok := md[k]; !ok {
|
if _, ok := md[k]; !ok {
|
||||||
md[k] = v
|
md[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return context.WithValue(ctx, contextKey, md)
|
return context.WithValue(ctx, metaKey{}, md)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user