-
Notifications
You must be signed in to change notification settings - Fork 28
/
tag.go
135 lines (119 loc) · 3.02 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package klog
import (
"errors"
"regexp"
"strings"
)
var HashTagPattern = regexp.MustCompile(`#([\p{L}\d_-]+)(=(("[^"]*")|('[^']*')|([\p{L}\d_-]*)))?`)
var unquotedValuePattern = regexp.MustCompile(`^[\p{L}\d_-]+$`)
type Tag struct {
name string
value string
}
func NewTagFromString(tag string) (Tag, error) {
if !strings.HasPrefix(tag, "#") {
tag = "#" + tag
}
match := HashTagPattern.FindStringSubmatch(tag)
if match == nil {
// The tag pattern didn’t match at all.
return Tag{}, errors.New("INVALID_TAG")
}
name := match[1]
value := func() string {
v := match[3]
if strings.HasPrefix(v, `"`) {
return strings.Trim(v, `"`)
}
if strings.HasPrefix(v, `'`) {
return strings.Trim(v, `'`)
}
return v
}()
if len(match[0]) != len(tag) {
// The original tag contains more/other characters.
return Tag{}, errors.New("INVALID_TAG")
}
return NewTagOrPanic(name, value), nil
}
// NewTagOrPanic constructs a new tag but will panic if the
// parameters don’t yield a valid tag.
func NewTagOrPanic(name string, value string) Tag {
if strings.Contains(value, "\"") && strings.Contains(value, "'") {
// A tag value can never contain both ' and " at the same time.
panic("Invalid tag")
}
return Tag{strings.ToLower(name), value}
}
func (t Tag) Name() string {
return t.name
}
func (t Tag) Value() string {
return t.value
}
func (t Tag) ToString() string {
result := "#" + t.name
if t.value != "" {
result += "="
quotation := ""
if !unquotedValuePattern.MatchString(t.value) {
if strings.Contains(t.value, `"`) {
quotation = `'`
} else {
quotation = "\""
}
}
result += quotation + t.value + quotation
}
return result
}
type TagSet struct {
lookup map[Tag]bool
original []Tag
}
func NewEmptyTagSet() TagSet {
return TagSet{
lookup: make(map[Tag]bool),
original: []Tag{},
}
}
// Put inserts the tag into the TagSet.
func (ts *TagSet) Put(tag Tag) {
ts.lookup[tag] = true
ts.lookup[NewTagOrPanic(tag.Name(), "")] = true
ts.original = append(ts.original, tag)
}
// Contains checks whether the TagSet contains the given tag.
// Note that if the TagSet contains a tag with value, then this
// will always yield a match against the base tag (without value).
func (ts *TagSet) Contains(tag Tag) bool {
return ts.lookup[tag]
}
// IsEmpty checks whether the TagSet contains something or not.
func (ts *TagSet) IsEmpty() bool {
return len(ts.lookup) == 0
}
// ForLookup returns a denormalised and unordered representation
// of the TagSet.
func (ts *TagSet) ForLookup() map[Tag]bool {
return ts.lookup
}
// ToStrings returns the tags as string, in their original order
// and without deduplication or normalisation.
func (ts *TagSet) ToStrings() []string {
tags := make([]string, len(ts.original))
for i, t := range ts.original {
tags[i] = t.ToString()
}
return tags
}
// Merge combines multiple tag sets into a new one.
func Merge(tagSets ...*TagSet) TagSet {
result := NewEmptyTagSet()
for _, ts := range tagSets {
for t := range ts.lookup {
result.Put(t)
}
}
return result
}