add interfaces to remove WriterTo
This commit is contained in:
parent
33b52f69ec
commit
0da6c3db75
25
internal/ext/basicreader.go
Normal file
25
internal/ext/basicreader.go
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package ext
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// Restrict the underlying io.Reader to only exposed the io.Reader interface.
|
||||
// Removes the io.WriterTo interface.
|
||||
func NewReadCloser(r io.ReadCloser) io.ReadCloser {
|
||||
return basicReadCloser{r}
|
||||
}
|
||||
|
||||
type basicReadCloser struct {
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
func NewReader(r io.Reader) io.Reader {
|
||||
return basicReader{r}
|
||||
}
|
||||
|
||||
type basicReader struct {
|
||||
io.Reader
|
||||
}
|
30
internal/ext/basicreader_test.go
Normal file
30
internal/ext/basicreader_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package ext
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"strings"
|
||||
_ "fmt"
|
||||
_ "log"
|
||||
"io"
|
||||
)
|
||||
|
||||
func TestNewBasicReader(t *testing.T) {
|
||||
testReader := strings.NewReader("some test data")
|
||||
basicReader := NewReader(testReader)
|
||||
assert.NotNil(t, basicReader)
|
||||
_, ok := basicReader.(io.WriterTo)
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestNewBasicReadCloser(t *testing.T) {
|
||||
testReader := strings.NewReader("some test data")
|
||||
basicReader := NewReadCloser(io.NopCloser(testReader))
|
||||
assert.NotNil(t, basicReader)
|
||||
_, ok := basicReader.(io.WriterTo)
|
||||
assert.False(t, ok)
|
||||
_, hasCloser := basicReader.(io.Closer)
|
||||
assert.True(t, hasCloser)
|
||||
}
|
16
internal/ext/stringreader.go
Normal file
16
internal/ext/stringreader.go
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package ext
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewStringReader(value string) io.Reader {
|
||||
return stringReader{strings.NewReader(value)}
|
||||
}
|
||||
|
||||
type stringReader struct {
|
||||
io.Reader
|
||||
}
|
18
internal/ext/stringreader_test.go
Normal file
18
internal/ext/stringreader_test.go
Normal file
@ -0,0 +1,18 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package ext
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
_ "fmt"
|
||||
_ "log"
|
||||
"io"
|
||||
)
|
||||
|
||||
func TestNewStringReader(t *testing.T) {
|
||||
basicReader := NewStringReader("some test data")
|
||||
assert.NotNil(t, basicReader)
|
||||
_, ok := basicReader.(io.WriterTo)
|
||||
assert.False(t, ok)
|
||||
}
|
Loading…
Reference in New Issue
Block a user