response.go 4.41 KB
Newer Older
Matt Bell's avatar
Matt Bell committed
1 2
package commands

3
import (
4
	"bytes"
Matt Bell's avatar
Matt Bell committed
5 6 7
	"encoding/json"
	"encoding/xml"
	"fmt"
8
	"io"
Matt Bell's avatar
Matt Bell committed
9
	"strings"
10 11
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
// ErrorType signfies a category of errors
Matt Bell's avatar
Matt Bell committed
13
type ErrorType uint
Matt Bell's avatar
Matt Bell committed
14

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
// ErrorTypes convey what category of error ocurred
Matt Bell's avatar
Matt Bell committed
16
const (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18
	ErrNormal ErrorType = iota // general errors
	ErrClient                  // error was caused by the client, (e.g. invalid CLI usage)
Matt Bell's avatar
Matt Bell committed
19
	// TODO: add more types of errors for better error-specific handling
Matt Bell's avatar
Matt Bell committed
20 21
)

22 23
// Error is a struct for marshalling errors
type Error struct {
Matt Bell's avatar
Matt Bell committed
24 25
	Message string
	Code    ErrorType
26 27
}

28
func (e Error) Error() string {
29
	return e.Message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31 32
}

// EncodingType defines a supported encoding
33
type EncodingType string
Matt Bell's avatar
Matt Bell committed
34

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
// Supported EncodingType constants.
36
const (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37 38
	JSON = "json"
	XML  = "xml"
39
	Text = "text"
Matt Bell's avatar
Matt Bell committed
40
	// TODO: support more encoding types
41 42
)

43 44 45 46 47 48 49 50
func marshalJson(value interface{}) (io.Reader, error) {
	b, err := json.MarshalIndent(value, "", "  ")
	if err != nil {
		return nil, err
	}
	return bytes.NewReader(b), nil
}

51
var marshallers = map[EncodingType]Marshaler{
52
	JSON: func(res Response) (io.Reader, error) {
53 54
		ch, ok := res.Output().(<-chan interface{})
		if ok {
55 56 57 58 59 60
			return &ChannelMarshaler{
				Channel:   ch,
				Marshaler: marshalJson,
			}, nil
		}

61
		var value interface{}
62
		if res.Error() != nil {
63 64 65
			value = res.Error()
		} else {
			value = res.Output()
66
		}
67
		return marshalJson(value)
68
	},
69 70
	XML: func(res Response) (io.Reader, error) {
		var value interface{}
71
		if res.Error() != nil {
72 73 74 75 76 77 78 79
			value = res.Error()
		} else {
			value = res.Output()
		}

		b, err := xml.Marshal(value)
		if err != nil {
			return nil, err
80
		}
81
		return bytes.NewReader(b), nil
82
	},
83 84
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85 86
// Response is the result of a command request. Handlers write to the response,
// setting Error or Value. Response is returned to the client.
87 88 89 90 91
type Response interface {
	Request() Request

	// Set/Return the response Error
	SetError(err error, code ErrorType)
92
	Error() *Error
93 94

	// Sets/Returns the response value
95 96
	SetOutput(interface{})
	Output() interface{}
97

98 99 100 101
	// Sets/Returns the length of the output
	SetLength(uint64)
	Length() uint64

102
	// Marshal marshals out the response into a buffer. It uses the EncodingType
103
	// on the Request to chose a Marshaler (Codec).
104
	Marshal() (io.Reader, error)
105 106 107

	// Gets a io.Reader that reads the marshalled output
	Reader() (io.Reader, error)
108 109 110
}

type response struct {
111 112 113 114 115
	req    Request
	err    *Error
	value  interface{}
	out    io.Reader
	length uint64
116 117 118 119 120 121
}

func (r *response) Request() Request {
	return r.req
}

122
func (r *response) Output() interface{} {
123 124 125
	return r.value
}

126
func (r *response) SetOutput(v interface{}) {
127 128 129
	r.value = v
}

130 131 132 133 134 135 136 137
func (r *response) Length() uint64 {
	return r.length
}

func (r *response) SetLength(l uint64) {
	r.length = l
}

138
func (r *response) Error() *Error {
139
	return r.err
Matt Bell's avatar
Matt Bell committed
140 141
}

142 143
func (r *response) SetError(err error, code ErrorType) {
	r.err = &Error{Message: err.Error(), Code: code}
144 145
}

146
func (r *response) Marshal() (io.Reader, error) {
147
	if r.err == nil && r.value == nil {
148
		return bytes.NewReader([]byte{}), nil
Matt Bell's avatar
Matt Bell committed
149
	}
150

151
	enc, found, err := r.req.Option(EncShort).String()
152 153 154
	if err != nil {
		return nil, err
	}
155 156 157
	if !found {
		return nil, fmt.Errorf("No encoding type was specified")
	}
158
	encType := EncodingType(strings.ToLower(enc))
Matt Bell's avatar
Matt Bell committed
159

160 161
	// Special case: if text encoding and an error, just print it out.
	if encType == Text && r.Error() != nil {
162
		return strings.NewReader(r.Error().Error()), nil
163 164
	}

165 166 167
	var marshaller Marshaler
	if r.req.Command() != nil && r.req.Command().Marshalers != nil {
		marshaller = r.req.Command().Marshalers[encType]
168
	}
169
	if marshaller == nil {
170
		var ok bool
171 172 173 174
		marshaller, ok = marshallers[encType]
		if !ok {
			return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
		}
Matt Bell's avatar
Matt Bell committed
175
	}
176

177 178 179 180 181 182 183 184
	output, err := marshaller(r)
	if err != nil {
		return nil, err
	}
	if output == nil {
		return bytes.NewReader([]byte{}), nil
	}
	return output, nil
185 186
}

187 188 189
// 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) {
190 191
	if r.out == nil {
		if out, ok := r.value.(io.Reader); ok {
192
			// if command returned a io.Reader, use that as our reader
193 194
			r.out = out

195 196 197 198 199 200
		} else {
			// otherwise, use the response marshaler output
			marshalled, err := r.Marshal()
			if err != nil {
				return nil, err
			}
201

202 203
			r.out = marshalled
		}
204 205
	}

206
	return r.out, nil
207 208
}

209
// NewResponse returns a response to match given Request
210 211
func NewResponse(req Request) Response {
	return &response{req: req}
212
}