-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.go
64 lines (54 loc) · 1.23 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
package textra
import (
"strings"
)
// Tags is a slice of tags.
type Tags []Tag
// ByName returns a tag by its name, if it exists.
func (t Tags) ByName(name string) (Tag, bool) {
for _, tag := range t {
if tag.Tag == name {
return tag, true
}
}
return Tag{}, false
}
func (t Tags) String() string {
tags := make([]string, 0, len(t))
for _, tag := range t {
tags = append(tags, tag.String())
}
return "[" + strings.Join(tags, " ") + "]"
}
// Tag represents a single struct tag, like
//
// `json:"value"`.
type Tag struct {
// Tag holds tag's name.
Tag string `json:"tag"`
Value string `json:"value"`
// Optional holds the rest of the value which comes after the comma.
// Example: In a tag like
// `json:"id,pk,omitempty"`
// Optional will contain
// ["pk", "omitempty"]
Optional []string `json:"optional,omitempty"`
}
// OmitEmpty returns true if t.Optional contains "omitempty".
func (t Tag) OmitEmpty() bool {
m := toUniqueMap(t.Optional...)
_, ok := m["omitempty"]
return ok
}
// Ignored is a shortcut for t.Value == "-".
func (t Tag) Ignored() bool {
return t.Value == "-"
}
func (t Tag) String() string {
s := t.Tag + `:"` + t.Value
for _, v := range t.Optional {
s += "," + v
}
s += `"`
return s
}