Added more error handling and necessary functions

This commit is contained in:
2024-10-19 12:28:09 +02:00
parent d4e7bce5e2
commit f4dfd6d060
22 changed files with 317 additions and 122 deletions

57
feed.go
View File

@@ -2,6 +2,7 @@ package atom
import (
"encoding/xml"
"errors"
"fmt"
"time"
)
@@ -44,8 +45,12 @@ func NewFeed(title string) (*Feed, error) {
}, nil
}
// AddAuthor adds the Person as an author to the Feed.
func (f *Feed) AddAuthor(p *Person) {
// AddAuthor adds the Person as an author to the Feed. It returns an error.
func (f *Feed) AddAuthor(p *Person) error {
if p == nil {
return errors.New("error adding author element to feed: *Person is nil")
}
if f.Authors == nil {
f.Authors = make([]*Person, 1)
f.Authors[0] = p
@@ -54,10 +59,15 @@ func (f *Feed) AddAuthor(p *Person) {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddCategory adds the Category to the Feed.
func (f *Feed) AddCategory(c *Category) {
// AddCategory adds the Category to the Feed. It returns an error.
func (f *Feed) AddCategory(c *Category) error {
if c == nil {
return errors.New("error adding category element to feed: *Category is nil")
}
if f.Categories == nil {
f.Categories = make([]*Category, 1)
f.Categories[0] = c
@@ -66,10 +76,16 @@ func (f *Feed) AddCategory(c *Category) {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddContributor adds the Person as a contributor to the Feed.
func (f *Feed) AddContributor(c *Person) {
// AddContributor adds the Person as a contributor to the Feed. It returns an
// error.
func (f *Feed) AddContributor(c *Person) error {
if c == nil {
return errors.New("error adding contributor element to feed: *Person is nil")
}
if f.Contributors == nil {
f.Contributors = make([]*Person, 1)
f.Contributors[0] = c
@@ -78,10 +94,16 @@ func (f *Feed) AddContributor(c *Person) {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddLink adds the Link to the Feed. There should be one Link with Rel "self".
func (f *Feed) AddLink(l *Link) {
// AddLink adds the Link to the Feed. It returns an error. There should be one
// Link with Rel "self".
func (f *Feed) AddLink(l *Link) error {
if l == nil {
return errors.New("error adding link element to feed: *Link is nil")
}
if f.Links == nil {
f.Links = make([]*Link, 1)
f.Links[0] = l
@@ -90,10 +112,15 @@ func (f *Feed) AddLink(l *Link) {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddExtension adds the Extension to the Feed.
func (f *Feed) AddExtension(e *ExtensionElement) {
// AddExtension adds the Extension to the Feed. It returns an error.
func (f *Feed) AddExtension(e *ExtensionElement) error {
if e == nil {
return errors.New("error adding extension element to feed: *ExtensionElement is nil")
}
if f.Extensions == nil {
f.Extensions = make([]*ExtensionElement, 1)
f.Extensions[0] = e
@@ -102,10 +129,15 @@ func (f *Feed) AddExtension(e *ExtensionElement) {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddEntry adds the Entry to the Feed.
func (f *Feed) AddEntry(e *Entry) {
// AddEntry adds the Entry to the Feed. It returns an error.
func (f *Feed) AddEntry(e *Entry) error {
if e == nil {
return errors.New("error adding entry element to feed: *Entry is nil")
}
if f.Entries == nil {
f.Entries = make([]*Entry, 1)
f.Entries[0] = e
@@ -114,6 +146,7 @@ func (f *Feed) AddEntry(e *Entry) {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// Check checks the Feed for incompatibilities with RFC4287. It returns an