-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagfast.go
178 lines (161 loc) · 3.58 KB
/
tagfast.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package tagfast
import (
"reflect"
"strconv"
"sync"
)
//[struct_name][field_name]
var CachedStructTags map[string]map[string]*TagFast = make(map[string]map[string]*TagFast)
var lock *sync.RWMutex = new(sync.RWMutex)
func CacheTag(struct_name string, field_name string, value *TagFast) {
lock.Lock()
defer lock.Unlock()
if _, ok := CachedStructTags[struct_name]; !ok {
CachedStructTags[struct_name] = make(map[string]*TagFast)
}
CachedStructTags[struct_name][field_name] = value
}
func GetTag(struct_name string, field_name string) (r *TagFast, ok bool) {
lock.RLock()
defer lock.RUnlock()
var v map[string]*TagFast
v, ok = CachedStructTags[struct_name]
if !ok {
return
}
r, ok = v[field_name]
return
}
//usage: Tag1(t, i, "form")
func Tag1(t reflect.Type, field_no int, key string) (tag string) {
f := t.Field(field_no)
tag = Tag(t, f, key)
return
}
//usage: Tag2(t, "Id", "form")
func Tag2(t reflect.Type, field_name string, key string) (tag string) {
f, ok := t.FieldByName(field_name)
if !ok {
return ""
}
tag = Tag(t, f, key)
return
}
func Tag(t reflect.Type, f reflect.StructField, key string) (tag string) {
if f.Tag == "" {
return ""
}
if v, ok := GetTag(t.String(), f.Name); ok {
tag = v.Get(key)
} else {
v := TagFast{Tag: f.Tag}
tag = v.Get(key)
CacheTag(t.String(), f.Name, &v)
}
return
}
func Tago(t reflect.Type, f reflect.StructField, key string) (tag string, tf *TagFast) {
if f.Tag == "" {
return "", nil
}
if v, ok := GetTag(t.String(), f.Name); ok {
tag = v.Get(key)
tf = v
} else {
tf = &TagFast{Tag: f.Tag}
tag = tf.Get(key)
CacheTag(t.String(), f.Name, tf)
}
return
}
func ClearTag() {
CachedStructTags = make(map[string]map[string]*TagFast)
}
func ParseStructTag(tag string) map[string]string {
lock.Lock()
defer lock.Unlock()
var tagsArray map[string]string = make(map[string]string)
for tag != "" {
// skip leading space
i := 0
for i < len(tag) && tag[i] == ' ' {
i++
}
tag = tag[i:]
if tag == "" {
break
}
// scan to colon.
// a space or a quote is a syntax error
i = 0
for i < len(tag) && tag[i] != ' ' && tag[i] != ':' && tag[i] != '"' {
i++
}
if i+1 >= len(tag) || tag[i] != ':' || tag[i+1] != '"' {
break
}
name := string(tag[:i])
tag = tag[i+1:]
// scan quoted string to find value
i = 1
for i < len(tag) && tag[i] != '"' {
if tag[i] == '\\' {
i++
}
i++
}
if i >= len(tag) {
break
}
qvalue := string(tag[:i+1])
tag = tag[i+1:]
value, _ := strconv.Unquote(qvalue)
tagsArray[name] = value
}
return tagsArray
}
type TagFast struct {
Tag reflect.StructTag
Cached map[string]string
Parsed map[string]interface{}
}
func (a *TagFast) Get(key string) string {
if a.Cached == nil {
a.Cached = ParseStructTag(string(a.Tag))
}
lock.RLock()
defer lock.RUnlock()
if v, ok := a.Cached[key]; ok {
return v
}
return ""
}
func (a *TagFast) GetParsed(key string, fns ...func() interface{}) interface{} {
if a.Parsed == nil {
a.Parsed = make(map[string]interface{})
}
lock.RLock()
if v, ok := a.Parsed[key]; ok {
lock.RUnlock()
return v
}
lock.RUnlock()
if len(fns) > 0 {
fn := fns[0]
if fn != nil {
v := fn()
a.SetParsed(key, v)
return v
}
}
return nil
}
func (a *TagFast) SetParsed(key string, value interface{}) bool {
if a.Parsed == nil {
a.Parsed = make(map[string]interface{})
}
lock.Lock()
defer lock.Unlock()
a.Parsed[key] = value
return true
}