Get rid of checks when creating constructs. Check should handle this.

This commit is contained in:
2024-10-19 14:12:51 +02:00
parent 960889f9e7
commit 57db4178d0
21 changed files with 143 additions and 345 deletions

77
feed.go
View File

@@ -2,7 +2,6 @@ package atom
import (
"encoding/xml"
"errors"
"fmt"
"time"
)
@@ -26,31 +25,17 @@ type Feed struct {
Entries []*Entry `xml:",omitempty"`
}
// NewFeed creates a new Feed. It returns a *Feed and an error.
func NewFeed(title string) (*Feed, error) {
text, err := NewText("text", title)
if err != nil {
return nil, fmt.Errorf("error creating new feed: %v", err)
}
id, err := NewID(NewURN())
if err != nil {
return nil, fmt.Errorf("error creating new feed: %v", err)
}
// NewFeed creates a new Feed. It returns a *Feed.
func NewFeed(title string) *Feed {
return &Feed{
ID: id,
Title: text,
ID: NewID(NewURN()),
Title: NewText("text", title),
Updated: NewDate(time.Now()),
}, nil
}
}
// 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")
}
// AddAuthor adds the Person as an author to the Feed.
func (f *Feed) AddAuthor(p *Person) {
if f.Authors == nil {
f.Authors = make([]*Person, 1)
f.Authors[0] = p
@@ -59,15 +44,10 @@ func (f *Feed) AddAuthor(p *Person) error {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// 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")
}
// AddCategory adds the Category to the Feed.
func (f *Feed) AddCategory(c *Category) {
if f.Categories == nil {
f.Categories = make([]*Category, 1)
f.Categories[0] = c
@@ -76,16 +56,10 @@ func (f *Feed) AddCategory(c *Category) error {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// 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")
}
// AddContributor adds the Person as a contributor to the Feed.
func (f *Feed) AddContributor(c *Person) {
if f.Contributors == nil {
f.Contributors = make([]*Person, 1)
f.Contributors[0] = c
@@ -94,16 +68,10 @@ func (f *Feed) AddContributor(c *Person) error {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// 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")
}
// AddLink adds the Link to the Feed. There should be one Link with Rel "self".
func (f *Feed) AddLink(l *Link) {
if f.Links == nil {
f.Links = make([]*Link, 1)
f.Links[0] = l
@@ -112,15 +80,10 @@ func (f *Feed) AddLink(l *Link) error {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// 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")
}
// AddExtension adds the Extension to the Feed.
func (f *Feed) AddExtension(e *ExtensionElement) {
if f.Extensions == nil {
f.Extensions = make([]*ExtensionElement, 1)
f.Extensions[0] = e
@@ -129,15 +92,10 @@ func (f *Feed) AddExtension(e *ExtensionElement) error {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// 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")
}
// AddEntry adds the Entry to the Feed.
func (f *Feed) AddEntry(e *Entry) {
if f.Entries == nil {
f.Entries = make([]*Entry, 1)
f.Entries[0] = e
@@ -146,7 +104,6 @@ func (f *Feed) AddEntry(e *Entry) error {
}
f.Updated.DateTime = DateTime(time.Now())
return nil
}
// Check checks the Feed for incompatibilities with RFC4287. It returns an