forked from masseelch/elk
/
response.go
277 lines (248 loc) · 6.53 KB
/
response.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Code generated by entc, DO NOT EDIT.
package http
import (
"net/http"
"strconv"
"github.com/google/uuid"
"github.com/mailru/easyjson"
"github.com/chestarss/elk/internal/simple/ent"
collar "github.com/chestarss/elk/internal/simple/ent/collar"
)
// Basic HTTP Error Response
type ErrResponse struct {
Code int `json:"code"` // http response status code
Status string `json:"status"` // user-level status message
Errors interface{} `json:"errors,omitempty"` // application-level error
}
func (e ErrResponse) MarshalToHTTPResponseWriter(w http.ResponseWriter) (int, error) {
d, err := easyjson.Marshal(e)
if err != nil {
return 0, err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(d)))
w.WriteHeader(e.Code)
return w.Write(d)
}
func BadRequest(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusBadRequest,
Status: http.StatusText(http.StatusBadRequest),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func Conflict(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusConflict,
Status: http.StatusText(http.StatusConflict),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func Forbidden(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusForbidden,
Status: http.StatusText(http.StatusForbidden),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func InternalServerError(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusInternalServerError,
Status: http.StatusText(http.StatusInternalServerError),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func NotFound(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusNotFound,
Status: http.StatusText(http.StatusNotFound),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
func Unauthorized(w http.ResponseWriter, msg interface{}) (int, error) {
return ErrResponse{
Code: http.StatusUnauthorized,
Status: http.StatusText(http.StatusUnauthorized),
Errors: msg,
}.MarshalToHTTPResponseWriter(w)
}
type (
// Category4094953247View represents the data serialized for the following serialization group combinations:
// []
// [owner pet pet:owner]
Category4094953247View struct {
ID uint64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
Category4094953247Views []*Category4094953247View
)
func NewCategory4094953247View(e *ent.Category) *Category4094953247View {
if e == nil {
return nil
}
return &Category4094953247View{
ID: e.ID,
Name: e.Name,
}
}
func NewCategory4094953247Views(es []*ent.Category) Category4094953247Views {
if len(es) == 0 {
return nil
}
r := make(Category4094953247Views, len(es))
for i, e := range es {
r[i] = NewCategory4094953247View(e)
}
return r
}
type (
// Collar1522160880View represents the data serialized for the following serialization group combinations:
// []
// [owner pet pet:owner]
Collar1522160880View struct {
ID int `json:"id,omitempty"`
Color collar.Color `json:"color,omitempty"`
}
Collar1522160880Views []*Collar1522160880View
)
func NewCollar1522160880View(e *ent.Collar) *Collar1522160880View {
if e == nil {
return nil
}
return &Collar1522160880View{
ID: e.ID,
Color: e.Color,
}
}
func NewCollar1522160880Views(es []*ent.Collar) Collar1522160880Views {
if len(es) == 0 {
return nil
}
r := make(Collar1522160880Views, len(es))
for i, e := range es {
r[i] = NewCollar1522160880View(e)
}
return r
}
type (
// Media1941033838View represents the data serialized for the following serialization group combinations:
// []
// [owner pet pet:owner]
Media1941033838View struct {
ID int `json:"id,omitempty"`
}
Media1941033838Views []*Media1941033838View
)
func NewMedia1941033838View(e *ent.Media) *Media1941033838View {
if e == nil {
return nil
}
return &Media1941033838View{
ID: e.ID,
}
}
func NewMedia1941033838Views(es []*ent.Media) Media1941033838Views {
if len(es) == 0 {
return nil
}
r := make(Media1941033838Views, len(es))
for i, e := range es {
r[i] = NewMedia1941033838View(e)
}
return r
}
type (
// Owner139708381View represents the data serialized for the following serialization group combinations:
// []
// [owner pet pet:owner]
Owner139708381View struct {
ID uuid.UUID `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
}
Owner139708381Views []*Owner139708381View
)
func NewOwner139708381View(e *ent.Owner) *Owner139708381View {
if e == nil {
return nil
}
return &Owner139708381View{
ID: e.ID,
Name: e.Name,
Age: e.Age,
}
}
func NewOwner139708381Views(es []*ent.Owner) Owner139708381Views {
if len(es) == 0 {
return nil
}
r := make(Owner139708381Views, len(es))
for i, e := range es {
r[i] = NewOwner139708381View(e)
}
return r
}
type (
// Pet1876743790View represents the data serialized for the following serialization group combinations:
// [owner pet pet:owner]
Pet1876743790View struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Owner *Owner139708381View `json:"owner,omitempty"`
Friends Pet359800019Views `json:"friends,omitempty"`
}
Pet1876743790Views []*Pet1876743790View
)
func NewPet1876743790View(e *ent.Pet) *Pet1876743790View {
if e == nil {
return nil
}
return &Pet1876743790View{
ID: e.ID,
Name: e.Name,
Age: e.Age,
Owner: NewOwner139708381View(e.Edges.Owner),
Friends: NewPet359800019Views(e.Edges.Friends),
}
}
func NewPet1876743790Views(es []*ent.Pet) Pet1876743790Views {
if len(es) == 0 {
return nil
}
r := make(Pet1876743790Views, len(es))
for i, e := range es {
r[i] = NewPet1876743790View(e)
}
return r
}
type (
// Pet359800019View represents the data serialized for the following serialization group combinations:
// []
Pet359800019View struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
}
Pet359800019Views []*Pet359800019View
)
func NewPet359800019View(e *ent.Pet) *Pet359800019View {
if e == nil {
return nil
}
return &Pet359800019View{
ID: e.ID,
Name: e.Name,
Age: e.Age,
}
}
func NewPet359800019Views(es []*ent.Pet) Pet359800019Views {
if len(es) == 0 {
return nil
}
r := make(Pet359800019Views, len(es))
for i, e := range es {
r[i] = NewPet359800019View(e)
}
return r
}