2024-10-16 21:28:04 +02:00
|
|
|
package atom
|
2024-10-13 17:19:40 +02:00
|
|
|
|
2024-10-17 20:10:18 +02:00
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
)
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
type Source struct {
|
2024-10-17 20:10:18 +02:00
|
|
|
XMLName xml.Name `xml:"source"`
|
2024-10-13 17:19:40 +02:00
|
|
|
*CommonAttributes
|
|
|
|
Authors []*Person `xml:"author,omitempty"`
|
2024-10-17 20:10:18 +02:00
|
|
|
Categories []*Category `xml:",omitempty"`
|
2024-10-13 17:19:40 +02:00
|
|
|
Contributors []*Person `xml:"contributor,omitempty"`
|
2024-10-17 20:10:18 +02:00
|
|
|
Generator *Generator `xml:",omitempty"`
|
|
|
|
Icon *Icon `xml:",omitempty"`
|
|
|
|
ID *ID `xml:",omitempty"`
|
|
|
|
Links []*Link `xml:",omitempty"`
|
|
|
|
Logo *Logo `xml:",omitempty"`
|
2024-10-16 18:41:42 +02:00
|
|
|
Rights Text `xml:"rights,omitempty"`
|
|
|
|
Subtitle Text `xml:"subtitle,omitempty"`
|
|
|
|
Title Text `xml:"title,omitempty"`
|
2024-10-13 17:19:40 +02:00
|
|
|
Updated *Date `xml:"updated,omitempty"`
|
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
|
|
|
// NewSource creates a new Source. It returns a *Source.
|
|
|
|
func NewSource() *Source {
|
2024-10-19 14:52:19 +02:00
|
|
|
return &Source{CommonAttributes: newCommonAttributes()}
|
2024-10-19 12:28:09 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
// AddAuthor adds the Person as an author to the Source.
|
2024-10-20 10:42:12 +02:00
|
|
|
func (s *Source) AddAuthor(p *Person) {
|
2024-10-20 12:03:26 +02:00
|
|
|
addToSlice(s.Authors, p)
|
2024-10-20 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
// DeleteAuthor deletes the Person from the Source. It return an error.
|
|
|
|
func (s *Source) DeleteAuthor(id int) error {
|
2024-10-20 12:03:26 +02:00
|
|
|
if err := deleteFromSlice(s.Authors, id); err != nil {
|
|
|
|
return fmt.Errorf("error deleting author %v from source %v: %v", id, s, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCategory adds the Category to the Source.
|
2024-10-20 10:42:12 +02:00
|
|
|
func (s *Source) AddCategory(c *Category) {
|
2024-10-20 12:03:26 +02:00
|
|
|
addToSlice(s.Categories, c)
|
2024-10-20 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
// DeleteCategory deletes the Category from the Source. It return an error.
|
|
|
|
func (s *Source) DeleteCategory(id int) error {
|
2024-10-20 12:03:26 +02:00
|
|
|
if err := deleteFromSlice(s.Categories, id); err != nil {
|
|
|
|
return fmt.Errorf("error deleting category %v from source %v: %v", id, s, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddContributor adds the Person as a contributor to the Source.
|
2024-10-20 10:42:12 +02:00
|
|
|
func (s *Source) AddContributor(c *Person) {
|
2024-10-20 12:03:26 +02:00
|
|
|
addToSlice(s.Contributors, c)
|
2024-10-20 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
// DeleteContributor deletes the Person from the Source. It return an error.
|
|
|
|
func (s *Source) DeleteContributor(id int) error {
|
2024-10-20 12:03:26 +02:00
|
|
|
if err := deleteFromSlice(s.Contributors, id); err != nil {
|
|
|
|
return fmt.Errorf("error deleting contributor %v from source %v: %v", id, s, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLink adds the Link to the Source.
|
2024-10-20 10:42:12 +02:00
|
|
|
func (s *Source) AddLink(l *Link) {
|
2024-10-20 12:03:26 +02:00
|
|
|
addToSlice(s.Links, l)
|
2024-10-20 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
// DeleteLink deletes the Link from the Source. It return an error.
|
|
|
|
func (s *Source) DeleteLink(id int) error {
|
2024-10-20 12:03:26 +02:00
|
|
|
if err := deleteFromSlice(s.Links, id); err != nil {
|
|
|
|
return fmt.Errorf("error deleting link %v from source %v: %v", id, s, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddExtension adds the ExtensionElement to the Source.
|
2024-10-20 10:42:12 +02:00
|
|
|
func (s *Source) AddExtension(e *ExtensionElement) {
|
2024-10-20 12:03:26 +02:00
|
|
|
addToSlice(s.Extensions, e)
|
2024-10-20 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
// DeleteExtension deletes the Extension from the Source. It return an error.
|
|
|
|
func (s *Source) DeleteExtension(id int) error {
|
2024-10-20 12:03:26 +02:00
|
|
|
if err := deleteFromSlice(s.Extensions, id); err != nil {
|
|
|
|
return fmt.Errorf("error deleting extension %v from source %v: %v", id, s, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// Check checks the Source for incompatibilities with RFC4287. It returns an
|
|
|
|
// error.
|
2024-10-13 17:19:40 +02:00
|
|
|
func (s *Source) Check() error {
|
2024-10-16 18:41:42 +02:00
|
|
|
for i, a := range s.Authors {
|
|
|
|
if err := a.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("author element %v of source %v: %v", i, s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 18:41:42 +02:00
|
|
|
for i, c := range s.Categories {
|
|
|
|
if err := c.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("category element %v of source %v: %v", i, s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 18:41:42 +02:00
|
|
|
for i, c := range s.Contributors {
|
|
|
|
if err := c.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("contributor element %v of source %v: %v", i, s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Generator != nil {
|
|
|
|
if err := s.Generator.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("generator element of source %v: %v", s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Icon != nil {
|
|
|
|
if err := s.Icon.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("icon element of source %v: %v", s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.ID != nil {
|
|
|
|
if err := s.ID.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("id element of source %v: %v", s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 18:41:42 +02:00
|
|
|
for i, l := range s.Links {
|
|
|
|
if err := l.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("link element %v of source %v: %v", i, s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Logo != nil {
|
|
|
|
if err := s.Logo.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("logo element of source %v: %v", s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Rights != nil {
|
2024-10-16 18:41:42 +02:00
|
|
|
if err := s.Rights.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("rights element of source %v: %v", s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Subtitle != nil {
|
2024-10-16 18:41:42 +02:00
|
|
|
if err := s.Subtitle.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("subtitle element of source %v: %v", s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Title != nil {
|
2024-10-16 18:41:42 +02:00
|
|
|
if err := s.Title.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("title element of source %v: %v", s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Updated != nil {
|
|
|
|
if err := s.Updated.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("updated element of source %v: %v", s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 18:41:42 +02:00
|
|
|
for i, e := range s.Extensions {
|
|
|
|
if err := e.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("extension element %v of source %v: %v", i, s, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|