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