Skip to content

Commit

Permalink
metadata: add Delete method to MD (#4549)
Browse files Browse the repository at this point in the history
  • Loading branch information
konradreiche committed Jun 16, 2021
1 parent 4c651ed commit 7e35356
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions metadata/metadata.go
Expand Up @@ -123,6 +123,13 @@ func (md MD) Append(k string, vals ...string) {
md[k] = append(md[k], vals...)
}

// Delete removes the values for a given key k which is converted to lowercase
// before removing it from md.
func (md MD) Delete(k string) {
k = strings.ToLower(k)
delete(md, k)
}

// 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
Expand Down
29 changes: 29 additions & 0 deletions metadata/metadata_test.go
Expand Up @@ -169,6 +169,35 @@ func (s) TestAppend(t *testing.T) {
}
}

func (s) TestDelete(t *testing.T) {
for _, test := range []struct {
md MD
deleteKey string
want MD
}{
{
md: Pairs("My-Optional-Header", "42"),
deleteKey: "My-Optional-Header",
want: Pairs(),
},
{
md: Pairs("My-Optional-Header", "42"),
deleteKey: "Other-Key",
want: Pairs("my-optional-header", "42"),
},
{
md: Pairs("My-Optional-Header", "42"),
deleteKey: "my-OptIoNal-HeAder",
want: Pairs(),
},
} {
test.md.Delete(test.deleteKey)
if !reflect.DeepEqual(test.md, test.want) {
t.Errorf("value of metadata is %v, want %v", test.md, test.want)
}
}
}

func (s) TestAppendToOutgoingContext(t *testing.T) {
// Pre-existing metadata
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down

0 comments on commit 7e35356

Please sign in to comment.