-
Notifications
You must be signed in to change notification settings - Fork 129
/
binding.go
executable file
·110 lines (101 loc) · 2.13 KB
/
binding.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
package antd_apis
import (
"fmt"
"github.com/gin-gonic/gin/binding"
"reflect"
"strings"
"sync"
)
const (
_ uint8 = iota
json
xml
yaml
form
query
)
var constructor = &bindConstructor{}
type bindConstructor struct {
cache map[string][]uint8
mux sync.Mutex
}
func (e *bindConstructor) GetBindingForGin(d interface{}) []binding.Binding {
bs := e.getBinding(reflect.TypeOf(d).String())
if bs == nil {
//重新构建
bs = e.resolve(d)
}
gbs := make([]binding.Binding, 0)
mp := make(map[uint8]binding.Binding, 0)
for _, b := range bs {
switch b {
case json:
mp[json] = binding.JSON
case xml:
mp[xml] = binding.XML
case yaml:
mp[yaml] = binding.YAML
case form:
mp[form] = binding.Form
case query:
mp[query] = binding.Query
default:
mp[0] = nil
}
}
for e := range mp {
gbs=append(gbs, mp[e])
}
return gbs
}
func (e *bindConstructor) resolve(d interface{}) []uint8 {
bs := make([]uint8, 0)
qType := reflect.TypeOf(d).Elem()
var tag reflect.StructTag
var ok bool
fmt.Println(qType.Kind())
for i := 0; i < qType.NumField(); i++ {
tag = qType.Field(i).Tag
if _, ok = tag.Lookup("json"); ok {
bs = append(bs, json)
}
if _, ok = tag.Lookup("xml"); ok {
bs = append(bs, xml)
}
if _, ok = tag.Lookup("yaml"); ok {
bs = append(bs, yaml)
}
if _, ok = tag.Lookup("form"); ok {
bs = append(bs, form)
}
if _, ok = tag.Lookup("query"); ok {
bs = append(bs, query)
}
if _, ok = tag.Lookup("uri"); ok {
bs = append(bs, 0)
}
if t, ok := tag.Lookup("binding"); ok && strings.Index(t, "dive") > -1 {
qValue := reflect.ValueOf(d)
bs = append(bs, e.resolve(qValue.Field(i))...)
continue
}
if t, ok := tag.Lookup("validate"); ok && strings.Index(t, "dive") > -1 {
qValue := reflect.ValueOf(d)
bs = append(bs, e.resolve(qValue.Field(i))...)
}
}
return bs
}
func (e *bindConstructor) getBinding(name string) []uint8 {
e.mux.Lock()
defer e.mux.Unlock()
return e.cache[name]
}
func (e *bindConstructor) setBinding(name string, bs []uint8) {
e.mux.Lock()
defer e.mux.Unlock()
if e.cache == nil {
e.cache = make(map[string][]uint8)
}
e.cache[name] = bs
}