metadata: sync with grpc
Some checks failed
coverage / build (push) Failing after 41s
test / test (push) Successful in 2m31s
sync / sync (push) Successful in 46s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2025-04-29 23:13:57 +03:00
parent 7ff7a3dbe0
commit e4ee705eb2
2 changed files with 11 additions and 25 deletions

View File

@@ -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) {

View File

@@ -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 {