-
Notifications
You must be signed in to change notification settings - Fork 52
/
value.go
43 lines (39 loc) · 980 Bytes
/
value.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
package znet
import (
"errors"
"reflect"
"github.com/sohaha/zlsgo/zutil"
)
// Content-Type MIME of the most common data formats
const (
MIMEJSON = "application/json"
MIMEPlain = "text/plain"
MIMEPOSTForm = "application/x-www-form-urlencoded"
MIMEMultipartPOSTForm = "multipart/form-data"
)
func (c *Context) Bind(obj interface{}) (err error) {
v := reflect.ValueOf(obj)
if v.Kind() != reflect.Ptr {
err = errors.New("assignment requires the use of pointers")
return
}
vv := v.Elem()
zutil.ReflectForNumField(vv, func(fieldTag string, kind reflect.Kind, field reflect.Value) bool {
var (
value interface{}
ok bool
)
if kind == reflect.Slice {
value, ok = c.GetPostFormArray(fieldTag)
} else if kind == reflect.Struct {
value, ok = c.GetPostFormMap(fieldTag)
} else {
value, ok = c.GetPostForm(fieldTag)
}
if ok {
err = zutil.SetValue(kind, field, value)
}
return err == nil
})
return
}