From bdc31f6862fe855d54e04fa56169339f2eaa4b51 Mon Sep 17 00:00:00 2001 From: huamihe Date: Fri, 12 Apr 2024 17:29:15 +0800 Subject: [PATCH] enhance uniqueGrantID method --- cos.go | 8 ++++---- cos_test.go | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cos.go b/cos.go index a1ea9b4..03a1879 100644 --- a/cos.go +++ b/cos.go @@ -627,13 +627,13 @@ func decodeACL(resp *Response, res *ACLXml) { } func uniqueGrantID(grantIDs []string) string { - res := []string{} - filter := make(map[string]int) + res := make([]string, 0, len(grantIDs)) + filter := make(map[string]struct{}) for _, id := range grantIDs { - if filter[id] != 0 { + if _, ok := filter[id]; ok { continue } - filter[id] = 1 + filter[id] = struct{}{} res = append(res, id) } return strings.Join(res, ",") diff --git a/cos_test.go b/cos_test.go index 2009d99..d171d8a 100644 --- a/cos_test.go +++ b/cos_test.go @@ -231,3 +231,11 @@ func Test_SwitchHost(t *testing.T) { } } + +func TestUniqueGrantID(t *testing.T) { + ids := []string{"abc", "abc", "ab"} + actual := uniqueGrantID(ids) + if actual != "abc,ab" { + t.Errorf("expect uniqueIDs to be abc,ab, got %v", actual) + } +}