Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metadata: add Delete method to MD #4549

Merged
merged 1 commit into from Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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