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 // WithFileName sets the filename of the File
func WithFileName(n string) FileOption { func WithFileName(name string) FileOption {
return func(f *File) { return func(f *File) {
f.Name = n f.Name = name
} }
} }
// WithFileDescription sets an optional file description of the File that will be // WithFileDescription sets an optional file description of the File that will be
// added as Content-Description part // added as Content-Description part
func WithFileDescription(d string) FileOption { func WithFileDescription(description string) FileOption {
return func(f *File) { 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. // 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 // 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 // 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) { return func(f *File) {
if e == EncodingQP { if encoding == EncodingQP {
return 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 // 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 // this to a specific type. For such situations this override method can
// be used // be used
func WithFileContentType(t ContentType) FileOption { func WithFileContentType(contentType ContentType) FileOption {
return func(f *File) { return func(f *File) {
f.ContentType = t f.ContentType = contentType
} }
} }
// setHeader sets header fields to a File // setHeader sets header fields to a File
func (f *File) setHeader(h Header, v string) { func (f *File) setHeader(header Header, value string) {
f.Header.Set(string(h), v) f.Header.Set(string(header), value)
} }
// getHeader return header fields of a File // getHeader return header fields of a File
func (f *File) getHeader(h Header) (string, bool) { func (f *File) getHeader(header Header) (string, bool) {
v := f.Header.Get(string(h)) v := f.Header.Get(string(header))
return v, v != "" return v, v != ""
} }

12
msg.go
View file

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

View file

@ -1251,7 +1251,7 @@ func TestMsg_SetBodyString(t *testing.T) {
} }
part := m.parts[0] part := m.parts[0]
res := bytes.Buffer{} 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) t.Errorf("WriteFunc of part failed: %s", err)
} }
if res.String() != tt.want { if res.String() != tt.want {
@ -1286,7 +1286,7 @@ func TestMsg_AddAlternativeString(t *testing.T) {
} }
apart := m.parts[1] apart := m.parts[1]
res := bytes.Buffer{} 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) t.Errorf("WriteFunc of part failed: %s", err)
} }
if res.String() != tt.want { if res.String() != tt.want {

View file

@ -114,7 +114,7 @@ func (mw *msgWriter) writeMsg(msg *Msg) {
} }
for _, part := range msg.parts { for _, part := range msg.parts {
if !part.del { if !part.isDeleted {
mw.writePart(part, msg.charset) 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 // writePart writes the corresponding part to the Msg body
func (mw *msgWriter) writePart(part *Part, charset Charset) { func (mw *msgWriter) writePart(part *Part, charset Charset) {
partCharset := part.cset partCharset := part.charset
if partCharset.String() == "" { if partCharset.String() == "" {
partCharset = charset partCharset = charset
} }
contentType := fmt.Sprintf("%s; charset=%s", part.ctype, partCharset) contentType := fmt.Sprintf("%s; charset=%s", part.contentType, partCharset)
contentTransferEnc := part.enc.String() contentTransferEnc := part.encoding.String()
if mw.depth == 0 { if mw.depth == 0 {
mw.writeHeader(HeaderContentType, contentType) mw.writeHeader(HeaderContentType, contentType)
mw.writeHeader(HeaderContentTransferEnc, contentTransferEnc) mw.writeHeader(HeaderContentTransferEnc, contentTransferEnc)
@ -262,14 +262,14 @@ func (mw *msgWriter) writePart(part *Part, charset Charset) {
} }
if mw.depth > 0 { if mw.depth > 0 {
mimeHeader := textproto.MIMEHeader{} mimeHeader := textproto.MIMEHeader{}
if part.desc != "" { if part.description != "" {
mimeHeader.Add(string(HeaderContentDescription), part.desc) mimeHeader.Add(string(HeaderContentDescription), part.description)
} }
mimeHeader.Add(string(HeaderContentType), contentType) mimeHeader.Add(string(HeaderContentType), contentType)
mimeHeader.Add(string(HeaderContentTransferEnc), contentTransferEnc) mimeHeader.Add(string(HeaderContentTransferEnc), contentTransferEnc)
mw.newPart(mimeHeader) 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 // 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 // Part is a part of the Msg
type Part struct { type Part struct {
ctype ContentType contentType ContentType
cset Charset charset Charset
desc string description string
enc Encoding encoding Encoding
del bool isDeleted bool
w func(io.Writer) (int64, error) writeFunc func(io.Writer) (int64, error)
} }
// GetContent executes the WriteFunc of the Part and returns the content as byte slice // GetContent executes the WriteFunc of the Part and returns the content as byte slice
func (p *Part) GetContent() ([]byte, error) { func (p *Part) GetContent() ([]byte, error) {
var b bytes.Buffer var b bytes.Buffer
if _, err := p.w(&b); err != nil { if _, err := p.writeFunc(&b); err != nil {
return nil, err return nil, err
} }
return b.Bytes(), nil return b.Bytes(), nil
@ -33,83 +33,83 @@ func (p *Part) GetContent() ([]byte, error) {
// GetCharset returns the currently set Charset of the Part // GetCharset returns the currently set Charset of the Part
func (p *Part) GetCharset() Charset { func (p *Part) GetCharset() Charset {
return p.cset return p.charset
} }
// GetContentType returns the currently set ContentType of the Part // GetContentType returns the currently set ContentType of the Part
func (p *Part) GetContentType() ContentType { func (p *Part) GetContentType() ContentType {
return p.ctype return p.contentType
} }
// GetEncoding returns the currently set Encoding of the Part // GetEncoding returns the currently set Encoding of the Part
func (p *Part) GetEncoding() Encoding { func (p *Part) GetEncoding() Encoding {
return p.enc return p.encoding
} }
// GetWriteFunc returns the currently set WriterFunc of the Part // GetWriteFunc returns the currently set WriterFunc of the Part
func (p *Part) GetWriteFunc() func(io.Writer) (int64, error) { 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 // GetDescription returns the currently set Content-Description of the Part
func (p *Part) GetDescription() string { func (p *Part) GetDescription() string {
return p.desc return p.description
} }
// SetContent overrides the content of the Part with the given string // SetContent overrides the content of the Part with the given string
func (p *Part) SetContent(c string) { func (p *Part) SetContent(content string) {
buf := bytes.NewBufferString(c) buffer := bytes.NewBufferString(content)
p.w = writeFuncFromBuffer(buf) p.writeFunc = writeFuncFromBuffer(buffer)
} }
// SetContentType overrides the ContentType of the Part // SetContentType overrides the ContentType of the Part
func (p *Part) SetContentType(c ContentType) { func (p *Part) SetContentType(contentType ContentType) {
p.ctype = c p.contentType = contentType
} }
// SetCharset overrides the Charset of the Part // SetCharset overrides the Charset of the Part
func (p *Part) SetCharset(c Charset) { func (p *Part) SetCharset(charset Charset) {
p.cset = c p.charset = charset
} }
// SetEncoding creates a new mime.WordEncoder based on the encoding setting of the message // SetEncoding creates a new mime.WordEncoder based on the encoding setting of the message
func (p *Part) SetEncoding(e Encoding) { func (p *Part) SetEncoding(encoding Encoding) {
p.enc = e p.encoding = encoding
} }
// SetDescription overrides the Content-Description of the Part // SetDescription overrides the Content-Description of the Part
func (p *Part) SetDescription(d string) { func (p *Part) SetDescription(description string) {
p.desc = d p.description = description
} }
// SetWriteFunc overrides the WriteFunc of the Part // SetWriteFunc overrides the WriteFunc of the Part
func (p *Part) SetWriteFunc(w func(io.Writer) (int64, error)) { func (p *Part) SetWriteFunc(writeFunc func(io.Writer) (int64, error)) {
p.w = w p.writeFunc = writeFunc
} }
// Delete removes the current part from the parts list of the Msg by setting the // 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() { func (p *Part) Delete() {
p.del = true p.isDeleted = true
} }
// WithPartCharset overrides the default Part charset // WithPartCharset overrides the default Part charset
func WithPartCharset(c Charset) PartOption { func WithPartCharset(charset Charset) PartOption {
return func(p *Part) { return func(p *Part) {
p.cset = c p.charset = charset
} }
} }
// WithPartEncoding overrides the default Part encoding // WithPartEncoding overrides the default Part encoding
func WithPartEncoding(e Encoding) PartOption { func WithPartEncoding(encoding Encoding) PartOption {
return func(p *Part) { return func(p *Part) {
p.enc = e p.encoding = encoding
} }
} }
// WithPartContentDescription overrides the default Part Content-Description // WithPartContentDescription overrides the default Part Content-Description
func WithPartContentDescription(d string) PartOption { func WithPartContentDescription(description string) PartOption {
return func(p *Part) { 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") t.Errorf("newPart() WithPartEncoding() failed: no part returned")
return 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, 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) 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, 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") t.Errorf("newPart() WithPartCharset() failed: no part returned")
return return
} }
if part.cset.String() != tt.want { if part.charset.String() != tt.want {
t.Errorf("newPart() WithPartCharset() failed: expected charset: %s, got: %s", 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") t.Errorf("newPart() WithPartContentDescription() failed: no part returned")
return return
} }
if part.desc != tt.desc { if part.description != tt.desc {
t.Errorf("newPart() WithPartContentDescription() failed: expected: %s, got: %s", 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) part.SetDescription(tt.desc)
if part.desc != tt.desc { if part.description != tt.desc {
t.Errorf("newPart() SetDescription() failed: expected: %s, got: %s", tt.desc, part.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) t.Errorf("failed: %s", err)
return return
} }
pl[0].w = func(io.Writer) (int64, error) { pl[0].writeFunc = func(io.Writer) (int64, error) {
return 0, fmt.Errorf("broken") return 0, fmt.Errorf("broken")
} }
_, err = pl[0].GetContent() _, 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) t.Errorf("Part.GetDescription failed. Expected empty description but got: %s", pd)
} }
pl[0].SetDescription(d) pl[0].SetDescription(d)
if pl[0].desc != d { if pl[0].description != d {
t.Errorf("Part.SetDescription failed. Expected desc to be: %s, got: %s", d, pd) t.Errorf("Part.SetDescription failed. Expected description to be: %s, got: %s", d, pd)
} }
pd = pl[0].GetDescription() pd = pl[0].GetDescription()
if pd != d { if pd != d {
@ -334,8 +334,8 @@ func TestPart_Delete(t *testing.T) {
return return
} }
pl[0].Delete() pl[0].Delete()
if !pl[0].del { if !pl[0].isDeleted {
t.Errorf("Delete failed. Expected: %t, got: %t", true, pl[0].del) t.Errorf("Delete failed. Expected: %t, got: %t", true, pl[0].isDeleted)
} }
} }