Skip to content

Commit

Permalink
add tag builder for slices
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Dec 20, 2022
1 parent 4f405ac commit 0706081
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/fix-tags-pkg.md
Original file line number Diff line number Diff line change
@@ -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
21 changes: 13 additions & 8 deletions pkg/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,5 +112,6 @@ func (t *Tags) addTags(s string) []string {
t.numtags++
}

t.t = append(added, t.t...)
return added
}
31 changes: 29 additions & 2 deletions pkg/tags/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -171,11 +171,38 @@ 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)
require.Equal(t, tc.ExpectedTags, ts.AsList(), tc.Alias)
}

}

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())
}
}

0 comments on commit 0706081

Please sign in to comment.