micro/context/context.go

30 lines
488 B
Go
Raw Normal View History

2015-05-23 13:53:40 +03:00
package context
import (
"golang.org/x/net/context"
)
type key int
const (
mdKey = key(0)
)
2015-05-27 00:39:48 +03:00
type Metadata map[string]string
2015-05-23 13:53:40 +03:00
2015-05-27 00:39:48 +03:00
func GetMetadata(ctx context.Context) (Metadata, bool) {
md, ok := ctx.Value(mdKey).(Metadata)
2015-05-23 13:53:40 +03:00
return md, ok
}
2015-05-27 00:39:48 +03:00
func WithMetadata(ctx context.Context, md Metadata) context.Context {
if emd, ok := ctx.Value(mdKey).(Metadata); ok {
for k, v := range emd {
if _, ok := md[k]; !ok {
md[k] = v
}
}
}
2015-05-23 13:53:40 +03:00
return context.WithValue(ctx, mdKey, md)
}