-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbind.go
54 lines (42 loc) · 1.45 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
package otto
import (
"encoding/json"
"io"
"net/http"
"strings"
"github.com/pkg/errors"
)
// BindFunc defines func to run bind
type BindFunc func(Context, interface{}) error
// DefaultBinder checks Content Type from request and tries to decode
// the body with appropriate decoder
func DefaultBinder(ctx Context, dest interface{}) error {
ct := ctx.Request().Header.Get(HeaderContentType)
body := ctx.Request().Body
if isSupported(ctx.Request().Method) {
err := errors.Errorf("Bind is not supported for %s method", ctx.Request().Method)
return ctx.Error(http.StatusBadRequest, err)
}
if ctx.Request().ContentLength == 0 {
return ctx.Error(http.StatusBadRequest, errors.New("Request body cannot be empty"))
}
if strings.HasPrefix(ct, MIMEApplicationJSON) {
return ctx.Error(http.StatusBadRequest, decodeJSON(body, dest))
}
return errors.Errorf("No support for content type '%s'", ct)
}
func isSupported(method string) bool {
return method == "GET" || method == "DELETE"
}
func decodeJSON(r io.Reader, dest interface{}) error {
if err := json.NewDecoder(r).Decode(dest); err != nil {
if u, ok := err.(*json.UnmarshalTypeError); ok {
return errors.Wrapf(err, "Unmarshal type error: expected=%v, got=%v, offset=%v", u.Type, u.Value, u.Offset)
}
if s, ok := err.(*json.SyntaxError); ok {
return errors.Wrapf(err, "Syntax error: offset=%v, error=%v", s.Offset, s.Error())
}
return errors.Wrap(err, "Could not decode json")
}
return nil
}