Compare commits

..

No commits in common. "main" and "rss-v0.1.0" have entirely different histories.

24
rss.go
View File

@ -35,6 +35,11 @@ type Source struct {
Value string
}
type Content struct {
XMLName xml.Name `xml:"content:encoded"`
Value string `xml:",cdata"`
}
type Item struct {
XMLName xml.Name `xml:"item"`
Title string `xml:"title,omitempty"`
@ -46,6 +51,7 @@ type Item struct {
Guid string `xml:"guid,omitempty"`
PubDate string `xml:"pubDate,omitempty"`
Source *Source
Content *Content
Categories []string `xml:"category,omitempty"`
}
@ -136,6 +142,14 @@ func (s *Source) check() error {
return nil
}
func (c *Content) check() error {
if len(c.Value) == 0 {
return fmt.Errorf("error: value not set")
}
return nil
}
func (i *Item) check() error {
if len(i.Title) == 0 && len(i.Description) == 0 {
return fmt.Errorf("error: neither title nor description set")
@ -153,6 +167,12 @@ func (i *Item) check() error {
}
}
if i.Content != nil {
if err := i.Content.check(); err != nil {
return fmt.Errorf("error checking content: %v", err)
}
}
return nil
}
@ -209,7 +229,7 @@ func NewFeed() *Feed {
}
}
func (f *Feed) ToXML(encoding string) (string, error) {
func (f *Feed) ToXML() (string, error) {
if err := f.check(); err != nil {
return "", fmt.Errorf("error checking RSS feed: %v", err)
}
@ -219,5 +239,5 @@ func (f *Feed) ToXML(encoding string) (string, error) {
return "", fmt.Errorf("error XML encoding feed: %v", err)
}
return fmt.Sprintln(`<?xml version="1.0" encoding="`+encoding+`"?>`) + string(xml), nil
return `<?xml version="1.0" encoding="UTF-8"?>` + string(xml), nil
}