From 06ae7bc0b2c73234e12f0152d6a2db3a2f3eebdd Mon Sep 17 00:00:00 2001 From: Spencer Connaughton Date: Mon, 19 Feb 2024 17:10:17 -0500 Subject: [PATCH] Use constants for disposition. --- examples/helpers/mail/example.go | 5 ++--- helpers/mail/mail_v3.go | 21 ++++++++++++++------- helpers/mail/mail_v3_test.go | 2 +- use-cases/attachments-with-mailer-helper.md | 8 ++++---- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/examples/helpers/mail/example.go b/examples/helpers/mail/example.go index 53c68dbd..a7cda206 100644 --- a/examples/helpers/mail/example.go +++ b/examples/helpers/mail/example.go @@ -6,7 +6,6 @@ import ( "fmt" "os" - "github.com/sendgrid/sendgrid-go" "github.com/sendgrid/sendgrid-go/helpers/mail" ) @@ -102,14 +101,14 @@ func kitchenSink() []byte { a.SetContent("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") a.SetType("application/pdf") a.SetFilename("balance_001.pdf") - a.SetDisposition("attachment") + a.SetDisposition(mail.DispositionAttachment) m.AddAttachment(a) a2 := mail.NewAttachment() a2.SetContent("BwdW") a2.SetType("image/png") a2.SetFilename("banner.png") - a2.SetDisposition("inline") + a2.SetDisposition(mail.DispositionInline) // Content-ID header is included when the attachment disposition is set to "inline" a2.SetContentID("Banner") m.AddAttachment(a2) diff --git a/helpers/mail/mail_v3.go b/helpers/mail/mail_v3.go index 6d7ac6b4..7cbb3701 100644 --- a/helpers/mail/mail_v3.go +++ b/helpers/mail/mail_v3.go @@ -66,14 +66,21 @@ type Content struct { Value string `json:"value,omitempty"` } +type disposition string + +const ( + DispositionInline disposition = "inline" + DispositionAttachment disposition = "attachment" +) + // Attachment holds attachement information type Attachment struct { - Content string `json:"content,omitempty"` - Type string `json:"type,omitempty"` - Name string `json:"name,omitempty"` - Filename string `json:"filename,omitempty"` - Disposition string `json:"disposition,omitempty"` - ContentID string `json:"content_id,omitempty"` + Content string `json:"content,omitempty"` + Type string `json:"type,omitempty"` + Name string `json:"name,omitempty"` + Filename string `json:"filename,omitempty"` + Disposition disposition `json:"disposition,omitempty"` + ContentID string `json:"content_id,omitempty"` } // Asm contains Grpip Id and int array of groups ID @@ -393,7 +400,7 @@ func (a *Attachment) SetFilename(filename string) *Attachment { } // SetDisposition ... -func (a *Attachment) SetDisposition(disposition string) *Attachment { +func (a *Attachment) SetDisposition(disposition disposition) *Attachment { a.Disposition = disposition return a } diff --git a/helpers/mail/mail_v3_test.go b/helpers/mail/mail_v3_test.go index 28af9192..40139ca2 100644 --- a/helpers/mail/mail_v3_test.go +++ b/helpers/mail/mail_v3_test.go @@ -406,7 +406,7 @@ func TestV3AttachmentSetContentID(t *testing.T) { } func TestV3AttachmentSetDisposition(t *testing.T) { - disposition := "inline" + disposition := DispositionInline a := NewAttachment().SetDisposition(disposition) assert.Equal(t, disposition, a.Disposition, fmt.Sprintf("Disposition should be %s, got %s", disposition, a.Disposition)) diff --git a/use-cases/attachments-with-mailer-helper.md b/use-cases/attachments-with-mailer-helper.md index a6da7e5c..6f272c85 100644 --- a/use-cases/attachments-with-mailer-helper.md +++ b/use-cases/attachments-with-mailer-helper.md @@ -12,7 +12,7 @@ import ( "github.com/sendgrid/sendgrid-go" "github.com/sendgrid/sendgrid-go/helpers/mail" ) - +[]() func main() { // create new *SGMailV3 m := mail.NewV3Mail() @@ -42,7 +42,7 @@ func main() { a_txt.SetContent(encoded) a_txt.SetType("text/plain") a_txt.SetFilename("testing.txt") - a_txt.SetDisposition("attachment") + a_txt.SetDisposition(mail.DispositionAttachment) // read/attach .pdf file a_pdf := mail.NewAttachment() @@ -54,7 +54,7 @@ func main() { a_pdf.SetContent(encoded) a_pdf.SetType("application/pdf") a_pdf.SetFilename("testing.pdf") - a_pdf.SetDisposition("attachment") + a_pdf.SetDisposition(mail.DispositionAttachment) // read/attach inline .jpg file a_jpg := mail.NewAttachment() @@ -66,7 +66,7 @@ func main() { a_jpg.SetContent(encoded) a_jpg.SetType("image/jpeg") a_jpg.SetFilename("testing.jpg") - a_jpg.SetDisposition("inline") + a_jpg.SetDisposition(mail.DispositionInline) a_jpg.SetContentID("Test Attachment") // add `a_txt`, `a_pdf` and `a_jpg` to `m`