提交 f6c38882 作者: Matt Bell 提交者: Juan Batiz-Benet

commands: Return a reader in a Response#Reader method, instead of making…

commands: Return a reader in a Response#Reader method, instead of making Response implementing io.Reader
上级 69a56de0
...@@ -56,10 +56,13 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -56,10 +56,13 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
_, err = io.Copy(w, res) out, err := res.Reader()
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return
} }
io.Copy(w, out)
} }
...@@ -74,8 +74,6 @@ var marshallers = map[EncodingType]Marshaller{ ...@@ -74,8 +74,6 @@ var marshallers = map[EncodingType]Marshaller{
// Response is the result of a command request. Handlers write to the response, // Response is the result of a command request. Handlers write to the response,
// setting Error or Value. Response is returned to the client. // setting Error or Value. Response is returned to the client.
type Response interface { type Response interface {
io.Reader
Request() Request Request() Request
// Set/Return the response Error // Set/Return the response Error
...@@ -89,6 +87,9 @@ type Response interface { ...@@ -89,6 +87,9 @@ type Response interface {
// Marshal marshals out the response into a buffer. It uses the EncodingType // Marshal marshals out the response into a buffer. It uses the EncodingType
// on the Request to chose a Marshaller (Codec). // on the Request to chose a Marshaller (Codec).
Marshal() ([]byte, error) Marshal() ([]byte, error)
// Gets a io.Reader that reads the marshalled output
Reader() (io.Reader, error)
} }
type response struct { type response struct {
...@@ -137,28 +138,28 @@ func (r *response) Marshal() ([]byte, error) { ...@@ -137,28 +138,28 @@ func (r *response) Marshal() ([]byte, error) {
return marshaller(r) return marshaller(r)
} }
func (r *response) Read(p []byte) (int, error) { // Reader returns an `io.Reader` representing marshalled output of this Response
// if command set value to a io.Reader, set that as our output stream // Note that multiple calls to this will return a reference to the same io.Reader
func (r *response) Reader() (io.Reader, error) {
// if command set value to a io.Reader, use that as our reader
if r.out == nil { if r.out == nil {
if out, ok := r.value.(io.Reader); ok { if out, ok := r.value.(io.Reader); ok {
r.out = out r.out = out
} }
} }
// if there is an output stream set, read from it if r.out == nil {
if r.out != nil { // no reader set, so marshal the error or value
return r.out.Read(p) marshalled, err := r.Marshal()
if err != nil {
return nil, err
} }
// no stream set, so marshal the error or value // create a Reader from the marshalled data
output, err := r.Marshal() r.out = bytes.NewReader(marshalled)
if err != nil {
return 0, err
} }
// then create a Reader from the marshalled data, and use it as our output stream return r.out, nil
r.out = bytes.NewReader(output)
return r.out.Read(p)
} }
// NewResponse returns a response to match given Request // NewResponse returns a response to match given Request
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论