-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
162 lines (143 loc) · 3.36 KB
/
request.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
package request
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"strings"
"github.com/gorilla/mux"
)
// Decode an HTTP request into the provided struct
func Decode(r *http.Request, data interface{}) error {
typ := reflect.TypeOf(data)
if typ == nil {
return fmt.Errorf("invalid decode type: nil")
}
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return fmt.Errorf("invalid decode type: %v", typ.Kind())
}
return decodeRequest(r, typ, data)
}
func decodeRequest(r *http.Request, t reflect.Type, data interface{}) error {
body, err := decodeStruct(r, t, data)
if err != nil {
return err
}
if !body {
err := decodeBody(r, data)
if err != nil {
return err
}
}
return nil
}
func decodeStruct(r *http.Request, t reflect.Type, data interface{}) (bool, error) {
query := r.URL.Query()
vars := mux.Vars(r)
body := false
for i := 0; i < t.NumField(); i++ {
typ := t.Field(i)
field := reflect.ValueOf(data).Elem().Field(i)
if typ.Type.Kind() == reflect.Struct {
var err error
if body, err = decodeStruct(r, typ.Type, field.Addr().Interface()); err != nil {
return body, err
}
}
if queryTag := typ.Tag.Get("query"); queryTag != "" {
if err := decodeQuery(field, typ.Type, query, queryTag); err != nil {
return body, err
}
}
if pathTag := typ.Tag.Get("path"); pathTag != "" {
if err := decodePath(field, typ.Type, vars, pathTag); err != nil {
return body, err
}
}
if headerTag := typ.Tag.Get("header"); headerTag != "" {
if err := decodeHeader(field, typ.Type, r.Header, headerTag); err != nil {
return body, err
}
}
bodyTag := typ.Tag.Get("body")
if bodyTag != "" {
body = true
if err := decodeBody(r, field.Addr().Interface()); err != nil {
return body, err
}
}
}
return body, nil
}
func decodeQuery(field reflect.Value, typ reflect.Type, query url.Values, tag string) error {
parts := strings.Split(tag, ",")
if query.Has(parts[0]) {
if field.Kind() == reflect.Slice {
var explode bool
for _, p := range parts[1:] {
if p == "explode" {
explode = true
}
}
var value []string
if explode {
value = query[parts[0]]
} else {
value = strings.Split(query.Get(parts[0]), ",")
}
if err := resolveValues(field, typ, value); err != nil {
return err
}
return nil
}
if err := resolveValue(field, typ, query.Get(parts[0])); err != nil {
return err
}
}
return nil
}
func decodePath(field reflect.Value, typ reflect.Type, vars map[string]string, tag string) error {
if path, ok := vars[tag]; ok {
if err := resolveValue(field, typ, path); err != nil {
return err
}
}
return nil
}
func decodeHeader(field reflect.Value, typ reflect.Type, header http.Header, tag string) error {
if field.Kind() == reflect.Slice {
if err := resolveValues(field, typ, header.Values(tag)); err != nil {
return err
}
return nil
}
if header.Get(tag) != "" {
if err := resolveValue(field, typ, header.Get(tag)); err != nil {
return err
}
}
return nil
}
func decodeBody(r *http.Request, data interface{}) error {
b, err := io.ReadAll(r.Body)
if err != nil {
return err
}
defer r.Body.Close()
if len(b) == 0 {
return nil
}
switch r.Header.Get("Content-Type") {
case "application/json":
err := json.Unmarshal(b, &data)
if err != nil {
return err
}
}
return nil
}