Commit 9f807fcc authored by Graham Miller's avatar Graham Miller Committed by David Symonds

net/mail: make address parsing (more) public

Code for parsing email addresses was already partially part of the public API with "func (Header) AddressList".  This CL adds a trivial implementation for two public methods to parse address and lists from a string. With tests.

R=dsymonds
CC=golang-dev
https://golang.org/cl/5676067
parent 70ab57ea
...@@ -127,7 +127,7 @@ func (h Header) AddressList(key string) ([]*Address, error) { ...@@ -127,7 +127,7 @@ func (h Header) AddressList(key string) ([]*Address, error) {
if hdr == "" { if hdr == "" {
return nil, ErrHeaderNotPresent return nil, ErrHeaderNotPresent
} }
return newAddrParser(hdr).parseAddressList() return ParseAddressList(hdr)
} }
// Address represents a single mail address. // Address represents a single mail address.
...@@ -138,6 +138,16 @@ type Address struct { ...@@ -138,6 +138,16 @@ type Address struct {
Address string // user@domain Address string // user@domain
} }
// Parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>"
func ParseAddress(address string) (*Address, error) {
return newAddrParser(address).parseAddress()
}
// ParseAddressList parses the given string as a list of addresses.
func ParseAddressList(list string) ([]*Address, error) {
return newAddrParser(list).parseAddressList()
}
// String formats the address as a valid RFC 5322 address. // String formats the address as a valid RFC 5322 address.
// If the address's name contains non-ASCII characters // If the address's name contains non-ASCII characters
// the name will be rendered according to RFC 2047. // the name will be rendered according to RFC 2047.
......
...@@ -227,13 +227,24 @@ func TestAddressParsing(t *testing.T) { ...@@ -227,13 +227,24 @@ func TestAddressParsing(t *testing.T) {
}, },
} }
for _, test := range tests { for _, test := range tests {
addrs, err := newAddrParser(test.addrsStr).parseAddressList() if len(test.exp) == 1 {
addr, err := ParseAddress(test.addrsStr)
if err != nil { if err != nil {
t.Errorf("Failed parsing %q: %v", test.addrsStr, err) t.Errorf("Failed parsing (single) %q: %v", test.addrsStr, err)
continue
}
if !reflect.DeepEqual([]*Address{addr}, test.exp) {
t.Errorf("Parse (single) of %q: got %+v, want %+v", test.addrsStr, addr, test.exp)
}
}
addrs, err := ParseAddressList(test.addrsStr)
if err != nil {
t.Errorf("Failed parsing (list) %q: %v", test.addrsStr, err)
continue continue
} }
if !reflect.DeepEqual(addrs, test.exp) { if !reflect.DeepEqual(addrs, test.exp) {
t.Errorf("Parse of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp) t.Errorf("Parse (list) of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp)
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment