Skip to content

Commit

Permalink
transport.NewHTTPReadSeeker: return concrete type, deprecate ReadSeek…
Browse files Browse the repository at this point in the history
…Closer

General convention is to define interfaces on the receiver side, and
to return concrete types.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Nov 10, 2022
1 parent 019ead8 commit d71ad5b
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions registry/client/transport/http_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@ var (
)

// ReadSeekCloser combines io.ReadSeeker with io.Closer.
type ReadSeekCloser interface {
io.ReadSeeker
io.Closer
}
//
// Deprecated: use [io.ReadSeekCloser].
type ReadSeekCloser = io.ReadSeekCloser

// NewHTTPReadSeeker handles reading from an HTTP endpoint using a GET
// request. When seeking and starting a read from a non-zero offset
// the a "Range" header will be added which sets the offset.
//
// TODO(dmcgowan): Move this into a separate utility package
func NewHTTPReadSeeker(ctx context.Context, client *http.Client, url string, errorHandler func(*http.Response) error) ReadSeekCloser {
return &httpReadSeeker{
func NewHTTPReadSeeker(ctx context.Context, client *http.Client, url string, errorHandler func(*http.Response) error) *HTTPReadSeeker {
return &HTTPReadSeeker{
ctx: ctx,
client: client,
url: url,
errorHandler: errorHandler,
}
}

type httpReadSeeker struct {
// HTTPReadSeeker implements an [io.ReadSeekCloser].
type HTTPReadSeeker struct {
ctx context.Context
client *http.Client
url string
Expand All @@ -63,7 +64,7 @@ type httpReadSeeker struct {
err error
}

func (hrs *httpReadSeeker) Read(p []byte) (n int, err error) {
func (hrs *HTTPReadSeeker) Read(p []byte) (n int, err error) {
if hrs.err != nil {
return 0, hrs.err
}
Expand Down Expand Up @@ -92,7 +93,7 @@ func (hrs *httpReadSeeker) Read(p []byte) (n int, err error) {
return n, err
}

func (hrs *httpReadSeeker) Seek(offset int64, whence int) (int64, error) {
func (hrs *HTTPReadSeeker) Seek(offset int64, whence int) (int64, error) {
if hrs.err != nil {
return 0, hrs.err
}
Expand Down Expand Up @@ -135,7 +136,7 @@ func (hrs *httpReadSeeker) Seek(offset int64, whence int) (int64, error) {
return hrs.seekOffset, err
}

func (hrs *httpReadSeeker) Close() error {
func (hrs *HTTPReadSeeker) Close() error {
if hrs.err != nil {
return hrs.err
}
Expand All @@ -152,7 +153,7 @@ func (hrs *httpReadSeeker) Close() error {
return nil
}

func (hrs *httpReadSeeker) reset() {
func (hrs *HTTPReadSeeker) reset() {
if hrs.err != nil {
return
}
Expand All @@ -162,7 +163,7 @@ func (hrs *httpReadSeeker) reset() {
}
}

func (hrs *httpReadSeeker) reader() (io.Reader, error) {
func (hrs *HTTPReadSeeker) reader() (io.Reader, error) {
if hrs.err != nil {
return nil, hrs.err
}
Expand Down

0 comments on commit d71ad5b

Please sign in to comment.