-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag.go
113 lines (94 loc) · 1.97 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
package stroo
import (
"errors"
"fmt"
"strconv"
"strings"
)
type Tags []*Tag
type Tag struct {
Key string // i.e: `json:"fieldName,omitempty". Here key is: "json"
Name string // i.e: `json:"fieldName,omitempty". Here name is: "fieldName"
Options []string // `json:"fieldName,omitempty". Here options is: ["omitempty"]
}
var (
errTagSyntax = errors.New("bad syntax for struct tag pair")
errTagKeySyntax = errors.New("bad syntax for struct tag key")
errTagValueSyntax = errors.New("bad syntax for struct tag value")
errTagNotExist = errors.New("tag does not exist")
)
func ParseTags(tag string) (Tags, error) {
if tag == "" {
return nil, nil
}
if tag[0] == '`' && tag[len(tag)-1] == '`' {
tag = tag[1 : len(tag)-1]
}
var tags Tags
i := 0
for i < len(tag) && tag[i] == ' ' {
i++
}
tag = tag[i:]
if tag == "" {
return nil, nil
}
i = 0
for i < len(tag) && tag[i] > ' ' && tag[i] != ':' && tag[i] != '"' && tag[i] != 0x7f {
i++
}
if i == 0 {
return nil, errTagKeySyntax
}
if i+1 >= len(tag) || tag[i] != ':' {
return nil, errTagSyntax
}
if tag[i+1] != '"' {
return nil, errTagValueSyntax
}
key := string(tag[:i])
tag = tag[i+1:]
i = 1
for i < len(tag) && tag[i] != '"' {
if tag[i] == '\\' {
i++
}
i++
}
if i >= len(tag) {
return nil, errTagValueSyntax
}
qValue := string(tag[:i+1])
tag = tag[i+1:]
value, err := strconv.Unquote(qValue)
if err != nil {
return nil, errTagValueSyntax
}
res := strings.Split(value, ",")
name := res[0]
options := res[1:]
if len(options) == 0 {
options = nil
}
tags = append(tags, &Tag{
Key: key,
Name: name,
Options: options,
})
return tags, nil
}
func (t *Tag) Value() string {
options := strings.Join(t.Options, ",")
if options != "" {
return fmt.Sprintf(`%s,%s`, t.Name, options)
}
return t.Name
}
func (t Tags) Get(key string) (*Tag, error) {
for _, tag := range t {
if tag.Key == key {
return tag, nil
}
}
return nil, errTagNotExist
}