From f66ac9736b918761eee3f5141ded23aebd1da70f Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 9 Mar 2024 23:50:40 +0300 Subject: [PATCH] metadata: allow to exclude some keys in Copy func (#321) Reviewed-on: https://git.unistack.org/unistack-org/micro/pulls/321 Co-authored-by: Vasiliy Tolstov Co-committed-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 4718f420..bf7a8d3a 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -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 } diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index eccea7a7..a5381ede 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -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") + } +}