Skip to content

Commit f81c1cf

Browse files
committed
Tags as Map
1 parent ff08225 commit f81c1cf

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

builder.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func new() OopsErrorBuilder {
4545

4646
// context
4747
domain: "",
48-
tags: []string{},
48+
tags: make(map[string]struct{}),
4949
context: map[string]any{},
5050

5151
trace: "",
@@ -240,7 +240,9 @@ func (o OopsErrorBuilder) In(domain string) OopsErrorBuilder {
240240
// Tags adds multiple tags, describing the feature returning an error.
241241
func (o OopsErrorBuilder) Tags(tags ...string) OopsErrorBuilder {
242242
o2 := o.copy()
243-
o2.tags = append(o2.tags, tags...)
243+
for _, tag := range tags {
244+
o2.tags[tag] = struct{}{}
245+
}
244246
return o2
245247
}
246248

error.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type OopsError struct {
3030

3131
// context
3232
domain string
33-
tags []string
33+
tags map[string]struct{}
3434
context map[string]any
3535

3636
trace string
@@ -118,18 +118,22 @@ func (o OopsError) Domain() string {
118118

119119
// Tags returns the tags of the error.
120120
func (o OopsError) Tags() []string {
121-
tags := []string{}
121+
tags := make(map[string]struct{})
122122

123123
recursive(o, func(e OopsError) {
124-
tags = append(tags, e.tags...)
124+
for tag := range e.tags {
125+
tags[tag] = struct{}{}
126+
}
125127
})
126128

127-
return lo.Uniq(tags)
129+
return lo.MapToSlice(tags, func(key string, _ struct{}) string {
130+
return key
131+
})
128132
}
129133

130134
// HasTag returns true if the tags of the error contain provided value.
131135
func (o OopsError) HasTag(tag string) bool {
132-
if lo.Contains(o.tags, tag) {
136+
if _, ok := o.tags[tag]; ok {
133137
return true
134138
}
135139

oops_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ func TestOopsTags(t *testing.T) {
172172
is.Len(join.Unwrap(), 2)
173173
is.Equal(assert.AnError, join.Unwrap()[0].(OopsError).err)
174174
is.Equal(assert.AnError, join.Unwrap()[1].(OopsError).err)
175-
is.Equal([]string{"iam", "authz", "iam"}, err.(OopsError).tags) // not deduplicated
176-
is.Equal([]string{"iam", "internal"}, join.Unwrap()[0].(OopsError).tags)
177-
is.Equal([]string{"not-found"}, join.Unwrap()[1].(OopsError).tags)
178-
is.Equal([]string{"iam", "authz", "internal"}, err.(OopsError).Tags()) // deduplicated and recursive
175+
is.ElementsMatch([]string{"iam", "authz"}, lo.Keys(err.(OopsError).tags))
176+
is.ElementsMatch([]string{"iam", "internal"}, lo.Keys(join.Unwrap()[0].(OopsError).tags))
177+
is.ElementsMatch([]string{"not-found"}, lo.Keys(join.Unwrap()[1].(OopsError).tags))
178+
is.ElementsMatch([]string{"iam", "authz", "internal"}, err.(OopsError).Tags()) // recursive
179179
}
180180

181181
func TestOopsHasTag(t *testing.T) {
@@ -660,7 +660,11 @@ func TestOopsLogValuer(t *testing.T) {
660660
for i := range got {
661661
is.Equal(expectedAttrs[i].Key, got[i].Key)
662662
is.Equal(expectedAttrs[i].Value.Kind(), got[i].Value.Kind())
663-
is.EqualValues(expectedAttrs[i].Value.Any(), got[i].Value.Any())
663+
if "tags" == got[i].Key {
664+
is.ElementsMatch(expectedAttrs[i].Value.Any(), got[i].Value.Any())
665+
} else {
666+
is.EqualValues(expectedAttrs[i].Value.Any(), got[i].Value.Any())
667+
}
664668
}
665669
}
666670

0 commit comments

Comments
 (0)