add Close to the reader

This commit is contained in:
Matthew Rich 2024-09-19 08:18:48 +00:00
parent 8910fced57
commit 01933df053
2 changed files with 8 additions and 3 deletions

View File

@ -10,11 +10,11 @@ import (
type ReadFilter func([]byte, int, error) (int, error) type ReadFilter func([]byte, int, error) (int, error)
type Reader struct { type Reader struct {
input io.Reader input io.ReadCloser
filter ReadFilter filter ReadFilter
} }
func NewReader(reader io.Reader, filter ReadFilter) *Reader { func NewReader(reader io.ReadCloser, filter ReadFilter) *Reader {
return &Reader{ return &Reader{
input: reader, input: reader,
filter: filter, filter: filter,
@ -30,3 +30,7 @@ func (r *Reader) Read(p []byte) (n int, err error) {
} }
return n, err return n, err
} }
func (r *Reader) Close() error {
return r.input.Close()
}

View File

@ -6,11 +6,12 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
"strings" "strings"
"io"
) )
func TestNewReader(t *testing.T) { func TestNewReader(t *testing.T) {
buffer := make([]byte, 20) buffer := make([]byte, 20)
reader := NewReader(strings.NewReader("testdata"), func(p []byte, in int, e error) (int, error) { reader := NewReader(io.NopCloser(strings.NewReader("testdata")), func(p []byte, in int, e error) (int, error) {
copy(p, []byte("foo")) copy(p, []byte("foo"))
return 3, nil return 3, nil
}) })