2024-10-13 17:19:40 +02:00
|
|
|
package atomfeed
|
|
|
|
|
|
|
|
import (
|
2024-10-15 16:19:47 +02:00
|
|
|
"encoding/xml"
|
2024-10-13 17:19:40 +02:00
|
|
|
"errors"
|
|
|
|
"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-16 17:33:25 +02:00
|
|
|
URI IRI `xml:"uri,omitempty"`
|
2024-10-13 17:19:40 +02:00
|
|
|
Email EmailAddress `xml:"email,omitempty"`
|
2024-10-13 20:42:17 +02:00
|
|
|
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-15 16:07:41 +02:00
|
|
|
func NewPerson(name string) *Person {
|
|
|
|
return &Person{Name: name}
|
|
|
|
}
|
|
|
|
|
2024-10-15 16:19:47 +02:00
|
|
|
func (p *Person) AddExtension(name string, value any) {
|
|
|
|
p.Extensions = append(p.Extensions, &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value})
|
|
|
|
}
|
|
|
|
|
2024-10-13 17:19:40 +02:00
|
|
|
func (p *Person) Check() error {
|
|
|
|
if p.Name == "" {
|
|
|
|
return errors.New("name element of person element empty")
|
|
|
|
}
|
|
|
|
|
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-15 21:10:12 +02:00
|
|
|
return fmt.Errorf("uri element of person %v not correctly formatted", p.Name)
|
2024-10-15 16:20:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Email != "" {
|
|
|
|
if _, err := mail.ParseAddress(string(p.Email)); err != nil {
|
|
|
|
return fmt.Errorf("email element of person %v not correctly formatted", p.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 16:07:11 +02:00
|
|
|
if p.Extensions != nil {
|
|
|
|
for i, e := range p.Extensions {
|
|
|
|
if err := e.Check(); err != nil {
|
|
|
|
return fmt.Errorf("extension element %v of person %v: %v", i, p.Name, err)
|
|
|
|
}
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|