-
Notifications
You must be signed in to change notification settings - Fork 16
/
binding.go
77 lines (63 loc) · 1.94 KB
/
binding.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
// Package binding provide some common binder for binding http.Request data to strcut
package binding
import (
"errors"
"net/http"
"strings"
)
// DefaultMaxMemory for parse data form
var DefaultMaxMemory int64 = 32 << 20 // 32 MB
// MustBind auto bind request data to an struct ptr
func MustBind(r *http.Request, obj any) {
err := Auto(r, obj)
if err != nil {
panic(err)
}
}
// Bind auto bind request data to an struct ptr
func Bind(r *http.Request, obj any) error {
return Auto(r, obj)
}
// Auto bind request data to a ptr value
//
// body, err := ioutil.ReadAll(c.Request().Body)
// if err != nil {
// c.Logger().Errorf("could not read request body: %v", err)
// }
// c.Set("request_body", body)
// // fix: can not read request body multiple times
// c.Request().Body = ioutil.NopCloser(bytes.NewReader(body))
func Auto(r *http.Request, obj any) (err error) {
method := r.Method
// no body, query data binding. like GET DELETE OPTION ....
if method != "POST" && method != "PUT" && method != "PATCH" {
return Query.BindValues(r.URL.Query(), obj)
}
// binding body data by content type.
cType := r.Header.Get("Content-Type")
// basic POST form data binding. content type: "application/x-www-form-urlencoded"
if strings.Contains(cType, "/x-www-form-urlencoded") {
if err = r.ParseForm(); err != nil {
return err
}
return Form.BindValues(r.PostForm, obj)
}
// contains file uploaded form: "multipart/form-data" "multipart/mixed"
// strings.HasPrefix(mediaType, "multipart/")
if strings.Contains(cType, "/form-data") {
err = r.ParseMultipartForm(DefaultMaxMemory)
if err != nil {
return err
}
return Form.BindValues(r.PostForm, obj)
}
// JSON body request: "application/json"
if strings.Contains(cType, "/json") {
return JSON.Bind(r, obj)
}
// XML body request: "text/xml"
if strings.Contains(cType, "/xml") {
return XML.Bind(r, obj)
}
return errors.New("cannot auto binding request data, content-type: " + cType)
}