Copy metadata
This commit is contained in:
parent
7b89b36e37
commit
617db003d4
@ -12,6 +12,14 @@ type metaKey struct{}
|
||||
// from Transport headers.
|
||||
type Metadata map[string]string
|
||||
|
||||
func Copy(md Metadata) Metadata {
|
||||
cmd := make(Metadata)
|
||||
for k, v := range md {
|
||||
cmd[k] = v
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
func FromContext(ctx context.Context) (Metadata, bool) {
|
||||
md, ok := ctx.Value(metaKey{}).(Metadata)
|
||||
return md, ok
|
||||
|
@ -5,6 +5,21 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMetadataCopy(t *testing.T) {
|
||||
md := Metadata{
|
||||
"foo": "bar",
|
||||
"bar": "baz",
|
||||
}
|
||||
|
||||
cp := Copy(md)
|
||||
|
||||
for k, v := range md {
|
||||
if cv := cp[k]; cv != v {
|
||||
t.Fatalf("Got %s:%s for %s:%s", k, cv, k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataContext(t *testing.T) {
|
||||
md := Metadata{
|
||||
"foo": "bar",
|
||||
|
12
wrapper.go
12
wrapper.go
@ -13,15 +13,11 @@ type clientWrapper struct {
|
||||
}
|
||||
|
||||
func (c *clientWrapper) setHeaders(ctx context.Context) context.Context {
|
||||
md := make(metadata.Metadata)
|
||||
|
||||
if mda, ok := metadata.FromContext(ctx); ok {
|
||||
// make copy of metadata
|
||||
for k, v := range mda {
|
||||
md[k] = v
|
||||
}
|
||||
}
|
||||
// copy metadata
|
||||
mda, _ := metadata.FromContext(ctx)
|
||||
md := metadata.Copy(mda)
|
||||
|
||||
// set headers
|
||||
for k, v := range c.headers {
|
||||
if _, ok := md[k]; !ok {
|
||||
md[k] = v
|
||||
|
Loading…
Reference in New Issue
Block a user