/
bind.go
72 lines (64 loc) · 1.76 KB
/
bind.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/gorilla/schema"
)
var (
decoder = newDecoder()
)
func newDecoder() *schema.Decoder {
d := schema.NewDecoder()
d.IgnoreUnknownKeys(true)
d.ZeroEmpty(true)
d.SetAliasTag("json")
d.RegisterConverter([]string{}, func(input string) reflect.Value {
return reflect.ValueOf(stringSeparator(input, ','))
})
return d
}
// bind bindding request parameter
func bind(out interface{}, r *http.Request) error {
ctx := r.Context()
switch r.Method {
case http.MethodGet:
if err := bindQuery(out, r); err != nil {
appLogger.Warnf(ctx, "Could not `bindQuery`, url=%s, err=%+v", r.URL.RequestURI(), err)
return err
}
case http.MethodPost, http.MethodPut, http.MethodDelete:
if err := bindBodyJSON(out, r); err != nil {
appLogger.Warnf(ctx, "Could not `bindBodyJSON`, url=%s, err=%+v", r.URL.RequestURI(), err)
return err
}
default:
appLogger.Warnf(ctx, "Unexpected HTTP Method, method=%s", r.Method)
return fmt.Errorf("unexpected HTTP Method, method=%s", r.Method)
}
return nil
}
// bindQuery binding query parameter
func bindQuery(out interface{}, r *http.Request) error {
return decoder.Decode(out, r.URL.Query())
}
// bindBodyJSON bindding body parameter binding
func bindBodyJSON(out interface{}, r *http.Request) error {
// body, err := ioutil.ReadAll(r.Body)
// if err != nil {
// return err
// }
// return json.Unmarshal(body, out) // Need to read the body several times.(json.Decoder is only once)
return json.NewDecoder(r.Body).Decode(out)
}
func stringSeparator(input string, delimiter rune) []string {
separated := []string{}
for _, p := range strings.Split(input, string(delimiter)) {
if p != "" {
separated = append(separated, p)
}
}
return separated
}