add interfaces to remove WriterTo
Some checks failed
Lint / golangci-lint (push) Failing after 10m32s
Declarative Tests / test (push) Successful in 35s

This commit is contained in:
Matthew Rich 2024-10-02 19:22:47 +00:00
parent 33b52f69ec
commit 0da6c3db75
4 changed files with 89 additions and 0 deletions

View 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
}

View 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)
}

View 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
}

View 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)
}