mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 06:46:34 +00:00
feat(tags): Create Flatten() method
This commit is contained in:
parent
9c41e4a343
commit
334d8ce724
@ -40,6 +40,20 @@ func (l TagLists) String() string {
|
||||
return fmt.Sprintf("%v", []TagList(l))
|
||||
}
|
||||
|
||||
// Flatten returns the list of all tags provided in TagLists
|
||||
func (l TagLists) Flatten() (tags TagList) {
|
||||
tags = make([]string, 0)
|
||||
for _, list := range l {
|
||||
for _, tag := range list {
|
||||
if tag != "" {
|
||||
tags = append(tags, tag)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tags
|
||||
}
|
||||
|
||||
// Set updates the TagList's value.
|
||||
func (l *TagLists) Set(s string) error {
|
||||
*l = append(*l, splitTagList(s))
|
||||
|
45
internal/restic/tag_list_test.go
Normal file
45
internal/restic/tag_list_test.go
Normal file
@ -0,0 +1,45 @@
|
||||
package restic
|
||||
|
||||
import (
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTagLists_Flatten(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
l TagLists
|
||||
want TagList
|
||||
}{
|
||||
{
|
||||
name: "4 tags",
|
||||
l: TagLists{
|
||||
TagList{
|
||||
"tag1",
|
||||
"tag2",
|
||||
},
|
||||
TagList{
|
||||
"tag3",
|
||||
"tag4",
|
||||
},
|
||||
},
|
||||
want: TagList{"tag1", "tag2", "tag3", "tag4"},
|
||||
},
|
||||
{
|
||||
name: "No tags",
|
||||
l: nil,
|
||||
want: TagList{},
|
||||
},
|
||||
{
|
||||
name: "Empty tags",
|
||||
l: TagLists{[]string{""}},
|
||||
want: TagList{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.l.Flatten()
|
||||
rtest.Equals(t, got, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user