metadata: allow to exclude some keys in Copy func #321

Merged
vtolstov merged 2 commits from metadata-copy-exclude into v3 2024-03-09 23:50:42 +03:00
2 changed files with 13 additions and 1 deletions

View File

@ -98,11 +98,12 @@ func (md Metadata) Del(keys ...string) {
} }
// Copy makes a copy of the metadata // Copy makes a copy of the metadata
func Copy(md Metadata) Metadata { func Copy(md Metadata, exclude ...string) Metadata {
nmd := New(len(md)) nmd := New(len(md))
for key, val := range md { for key, val := range md {
nmd.Set(key, val) nmd.Set(key, val)
} }
nmd.Del(exclude...)
return nmd return nmd
} }

View File

@ -190,3 +190,14 @@ func TestMetadataContext(t *testing.T) {
t.Errorf("Expected metadata length 1 got %d", i) t.Errorf("Expected metadata length 1 got %d", i)
} }
} }
func TestCopy(t *testing.T) {
md := New(2)
md.Set("key1", "val1", "key2", "val2")
nmd := Copy(md, "key2")
if len(nmd) != 1 {
t.Fatal("Copy exclude not works")
} else if nmd["Key1"] != "val1" {
t.Fatal("Copy exclude not works")
}
}