From 13385d50eaf61364660fd8ecf4020ad395e40e14 Mon Sep 17 00:00:00 2001 From: Alexandre Cesaro Date: Tue, 14 Jul 2015 22:26:22 +0200 Subject: [PATCH] Added method Reset() to Message Fixes #31 --- message.go | 11 +++++++++++ message_test.go | 23 ++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/message.go b/message.go index d701bf8..5fec2b8 100644 --- a/message.go +++ b/message.go @@ -52,6 +52,17 @@ func NewMessage(settings ...MessageSetting) *Message { 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) { for _, s := range settings { s(msg) diff --git a/message_test.go b/message_test.go index db851ec..7e54084 100644 --- a/message_test.go +++ b/message_test.go @@ -411,6 +411,22 @@ func TestFullMessage(t *testing.T) { } 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) { @@ -588,12 +604,16 @@ func testFile(name string) *File { } func BenchmarkFull(b *testing.B) { + buf := new(bytes.Buffer) emptyFunc := func(from string, to []string, msg io.WriterTo) error { + msg.WriteTo(buf) + buf.Reset() return nil } + msg := NewMessage() + b.ResetTimer() for n := 0; n < b.N; n++ { - msg := NewMessage() msg.SetAddressHeader("From", "from@example.com", "SeƱor From") msg.SetHeaders(map[string][]string{ "To": {"to@example.com"}, @@ -609,5 +629,6 @@ func BenchmarkFull(b *testing.B) { if err := Send(SendFunc(emptyFunc), msg); err != nil { panic(err) } + msg.Reset() } }