diff --git a/internal/ext/basicreader.go b/internal/ext/basicreader.go new file mode 100644 index 0000000..34c9b79 --- /dev/null +++ b/internal/ext/basicreader.go @@ -0,0 +1,25 @@ +// Copyright 2024 Matthew Rich . 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 +} diff --git a/internal/ext/basicreader_test.go b/internal/ext/basicreader_test.go new file mode 100644 index 0000000..aeb96fd --- /dev/null +++ b/internal/ext/basicreader_test.go @@ -0,0 +1,30 @@ +// Copyright 2024 Matthew Rich . 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) +} diff --git a/internal/ext/stringreader.go b/internal/ext/stringreader.go new file mode 100644 index 0000000..687fe4b --- /dev/null +++ b/internal/ext/stringreader.go @@ -0,0 +1,16 @@ +// Copyright 2024 Matthew Rich . All rights reserved. + +package ext + +import ( + "io" + "strings" +) + +func NewStringReader(value string) io.Reader { + return stringReader{strings.NewReader(value)} +} + +type stringReader struct { + io.Reader +} diff --git a/internal/ext/stringreader_test.go b/internal/ext/stringreader_test.go new file mode 100644 index 0000000..794a9dd --- /dev/null +++ b/internal/ext/stringreader_test.go @@ -0,0 +1,18 @@ +// Copyright 2024 Matthew Rich . 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) +}