Refactor variable names for readability in reader.go

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.
This commit is contained in:
Winni Neessen 2024-02-26 00:56:29 +01:00
parent f18335fa1d
commit b2f735854d
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
3 changed files with 15 additions and 15 deletions

4
msg.go
View file

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

View file

@ -10,8 +10,8 @@ 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)]
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
}
@ -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 }

View file

@ -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)
@ -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) {