-
Notifications
You must be signed in to change notification settings - Fork 146
/
recipients.go
42 lines (35 loc) · 803 Bytes
/
recipients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package mailgun
import "fmt"
import "strings"
type Recipient struct {
Name string `json:"-"`
Email string `json:"-"`
}
func (r Recipient) String() string {
if r.Name != "" {
return fmt.Sprintf("%s <%s>", r.Name, r.Email)
}
return r.Email
}
// MarshalText satisfies TextMarshaler
func (r Recipient) MarshalText() ([]byte, error) {
return []byte(r.String()), nil
}
// UnmarshalText satisfies TextUnmarshaler
func (r *Recipient) UnmarshalText(text []byte) error {
s := string(text)
if s[len(s)-1:] != ">" {
*r = Recipient{Email: s}
return nil
}
i := strings.Index(s, "<")
// at least 1 char followed by a space
if i < 2 {
return fmt.Errorf("malformed recipient string '%s'", s)
}
*r = Recipient{
Name: strings.TrimSpace(s[:i]),
Email: s[i+1 : len(s)-1],
}
return nil
}