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

View File

@@ -2,6 +2,7 @@ package atom
import (
"encoding/xml"
"errors"
"fmt"
"strings"
"time"
@@ -75,8 +76,12 @@ func NewEntry(title string) (*Entry, error) {
}, nil
}
// AddAuthor adds the Person as an author to the Entry.
func (e *Entry) AddAuthor(p *Person) {
// AddAuthor adds the Person as an author to the Entry. It returns an error.
func (e *Entry) AddAuthor(p *Person) error {
if p == nil {
return errors.New("error adding author element to entry: *Person is nil")
}
if e.Authors == nil {
e.Authors = make([]*Person, 1)
e.Authors[0] = p
@@ -85,10 +90,15 @@ func (e *Entry) AddAuthor(p *Person) {
}
e.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddCategory adds the Category to the Entry.
func (e *Entry) AddCategory(c *Category) {
// AddCategory adds the Category to the Entry. It returns an error.
func (e *Entry) AddCategory(c *Category) error {
if c == nil {
return errors.New("error adding category element to entry: *Category is nil")
}
if e.Categories == nil {
e.Categories = make([]*Category, 1)
e.Categories[0] = c
@@ -97,10 +107,16 @@ func (e *Entry) AddCategory(c *Category) {
}
e.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddContributor adds the Person as a contributor to the Entry.
func (e *Entry) AddContributor(c *Person) {
// AddContributor adds the Person as a contributor to the Entry. It returns an
// error.
func (e *Entry) AddContributor(c *Person) error {
if c == nil {
return errors.New("error adding contributor element to entry: *Person is nil")
}
if e.Contributors == nil {
e.Contributors = make([]*Person, 1)
e.Contributors[0] = c
@@ -109,10 +125,15 @@ func (e *Entry) AddContributor(c *Person) {
}
e.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddLink adds the Link to the Entry.
func (e *Entry) AddLink(l *Link) {
// AddLink adds the Link to the Entry. It returns an error.
func (e *Entry) AddLink(l *Link) error {
if l == nil {
return errors.New("error adding link element to entry: *Link is nil")
}
if e.Links == nil {
e.Links = make([]*Link, 1)
e.Links[0] = l
@@ -121,10 +142,15 @@ func (e *Entry) AddLink(l *Link) {
}
e.Updated.DateTime = DateTime(time.Now())
return nil
}
// AddExtension adds the ExtensionElement to the Entry.
func (e *Entry) AddExtension(x *ExtensionElement) {
// AddExtension adds the ExtensionElement to the Entry. It returns an error.
func (e *Entry) AddExtension(x *ExtensionElement) error {
if x == nil {
return errors.New("error adding extension element to entry: *ExtensionElement is nil")
}
if e.Extensions == nil {
e.Extensions = make([]*ExtensionElement, 1)
e.Extensions[0] = x
@@ -133,6 +159,7 @@ func (e *Entry) AddExtension(x *ExtensionElement) {
}
e.Updated.DateTime = DateTime(time.Now())
return nil
}
// Check checks the Entry for incompatibilities with RFC4287. It returns an