Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing Msg part deletion #108

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,13 @@ func (m *Msg) encodeString(s string) string {

// hasAlt returns true if the Msg has more than one part
func (m *Msg) hasAlt() bool {
return len(m.parts) > 1
c := 0
for _, p := range m.parts {
if !p.del {
c++
}
}
return c > 1
}

// hasMixed returns true if the Msg has mixed parts
Expand Down
4 changes: 3 additions & 1 deletion msgwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ func (mw *msgWriter) writeMsg(m *Msg) {
}

for _, p := range m.parts {
mw.writePart(p, m.charset)
if !p.del {
mw.writePart(p, m.charset)
}
}
if m.hasAlt() {
mw.stopMP()
Expand Down
7 changes: 7 additions & 0 deletions part.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type PartOption func(*Part)
type Part struct {
ctype ContentType
enc Encoding
del bool
w func(io.Writer) (int64, error)
}

Expand Down Expand Up @@ -64,6 +65,12 @@ func (p *Part) SetWriteFunc(w func(io.Writer) (int64, error)) {
p.w = w
}

// 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
func (p *Part) Delete() {
p.del = true
}

// WithPartEncoding overrides the default Part encoding
func WithPartEncoding(e Encoding) PartOption {
return func(p *Part) {
Expand Down
16 changes: 16 additions & 0 deletions part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ func TestPart_SetContent(t *testing.T) {
}
}

// TestPart_Delete tests Part.Delete
func TestPart_Delete(t *testing.T) {
c := "This is a test with ümläutß"
m := NewMsg()
m.SetBodyString(TypeTextPlain, c)
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
pl[0].Delete()
if !pl[0].del {
t.Errorf("Delete failed. Expected: %t, got: %t", true, pl[0].del)
}
}

// getPartList is a helper function
func getPartList(m *Msg) ([]*Part, error) {
pl := m.GetParts()
Expand Down