From 0da6c3db75837e7bb66d891c6ffe8a377a91573f Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Wed, 2 Oct 2024 19:22:47 +0000 Subject: [PATCH] add interfaces to remove WriterTo --- internal/ext/basicreader.go | 25 +++++++++++++++++++++++++ internal/ext/basicreader_test.go | 30 ++++++++++++++++++++++++++++++ internal/ext/stringreader.go | 16 ++++++++++++++++ internal/ext/stringreader_test.go | 18 ++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 internal/ext/basicreader.go create mode 100644 internal/ext/basicreader_test.go create mode 100644 internal/ext/stringreader.go create mode 100644 internal/ext/stringreader_test.go 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) +}