-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinding.go
204 lines (178 loc) · 4.63 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2017 King Qiu. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
// https://github.com/qjw/kelly
package kelly
import (
"github.com/qjw/kelly/binding"
"gopkg.in/go-playground/validator.v9"
"net/http"
"reflect"
)
type binder interface {
// 绑定一个对象,根据Content-type自动判断类型
Bind(interface{}) (error, []string)
// 绑定json,从body取数据
BindJson(interface{}) (error, []string)
// 绑定xml,从body取数据
BindXml(interface{}) (error, []string)
// 绑定form,从body/query取数据
BindForm(interface{}) (error, []string)
// 绑定path变量
BindPath(interface{}) (error, []string)
GetBindParameter() interface{}
GetBindJsonParameter() interface{}
GetBindXmlParameter() interface{}
GetBindFormParameter() interface{}
GetBindPathParameter() interface{}
}
type binderImp struct {
c *Context
}
func newBinder(c *Context) binder {
return &binderImp{
c: c,
}
}
func (b *binderImp) Bind(obj interface{}) (error, []string) {
bind := binding.Default(b.c.Request().Method, b.c.ContentType())
return bindWith(b.c, obj, bind)
}
func (b *binderImp) BindJson(obj interface{}) (error, []string) {
return bindWith(b.c, obj, binding.JSON)
}
func (b *binderImp) BindXml(obj interface{}) (error, []string) {
return bindWith(b.c, obj, binding.XML)
}
func (b *binderImp) BindForm(obj interface{}) (error, []string) {
return bindWith(b.c, obj, binding.Form)
}
func (b *binderImp) BindPath(obj interface{}) (error, []string) {
return bindWith(b.c, obj, &pathBinding{
r: b.c,
})
}
func parseError(err error, obj interface{}) (error, []string) {
if err == nil {
return nil, nil
} else {
tips := make([]string, 0)
real_err, ok := err.(validator.ValidationErrors)
if ok {
objt := reflect.TypeOf(obj).Elem()
if objt.Kind() != reflect.Struct {
return real_err, tips
}
for _, v := range real_err {
elem, ok := objt.FieldByName(v.StructField())
if !ok {
continue
}
str, ok := elem.Tag.Lookup("error")
if ok {
// log.Printf("tag : %s\n",str)
tips = append(tips, str)
}
}
return real_err, tips
} else {
return err, tips
}
}
}
// BindWith binds the passed struct pointer using the specified binding engine.
// See the binding package.
func bindWith(c *Context, obj interface{}, b binding.Binding) (error, []string) {
err := b.Bind(c.Request(), obj)
return parseError(err, obj)
}
func handleValidateErr(c *Context, err error, msgs []string, obj interface{}) {
c.WriteJson(http.StatusUnprocessableEntity, H{
"code": http.StatusUnprocessableEntity,
"error": err.Error(),
"msgs": msgs,
"obj": obj,
})
}
const (
contextBindKey = "_bind_key"
contextBindJsonKey = "_bind_json_key"
contextBindXmlKey = "_bind_xml_key"
contextBindFormKey = "_bind_form_key"
contextBindPathKey = "_bind_path_key"
)
func (b *binderImp) GetBindParameter() interface{} {
return b.c.MustGet(contextBindKey)
}
func (b *binderImp) GetBindJsonParameter() interface{} {
return b.c.MustGet(contextBindJsonKey)
}
func (b *binderImp) GetBindXmlParameter() interface{} {
return b.c.MustGet(contextBindXmlKey)
}
func (b *binderImp) GetBindFormParameter() interface{} {
return b.c.MustGet(contextBindFormKey)
}
func (b *binderImp) GetBindPathParameter() interface{} {
return b.c.MustGet(contextBindPathKey)
}
func BindMiddleware(objG func()interface{}) HandlerFunc {
return func(c *Context) {
obj := objG()
err, msgs := c.Bind(obj)
if err == nil {
c.Set(contextBindKey, obj)
c.InvokeNext()
} else {
handleValidateErr(c, err, msgs, obj)
}
}
}
func BindJsonMiddleware(objG func()interface{}) HandlerFunc {
return func(c *Context) {
obj := objG()
err, msgs := c.BindJson(obj)
if err == nil {
c.Set(contextBindJsonKey, obj)
c.InvokeNext()
} else {
handleValidateErr(c, err, msgs, obj)
}
}
}
func BindXmlMiddleware(objG func()interface{}) HandlerFunc {
return func(c *Context) {
obj := objG()
err, msgs := c.BindXml(obj)
if err == nil {
c.Set(contextBindXmlKey, obj)
c.InvokeNext()
} else {
handleValidateErr(c, err, msgs, obj)
}
}
}
func BindFormMiddleware(objG func()interface{}) HandlerFunc {
return func(c *Context) {
obj := objG()
err, msgs := c.BindForm(obj)
if err == nil {
c.Set(contextBindFormKey, obj)
c.InvokeNext()
} else {
handleValidateErr(c, err, msgs, obj)
}
}
}
func BindPathMiddleware(objG func()interface{}) HandlerFunc {
return func(c *Context) {
obj := objG()
err, msgs := c.BindPath(obj)
if err == nil {
c.Set(contextBindPathKey, obj)
c.InvokeNext()
} else {
handleValidateErr(c, err, msgs, obj)
}
}
}