-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
94 lines (87 loc) · 2.49 KB
/
decode.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
package ferry
import (
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"strconv"
)
// decodeJSON decodes *http.Request into target struct.
// Request must have "Content-Type" header set to "application/json".
func decodeJSON[T any](r *http.Request, v *T) error {
if r.Header.Get("Content-Type") != "application/json" {
return ClientError{
Code: http.StatusUnsupportedMediaType,
Message: "application/json content-type expected",
}
}
if err := json.NewDecoder(io.LimitReader(r.Body, 1024*1024)).Decode(v); err != nil {
if err == io.EOF {
return ClientError{
Code: http.StatusBadRequest,
Message: "empty request body",
}
}
return fmt.Errorf("decode request body: %w", err)
}
return r.Body.Close()
}
// decodeQuery decodes query values from http.Request into target struct.
// This function maps r.URL.Query values to struct properties by `query` tag.
func decodeQuery[T any](r *http.Request, v *T) error {
targetType := reflect.TypeOf(v)
targetValue := reflect.ValueOf(v)
for i := 0; i < targetType.Elem().NumField(); i++ {
field := targetType.Elem().Field(i)
key := field.Tag.Get("query")
kind := field.Type.Kind()
// Get the value from query params with given key
val := r.URL.Query().Get(key)
// Get reference of field value provided to input `d`
result := targetValue.Elem().Field(i)
switch kind {
case reflect.String:
result.SetString(val)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
num, err := strconv.Atoi(val)
if err != nil {
return ClientError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("can not convert %q to int", val),
}
}
result.SetInt(int64(num))
case reflect.Float32:
num, err := strconv.ParseFloat(val, 32)
if err != nil {
return ClientError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("can not convert %q to float32", val),
}
}
result.SetFloat(num)
case reflect.Float64:
num, err := strconv.ParseFloat(val, 64)
if err != nil {
return ClientError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("can not convert %q to float64", val),
}
}
result.SetFloat(num)
case reflect.Bool:
boolean, err := strconv.ParseBool(val)
if err != nil {
return ClientError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("can not convert %q to bool", val),
}
}
result.SetBool(boolean)
default:
return fmt.Errorf("unsupported kind %q in request query", kind)
}
}
return nil
}