-
-
Notifications
You must be signed in to change notification settings - Fork 397
/
tag.go
122 lines (106 loc) · 1.75 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
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
key string
}
func (p *tagParser) setTagOption(key, value string) {
if p.tag.Options == nil {
p.tag.Options = make(map[string]string)
if value == "" && p.tag.Name == "" {
p.tag.Name = key
return
}
}
p.tag.Options[key] = value
}
func (p *tagParser) parseKey() {
var b []byte
for p.Valid() {
c := p.Read()
switch c {
case ',':
p.Skip(' ')
p.setTagOption(string(b), "")
p.parseKey()
return
case ':':
p.key = string(b)
p.parseValue()
return
default:
b = append(b, c)
}
}
if len(b) > 0 {
p.setTagOption(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)
}
}
if len(b) > 0 {
p.setTagOption(p.key, string(b))
}
}
func (p *tagParser) parseQuotedValue() {
const quote = '\''
if !p.Skip(quote) {
panic("not reached")
}
var b []byte
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...)
p.Skip(quote)
p.setTagOption(p.key, string(b))
p.parseKey()
return
}
if len(b) > 0 {
p.setTagOption(p.key, string(b))
}
}