Skip to content

Commit

Permalink
Refactor variable names for readability in reader.go
Browse files Browse the repository at this point in the history
Variable names in reader.go have been changed to be more expressive for improved readability. 'buf' and 'off' were renamed to 'buffer' and 'offset' respectively throughout the file. The changes also include corresponding adjustments in code comments and related test cases, contributing to consistent and understandable code.
  • Loading branch information
wneessen committed Feb 25, 2024
1 parent f18335f commit b2f7358
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ func (m *Msg) NewReader() *Reader {
if err != nil {
reader.err = fmt.Errorf("failed to write Msg to Reader buffer: %w", err)
}
reader.buf = buffer.Bytes()
reader.buffer = buffer.Bytes()
return reader
}

Expand All @@ -1097,7 +1097,7 @@ func (m *Msg) UpdateReader(reader *Reader) {
buffer := bytes.Buffer{}
_, err := m.Write(&buffer)
reader.Reset()
reader.buf = buffer.Bytes()
reader.buffer = buffer.Bytes()
reader.err = err
}

Expand Down
22 changes: 11 additions & 11 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

// Reader is a type that implements the io.Reader interface for a Msg
type Reader struct {
buf []byte // contents are the bytes buf[off : len(buf)]
off int // read at &buf[off], write at &buf[len(buf)]
err error // initialization error
buffer []byte // contents are the bytes buffer[offset : len(buffer)]
offset int // read at &buffer[offset], write at &buffer[len(buffer)]
err error // initialization error
}

// Error returns an error if the Reader err field is not nil
Expand All @@ -21,28 +21,28 @@ func (r *Reader) Error() error {
}

// Read reads the length of p of the Msg buffer to satisfy the io.Reader interface
func (r *Reader) Read(p []byte) (n int, err error) {
func (r *Reader) Read(payload []byte) (n int, err error) {
if r.err != nil {
return 0, r.err
}
if r.empty() || r.buf == nil {
if r.empty() || r.buffer == nil {
r.Reset()
if len(p) == 0 {
if len(payload) == 0 {
return 0, nil
}
return 0, io.EOF
}
n = copy(p, r.buf[r.off:])
r.off += n
n = copy(payload, r.buffer[r.offset:])
r.offset += n
return n, err
}

// Reset resets the Reader buffer to be empty, but it retains the underlying storage
// for use by future writes.
func (r *Reader) Reset() {
r.buf = r.buf[:0]
r.off = 0
r.buffer = r.buffer[:0]
r.offset = 0
}

// empty reports whether the unread portion of the Reader buffer is empty.
func (r *Reader) empty() bool { return len(r.buf) <= r.off }
func (r *Reader) empty() bool { return len(r.buffer) <= r.offset }
4 changes: 2 additions & 2 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestReader_Read_error(t *testing.T) {

// TestReader_Read_empty tests the Reader.Read method with an empty buffer
func TestReader_Read_empty(t *testing.T) {
r := Reader{buf: []byte{}}
r := Reader{buffer: []byte{}}
p := make([]byte, 1)
p[0] = 'a'
_, err := r.Read(p)
Expand All @@ -76,7 +76,7 @@ func TestReader_Read_empty(t *testing.T) {

// TestReader_Read_nil tests the Reader.Read method with a nil buffer
func TestReader_Read_nil(t *testing.T) {
r := Reader{buf: nil, off: -10}
r := Reader{buffer: nil, offset: -10}
p := make([]byte, 0)
_, err := r.Read(p)
if err != nil && !errors.Is(err, io.EOF) {
Expand Down

0 comments on commit b2f7358

Please sign in to comment.