64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package atom
|
|
|
|
import (
|
|
"fmt"
|
|
"net/mail"
|
|
)
|
|
|
|
type Person struct {
|
|
*CommonAttributes
|
|
Name string `xml:"name"`
|
|
URI string `xml:"uri,omitempty"` // IRI
|
|
Email string `xml:"email,omitempty"` // EmailAddress
|
|
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
|
}
|
|
|
|
// NewPerson creates a new Person. It returns a *Person.
|
|
func NewPerson(name string) *Person {
|
|
return &Person{
|
|
CommonAttributes: newCommonAttributes(),
|
|
Name: name,
|
|
}
|
|
}
|
|
|
|
// AddExtension adds the Extension to the Person.
|
|
func (p *Person) AddExtension(e *ExtensionElement) {
|
|
addToSlice(&p.Extensions, e)
|
|
}
|
|
|
|
// DeleteExtension deletes the Extension from the Person. It return an error.
|
|
func (p *Person) DeleteExtension(id int) error {
|
|
if err := deleteFromSlice(&p.Extensions, id); err != nil {
|
|
return fmt.Errorf("error deleting extension %v from person %v: %v", id, p, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Check checks the Person for incompatibilities with RFC4287. It returns an
|
|
// error.
|
|
func (p *Person) Check() error {
|
|
if p.Name == "" {
|
|
return fmt.Errorf("name element of person %v empty", p)
|
|
}
|
|
|
|
if p.URI != "" {
|
|
if !isValidIRI(p.URI) {
|
|
return fmt.Errorf("uri element of person %v not correctly formatted", p)
|
|
}
|
|
}
|
|
|
|
if p.Email != "" {
|
|
if _, err := mail.ParseAddress(p.Email); err != nil {
|
|
return fmt.Errorf("email element of person %v not correctly formatted", p)
|
|
}
|
|
}
|
|
|
|
for i, e := range p.Extensions {
|
|
if err := e.Check(); err != nil {
|
|
return fmt.Errorf("extension element %v of person %v: %v", i, p, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|