Skip to content

Commit

Permalink
Merge pull request #1 from visiperf/feat/tags
Browse files Browse the repository at this point in the history
Add tags on email
  • Loading branch information
romain-jeannoutot authored Aug 31, 2022
2 parents 956ab16 + 765603b commit 2482455
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
17 changes: 16 additions & 1 deletion email.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Email struct {
subject string
replyTo Recipient
attachments []Attachment
tags []Tag
}

func (e *Email) Validate() error {
Expand Down Expand Up @@ -85,11 +86,17 @@ func (e *Email) Validate() error {
}
}

for _, t := range e.tags {
if t.IsEmpty() {
return ErrEmptyTag
}
}

return nil
}

func (e Email) copy() *Email {
return &Email{e.from, e.to, e.cc, e.bcc, e.body, e.subject, e.replyTo, e.attachments}
return &Email{e.from, e.to, e.cc, e.bcc, e.body, e.subject, e.replyTo, e.attachments, e.tags}
}

func (e Email) MarshalJSON() ([]byte, error) {
Expand All @@ -111,6 +118,7 @@ func (e Email) MarshalJSON() ([]byte, error) {
Subject string `json:"subject,omitempty"`
ReplyTo Sender `json:"replyTo"`
Attachments []Attachment `json:"attachment,omitempty"`
Tags []Tag `json:"tags,omitempty"`
}{
Sender: e.from,
To: e.to,
Expand All @@ -119,6 +127,7 @@ func (e Email) MarshalJSON() ([]byte, error) {
Subject: e.subject,
ReplyTo: e.replyTo,
Attachments: e.attachments,
Tags: e.tags,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -190,6 +199,12 @@ func (b *EmailBuilder) AppendAttachment(attachment Attachment) *EmailBuilder {
return b
}

func (b *EmailBuilder) AppendTag(tag Tag) *EmailBuilder {
b.email.tags = append(b.email.tags, tag)

return b
}

func (b *EmailBuilder) Build() *Email {
return b.email
}
Expand Down
12 changes: 12 additions & 0 deletions email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ func TestEmaiValidate(t *testing.T) {
return e
},
err: ErrSubjectRequired,
}, {
name: "empty tag",
email: func() *Email {
e := email.copy()

e.tags = []Tag{
Tag(""),
}

return e
},
err: ErrEmptyTag,
}}

for _, test := range tests {
Expand Down
17 changes: 17 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package visimail

import "errors"

var (
ErrEmptyTag = errors.New("tag is empty")
)

type Tag string

func (t Tag) IsEmpty() bool {
return t == ""
}

func (t Tag) String() string {
return string(t)
}

0 comments on commit 2482455

Please sign in to comment.