Commit 22c43a4f authored by Kirill Smelkov's avatar Kirill Smelkov

encoder: Don't forget to handle/propagate error in Encode()

Previously errors from underlying .encode() were ignored completely and
not returned to callers.
parent 28bae785
......@@ -29,9 +29,12 @@ func NewEncoder(w io.Writer) *Encoder {
// Encode writes the pickle encoding of v to w, the encoder's writer
func (e *Encoder) Encode(v interface{}) error {
rv := reflectValueOf(v)
e.encode(rv)
e.w.Write([]byte{opStop})
return nil
err := e.encode(rv)
if err != nil {
return err
}
_, err = e.w.Write([]byte{opStop})
return err
}
func (e *Encoder) encode(rv reflect.Value) error {
......
......@@ -2,6 +2,7 @@ package ogórek
import (
"bytes"
"io"
"reflect"
"testing"
)
......@@ -38,7 +39,10 @@ func TestEncode(t *testing.T) {
for _, tt := range tests {
p := &bytes.Buffer{}
e := NewEncoder(p)
e.Encode(tt.input)
err := e.Encode(tt.input)
if err != nil {
t.Errorf("%s: encode error: %v", tt.name, err)
}
d := NewDecoder(bytes.NewReader(p.Bytes()))
output, _ := d.Decode()
......@@ -52,5 +56,35 @@ func TestEncode(t *testing.T) {
t.Errorf("%s: got\n%q\n expected\n%q", tt.name, output, want)
}
for l := int64(p.Len())-1; l >= 0; l-- {
p.Reset()
e := NewEncoder(LimitWriter(p, l))
err = e.Encode(tt.input)
if err != io.EOF {
t.Errorf("%s: encoder did not handle write error @%v: got %#v", tt.name, l, err)
}
}
}
}
// like io.LimitedReader but for writes
// XXX it would be good to have it in stdlib
type LimitedWriter struct {
W io.Writer
N int64
}
func (l *LimitedWriter) Write(p []byte) (n int, err error) {
if l.N <= 0 {
return 0, io.EOF
}
if int64(len(p)) > l.N {
p = p[0:l.N]
}
n, err = l.W.Write(p)
l.N -= int64(n)
return
}
func LimitWriter(w io.Writer, n int64) io.Writer { return &LimitedWriter{w, n} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment