-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdecoder.go
294 lines (262 loc) · 7.36 KB
/
decoder.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package lmdrouter
// nolint: unused
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/aws/aws-lambda-go/events"
)
var boolRegex = regexp.MustCompile(`^1|true|on|enabled$`)
// UnmarshalRequest "fills" out a target Go struct with data from the request.
// If body is true, then the request body is assumed to be JSON and simply
// unmarshaled into the target (taking into account that the request body may
// be base-64 encoded). After that, or if body is false, the function will
// traverse the exported fields of the target struct, and fill those that
// include the "lambda" struct tag with values taken from the request's query
// string parameters, path parameters and headers, according to the field's
// struct tag definition. This means a struct value can be filled with data from
// the body, the path, the query string and the headers at the same time.
//
// Field types are currently limited to string, all integer types, all unsigned
// integer types, all float types, booleans, slices of the aforementioned types
// and pointers of these types.
//
// Note that custom types that alias any of the aforementioned types are also
// accepted and the appropriate constant values will be generated. Boolean
// fields accept (in a case-insensitive way) the values "1", "true", "on" and
// "enabled". Any other value is considered false.
//
// Example struct (no body):
//
// type ListPostsInput struct {
// ID uint64 `lambda:"path.id"`
// Page uint64 `lambda:"query.page"`
// PageSize uint64 `lambda:"query.page_size"`
// Search string `lambda:"query.search"`
// ShowDrafts bool `lambda:"query.show_hidden"`
// Languages []string `lambda:"header.Accept-Language"`
// }
//
// Example struct (JSON body):
//
// type UpdatePostInput struct {
// ID uint64 `lambda:"path.id"`
// Author string `lambda:"header.Author"`
// Title string `json:"title"`
// Content string `json:"content"`
// }
//
func UnmarshalRequest(
req events.APIGatewayProxyRequest,
body bool,
target interface{},
) error {
if body {
err := unmarshalBody(req, target)
if err != nil {
return err
}
}
return unmarshalEvent(req, target)
}
func unmarshalEvent(req events.APIGatewayProxyRequest, target interface{}) error {
rv := reflect.ValueOf(target)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("invalid unmarshal target, must be pointer to struct")
}
v := rv.Elem()
t := v.Type()
for i := 0; i < t.NumField(); i++ {
typeField := t.Field(i)
valueField := v.Field(i)
lambdaTag := typeField.Tag.Get("lambda")
if lambdaTag == "" {
continue
}
components := strings.Split(lambdaTag, ".")
if len(components) != 2 {
return fmt.Errorf("invalid lambda tag for field %s", typeField.Name)
}
var sourceMap map[string]string
var multiMap map[string][]string
switch components[0] {
case "query":
sourceMap = req.QueryStringParameters
multiMap = req.MultiValueQueryStringParameters
case "path":
sourceMap = req.PathParameters
case "header":
sourceMap = req.Headers
multiMap = req.MultiValueHeaders
default:
return fmt.Errorf(
"invalid param location %q for field %s",
components[0], typeField.Name,
)
}
err := unmarshalField(
typeField.Type,
valueField,
sourceMap,
multiMap,
components[1],
)
if err != nil {
return err
}
}
return nil
}
func unmarshalBody(req events.APIGatewayProxyRequest, target interface{}) (
err error,
) {
if req.IsBase64Encoded {
var body []byte
body, err = base64.StdEncoding.DecodeString(req.Body)
if err != nil {
return fmt.Errorf("failed decoding body: %w", err)
}
err = json.Unmarshal(body, target)
} else {
err = json.Unmarshal([]byte(req.Body), target)
}
if err != nil {
return HTTPError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("invalid request body: %s", err),
}
}
return nil
}
func unmarshalField(
typeField reflect.Type,
valueField reflect.Value,
params map[string]string,
multiParam map[string][]string,
param string,
) error {
switch typeField.Kind() {
case reflect.String:
valueField.SetString(params[param])
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
str, ok := params[param]
value, err := parseInt64Param(param, str, ok)
if err != nil {
return err
}
valueField.SetInt(value)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
str, ok := params[param]
value, err := parseUint64Param(param, str, ok)
if err != nil {
return err
}
valueField.SetUint(value)
case reflect.Float32, reflect.Float64:
str, ok := params[param]
value, err := parseFloat64Param(param, str, ok)
if err != nil {
return err
}
valueField.SetFloat(value)
case reflect.Bool:
valueField.SetBool(boolRegex.MatchString(strings.ToLower(params[param])))
case reflect.Ptr:
if val, ok := params[param]; ok {
switch typeField.Elem().Kind() {
case reflect.Int, reflect.Int32, reflect.Int64, reflect.String, reflect.Float32, reflect.Float64:
valueField.Set(reflect.ValueOf(&val).Convert(typeField))
case reflect.Struct:
if typeField.Elem() == reflect.TypeOf(time.Now()) {
parsedTime, err := time.Parse(time.RFC3339, val)
if err != nil {
return err
}
valueField.Set(reflect.ValueOf(&parsedTime))
}
case reflect.Bool:
b := boolRegex.MatchString(strings.ToLower(val))
valueField.Set(reflect.ValueOf(&b))
}
}
case reflect.Slice:
// we'll be extracting values from multiParam, generating a slice and
// putting it in valueField
strs, ok := multiParam[param]
if ok {
slice := reflect.MakeSlice(typeField, len(strs), len(strs))
for i, str := range strs {
err := unmarshalField(
typeField.Elem(),
slice.Index(i),
map[string]string{"param": str},
nil,
"param",
)
if err != nil {
return err
}
}
valueField.Set(slice)
} else {
str, ok := params[param]
if ok {
stringParts := strings.Split(str, ",")
slice := reflect.MakeSlice(typeField, len(stringParts), len(stringParts))
for i, p := range stringParts {
inVal := reflect.ValueOf(p)
asVal := inVal.Convert(typeField.Elem())
slice.Index(i).Set(asVal)
}
valueField.Set(slice)
}
}
}
return nil
}
func parseInt64Param(param, str string, ok bool) (value int64, err error) {
if !ok {
return value, nil
}
value, err = strconv.ParseInt(str, 10, 64)
if err != nil {
return value, HTTPError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("%s must be a valid integer", param),
}
}
return value, nil
}
func parseUint64Param(param, str string, ok bool) (value uint64, err error) {
if !ok {
return value, nil
}
value, err = strconv.ParseUint(str, 10, 64)
if err != nil {
return value, HTTPError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("%s must be a valid, positive integer", param),
}
}
return value, nil
}
func parseFloat64Param(param, str string, ok bool) (value float64, err error) {
if !ok {
return value, nil
}
value, err = strconv.ParseFloat(str, 64)
if err != nil {
return value, HTTPError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("%s must be a valid floating point number", param),
}
}
return value, nil
}