-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontracts_structs.go
212 lines (168 loc) · 6.9 KB
/
contracts_structs.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package shuttle
import (
"encoding/json"
"io"
"net/http"
)
// TextResult provides the ability render a result which contains text.
type TextResult struct {
// StatusCode, if provided, use this value, otherwise HTTP 200.
StatusCode int
// ContentType, if provided, use this value.
ContentType string
// Headers, if provided, are added to the response
Headers map[string][]string
// Content, if provided, use this value, otherwise no content will be written to the response stream.
Content string
}
// BinaryResult provides the ability render a result which contains binary data.
type BinaryResult struct {
// StatusCode, if provided, use this value, otherwise HTTP 200.
StatusCode int
// ContentType, if provided, use this value.
ContentType string
// ContentDisposition, if provided, use this value.
ContentDisposition string
// Headers, if provided, are added to the response
Headers map[string][]string
// Content, if provided, use this value, otherwise no content will be written to the response stream.
Content []byte
}
// StreamResult provides the ability render a result which is streamed from another source.
type StreamResult struct {
// StatusCode, if provided, use this value, otherwise HTTP 200.
StatusCode int
// ContentType, if provided, use this value.
ContentType string
// ContentDisposition, if provided, use this value.
ContentDisposition string
// Headers, if provided, are added to the response
Headers map[string][]string
// Content, if provided, use this value, otherwise no content will be written to the response stream.
Content io.Reader
}
// SerializeResult provides the ability render a serialized result.
type SerializeResult struct {
// StatusCode, if provided, use this value, otherwise HTTP 200.
StatusCode int
// ContentType, if provided, use this value.
ContentType string
// Headers, if provided, are added to the response
Headers map[string][]string
// Content, if provided, use this value, otherwise no content will be written to the response stream.
Content any
}
func (this *SerializeResult) SetContent(value any) { this.Content = value }
func (this *SerializeResult) Result() any { return this }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// InputError represents some kind of problem with the calling HTTP request.
type InputError struct {
// Fields indicates the exact location(s) of the errors including the part of the HTTP request itself this is
// invalid. Valid field prefixes include "path", "query", "header", "form", and "body".
Fields []string `json:"fields,omitempty"`
// ID represents the unique, numeric contractual identifier that can be used to associate this error with a particular front-end error message, if any.
ID int `json:"id,omitempty"`
// Name represents the unique string-based, contractual value that can be used to associate this error with a particular front-end error message, if any.
Name string `json:"name,omitempty"`
// Message represents a friendly, user-facing message to indicate why there was a problem with the input.
Message string `json:"message,omitempty"`
// Context represents a error-specific value in the form a serializable instance that the client can use to get more understanding about the error itself.
Context any `json:"context,omitempty"`
}
// InputErrors represents a set of problems with the calling HTTP request.
type InputErrors struct {
Errors []error `json:"errors,omitempty"`
}
func (this InputError) Error() string { return this.Message }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BaseInputModel allows enables struct embedding such that other InputModels don't necessarily need to re-implement each method.
type BaseInputModel struct{}
func (*BaseInputModel) Reset() {}
func (*BaseInputModel) Bind(*http.Request) error { return nil }
func (*BaseInputModel) Validate([]error) int { return 0 }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type fixedResultContainer struct{ ResultContainer }
type bindErrorContainer struct{ *SerializeResult }
type validationErrorContainer struct{ *SerializeResult }
func (this *fixedResultContainer) SetContent(any) {} // no-op
func (this *fixedResultContainer) Result() any { return this.ResultContainer }
func (this *bindErrorContainer) SetContent(value any) {
inputErrors := this.SerializeResult.Content.(*InputErrors)
inputErrors.Errors = inputErrors.Errors[0:0]
inputErrors.Errors = append(inputErrors.Errors, value.(error))
}
func (this *bindErrorContainer) Result() any { return this.SerializeResult }
func (this *validationErrorContainer) SetContent(value any) {
inputErrors := this.SerializeResult.Content.(*InputErrors)
inputErrors.Errors = inputErrors.Errors[0:0]
inputErrors.Errors = value.([]error)
}
func (this *validationErrorContainer) Result() any { return this.SerializeResult }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var (
notAcceptableResult = &TextResult{
StatusCode: http.StatusNotAcceptable,
ContentType: mimeTypeApplicationJSONUTF8,
Content: _serializeJSON(InputErrors{
Errors: []error{
InputError{
Fields: []string{"header:Accept"},
Name: "invalid-accept-header",
Message: "Unable to represent the application results using the Accept type.",
},
},
}),
}
unsupportedMediaTypeResult = &SerializeResult{
StatusCode: http.StatusUnsupportedMediaType,
Content: InputErrors{
Errors: []error{
InputError{
Fields: []string{"header:Content-Type"},
Name: "invalid-content-type-header",
Message: "The content type specified, if any, was not recognized.",
},
},
},
}
deserializationResult = &fixedResultContainer{ResultContainer: &SerializeResult{
StatusCode: http.StatusBadRequest,
Content: InputErrors{
Errors: []error{
InputError{
Fields: []string{"body"},
Name: "malformed-request-payload",
Message: "The body did not contain well-formed data and could not be properly deserialized.",
},
},
},
}}
parseFormedFailedResult = &SerializeResult{
StatusCode: http.StatusBadRequest,
Content: InputErrors{
Errors: []error{
InputError{
Fields: []string{"form"},
Name: "invalid-form-data",
Message: "The form data provided was not valid and could not be parsed.",
},
},
},
}
bindErrorResult = &bindErrorContainer{
SerializeResult: &SerializeResult{
StatusCode: http.StatusBadRequest,
Content: &InputErrors{},
},
}
validationResult = &validationErrorContainer{
SerializeResult: &SerializeResult{
StatusCode: http.StatusUnprocessableEntity,
Content: &InputErrors{},
},
}
)
func _serializeJSON(instance any) string {
raw, _ := json.Marshal(instance)
return string(raw)
}