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 19, 2022
1 parent 4f405ac commit 5eafdf2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 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
20 changes: 13 additions & 7 deletions pkg/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +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
return len(t.addTags(s)) > 0
}

// RemoveList removes a list of tags and returns true if at least one was removed
Expand Down Expand Up @@ -108,5 +113,6 @@ func (t *Tags) addTags(s string) []string {
t.numtags++
}

t.t = append(added, t.t...)
return added
}
26 changes: 26 additions & 0 deletions pkg/tags/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 5eafdf2

Please sign in to comment.