-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move internal writers to an internal package
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
- Loading branch information
Dave Henderson
committed
May 9, 2020
1 parent
66f381d
commit ae434c5
Showing
5 changed files
with
163 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package writers | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "io" | ||
| ) | ||
|
|
||
| type emptySkipper struct { | ||
| open func() (io.WriteCloser, error) | ||
|
|
||
| // internal | ||
| w io.WriteCloser | ||
| buf *bytes.Buffer | ||
| nw bool | ||
| } | ||
|
|
||
| // NewEmptySkipper creates an io.WriteCloser that will only start writing once a | ||
| // non-whitespace byte has been encountered. The wrapped io.WriteCloser must be | ||
| // provided by the `open` func. | ||
| func NewEmptySkipper(open func() (io.WriteCloser, error)) io.WriteCloser { | ||
| return &emptySkipper{ | ||
| w: nil, | ||
| buf: &bytes.Buffer{}, | ||
| nw: false, | ||
| open: open, | ||
| } | ||
| } | ||
|
|
||
| func (f *emptySkipper) Write(p []byte) (n int, err error) { | ||
| if !f.nw { | ||
| if allWhitespace(p) { | ||
| // buffer the whitespace | ||
| return f.buf.Write(p) | ||
| } | ||
|
|
||
| // first time around, so open the writer | ||
| f.nw = true | ||
| f.w, err = f.open() | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| if f.w == nil { | ||
| return 0, errors.New("nil writer returned by open") | ||
| } | ||
| // empty the buffer into the wrapped writer | ||
| _, err = f.buf.WriteTo(f.w) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| } | ||
|
|
||
| return f.w.Write(p) | ||
| } | ||
|
|
||
| // Close - implements io.Closer | ||
| func (f *emptySkipper) Close() error { | ||
| if f.w != nil { | ||
| return f.w.Close() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func allWhitespace(p []byte) bool { | ||
| for _, b := range p { | ||
| if b == ' ' || b == '\t' || b == '\n' || b == '\r' || b == '\v' { | ||
| continue | ||
| } | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // NopCloser returns a WriteCloser with a no-op Close method wrapping | ||
| // the provided io.Writer. | ||
| type NopCloser struct { | ||
| io.Writer | ||
| } | ||
|
|
||
| // Close - implements io.Closer | ||
| func (n *NopCloser) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| var ( | ||
| _ io.WriteCloser = (*NopCloser)(nil) | ||
| _ io.WriteCloser = (*emptySkipper)(nil) | ||
| ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package writers | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestAllWhitespace(t *testing.T) { | ||
| testdata := []struct { | ||
| in []byte | ||
| expected bool | ||
| }{ | ||
| {[]byte(" "), true}, | ||
| {[]byte("foo"), false}, | ||
| {[]byte(" \t\n\n\v\r\n"), true}, | ||
| {[]byte(" foo "), false}, | ||
| } | ||
|
|
||
| for _, d := range testdata { | ||
| assert.Equal(t, d.expected, allWhitespace(d.in)) | ||
| } | ||
| } | ||
|
|
||
| func TestEmptySkipper(t *testing.T) { | ||
| testdata := []struct { | ||
| in []byte | ||
| empty bool | ||
| }{ | ||
| {[]byte(" "), true}, | ||
| {[]byte("foo"), false}, | ||
| {[]byte(" \t\n\n\v\r\n"), true}, | ||
| {[]byte(" foo "), false}, | ||
| } | ||
|
|
||
| for _, d := range testdata { | ||
| w := &bufferCloser{&bytes.Buffer{}} | ||
| opened := false | ||
| f, ok := NewEmptySkipper(func() (io.WriteCloser, error) { | ||
| opened = true | ||
| return w, nil | ||
| }).(*emptySkipper) | ||
|
|
||
| assert.True(t, ok) | ||
| n, err := f.Write(d.in) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, len(d.in), n) | ||
| if d.empty { | ||
| assert.Nil(t, f.w) | ||
| assert.False(t, opened) | ||
| } else { | ||
| assert.NotNil(t, f.w) | ||
| assert.True(t, opened) | ||
| assert.EqualValues(t, d.in, w.Bytes()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type bufferCloser struct { | ||
| *bytes.Buffer | ||
| } | ||
|
|
||
| func (b *bufferCloser) Close() error { | ||
| return nil | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters