-
Notifications
You must be signed in to change notification settings - Fork 52
/
value.go
137 lines (131 loc) · 3.27 KB
/
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
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
package znet
import (
"errors"
"reflect"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/zutil"
"github.com/sohaha/zlsgo/zvalid"
)
// 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) {
return c.bind(obj, func(kind reflect.Kind, field reflect.Value, fieldName, fieldTag string,
value interface{}) error {
return zutil.SetValue(kind, field, value)
})
}
func (c *Context) BindValid(obj interface{}, elements map[string]zvalid.
Engine) (
err error) {
err = c.bind(obj, func(kind reflect.Kind, field reflect.Value,
fieldName, fieldTag string,
value interface{}) error {
validRule, ok := elements[fieldTag]
if ok {
delete(elements, fieldTag)
var validValue string
switch v := value.(type) {
case string:
validValue = v
}
value, err = validRule.Verifi(validValue).String()
if err != nil {
return err
}
}
return zutil.SetValue(kind, field, value)
})
if err == nil {
for _, v := range elements {
err = v.Verifi("").Error()
if err != nil {
return err
}
}
}
return err
}
func (c *Context) bind(obj interface{}, set func(kind reflect.Kind,
field reflect.Value, fieldName, fieldTag string, value interface{}) error) (err error) {
v := reflect.ValueOf(obj)
if v.Kind() != reflect.Ptr {
err = errors.New("assignment requires the use of pointers")
return
}
vv := v.Elem()
if vv.Kind() == reflect.Ptr {
err = errors.New("binding pointer values are not supported")
return
}
tag := c.Engine.BindTag
isJSON := c.ContentType() == c.ContentType(ContentTypeJSON)
jsonSet := func(fieldTag string, field reflect.Value) (ok bool, err error) {
jsonValue := c.GetJSON(fieldTag)
if jsonValue.Exists() {
y := reflect.New(field.Type()).Interface()
err = jsonValue.Unmarshal(y)
zutil.Try(func() {
field.Set(reflect.ValueOf(y).Elem())
}, func(e interface{}) {
err, _ = e.(error)
})
return true, err
}
return false, nil
}
err = zutil.ReflectForNumField(vv, func(fieldName, fieldTag string, kind reflect.Kind,
field reflect.Value) error {
var (
value interface{}
ok bool
err error
)
// If you close the tag, the parameters will be transferred to SnakeCase by default
if tag == "" {
fieldTag = zstring.CamelCaseToSnakeCase(fieldTag)
}
if kind == reflect.Slice {
ok, err = jsonSet(fieldTag, field)
if ok {
return err
}
value, ok = c.GetPostFormArray(fieldTag)
if !ok {
value, ok = c.GetQueryArray(fieldTag)
}
} else if kind == reflect.Struct {
ok, err = jsonSet(fieldTag, field)
if ok {
return err
}
value, ok = c.GetPostFormMap(fieldTag)
} else {
if isJSON {
jsonValue := c.GetJSON(fieldTag)
ok = jsonValue.Exists()
if ok {
value = jsonValue.String()
}
}
if !ok && (c.Request.Method != "GET") {
value, ok = c.GetPostForm(fieldTag)
}
if !ok {
value, ok = c.GetQuery(fieldTag)
}
}
if ok {
err = set(kind, field, fieldName, fieldTag, value)
}
// if err != nil {
// err = fmt.Errorf("key: %s, %v", fieldTag, err)
// }
return err
}, tag)
return
}