Merge branch 'master' into develop

This commit is contained in:
Torkel Ödegaard
2017-10-09 16:01:54 +02:00
176 changed files with 5939 additions and 2716 deletions
+60
View File
@@ -0,0 +1,60 @@
package models
import (
"strings"
)
type Tag struct {
Id int64
Key string
Value string
}
func ParseTagPairs(tagPairs []string) (tags []*Tag) {
if tagPairs == nil {
return []*Tag{}
}
for _, tagPair := range tagPairs {
var tag Tag
if strings.Contains(tagPair, ":") {
keyValue := strings.Split(tagPair, ":")
tag.Key = strings.Trim(keyValue[0], " ")
tag.Value = strings.Trim(keyValue[1], " ")
} else {
tag.Key = strings.Trim(tagPair, " ")
}
if tag.Key == "" || ContainsTag(tags, &tag) {
continue
}
tags = append(tags, &tag)
}
return tags
}
func ContainsTag(existingTags []*Tag, tag *Tag) bool {
for _, t := range existingTags {
if t.Key == tag.Key && t.Value == tag.Value {
return true
}
}
return false
}
func JoinTagPairs(tags []*Tag) []string {
tagPairs := []string{}
for _, tag := range tags {
if tag.Value != "" {
tagPairs = append(tagPairs, tag.Key+":"+tag.Value)
} else {
tagPairs = append(tagPairs, tag.Key)
}
}
return tagPairs
}
+95
View File
@@ -0,0 +1,95 @@
package models
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestParsingTags(t *testing.T) {
Convey("Testing parsing a tag pairs into tags", t, func() {
Convey("Can parse one empty tag", func() {
tags := ParseTagPairs([]string{""})
So(len(tags), ShouldEqual, 0)
})
Convey("Can parse valid tags", func() {
tags := ParseTagPairs([]string{"outage", "type:outage", "error"})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type")
So(tags[1].Value, ShouldEqual, "outage")
So(tags[2].Key, ShouldEqual, "error")
So(tags[2].Value, ShouldEqual, "")
})
Convey("Can parse tags with spaces", func() {
tags := ParseTagPairs([]string{" outage ", " type : outage ", "error "})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type")
So(tags[1].Value, ShouldEqual, "outage")
So(tags[2].Key, ShouldEqual, "error")
So(tags[2].Value, ShouldEqual, "")
})
Convey("Can parse empty tags", func() {
tags := ParseTagPairs([]string{" outage ", "", "", ":", "type : outage ", "error ", "", ""})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type")
So(tags[1].Value, ShouldEqual, "outage")
So(tags[2].Key, ShouldEqual, "error")
So(tags[2].Value, ShouldEqual, "")
})
Convey("Can parse tags with extra colons", func() {
tags := ParseTagPairs([]string{" outage", "type : outage:outage2 :outage3 ", "error :"})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type")
So(tags[1].Value, ShouldEqual, "outage")
So(tags[2].Key, ShouldEqual, "error")
So(tags[2].Value, ShouldEqual, "")
})
Convey("Can parse tags that contains key and values with spaces", func() {
tags := ParseTagPairs([]string{" outage 1", "type 1: outage 1 ", "has error "})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "outage 1")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "type 1")
So(tags[1].Value, ShouldEqual, "outage 1")
So(tags[2].Key, ShouldEqual, "has error")
So(tags[2].Value, ShouldEqual, "")
})
Convey("Can filter out duplicate tags", func() {
tags := ParseTagPairs([]string{"test", "test", "key:val1", "key:val2"})
So(len(tags), ShouldEqual, 3)
So(tags[0].Key, ShouldEqual, "test")
So(tags[0].Value, ShouldEqual, "")
So(tags[1].Key, ShouldEqual, "key")
So(tags[1].Value, ShouldEqual, "val1")
So(tags[2].Key, ShouldEqual, "key")
So(tags[2].Value, ShouldEqual, "val2")
})
Convey("Can join tag pairs", func() {
tagPairs := []*Tag{
{Key: "key1", Value: "val1"},
{Key: "key2", Value: ""},
{Key: "key3"},
}
tags := JoinTagPairs(tagPairs)
So(len(tags), ShouldEqual, 3)
So(tags[0], ShouldEqual, "key1:val1")
So(tags[1], ShouldEqual, "key2")
So(tags[2], ShouldEqual, "key3")
})
})
}