This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_model.go
164 lines (132 loc) · 3.38 KB
/
input_model.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
package detour
import (
"encoding/json"
"errors"
"net/http"
"strings"
)
func prepareInputModel(model interface{}, request *http.Request) (statusCode int, err error) {
if err = Bind(request, model); err != nil {
return statusCodeFromErrorOrDefault(err, http.StatusBadRequest)
}
sanitize(model)
if err = validate(model); err != nil {
return statusCodeFromErrorOrDefault(err, http.StatusUnprocessableEntity)
}
if err = serverError(model); err != nil {
return http.StatusInternalServerError, err
}
return 0, nil
}
// Bind is exported for use in testing.
func Bind(request *http.Request, message interface{}) error {
if context, ok := message.(BindContext); ok {
context.BindContext(request.Context())
}
err := bindJSON(request, message)
if err != nil {
return err
}
binder, isBinder := message.(Binder)
if !isBinder {
return nil
}
err = request.ParseForm()
if err != nil {
return err
}
err = binder.Bind(request)
if err == nil {
return nil
}
errs, isErrors := err.(Errors)
if isErrors && len(errs) == 0 {
return nil
}
diagnosticErrs, isDiagnosticErrors := err.(DiagnosticErrors)
if isDiagnosticErrors && len(diagnosticErrs) == 0 {
return nil
}
return err
}
func bindJSON(request *http.Request, message interface{}) error {
binder, ok := message.(BindJSON)
if !ok {
return nil
}
if !binder.BindJSON() {
return nil
}
if !isPutOrPost(request) {
return errMethodNotAllowed
}
if !hasJSONContent(request) {
return errUnsupportedMediaType
}
return json.NewDecoder(request.Body).Decode(&message)
}
func isPutOrPost(request *http.Request) bool {
return request.Method == http.MethodPost || request.Method == http.MethodPut
}
func hasJSONContent(request *http.Request) bool {
return strings.Contains(request.Header.Get("Content-Type"), "/json")
}
func statusCodeFromErrorOrDefault(err error, defaultStatusCode int) (int, error) {
status, ok := err.(ErrorCode)
if !ok {
return defaultStatusCode, err
}
code := status.StatusCode()
if code == 0 {
return defaultStatusCode, err
}
return code, err
}
func sanitize(message interface{}) {
if sanitizer, isSanitizer := message.(Sanitizer); isSanitizer {
sanitizer.Sanitize()
}
}
func validate(message interface{}) error {
validator, isValidator := message.(Validator)
if !isValidator {
return nil
}
err := validator.Validate()
if err == nil {
return nil
}
errs, isErrors := err.(Errors)
if isErrors && len(errs) == 0 {
return nil
}
diagnosticErrs, isDiagnosticErrors := err.(DiagnosticErrors)
if isDiagnosticErrors && len(diagnosticErrs) == 0 {
return nil
}
return err
}
func serverError(message interface{}) error {
if server, isServerError := message.(ServerError); isServerError && server.Error() {
return internalServerError
}
return nil
}
var (
internalServerError = errors.New(http.StatusText(http.StatusInternalServerError))
errUnsupportedMediaType = NewStatusCodeError(http.StatusUnsupportedMediaType)
errMethodNotAllowed = NewStatusCodeError(http.StatusMethodNotAllowed)
)
//////////////////////////////////////////////////////////////////////
type StatusCodeError struct {
statusCode int
}
func NewStatusCodeError(statusCode int) *StatusCodeError {
return &StatusCodeError{statusCode: statusCode}
}
func (this StatusCodeError) StatusCode() int {
return this.statusCode
}
func (this StatusCodeError) Error() string {
return http.StatusText(this.statusCode)
}