This repository has been archived by the owner on Feb 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reading.go
201 lines (179 loc) · 4.99 KB
/
reading.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
package egret
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"io"
"io/ioutil"
"reflect"
"strconv"
)
// DataReader is used by Context.Read() to read data from an HTTP request.
type DataReader interface {
// Read reads from the given HTTP request and populate the specified data.
Read(*Request, interface{}) error
}
var (
// DataReaders lists all supported content types and the corresponding data readers.
// Context.Read() will choose a matching reader from this list according to the "Content-Type"
// header from the current request.
// You may modify this variable to add new supported content types.
DataReaders = map[string]DataReader{
"application/x-www-form-urlencoded": &FormDataReader{},
"multipart/form-data": &FormDataReader{},
"application/json": &JSONDataReader{},
"application/xml": &XMLDataReader{},
"text/xml": &XMLDataReader{},
}
// DefaultFormDataReader is the reader used when there is no matching reader in DataReaders
// or if the current request is a GET request.
DefaultFormDataReader DataReader = &FormDataReader{}
)
// JSONDataReader reads the request body as JSON-formatted data.
type JSONDataReader struct{}
func readRequestBody(req *Request) io.Reader {
body, _ := ioutil.ReadAll(req.Body)
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return bytes.NewBuffer(body)
}
func (r *JSONDataReader) Read(req *Request, data interface{}) error {
return json.NewDecoder(readRequestBody(req)).Decode(data)
}
// XMLDataReader reads the request body as XML-formatted data.
type XMLDataReader struct{}
func (r *XMLDataReader) Read(req *Request, data interface{}) error {
return xml.NewDecoder(readRequestBody(req)).Decode(data)
}
// FormDataReader reads the query parameters and request body as form data.
type FormDataReader struct{}
func (r *FormDataReader) Read(req *Request, data interface{}) error {
// Do not check return result. Otherwise GET request will cause problem.
req.ParseMultipartForm(32 << 20)
return ReadFormData(req.Form, data)
}
const formTag = "form"
// ReadFormData populates the data variable with the data from the given form values.
func ReadFormData(form map[string][]string, data interface{}) error {
rv := reflect.ValueOf(data)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("data must be a pointer")
}
rv = indirect(rv)
if rv.Kind() != reflect.Struct {
return errors.New("data must be a pointer to a struct")
}
return readForm(form, "", rv)
}
func readForm(form map[string][]string, prefix string, rv reflect.Value) error {
rv = indirect(rv)
rt := rv.Type()
n := rt.NumField()
for i := 0; i < n; i++ {
field := rt.Field(i)
tag := field.Tag.Get(formTag)
// only handle anonymous or exported fields
if !field.Anonymous && field.PkgPath != "" || tag == "-" {
continue
}
ft := field.Type
if ft.Kind() == reflect.Ptr {
ft = ft.Elem()
}
name := tag
if name == "" && !field.Anonymous {
name = field.Name
}
if name != "" && prefix != "" {
name = prefix + "." + name
}
if ft.Kind() != reflect.Struct {
if err := readFormField(form, name, rv.Field(i)); err != nil {
return err
}
continue
}
if name == "" {
name = prefix
}
if err := readForm(form, name, rv.Field(i)); err != nil {
return err
}
}
return nil
}
func readFormField(form map[string][]string, name string, rv reflect.Value) error {
value, ok := form[name]
if !ok {
return nil
}
rv = indirect(rv)
if rv.Kind() != reflect.Slice {
return setFormFieldValue(rv, value[0])
}
n := len(value)
slice := reflect.MakeSlice(rv.Type(), n, n)
for i := 0; i < n; i++ {
if err := setFormFieldValue(slice.Index(i), value[i]); err != nil {
return err
}
}
rv.Set(slice)
return nil
}
func setFormFieldValue(rv reflect.Value, value string) error {
switch rv.Kind() {
case reflect.Bool:
if value == "" {
value = "false"
}
v, err := strconv.ParseBool(value)
if err == nil {
rv.SetBool(v)
}
return err
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if value == "" {
value = "0"
}
v, err := strconv.ParseInt(value, 10, 64)
if err == nil {
rv.SetInt(v)
}
return err
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if value == "" {
value = "0"
}
v, err := strconv.ParseUint(value, 10, 64)
if err == nil {
rv.SetUint(v)
}
return err
case reflect.Float32, reflect.Float64:
if value == "" {
value = "0"
}
v, err := strconv.ParseFloat(value, 64)
if err == nil {
rv.SetFloat(v)
}
return err
case reflect.String:
rv.SetString(value)
return nil
default:
return errors.New("Unknown type: " + rv.Kind().String())
}
}
// indirect dereferences pointers and returns the actual value it points to.
// If a pointer is nil, it will be initialized with a new value.
func indirect(v reflect.Value) reflect.Value {
for v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v
}