-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
166 lines (124 loc) · 3.23 KB
/
parse.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
package chirp
import (
"encoding"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strings"
"github.com/go-chi/chi"
"github.com/pkg/errors"
)
// Parse request body, chi path parts and query parameters into dst
func Parse(req *http.Request, dst interface{}) error {
value := reflect.ValueOf(dst)
if value.Kind() != reflect.Ptr {
return errors.New("dst should be an pointer")
}
if err := parse(req, value); err != nil {
return err
}
return nil
}
func parse(req *http.Request, value reflect.Value) *Error {
if err := parseQuery(req, value); err != nil {
err.part = "query"
return err
}
if err := parseBody(req, value); err != nil {
err.part = "body"
return err
}
if err := parseURLParams(req, value); err != nil {
err.part = "path"
return err
}
return nil
}
func parseQuery(req *http.Request, value reflect.Value) *Error {
return withStructTag(value, "query", false, func(tag string, field reflect.Value) *Error {
return unmarshalValue(req.URL.Query().Get(tag), field)
})
}
func parseBody(req *http.Request, value reflect.Value) *Error {
defer req.Body.Close()
switch req.Header.Get("Content-Type") {
case "application/x-www-form-urlencoded", "multipart/form-data":
return parseFormData(req, value)
default:
body, _ := ioutil.ReadAll(req.Body)
if err := json.Unmarshal(body, value.Interface()); err != nil {
return &Error{
source: string(body),
cause: errors.WithStack(err),
}
}
return nil
}
}
func parseFormData(req *http.Request, value reflect.Value) *Error {
return withStructTag(value, "json", true, func(tag string, field reflect.Value) *Error {
if tag == "-" {
return nil
}
return unmarshalValue(req.FormValue(tag), field)
})
}
func parseURLParams(req *http.Request, value reflect.Value) *Error {
return withStructTag(value, "path", false, func(tag string, field reflect.Value) *Error {
return unmarshalValue(chi.URLParam(req, tag), field)
})
}
func withStructTag(
value reflect.Value, tagName string, fieldNameOnEmpty bool,
cb func(tag string, field reflect.Value) *Error,
) *Error {
elem := value.Elem()
reflectType := elem.Type()
if reflectType.Kind() != reflect.Struct {
return nil
}
for i := 0; i < reflectType.NumField(); i++ {
fieldType := reflectType.Field(i)
tag, ok := tagOrFieldName(fieldType, tagName)
if !fieldNameOnEmpty && !ok {
continue
}
if err := cb(tag, elem.Field(i)); err != nil {
err.field = fieldType
return err
}
}
return nil
}
func unmarshalValue(source string, value reflect.Value) *Error {
if len(source) == 0 {
return nil
}
dst := reflect.New(value.Type()).Interface()
if unmarshaler, ok := dst.(encoding.TextUnmarshaler); ok {
if err := unmarshaler.UnmarshalText([]byte(source)); err != nil {
return &Error{
source: source,
cause: errors.WithStack(err),
}
}
} else {
if _, err := fmt.Sscan(source, dst); err != nil {
return &Error{
source: source,
cause: errors.WithStack(err),
}
}
}
value.Set(reflect.ValueOf(dst).Elem())
return nil
}
func tagOrFieldName(field reflect.StructField, tagName string) (string, bool) {
tag := strings.TrimSpace(strings.SplitN(field.Tag.Get(tagName), ",", 1)[0])
if len(tag) > 0 {
return tag, true
}
return field.Name, false
}