mirror of
https://github.com/golang/go
synced 2024-11-12 10:30:23 -07:00
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
This commit is contained in:
parent
70ab57ea2d
commit
9f807fcc4a
@ -127,7 +127,7 @@ func (h Header) AddressList(key string) ([]*Address, error) {
|
||||
if hdr == "" {
|
||||
return nil, ErrHeaderNotPresent
|
||||
}
|
||||
return newAddrParser(hdr).parseAddressList()
|
||||
return ParseAddressList(hdr)
|
||||
}
|
||||
|
||||
// Address represents a single mail address.
|
||||
@ -138,6 +138,16 @@ type Address struct {
|
||||
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.
|
||||
// If the address's name contains non-ASCII characters
|
||||
// the name will be rendered according to RFC 2047.
|
||||
|
@ -227,13 +227,24 @@ func TestAddressParsing(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
addrs, err := newAddrParser(test.addrsStr).parseAddressList()
|
||||
if len(test.exp) == 1 {
|
||||
addr, err := ParseAddress(test.addrsStr)
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user