Appending to metadata is a side effect. Let's not do that anymore.
This commit is contained in:
parent
0410508385
commit
089c457d6c
@ -17,12 +17,5 @@ func FromContext(ctx context.Context) (Metadata, bool) {
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, md Metadata) context.Context {
|
||||
if emd, ok := ctx.Value(metaKey{}).(Metadata); ok {
|
||||
for k, v := range emd {
|
||||
if _, ok := md[k]; !ok {
|
||||
md[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
return context.WithValue(ctx, metaKey{}, md)
|
||||
}
|
||||
|
19
wrapper.go
19
wrapper.go
@ -12,17 +12,30 @@ type clientWrapper struct {
|
||||
headers metadata.Metadata
|
||||
}
|
||||
|
||||
func (c *clientWrapper) setHeaders(ctx context.Context) context.Context {
|
||||
md, ok := metadata.FromContext(ctx)
|
||||
if !ok {
|
||||
md = metadata.Metadata{}
|
||||
}
|
||||
for k, v := range c.headers {
|
||||
if _, ok := md[k]; !ok {
|
||||
md[k] = v
|
||||
}
|
||||
}
|
||||
return metadata.NewContext(ctx, md)
|
||||
}
|
||||
|
||||
func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
||||
ctx = metadata.NewContext(ctx, c.headers)
|
||||
ctx = c.setHeaders(ctx)
|
||||
return c.Client.Call(ctx, req, rsp, opts...)
|
||||
}
|
||||
|
||||
func (c *clientWrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Streamer, error) {
|
||||
ctx = metadata.NewContext(ctx, c.headers)
|
||||
ctx = c.setHeaders(ctx)
|
||||
return c.Client.Stream(ctx, req, opts...)
|
||||
}
|
||||
|
||||
func (c *clientWrapper) Publish(ctx context.Context, p client.Publication, opts ...client.PublishOption) error {
|
||||
ctx = metadata.NewContext(ctx, c.headers)
|
||||
ctx = c.setHeaders(ctx)
|
||||
return c.Client.Publish(ctx, p, opts...)
|
||||
}
|
||||
|
55
wrapper_test.go
Normal file
55
wrapper_test.go
Normal file
@ -0,0 +1,55 @@
|
||||
package micro
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/micro/go-micro/metadata"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestWrapper(t *testing.T) {
|
||||
testData := []struct {
|
||||
existing metadata.Metadata
|
||||
headers metadata.Metadata
|
||||
overwrite bool
|
||||
}{
|
||||
{
|
||||
existing: metadata.Metadata{},
|
||||
headers: metadata.Metadata{
|
||||
"foo": "bar",
|
||||
},
|
||||
overwrite: true,
|
||||
},
|
||||
{
|
||||
existing: metadata.Metadata{
|
||||
"foo": "bar",
|
||||
},
|
||||
headers: metadata.Metadata{
|
||||
"foo": "baz",
|
||||
},
|
||||
overwrite: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, d := range testData {
|
||||
c := &clientWrapper{
|
||||
headers: d.headers,
|
||||
}
|
||||
|
||||
ctx := metadata.NewContext(context.Background(), d.existing)
|
||||
c.setHeaders(ctx)
|
||||
|
||||
md, _ := metadata.FromContext(ctx)
|
||||
|
||||
for k, v := range d.headers {
|
||||
if d.overwrite && md[k] != v {
|
||||
t.Fatalf("Expected %s=%s got %s=%s", k, v, k, md[k])
|
||||
}
|
||||
if !d.overwrite && md[k] != d.existing[k] {
|
||||
t.Fatalf("Expected %s=%s got %s=%s", k, d.existing[k], k, md[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user