Add functions to create a new feed and add objects to slices
This commit is contained in:
parent
ac78db9917
commit
16d8b577e3
89
feed.go
89
feed.go
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Feed struct {
|
type Feed struct {
|
||||||
@ -17,18 +18,89 @@ type Feed struct {
|
|||||||
ID *ID `xml:"id"`
|
ID *ID `xml:"id"`
|
||||||
Links []*Link `xml:"link,omitempty"` // There should be one link with rel "self"
|
Links []*Link `xml:"link,omitempty"` // There should be one link with rel "self"
|
||||||
Logo *Logo `xml:"logo,omitempty"`
|
Logo *Logo `xml:"logo,omitempty"`
|
||||||
Rights *Text `xml:"rights,omitempty"`
|
Rights Text `xml:"rights,omitempty"`
|
||||||
Subtitle *Text `xml:"subtitle,omitempty"`
|
Subtitle Text `xml:"subtitle,omitempty"`
|
||||||
Title *Text `xml:"title"`
|
Title Text `xml:"title"`
|
||||||
Updated *Date `xml:"updated"`
|
Updated *Date `xml:"updated"`
|
||||||
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
||||||
Entries []*Entry `xml:"entry,omitempty"`
|
Entries []*Entry `xml:"entry,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Feed) AddExtension(name string, value any) {
|
// NewFeed creates a new feed
|
||||||
f.Extensions = append(f.Extensions, &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value})
|
func NewFeed(title string, updated time.Time) (*Feed, error) {
|
||||||
|
text, err := NewText("text", title)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error creating new feed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Feed{
|
||||||
|
ID: NewID(),
|
||||||
|
Title: text,
|
||||||
|
Updated: NewDate(time.Now()),
|
||||||
|
}, 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
|
||||||
|
} else {
|
||||||
|
f.Authors = append(f.Authors, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
} else {
|
||||||
|
f.Categories = append(f.Categories, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddContributor adds the contributor to the feed
|
||||||
|
func (f *Feed) AddContributor(c *Person) {
|
||||||
|
if f.Contributors == nil {
|
||||||
|
f.Contributors = make([]*Person, 1)
|
||||||
|
f.Contributors[0] = c
|
||||||
|
} else {
|
||||||
|
f.Contributors = append(f.Contributors, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddLink adds the link to the feed
|
||||||
|
func (f *Feed) AddLink(l *Link) {
|
||||||
|
if f.Links == nil {
|
||||||
|
f.Links = make([]*Link, 1)
|
||||||
|
f.Links[0] = l
|
||||||
|
} else {
|
||||||
|
f.Links = append(f.Links, l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
} else {
|
||||||
|
f.Extensions = append(f.Extensions, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
} else {
|
||||||
|
f.Entries = append(f.Entries, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check checks the feed for incompatibilities with the Atom standard
|
||||||
func (f *Feed) Check() error {
|
func (f *Feed) Check() error {
|
||||||
if f.ID == nil {
|
if f.ID == nil {
|
||||||
return errors.New("no id element of feed")
|
return errors.New("no id element of feed")
|
||||||
@ -95,13 +167,13 @@ func (f *Feed) Check() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if f.Rights != nil {
|
if f.Rights != nil {
|
||||||
if err := (*f.Rights).Check(); err != nil {
|
if err := f.Rights.Check(); err != nil {
|
||||||
return fmt.Errorf("rights element of feed %v: %v", f.ID.URI, err)
|
return fmt.Errorf("rights element of feed %v: %v", f.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Subtitle != nil {
|
if f.Subtitle != nil {
|
||||||
if err := (*f.Subtitle).Check(); err != nil {
|
if err := f.Subtitle.Check(); err != nil {
|
||||||
return fmt.Errorf("subtitle element of feed %v: %v", f.ID.URI, err)
|
return fmt.Errorf("subtitle element of feed %v: %v", f.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +181,7 @@ func (f *Feed) Check() error {
|
|||||||
if f.Title == nil {
|
if f.Title == nil {
|
||||||
return fmt.Errorf("no title element of feed %v", f.ID.URI)
|
return fmt.Errorf("no title element of feed %v", f.ID.URI)
|
||||||
} else {
|
} else {
|
||||||
if err := (*f.Title).Check(); err != nil {
|
if err := f.Title.Check(); err != nil {
|
||||||
return fmt.Errorf("title element of feed %v: %v", f.ID.URI, err)
|
return fmt.Errorf("title element of feed %v: %v", f.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,6 +232,7 @@ func (f *Feed) Standardize() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToXML converts the feed to XML
|
||||||
func (f *Feed) ToXML(encoding string) (string, error) {
|
func (f *Feed) ToXML(encoding string) (string, error) {
|
||||||
xml, err := xml.MarshalIndent(f, "", " ")
|
xml, err := xml.MarshalIndent(f, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user