From c322ffd8cbce04cbbea0eedf57f52ca92891ddd2 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Fri, 10 Jan 2025 23:52:43 +0000 Subject: [PATCH] Rework FromAddress to prevent all the nesting --- fromaddress.go | 15 +++++++++++---- fromaddress_test.go | 8 ++++---- mail.go | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/fromaddress.go b/fromaddress.go index fbb695e..3510418 100644 --- a/fromaddress.go +++ b/fromaddress.go @@ -5,17 +5,24 @@ import ( "github.com/emersion/go-message/mail" ) -type FromAddress struct { - *mail.Address -} +type FromAddress mail.Address var _ encoding.TextUnmarshaler = &FromAddress{} +func (f FromAddress) String() string { + return f.toMailAddress().String() +} + func (f *FromAddress) UnmarshalText(b []byte) error { address, err := mail.ParseAddress(string(b)) if err != nil { return err } - f.Address = address + *f = FromAddress(*address) return nil } + +func (f FromAddress) toMailAddress() *mail.Address { + m := mail.Address(f) + return &m +} diff --git a/fromaddress_test.go b/fromaddress_test.go index 7ad0263..405ac17 100644 --- a/fromaddress_test.go +++ b/fromaddress_test.go @@ -11,13 +11,13 @@ func TestFromAddress_UnmarshalText(t *testing.T) { t.Run("json", func(t *testing.T) { var fromAddress FromAddress assert.NoError(t, json.Unmarshal([]byte(`"Jane Doe "`), &fromAddress)) - assert.Equal(t, "Jane Doe", fromAddress.Address.Name) - assert.Equal(t, "jane@example.com", fromAddress.Address.Address) + assert.Equal(t, "Jane Doe", fromAddress.Name) + assert.Equal(t, "jane@example.com", fromAddress.Address) }) t.Run("yaml", func(t *testing.T) { var fromAddress FromAddress assert.NoError(t, yaml.Unmarshal([]byte(`"Jane Doe "`), &fromAddress)) - assert.Equal(t, "Jane Doe", fromAddress.Address.Name) - assert.Equal(t, "jane@example.com", fromAddress.Address.Address) + assert.Equal(t, "Jane Doe", fromAddress.Name) + assert.Equal(t, "jane@example.com", fromAddress.Address) }) } diff --git a/mail.go b/mail.go index 6f3a6be..83e1be0 100644 --- a/mail.go +++ b/mail.go @@ -77,7 +77,7 @@ func (m *Mail) SendMail(subject string, to []*mail.Address, htmlBody, textBody i var h mail.Header h.SetDate(time.Now()) h.SetSubject(subject) - h.SetAddressList("From", []*mail.Address{m.From.Address}) + h.SetAddressList("From", []*mail.Address{m.From.toMailAddress()}) h.SetAddressList("To", to) h.Set("Content-Type", "multipart/alternative")