diff --git a/changelog/unreleased/fix-tags-pkg.md b/changelog/unreleased/fix-tags-pkg.md new file mode 100644 index 00000000000..af1946bfd53 --- /dev/null +++ b/changelog/unreleased/fix-tags-pkg.md @@ -0,0 +1,5 @@ +Enhancement: Fix tag pkg + +`tags` pkg needed an option to build the tags struct from a slice of tags. Here it is + +https://github.com/cs3org/reva/pull/3564 diff --git a/pkg/tags/tags.go b/pkg/tags/tags.go index c6bb765d5c7..2a9a4eb08e9 100644 --- a/pkg/tags/tags.go +++ b/pkg/tags/tags.go @@ -39,18 +39,22 @@ type Tags struct { numtags int } -// FromList creates a Tags struct from a list of tags -func FromList(s string) *Tags { - t := &Tags{sep: _tagsep, maxtags: _maxtags, exists: make(map[string]bool)} - t.t = t.addTags(s) +// New creates a Tag struct from a slice of tags, e.g. ["tag1", "tag2"] or a list of tags, e.g. "tag1,tag2" +func New(ts ...string) *Tags { + t := &Tags{sep: _tagsep, maxtags: _maxtags, exists: make(map[string]bool), t: make([]string, 0)} + for i := len(ts) - 1; i >= 0; i-- { + t.addTags(ts[i]) + } return t } // AddList appends a list of new tags and returns true if at least one was appended -func (t *Tags) AddList(s string) bool { - tags := t.addTags(s) - t.t = append(tags, t.t...) - return len(tags) > 0 +func (t *Tags) AddList(s ...string) bool { + var added []string + for _, tt := range s { + added = append(added, t.addTags(tt)...) + } + return len(added) > 0 } // RemoveList removes a list of tags and returns true if at least one was removed @@ -108,5 +112,6 @@ func (t *Tags) addTags(s string) []string { t.numtags++ } + t.t = append(added, t.t...) return added } diff --git a/pkg/tags/tags_test.go b/pkg/tags/tags_test.go index 0e91eef485f..2f85e606078 100644 --- a/pkg/tags/tags_test.go +++ b/pkg/tags/tags_test.go @@ -105,7 +105,7 @@ func TestAddTags(t *testing.T) { } for _, tc := range testCases { - ts := FromList(tc.InitialTags) + ts := New(tc.InitialTags) ts.maxtags = 10 added := ts.AddList(tc.TagsToAdd) @@ -171,7 +171,7 @@ func TestRemoveTags(t *testing.T) { } for _, tc := range testCases { - ts := FromList(tc.InitialTags) + ts := New(tc.InitialTags) removed := ts.RemoveList(tc.TagsToRemove) require.Equal(t, tc.ExpectNoTagsRemoved, !removed, tc.Alias) @@ -179,3 +179,30 @@ func TestRemoveTags(t *testing.T) { } } + +func TestBuilders(t *testing.T) { + testCases := []struct { + Alias string + List string + Slice []string + }{ + { + Alias: "simple", + List: "a,b,c", + Slice: []string{"a", "b", "c"}, + }, + { + Alias: "zero values", + List: "", + Slice: nil, + }, + } + + for _, tc := range testCases { + list := New(tc.List) + slice := New(tc.Slice...) + + require.Equal(t, list.AsSlice(), slice.AsSlice()) + require.Equal(t, list.AsList(), slice.AsList()) + } +}