Skip to content

Commit

Permalink
Add recipient struct with marshalling/unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
c2h5oh committed Mar 17, 2017
1 parent 78bfff3 commit 98daa62
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
23 changes: 5 additions & 18 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
)

type EventResponse struct {
type eventResponse struct {
Events []Event `json:"items"`
Paging
}
Expand Down Expand Up @@ -36,7 +36,7 @@ type Event struct {
// Message
// TODO: unify message types
Message *EventMessage
Recipient *string `json:"recipient,omitempty"`
Recipient *Recipient `json:"recipient,omitempty"`
Routes []Route `json:"routes,omitempty"`
Storage *Storage `json:"storage,omitempty"`
UserVariables map[string]map[string]string `json:"user-variables"`
Expand Down Expand Up @@ -135,7 +135,7 @@ type GetEventsOptions struct {

// EventIterator maintains the state necessary for paging though small parcels of a larger set of events.
type EventIterator struct {
EventResponse
eventResponse
mg Mailgun
err error
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func (mg *MailgunImpl) ListEvents(opts *EventsOptions) *EventIterator {
url, err := req.generateUrlWithParameters()
return &EventIterator{
mg: mg,
EventResponse: EventResponse{Paging: Paging{Next: url, First: url}},
eventResponse: eventResponse{Paging: Paging{Next: url, First: url}},
err: err,
}
}
Expand Down Expand Up @@ -433,18 +433,5 @@ func (ei *EventIterator) fetch(url string) error {
r.setClient(ei.mg.Client())
r.setBasicAuth(basicAuthUser, ei.mg.ApiKey())

return getResponseFromJSON(r, &ei.EventResponse)
}

func toMapInterface(field string, thingy map[string]interface{}) (map[string]interface{}, error) {
var empty map[string]interface{}
obj, ok := thingy[field]
if !ok {
return empty, errors.Errorf("'%s' field not found in event", field)
}
result, ok := obj.(map[string]interface{})
if !ok {
return empty, errors.Errorf("'%s' field not a map[string]interface{}", field)
}
return result, nil
return getResponseFromJSON(r, &ei.eventResponse)
}
42 changes: 42 additions & 0 deletions recipients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}

0 comments on commit 98daa62

Please sign in to comment.