From eb534a46d07ba39fa49bbd08bf485c825d2000b7 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 9 Mar 2024 23:53:04 +0300 Subject: [PATCH] metadata: add exclude to Copy func Signed-off-by: Vasiliy Tolstov --- metadata/metadata.go | 3 ++- metadata/metadata_test.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/metadata/metadata.go b/metadata/metadata.go index 13a1c406..f846dc71 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -67,11 +67,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 := make(Metadata, len(md)) for k, v := range md { nmd[k] = v } + nmd.Del(exclude...) return nmd } diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index 809f5274..5d133db0 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -189,3 +189,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"][0] != "val1" { + t.Fatal("Copy exclude not works") + } +}