mirror of https://github.com/grpc/grpc-go.git
				
				
				
			metadata: Add Get, Set, and Append methods to metadata.MD (#1940)
This commit is contained in:
		
							parent
							
								
									2eae9d0c74
								
							
						
					
					
						commit
						291de7f0ab
					
				| 
						 | 
				
			
			@ -95,6 +95,30 @@ func (md MD) Copy() MD {
 | 
			
		|||
	return Join(md)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get obtains the values for a given key.
 | 
			
		||||
func (md MD) Get(k string) []string {
 | 
			
		||||
	k = strings.ToLower(k)
 | 
			
		||||
	return md[k]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Set sets the value of a given key with a slice of values.
 | 
			
		||||
func (md MD) Set(k string, vals ...string) {
 | 
			
		||||
	if len(vals) == 0 {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	k = strings.ToLower(k)
 | 
			
		||||
	md[k] = vals
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Append adds the values to key k, not overwriting what was already stored at that key.
 | 
			
		||||
func (md MD) Append(k string, vals ...string) {
 | 
			
		||||
	if len(vals) == 0 {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	k = strings.ToLower(k)
 | 
			
		||||
	md[k] = append(md[k], vals...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Join joins any number of mds into a single MD.
 | 
			
		||||
// The order of values for each key is determined by the order in which
 | 
			
		||||
// the mds containing those values are presented to Join.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -73,6 +73,90 @@ func TestJoin(t *testing.T) {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestGet(t *testing.T) {
 | 
			
		||||
	for _, test := range []struct {
 | 
			
		||||
		md       MD
 | 
			
		||||
		key      string
 | 
			
		||||
		wantVals []string
 | 
			
		||||
	}{
 | 
			
		||||
		{md: Pairs("My-Optional-Header", "42"), key: "My-Optional-Header", wantVals: []string{"42"}},
 | 
			
		||||
		{md: Pairs("Header", "42", "Header", "43", "Header", "44", "other", "1"), key: "HEADER", wantVals: []string{"42", "43", "44"}},
 | 
			
		||||
		{md: Pairs("HEADER", "10"), key: "HEADER", wantVals: []string{"10"}},
 | 
			
		||||
	} {
 | 
			
		||||
		vals := test.md.Get(test.key)
 | 
			
		||||
		if !reflect.DeepEqual(vals, test.wantVals) {
 | 
			
		||||
			t.Errorf("value of metadata %v is %v, want %v", test.key, vals, test.wantVals)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSet(t *testing.T) {
 | 
			
		||||
	for _, test := range []struct {
 | 
			
		||||
		md      MD
 | 
			
		||||
		setKey  string
 | 
			
		||||
		setVals []string
 | 
			
		||||
		want    MD
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			md:      Pairs("My-Optional-Header", "42", "other-key", "999"),
 | 
			
		||||
			setKey:  "Other-Key",
 | 
			
		||||
			setVals: []string{"1"},
 | 
			
		||||
			want:    Pairs("my-optional-header", "42", "other-key", "1"),
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			md:      Pairs("My-Optional-Header", "42"),
 | 
			
		||||
			setKey:  "Other-Key",
 | 
			
		||||
			setVals: []string{"1", "2", "3"},
 | 
			
		||||
			want:    Pairs("my-optional-header", "42", "other-key", "1", "other-key", "2", "other-key", "3"),
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			md:      Pairs("My-Optional-Header", "42"),
 | 
			
		||||
			setKey:  "Other-Key",
 | 
			
		||||
			setVals: []string{},
 | 
			
		||||
			want:    Pairs("my-optional-header", "42"),
 | 
			
		||||
		},
 | 
			
		||||
	} {
 | 
			
		||||
		test.md.Set(test.setKey, test.setVals...)
 | 
			
		||||
		if !reflect.DeepEqual(test.md, test.want) {
 | 
			
		||||
			t.Errorf("value of metadata is %v, want %v", test.md, test.want)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestAppend(t *testing.T) {
 | 
			
		||||
	for _, test := range []struct {
 | 
			
		||||
		md         MD
 | 
			
		||||
		appendKey  string
 | 
			
		||||
		appendVals []string
 | 
			
		||||
		want       MD
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			md:         Pairs("My-Optional-Header", "42"),
 | 
			
		||||
			appendKey:  "Other-Key",
 | 
			
		||||
			appendVals: []string{"1"},
 | 
			
		||||
			want:       Pairs("my-optional-header", "42", "other-key", "1"),
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			md:         Pairs("My-Optional-Header", "42"),
 | 
			
		||||
			appendKey:  "my-OptIoNal-HeAder",
 | 
			
		||||
			appendVals: []string{"1", "2", "3"},
 | 
			
		||||
			want: Pairs("my-optional-header", "42", "my-optional-header", "1",
 | 
			
		||||
				"my-optional-header", "2", "my-optional-header", "3"),
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			md:         Pairs("My-Optional-Header", "42"),
 | 
			
		||||
			appendKey:  "my-OptIoNal-HeAder",
 | 
			
		||||
			appendVals: []string{},
 | 
			
		||||
			want:       Pairs("my-optional-header", "42"),
 | 
			
		||||
		},
 | 
			
		||||
	} {
 | 
			
		||||
		test.md.Append(test.appendKey, test.appendVals...)
 | 
			
		||||
		if !reflect.DeepEqual(test.md, test.want) {
 | 
			
		||||
			t.Errorf("value of metadata is %v, want %v", test.md, test.want)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestAppendToOutgoingContext(t *testing.T) {
 | 
			
		||||
	// Pre-existing metadata
 | 
			
		||||
	ctx := NewOutgoingContext(context.Background(), Pairs("k1", "v1", "k2", "v2"))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue