metadata: fix nil metadata from FromIncoming/FromOutgoing context

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-02-12 17:10:35 +03:00
parent 6751060d05
commit fd5ed64729
2 changed files with 6 additions and 5 deletions

View File

@ -16,7 +16,7 @@ func FromIncomingContext(ctx context.Context) (Metadata, bool) {
return nil, false return nil, false
} }
md, ok := ctx.Value(mdIncomingKey{}).(*rawMetadata) md, ok := ctx.Value(mdIncomingKey{}).(*rawMetadata)
if !ok { if !ok || md.md == nil {
return nil, false return nil, false
} }
return md.md, ok return md.md, ok
@ -29,7 +29,7 @@ func FromOutgoingContext(ctx context.Context) (Metadata, bool) {
return nil, false return nil, false
} }
md, ok := ctx.Value(mdOutgoingKey{}).(*rawMetadata) md, ok := ctx.Value(mdOutgoingKey{}).(*rawMetadata)
if !ok { if !ok || md.md == nil {
return nil, false return nil, false
} }
return md.md, ok return md.md, ok
@ -44,7 +44,7 @@ func FromContext(ctx context.Context) (Metadata, bool) {
return nil, false return nil, false
} }
md, ok := ctx.Value(mdKey{}).(*rawMetadata) md, ok := ctx.Value(mdKey{}).(*rawMetadata)
if !ok { if !ok || md.md == nil {
return nil, false return nil, false
} }
return md.md, ok return md.md, ok

View File

@ -2,7 +2,6 @@ package metadata
import ( import (
"context" "context"
"fmt"
"testing" "testing"
) )
@ -25,7 +24,9 @@ func TestPassing(t *testing.T) {
if !ok { if !ok {
t.Fatalf("missing metadata from outgoing context") t.Fatalf("missing metadata from outgoing context")
} }
fmt.Printf("%#+v\n", md) if v, ok := md.Get("Key1"); !ok || v != "Val1_new" {
t.Fatalf("invalid metadata value %#+v", md)
}
} }
func TestMerge(t *testing.T) { func TestMerge(t *testing.T) {