Added method Reset() to Message

Fixes #31
This commit is contained in:
Alexandre Cesaro 2015-07-14 22:26:22 +02:00
parent cab7c5adfb
commit 13385d50ea
2 changed files with 33 additions and 1 deletions

View File

@ -52,6 +52,17 @@ func NewMessage(settings ...MessageSetting) *Message {
return msg return msg
} }
// Reset resets the message so it can be reused. The message keeps its previous
// settings so it is in the same state that after a call to NewMessage.
func (msg *Message) Reset() {
for k := range msg.header {
delete(msg.header, k)
}
msg.parts = nil
msg.attachments = nil
msg.embedded = nil
}
func (msg *Message) applySettings(settings []MessageSetting) { func (msg *Message) applySettings(settings []MessageSetting) {
for _, s := range settings { for _, s := range settings {
s(msg) s(msg)

View File

@ -411,6 +411,22 @@ func TestFullMessage(t *testing.T) {
} }
testMessage(t, msg, 3, want) testMessage(t, msg, 3, want)
want = &message{
from: "from@example.com",
to: []string{"to@example.com"},
content: "From: from@example.com\r\n" +
"To: to@example.com\r\n" +
"Content-Type: text/plain; charset=UTF-8\r\n" +
"Content-Transfer-Encoding: quoted-printable\r\n" +
"\r\n" +
"Test reset",
}
msg.Reset()
msg.SetHeader("From", "from@example.com")
msg.SetHeader("To", "to@example.com")
msg.SetBody("text/plain", "Test reset")
testMessage(t, msg, 0, want)
} }
func TestQpLineLength(t *testing.T) { func TestQpLineLength(t *testing.T) {
@ -588,12 +604,16 @@ func testFile(name string) *File {
} }
func BenchmarkFull(b *testing.B) { func BenchmarkFull(b *testing.B) {
buf := new(bytes.Buffer)
emptyFunc := func(from string, to []string, msg io.WriterTo) error { emptyFunc := func(from string, to []string, msg io.WriterTo) error {
msg.WriteTo(buf)
buf.Reset()
return nil return nil
} }
for n := 0; n < b.N; n++ {
msg := NewMessage() msg := NewMessage()
b.ResetTimer()
for n := 0; n < b.N; n++ {
msg.SetAddressHeader("From", "from@example.com", "Señor From") msg.SetAddressHeader("From", "from@example.com", "Señor From")
msg.SetHeaders(map[string][]string{ msg.SetHeaders(map[string][]string{
"To": {"to@example.com"}, "To": {"to@example.com"},
@ -609,5 +629,6 @@ func BenchmarkFull(b *testing.B) {
if err := Send(SendFunc(emptyFunc), msg); err != nil { if err := Send(SendFunc(emptyFunc), msg); err != nil {
panic(err) panic(err)
} }
msg.Reset()
} }
} }