2024-10-16 21:28:04 +02:00
|
|
|
package atom
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
import (
|
2024-10-19 12:28:09 +02:00
|
|
|
"errors"
|
2024-10-13 17:19:40 +02:00
|
|
|
"fmt"
|
2024-10-15 16:20:04 +02:00
|
|
|
"net/mail"
|
2024-10-13 17:19:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Person struct {
|
|
|
|
*CommonAttributes
|
|
|
|
Name string `xml:"name"`
|
2024-10-19 12:28:09 +02:00
|
|
|
URI string `xml:"uri,omitempty"` // IRI
|
|
|
|
Email string `xml:"email,omitempty"` // EmailAddress
|
2024-10-13 20:42:17 +02:00
|
|
|
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-19 12:28:09 +02:00
|
|
|
// NewPerson creates a new Person. It returns a *Person and an error.
|
|
|
|
func NewPerson(name string) (*Person, error) {
|
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("error creating new person: name string empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Person{Name: name}, nil
|
2024-10-15 16:07:41 +02:00
|
|
|
}
|
|
|
|
|
2024-10-19 12:28:09 +02:00
|
|
|
// SetURI sets the URI element of the Person. It returns an error.
|
|
|
|
func (l *Link) SetURI(uri string) error {
|
|
|
|
if !isValidIRI(uri) {
|
|
|
|
return fmt.Errorf("uri %v not correctly formatted", uri)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddExtension adds the Extension to the Person. It returns an error.
|
|
|
|
func (p *Person) AddExtension(e *ExtensionElement) error {
|
|
|
|
if e == nil {
|
|
|
|
return errors.New("error adding extension element to person: *ExtensionElement is nil")
|
|
|
|
}
|
|
|
|
|
2024-10-16 17:38:03 +02:00
|
|
|
if p.Extensions == nil {
|
|
|
|
p.Extensions = make([]*ExtensionElement, 1)
|
|
|
|
p.Extensions[0] = e
|
|
|
|
} else {
|
|
|
|
p.Extensions = append(p.Extensions, e)
|
|
|
|
}
|
2024-10-19 12:28:09 +02:00
|
|
|
|
|
|
|
return nil
|
2024-10-15 16:19:47 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// Check checks the Person for incompatibilities with RFC4287. It returns an
|
|
|
|
// error.
|
2024-10-13 17:19:40 +02:00
|
|
|
func (p *Person) Check() error {
|
|
|
|
if p.Name == "" {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("name element of person %v empty", p)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-15 16:20:04 +02:00
|
|
|
if p.URI != "" {
|
2024-10-16 17:33:25 +02:00
|
|
|
if !isValidIRI(p.URI) {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("uri element of person %v not correctly formatted", p)
|
2024-10-15 16:20:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Email != "" {
|
2024-10-19 12:28:09 +02:00
|
|
|
if _, err := mail.ParseAddress(p.Email); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("email element of person %v not correctly formatted", p)
|
2024-10-15 16:20:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 16:07:11 +02:00
|
|
|
if p.Extensions != nil {
|
|
|
|
for i, e := range p.Extensions {
|
|
|
|
if err := e.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("extension element %v of person %v: %v", i, p, err)
|
2024-10-15 16:07:11 +02:00
|
|
|
}
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|