-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
252 lines (211 loc) · 5.22 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
package commands
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"os"
"strings"
)
// ErrorType signfies a category of errors
type ErrorType uint
// ErrorTypes convey what category of error ocurred
const (
ErrNormal ErrorType = iota // general errors
ErrClient // error was caused by the client, (e.g. invalid CLI usage)
ErrNotFound // == HTTP 404 Not Found
ErrImplementation // programmer error in the server
// TODO: add more types of errors for better error-specific handling
)
// Error is a struct for marshalling errors
type Error struct {
Message string
Code ErrorType
}
func (e Error) Error() string {
return e.Message
}
// EncodingType defines a supported encoding
type EncodingType string
// Supported EncodingType constants.
const (
JSON = "json"
XML = "xml"
Protobuf = "protobuf"
Text = "text"
// TODO: support more encoding types
)
func marshalJson(value interface{}) (io.Reader, error) {
b, err := json.Marshal(value)
if err != nil {
return nil, err
}
b = append(b, '\n')
return bytes.NewReader(b), nil
}
var marshallers = map[EncodingType]Marshaler{
JSON: func(res Response) (io.Reader, error) {
ch, ok := res.Output().(<-chan interface{})
if ok {
return &ChannelMarshaler{
Channel: ch,
Marshaler: marshalJson,
Res: res,
}, nil
}
var value interface{}
if res.Error() != nil {
value = res.Error()
} else {
value = res.Output()
}
return marshalJson(value)
},
XML: func(res Response) (io.Reader, error) {
var value interface{}
if res.Error() != nil {
value = res.Error()
} else {
value = res.Output()
}
b, err := xml.Marshal(value)
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
},
}
// Response is the result of a command request. Handlers write to the response,
// setting Error or Value. Response is returned to the client.
type Response interface {
Request() Request
// Set/Return the response Error
SetError(err error, code ErrorType)
Error() *Error
// Sets/Returns the response value
SetOutput(interface{})
Output() interface{}
// Sets/Returns the length of the output
SetLength(uint64)
Length() uint64
// underlying http connections need to be cleaned up, this is for that
Close() error
SetCloser(io.Closer)
// Marshal marshals out the response into a buffer. It uses the EncodingType
// on the Request to chose a Marshaler (Codec).
Marshal() (io.Reader, error)
// Gets a io.Reader that reads the marshalled output
Reader() (io.Reader, error)
// Gets Stdout and Stderr, for writing to console without using SetOutput
Stdout() io.Writer
Stderr() io.Writer
}
type response struct {
req Request
err *Error
value interface{}
out io.Reader
length uint64
stdout io.Writer
stderr io.Writer
closer io.Closer
}
func (r *response) Request() Request {
return r.req
}
func (r *response) Output() interface{} {
return r.value
}
func (r *response) SetOutput(v interface{}) {
r.value = v
}
func (r *response) Length() uint64 {
return r.length
}
func (r *response) SetLength(l uint64) {
r.length = l
}
func (r *response) Error() *Error {
return r.err
}
func (r *response) SetError(err error, code ErrorType) {
r.err = &Error{Message: err.Error(), Code: code}
}
func (r *response) Marshal() (io.Reader, error) {
if r.err == nil && r.value == nil {
return bytes.NewReader([]byte{}), nil
}
enc, found, err := r.req.Option(EncShort).String()
if err != nil {
return nil, err
}
if !found {
return nil, fmt.Errorf("No encoding type was specified")
}
encType := EncodingType(strings.ToLower(enc))
// Special case: if text encoding and an error, just print it out.
if encType == Text && r.Error() != nil {
return strings.NewReader(r.Error().Error()), nil
}
var marshaller Marshaler
if r.req.Command() != nil && r.req.Command().Marshalers != nil {
marshaller = r.req.Command().Marshalers[encType]
}
if marshaller == nil {
var ok bool
marshaller, ok = marshallers[encType]
if !ok {
return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
}
}
output, err := marshaller(r)
if err != nil {
return nil, err
}
if output == nil {
return bytes.NewReader([]byte{}), nil
}
return output, nil
}
// Reader returns an `io.Reader` representing marshalled output of this Response
// Note that multiple calls to this will return a reference to the same io.Reader
func (r *response) Reader() (io.Reader, error) {
if r.out == nil {
if out, ok := r.value.(io.Reader); ok {
// if command returned a io.Reader, use that as our reader
r.out = out
} else {
// otherwise, use the response marshaler output
marshalled, err := r.Marshal()
if err != nil {
return nil, err
}
r.out = marshalled
}
}
return r.out, nil
}
func (r *response) Close() error {
if r.closer != nil {
return r.closer.Close()
}
return nil
}
func (r *response) SetCloser(c io.Closer) {
r.closer = c
}
func (r *response) Stdout() io.Writer {
return r.stdout
}
func (r *response) Stderr() io.Writer {
return r.stderr
}
// NewResponse returns a response to match given Request
func NewResponse(req Request) Response {
return &response{
req: req,
stdout: os.Stdout,
stderr: os.Stderr,
}
}