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..3256990b8f9 100644 --- a/pkg/tags/tags.go +++ b/pkg/tags/tags.go @@ -39,17 +39,23 @@ 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) +// FromSlice creates a Tag struct from a slice of tags, e.g. ["tag1", "tag2"] +func FromSlice(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 } +// FromList creates a Tags struct from a list of tags, e.g. "tag1,tag2" +func FromList(s string) *Tags { + return FromSlice([]string{s}) +} + // 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 } @@ -108,5 +114,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..440b95eb992 100644 --- a/pkg/tags/tags_test.go +++ b/pkg/tags/tags_test.go @@ -179,3 +179,29 @@ 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 := FromList(tc.List) + slice := FromSlice(tc.Slice) + + require.Equal(t, list, slice) + } +}