diff --git a/file.go b/file.go index 5a4921c..1ad2d5a 100644 --- a/file.go +++ b/file.go @@ -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 != "" } diff --git a/msg.go b/msg.go index eeb01cf..ff3f018 100644 --- a/msg.go +++ b/msg.go @@ -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 diff --git a/msg_test.go b/msg_test.go index af668f3..7bcf06b 100644 --- a/msg_test.go +++ b/msg_test.go @@ -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 { diff --git a/msgwriter.go b/msgwriter.go index e584a99..08073db 100644 --- a/msgwriter.go +++ b/msgwriter.go @@ -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 diff --git a/part.go b/part.go index 78e8aed..7c76b7d 100644 --- a/part.go +++ b/part.go @@ -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 } } diff --git a/part_test.go b/part_test.go index 276fc69..a196986 100644 --- a/part_test.go +++ b/part_test.go @@ -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) } }