-
-
Notifications
You must be signed in to change notification settings - Fork 397
/
tag.go
143 lines (121 loc) · 2.03 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
136
137
138
139
140
141
142
143
package orm
import (
"github.com/go-pg/pg/internal/parser"
)
type tag struct {
Name string
Options map[string]string
}
func parseTag(s string) *tag {
p := &tagParser{
Parser: parser.NewString(s),
}
p.parseKey()
return &p.tag
}
type tagParser struct {
*parser.Parser
tag tag
hasName bool
key string
}
func (p *tagParser) setName(name string) {
if p.hasName || p.tag.Options != nil {
p.setTagOption(name, "")
return
}
p.hasName = true
p.tag.Name = name
}
func (p *tagParser) setTagOption(key, value string) {
if p.tag.Options == nil {
p.tag.Options = make(map[string]string)
}
p.tag.Options[key] = value
}
func (p *tagParser) parseKey() {
p.key = ""
var b []byte
for p.Valid() {
c := p.Read()
switch c {
case ',':
p.Skip(' ')
p.setName(string(b))
p.parseKey()
return
case ':':
p.key = string(b)
p.parseValue()
return
default:
b = append(b, c)
}
}
if len(b) > 0 {
p.setName(string(b))
}
}
func (p *tagParser) parseValue() {
const quote = '\''
c := p.Peek()
if c == quote {
p.parseQuotedValue()
return
}
var b []byte
for p.Valid() {
c = p.Read()
switch c {
case '\\':
c = p.Read()
b = append(b, c)
case ',':
p.Skip(' ')
p.setTagOption(p.key, string(b))
p.parseKey()
return
default:
b = append(b, c)
}
}
p.setTagOption(p.key, string(b))
}
func (p *tagParser) parseQuotedValue() {
const quote = '\''
if !p.Skip(quote) {
panic("not reached")
}
var b []byte
b = append(b, quote)
for p.Valid() {
bb, ok := p.ReadSep(quote)
if !ok {
b = append(b, bb...)
break
}
if len(bb) > 0 && bb[len(bb)-1] == '\\' {
b = append(b, bb[:len(bb)-1]...)
b = append(b, quote)
continue
}
b = append(b, bb...)
b = append(b, quote)
break
}
p.setTagOption(p.key, string(b))
if p.Skip(',') {
p.Skip(' ')
}
p.parseKey()
}
func unquote(s string) (string, bool) {
const quote = '\''
if len(s) < 2 {
return s, false
}
if s[0] == quote && s[len(s)-1] == quote {
return s[1 : len(s)-1], true
}
return s, false
}