metadata: sync with grpc
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -112,7 +112,7 @@ func (md Metadata) Get(k string) []string {
|
|||||||
v, ok = md[strings.ToLower(k)]
|
v, ok = md[strings.ToLower(k)]
|
||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
v, ok = md[textproto.CanonicalMIMEHeaderKey(k)]
|
v = md[textproto.CanonicalMIMEHeaderKey(k)]
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
@@ -124,24 +124,13 @@ func (md Metadata) GetJoined(k string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the value of a given key with a slice of values.
|
// Set sets the value of a given key with a slice of values.
|
||||||
func (md Metadata) Add(key string, vals ...string) {
|
func (md Metadata) Set(key string, vals ...string) {
|
||||||
if len(vals) == 0 {
|
if len(vals) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
md[key] = vals
|
md[key] = vals
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the value of a given key with a slice of values.
|
|
||||||
func (md Metadata) Set(kvs ...string) {
|
|
||||||
if len(kvs)%2 == 1 {
|
|
||||||
panic(fmt.Sprintf("metadata: Set got an odd number of input pairs for metadata: %d", len(kvs)))
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(kvs); i += 2 {
|
|
||||||
md[kvs[i]] = append(md[kvs[i]], kvs[i+1])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append adds the values to key k, not overwriting what was already stored at
|
// Append adds the values to key k, not overwriting what was already stored at
|
||||||
// that key.
|
// that key.
|
||||||
func (md Metadata) Append(key string, vals ...string) {
|
func (md Metadata) Append(key string, vals ...string) {
|
||||||
|
@@ -5,6 +5,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TesSet(t *testing.T) {
|
||||||
|
md := Pairs("key1", "val1", "key2", "val2")
|
||||||
|
md.Set("key1", "val2", "val3")
|
||||||
|
v := md.GetJoined("X-Request-Id")
|
||||||
|
if v != "val2, val3" {
|
||||||
|
t.Fatal("set not works")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
func TestAppendOutgoingContextModify(t *testing.T) {
|
func TestAppendOutgoingContextModify(t *testing.T) {
|
||||||
md := Pairs("key1", "val1")
|
md := Pairs("key1", "val1")
|
||||||
@@ -47,18 +56,6 @@ func TestMultipleUsage(t *testing.T) {
|
|||||||
_ = omd
|
_ = omd
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetadataSetMultiple(t *testing.T) {
|
|
||||||
md := New(4)
|
|
||||||
md.Set("key1", "val1", "key2", "val2")
|
|
||||||
|
|
||||||
if v := md.GetJoined("key1"); v != "val1" {
|
|
||||||
t.Fatalf("invalid kv %#+v", md)
|
|
||||||
}
|
|
||||||
if v := md.GetJoined("key2"); v != "val2" {
|
|
||||||
t.Fatalf("invalid kv %#+v", md)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPairs(t *testing.T) {
|
func TestPairs(t *testing.T) {
|
||||||
md := Pairs("key1", "val1", "key2", "val2")
|
md := Pairs("key1", "val1", "key2", "val2")
|
||||||
if v := md.Get("key1"); v == nil {
|
if v := md.Get("key1"); v == nil {
|
||||||
|
Reference in New Issue
Block a user