Refactor variable names for code readability

Updated variable names in multiple files to enhance code readability and maintainability by replacing abbreviations with full descriptive names. This ensures adherence to the best practices of naming conventions in Go.
This commit is contained in:
Winni Neessen 2024-02-27 11:21:28 +01:00
parent 40ea4fbfb3
commit 19a3cf61ed
Signed by: wneessen
GPG key ID: 385AC9889632126E
6 changed files with 78 additions and 78 deletions

26
file.go
View file

@ -23,17 +23,17 @@ type File struct {
}
// WithFileName sets the filename of the File
func WithFileName(n string) FileOption {
func WithFileName(name string) FileOption {
return func(f *File) {
f.Name = n
f.Name = name
}
}
// WithFileDescription sets an optional file description of the File that will be
// added as Content-Description part
func WithFileDescription(d string) FileOption {
func WithFileDescription(description string) FileOption {
return func(f *File) {
f.Desc = d
f.Desc = description
}
}
@ -41,12 +41,12 @@ func WithFileDescription(d string) FileOption {
// Base64 encoding but there might be exceptions, where this might come handy.
// Please note that quoted-printable should never be used for attachments/embeds. If this
// is provided as argument, the function will automatically override back to Base64
func WithFileEncoding(e Encoding) FileOption {
func WithFileEncoding(encoding Encoding) FileOption {
return func(f *File) {
if e == EncodingQP {
if encoding == EncodingQP {
return
}
f.Enc = e
f.Enc = encoding
}
}
@ -56,19 +56,19 @@ func WithFileEncoding(e Encoding) FileOption {
// could not be guessed. In some cases, however, it might be needed to force
// this to a specific type. For such situations this override method can
// be used
func WithFileContentType(t ContentType) FileOption {
func WithFileContentType(contentType ContentType) FileOption {
return func(f *File) {
f.ContentType = t
f.ContentType = contentType
}
}
// setHeader sets header fields to a File
func (f *File) setHeader(h Header, v string) {
f.Header.Set(string(h), v)
func (f *File) setHeader(header Header, value string) {
f.Header.Set(string(header), value)
}
// getHeader return header fields of a File
func (f *File) getHeader(h Header) (string, bool) {
v := f.Header.Get(string(h))
func (f *File) getHeader(header Header) (string, bool) {
v := f.Header.Get(string(header))
return v, v != ""
}

12
msg.go
View file

@ -723,7 +723,7 @@ func (m *Msg) SetBodyWriter(
opts ...PartOption,
) {
p := m.newPart(contentType, opts...)
p.w = writeFunc
p.writeFunc = writeFunc
m.parts = []*Part{p}
}
@ -770,7 +770,7 @@ func (m *Msg) AddAlternativeWriter(
opts ...PartOption,
) {
part := m.newPart(contentType, opts...)
part.w = writeFunc
part.writeFunc = writeFunc
m.parts = append(m.parts, part)
}
@ -1142,7 +1142,7 @@ func (m *Msg) encodeString(str string) string {
func (m *Msg) hasAlt() bool {
count := 0
for _, part := range m.parts {
if !part.del {
if !part.isDeleted {
count++
}
}
@ -1167,9 +1167,9 @@ func (m *Msg) hasPGPType() bool {
// newPart returns a new Part for the Msg
func (m *Msg) newPart(contentType ContentType, opts ...PartOption) *Part {
p := &Part{
ctype: contentType,
cset: m.charset,
enc: m.encoding,
contentType: contentType,
charset: m.charset,
encoding: m.encoding,
}
// Override defaults with optionally provided MsgOption functions

View file

@ -1251,7 +1251,7 @@ func TestMsg_SetBodyString(t *testing.T) {
}
part := m.parts[0]
res := bytes.Buffer{}
if _, err := part.w(&res); err != nil && !tt.sf {
if _, err := part.writeFunc(&res); err != nil && !tt.sf {
t.Errorf("WriteFunc of part failed: %s", err)
}
if res.String() != tt.want {
@ -1286,7 +1286,7 @@ func TestMsg_AddAlternativeString(t *testing.T) {
}
apart := m.parts[1]
res := bytes.Buffer{}
if _, err := apart.w(&res); err != nil && !tt.sf {
if _, err := apart.writeFunc(&res); err != nil && !tt.sf {
t.Errorf("WriteFunc of part failed: %s", err)
}
if res.String() != tt.want {

View file

@ -114,7 +114,7 @@ func (mw *msgWriter) writeMsg(msg *Msg) {
}
for _, part := range msg.parts {
if !part.del {
if !part.isDeleted {
mw.writePart(part, msg.charset)
}
}
@ -249,12 +249,12 @@ func (mw *msgWriter) newPart(header map[string][]string) {
// writePart writes the corresponding part to the Msg body
func (mw *msgWriter) writePart(part *Part, charset Charset) {
partCharset := part.cset
partCharset := part.charset
if partCharset.String() == "" {
partCharset = charset
}
contentType := fmt.Sprintf("%s; charset=%s", part.ctype, partCharset)
contentTransferEnc := part.enc.String()
contentType := fmt.Sprintf("%s; charset=%s", part.contentType, partCharset)
contentTransferEnc := part.encoding.String()
if mw.depth == 0 {
mw.writeHeader(HeaderContentType, contentType)
mw.writeHeader(HeaderContentTransferEnc, contentTransferEnc)
@ -262,14 +262,14 @@ func (mw *msgWriter) writePart(part *Part, charset Charset) {
}
if mw.depth > 0 {
mimeHeader := textproto.MIMEHeader{}
if part.desc != "" {
mimeHeader.Add(string(HeaderContentDescription), part.desc)
if part.description != "" {
mimeHeader.Add(string(HeaderContentDescription), part.description)
}
mimeHeader.Add(string(HeaderContentType), contentType)
mimeHeader.Add(string(HeaderContentTransferEnc), contentTransferEnc)
mw.newPart(mimeHeader)
}
mw.writeBody(part.w, part.enc)
mw.writeBody(part.writeFunc, part.encoding)
}
// writeString writes a string into the msgWriter's io.Writer interface

66
part.go
View file

@ -14,18 +14,18 @@ type PartOption func(*Part)
// Part is a part of the Msg
type Part struct {
ctype ContentType
cset Charset
desc string
enc Encoding
del bool
w func(io.Writer) (int64, error)
contentType ContentType
charset Charset
description string
encoding Encoding
isDeleted bool
writeFunc func(io.Writer) (int64, error)
}
// GetContent executes the WriteFunc of the Part and returns the content as byte slice
func (p *Part) GetContent() ([]byte, error) {
var b bytes.Buffer
if _, err := p.w(&b); err != nil {
if _, err := p.writeFunc(&b); err != nil {
return nil, err
}
return b.Bytes(), nil
@ -33,83 +33,83 @@ func (p *Part) GetContent() ([]byte, error) {
// GetCharset returns the currently set Charset of the Part
func (p *Part) GetCharset() Charset {
return p.cset
return p.charset
}
// GetContentType returns the currently set ContentType of the Part
func (p *Part) GetContentType() ContentType {
return p.ctype
return p.contentType
}
// GetEncoding returns the currently set Encoding of the Part
func (p *Part) GetEncoding() Encoding {
return p.enc
return p.encoding
}
// GetWriteFunc returns the currently set WriterFunc of the Part
func (p *Part) GetWriteFunc() func(io.Writer) (int64, error) {
return p.w
return p.writeFunc
}
// GetDescription returns the currently set Content-Description of the Part
func (p *Part) GetDescription() string {
return p.desc
return p.description
}
// SetContent overrides the content of the Part with the given string
func (p *Part) SetContent(c string) {
buf := bytes.NewBufferString(c)
p.w = writeFuncFromBuffer(buf)
func (p *Part) SetContent(content string) {
buffer := bytes.NewBufferString(content)
p.writeFunc = writeFuncFromBuffer(buffer)
}
// SetContentType overrides the ContentType of the Part
func (p *Part) SetContentType(c ContentType) {
p.ctype = c
func (p *Part) SetContentType(contentType ContentType) {
p.contentType = contentType
}
// SetCharset overrides the Charset of the Part
func (p *Part) SetCharset(c Charset) {
p.cset = c
func (p *Part) SetCharset(charset Charset) {
p.charset = charset
}
// SetEncoding creates a new mime.WordEncoder based on the encoding setting of the message
func (p *Part) SetEncoding(e Encoding) {
p.enc = e
func (p *Part) SetEncoding(encoding Encoding) {
p.encoding = encoding
}
// SetDescription overrides the Content-Description of the Part
func (p *Part) SetDescription(d string) {
p.desc = d
func (p *Part) SetDescription(description string) {
p.description = description
}
// SetWriteFunc overrides the WriteFunc of the Part
func (p *Part) SetWriteFunc(w func(io.Writer) (int64, error)) {
p.w = w
func (p *Part) SetWriteFunc(writeFunc func(io.Writer) (int64, error)) {
p.writeFunc = writeFunc
}
// Delete removes the current part from the parts list of the Msg by setting the
// del flag to true. The msgWriter will skip it then
// isDeleted flag to true. The msgWriter will skip it then
func (p *Part) Delete() {
p.del = true
p.isDeleted = true
}
// WithPartCharset overrides the default Part charset
func WithPartCharset(c Charset) PartOption {
func WithPartCharset(charset Charset) PartOption {
return func(p *Part) {
p.cset = c
p.charset = charset
}
}
// WithPartEncoding overrides the default Part encoding
func WithPartEncoding(e Encoding) PartOption {
func WithPartEncoding(encoding Encoding) PartOption {
return func(p *Part) {
p.enc = e
p.encoding = encoding
}
}
// WithPartContentDescription overrides the default Part Content-Description
func WithPartContentDescription(d string) PartOption {
func WithPartContentDescription(description string) PartOption {
return func(p *Part) {
p.desc = d
p.description = description
}
}

View file

@ -31,15 +31,15 @@ func TestPartEncoding(t *testing.T) {
t.Errorf("newPart() WithPartEncoding() failed: no part returned")
return
}
if part.enc.String() != tt.want {
if part.encoding.String() != tt.want {
t.Errorf("newPart() WithPartEncoding() failed: expected encoding: %s, got: %s", tt.want,
part.enc.String())
part.encoding.String())
}
part.enc = ""
part.encoding = ""
part.SetEncoding(tt.enc)
if part.enc.String() != tt.want {
if part.encoding.String() != tt.want {
t.Errorf("newPart() SetEncoding() failed: expected encoding: %s, got: %s", tt.want,
part.enc.String())
part.encoding.String())
}
})
}
@ -64,9 +64,9 @@ func TestWithPartCharset(t *testing.T) {
t.Errorf("newPart() WithPartCharset() failed: no part returned")
return
}
if part.cset.String() != tt.want {
if part.charset.String() != tt.want {
t.Errorf("newPart() WithPartCharset() failed: expected charset: %s, got: %s",
tt.want, part.cset.String())
tt.want, part.charset.String())
}
})
}
@ -89,14 +89,14 @@ func TestPart_WithPartContentDescription(t *testing.T) {
t.Errorf("newPart() WithPartContentDescription() failed: no part returned")
return
}
if part.desc != tt.desc {
if part.description != tt.desc {
t.Errorf("newPart() WithPartContentDescription() failed: expected: %s, got: %s", tt.desc,
part.desc)
part.description)
}
part.desc = ""
part.description = ""
part.SetDescription(tt.desc)
if part.desc != tt.desc {
t.Errorf("newPart() SetDescription() failed: expected: %s, got: %s", tt.desc, part.desc)
if part.description != tt.desc {
t.Errorf("newPart() SetDescription() failed: expected: %s, got: %s", tt.desc, part.description)
}
})
}
@ -236,7 +236,7 @@ func TestPart_GetContentBroken(t *testing.T) {
t.Errorf("failed: %s", err)
return
}
pl[0].w = func(io.Writer) (int64, error) {
pl[0].writeFunc = func(io.Writer) (int64, error) {
return 0, fmt.Errorf("broken")
}
_, err = pl[0].GetContent()
@ -314,8 +314,8 @@ func TestPart_SetDescription(t *testing.T) {
t.Errorf("Part.GetDescription failed. Expected empty description but got: %s", pd)
}
pl[0].SetDescription(d)
if pl[0].desc != d {
t.Errorf("Part.SetDescription failed. Expected desc to be: %s, got: %s", d, pd)
if pl[0].description != d {
t.Errorf("Part.SetDescription failed. Expected description to be: %s, got: %s", d, pd)
}
pd = pl[0].GetDescription()
if pd != d {
@ -334,8 +334,8 @@ func TestPart_Delete(t *testing.T) {
return
}
pl[0].Delete()
if !pl[0].del {
t.Errorf("Delete failed. Expected: %t, got: %t", true, pl[0].del)
if !pl[0].isDeleted {
t.Errorf("Delete failed. Expected: %t, got: %t", true, pl[0].isDeleted)
}
}