Skip to content

Commit

Permalink
Merge pull request #4 from kelindar/flush
Browse files Browse the repository at this point in the history
Add flush on writer
  • Loading branch information
kelindar committed Dec 22, 2021
2 parents c51846f + cae8fc6 commit 056b430
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions source_test.go
Expand Up @@ -117,6 +117,10 @@ func (w *limitWriter) Close() error {
return nil
}

func (w *limitWriter) Flush() error {
return nil
}

// --------------------------- Self Reader/Writer ---------------------------

type person struct {
Expand Down
13 changes: 12 additions & 1 deletion writer.go
Expand Up @@ -55,8 +55,19 @@ func (w *Writer) write(p []byte) error {
return err
}

// Flush flushes the writer to the underlying stream and returns its error. If
// the underlying io.Writer does not have a Flush() error method, it's a no-op.
func (w *Writer) Flush() error {
if flusher, ok := w.out.(interface {
Flush() error
}); ok {
return flusher.Flush()
}
return nil
}

// Close closes the writer's underlying stream and return its error. If the
// underlying stream is not an io.Closer, it is a no-op.
// underlying io.Writer is not an io.Closer, it's a no-op.
func (w *Writer) Close() error {
if closer, ok := w.out.(io.Closer); ok {
return closer.Close()
Expand Down
5 changes: 5 additions & 0 deletions writer_test.go
Expand Up @@ -225,6 +225,11 @@ func TestWriterClose(t *testing.T) {
assert.NoError(t, w.Close())
}

func TestWriterFlush(t *testing.T) {
assert.NoError(t, NewWriter(new(limitWriter)).Flush())
assert.NoError(t, NewWriter(bytes.NewBuffer(nil)).Flush())
}

// assertWrite asserts a single write operation
func assertWrite(t *testing.T, name string, fn func(*Writer) error, expect []byte) {
assertWriteN(t, name, fn, expect, 99999)
Expand Down

0 comments on commit 056b430

Please sign in to comment.