metadata: allow to exclude some keys in Copy func (#321)
Reviewed-on: #321 Co-authored-by: Vasiliy Tolstov <v.tolstov@unistack.org> Co-committed-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
ed7972a1fa
commit
f66ac9736b
@ -98,11 +98,12 @@ func (md Metadata) Del(keys ...string) {
|
||||
}
|
||||
|
||||
// Copy makes a copy of the metadata
|
||||
func Copy(md Metadata) Metadata {
|
||||
func Copy(md Metadata, exclude ...string) Metadata {
|
||||
nmd := New(len(md))
|
||||
for key, val := range md {
|
||||
nmd.Set(key, val)
|
||||
}
|
||||
nmd.Del(exclude...)
|
||||
return nmd
|
||||
}
|
||||
|
||||
|
@ -190,3 +190,14 @@ func TestMetadataContext(t *testing.T) {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user