Skip to content

Commit

Permalink
Refactors to remove unneeded errors, 100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Rens Rikkerink authored and ikkerens committed Mar 5, 2019
1 parent 4577f27 commit f374248
Show file tree
Hide file tree
Showing 16 changed files with 634 additions and 378 deletions.
24 changes: 11 additions & 13 deletions compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"reflect"
)

var _ variableReadWriter = (*compressionReadWriter)(nil)

type compressionReadWriter struct {
variable
handler readWriter
level int
}

func (c *compressionReadWriter) readVariable(r io.Reader, v reflect.Value) error {
func (c *compressionReadWriter) readVariable(r io.Reader, v reflect.Value) (err error) {
lb := make([]byte, 4)
if _, err := io.ReadFull(r, lb); err != nil {
return err
Expand All @@ -34,7 +36,9 @@ func (c *compressionReadWriter) readVariable(r io.Reader, v reflect.Value) error
}

z := flate.NewReader(bytes.NewBuffer(cb))
defer z.Close()
defer func() {
_ = z.Close() // Memory buffer, can never error
}()

return handleVariableReader(z, c.handler, v)
}
Expand All @@ -47,12 +51,8 @@ func (c *compressionReadWriter) writeVariable(w io.Writer, v reflect.Value) erro
return err
}

if err = handleVariableWriter(z, c.handler, v); err != nil {
return err
}
if err = z.Close(); err != nil {
return err
}
_ = handleVariableWriter(z, c.handler, v) // As we are using a memory buffer, these two calls can never err
_ = z.Close()

lb := make([]byte, 4)
binary.BigEndian.PutUint32(lb, uint32(b.Len()))
Expand All @@ -66,10 +66,8 @@ func (c *compressionReadWriter) writeVariable(w io.Writer, v reflect.Value) erro
return nil
}

func (c *compressionReadWriter) vLength(v reflect.Value) (int, error) {
func (c *compressionReadWriter) vLength(v reflect.Value) int {
var b bytes.Buffer
if err := c.writeVariable(&b, v); err != nil {
return 0, err
}
return b.Len(), nil
_ = c.writeVariable(&b, v)
return b.Len()
}
305 changes: 0 additions & 305 deletions ikea_test.go

This file was deleted.

10 changes: 5 additions & 5 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Packer interface {
Pack(w io.Writer) error
}

var _ variableReadWriter = (*customReadWriter)(nil)

type customReadWriter struct {
variable
fallback readWriter
Expand All @@ -46,10 +48,8 @@ func (c *customReadWriter) writeVariable(w io.Writer, v reflect.Value) error {
return err
}

func (c *customReadWriter) vLength(v reflect.Value) (int, error) {
func (c *customReadWriter) vLength(v reflect.Value) int {
var b bytes.Buffer
if err := c.writeVariable(&b, v); err != nil {
return 0, err
}
return b.Len(), nil
_ = c.writeVariable(&b, v)
return b.Len()
}
Loading

0 comments on commit f374248

Please sign in to comment.