Compare commits
8 Commits
4c38753ff7
...
764b143ff8
Author | SHA1 | Date | |
---|---|---|---|
764b143ff8 | |||
bcf2532372 | |||
b6b8970810 | |||
c0f5306715 | |||
8a00759c4b | |||
a49e853efb | |||
e0384904b4 | |||
e2986e70b1 |
@ -30,8 +30,6 @@ import (
|
||||
|
||||
func main() {
|
||||
feed := atom.NewFeed("Example Feed")
|
||||
feed.Title = atom.NewText("text", "Example Feed")
|
||||
feed.ID = atom.NewID(atom.NewURN())
|
||||
if err := feed.Check(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
@ -44,7 +42,6 @@ func main() {
|
||||
feed.AddAuthor(author)
|
||||
|
||||
entry := atom.NewEntry("First Entry")
|
||||
entry.ID = atom.NewID(atom.NewURN())
|
||||
entry.Content = atom.NewContent(atom.InlineText, "text", "This is the content of the first entry.")
|
||||
if err := entry.Check(); err != nil {
|
||||
log.Fatalln(err)
|
||||
|
25
atom.go
25
atom.go
@ -1,6 +1,7 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"html"
|
||||
"mime"
|
||||
@ -11,6 +12,30 @@ import (
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
type Countable interface {
|
||||
*xml.Attr | *Person | *Category | *Link | *ExtensionElement | *Entry
|
||||
}
|
||||
|
||||
// addToSlice adds a Countable to to a *[]Countable
|
||||
func addToSlice[C Countable](slice *[]C, countable C) {
|
||||
if *slice == nil {
|
||||
*slice = make([]C, 0)
|
||||
}
|
||||
*slice = append(*slice, countable)
|
||||
}
|
||||
|
||||
// deleteFromSlice deletes the Countable with the index from the *[]Countable.
|
||||
// It return an error.
|
||||
func deleteFromSlice[C Countable](slice *[]C, index int) error {
|
||||
length := len(*slice)
|
||||
if index > length {
|
||||
return fmt.Errorf("id %v out of range %v", index, length)
|
||||
}
|
||||
|
||||
*slice = append((*slice)[:index], (*slice)[index+1:]...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// isValidIRI checks whether an IRI is valid or not. It returns a bool.
|
||||
// https://www.w3.org/2011/04/XMLSchema/TypeLibrary-IRI-RFC3987.xsd
|
||||
func isValidIRI(iri string) bool {
|
||||
|
@ -17,14 +17,18 @@ func newCommonAttributes() *CommonAttributes {
|
||||
return new(CommonAttributes)
|
||||
}
|
||||
|
||||
// AddAttribute adds the Attribute to the CommonAttributes.
|
||||
// AddAttribute adds the attribute to the CommonAttributes.
|
||||
func (c *CommonAttributes) AddAttribute(name, value string) {
|
||||
if c.UndefinedAttributes == nil {
|
||||
c.UndefinedAttributes = make([]*xml.Attr, 1)
|
||||
c.UndefinedAttributes[0] = &xml.Attr{Name: xml.Name{Local: name}, Value: value}
|
||||
} else {
|
||||
c.UndefinedAttributes = append(c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
|
||||
addToSlice(&c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
|
||||
}
|
||||
|
||||
// DeleteAttribute deletes the attribute at index from the CommonAttributes. It
|
||||
// return an error.
|
||||
func (c *CommonAttributes) DeleteAttribute(index int) error {
|
||||
if err := deleteFromSlice(&c.UndefinedAttributes, index); err != nil {
|
||||
return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", index, c, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the CommonAttributes for incompatibilities with RFC4287. It
|
||||
|
127
entry.go
127
entry.go
@ -39,16 +39,16 @@ func (e *Entry) checkAuthors(authorInFeed bool) error {
|
||||
if e.Authors == nil {
|
||||
if !authorInFeed {
|
||||
if e.Source == nil {
|
||||
return fmt.Errorf("no authors set in entry %v", e)
|
||||
return fmt.Errorf("no authors set in entry %v", e.ID.URI)
|
||||
}
|
||||
if e.Source.Authors == nil {
|
||||
return fmt.Errorf("no authors set in entry %v", e)
|
||||
return fmt.Errorf("no authors set in entry %v", e.ID.URI)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i, a := range e.Authors {
|
||||
if err := a.Check(); err != nil {
|
||||
return fmt.Errorf("author element %v of entry %v: %v", i, e, err)
|
||||
return fmt.Errorf("author element %v of entry %v: %v", i, e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -68,62 +68,85 @@ func NewEntry(title string) *Entry {
|
||||
|
||||
// AddAuthor adds the Person as an author to the Entry.
|
||||
func (e *Entry) AddAuthor(p *Person) {
|
||||
if e.Authors == nil {
|
||||
e.Authors = make([]*Person, 1)
|
||||
e.Authors[0] = p
|
||||
} else {
|
||||
e.Authors = append(e.Authors, p)
|
||||
addToSlice(&e.Authors, p)
|
||||
e.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteAuthor deletes the Person at index from the Entry. It return an error.
|
||||
func (e *Entry) DeleteAuthor(index int) error {
|
||||
if err := deleteFromSlice(&e.Authors, index); err != nil {
|
||||
return fmt.Errorf("error deleting author %v from entry %v: %v", index, e.ID.URI, err)
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
e.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddCategory adds the Category to the Entry.
|
||||
func (e *Entry) AddCategory(c *Category) {
|
||||
if e.Categories == nil {
|
||||
e.Categories = make([]*Category, 1)
|
||||
e.Categories[0] = c
|
||||
} else {
|
||||
e.Categories = append(e.Categories, c)
|
||||
addToSlice(&e.Categories, c)
|
||||
e.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteCategory deletes the Category at index from the Entry. It return an
|
||||
// error.
|
||||
func (e *Entry) DeleteCategory(index int) error {
|
||||
if err := deleteFromSlice(&e.Categories, index); err != nil {
|
||||
return fmt.Errorf("error deleting category %v from entry %v: %v", index, e.ID.URI, err)
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
e.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddContributor adds the Person as a contributor to the Entry.
|
||||
func (e *Entry) AddContributor(c *Person) {
|
||||
if e.Contributors == nil {
|
||||
e.Contributors = make([]*Person, 1)
|
||||
e.Contributors[0] = c
|
||||
} else {
|
||||
e.Contributors = append(e.Contributors, c)
|
||||
addToSlice(&e.Contributors, c)
|
||||
e.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteContributor deletes the Person at index from the Entry. It return an
|
||||
// error.
|
||||
func (e *Entry) DeleteContributor(index int) error {
|
||||
if err := deleteFromSlice(&e.Contributors, index); err != nil {
|
||||
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, e.ID.URI, err)
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
e.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddLink adds the Link to the Entry.
|
||||
func (e *Entry) AddLink(l *Link) {
|
||||
if e.Links == nil {
|
||||
e.Links = make([]*Link, 1)
|
||||
e.Links[0] = l
|
||||
} else {
|
||||
e.Links = append(e.Links, l)
|
||||
addToSlice(&e.Links, l)
|
||||
e.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteLink deletes the Link at index from the Entry. It return an error.
|
||||
func (e *Entry) DeleteLink(index int) error {
|
||||
if err := deleteFromSlice(&e.Links, index); err != nil {
|
||||
return fmt.Errorf("error deleting link %v from entry %v: %v", index, e.ID.URI, err)
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
e.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddExtension adds the ExtensionElement to the Entry.
|
||||
func (e *Entry) AddExtension(x *ExtensionElement) {
|
||||
if e.Extensions == nil {
|
||||
e.Extensions = make([]*ExtensionElement, 1)
|
||||
e.Extensions[0] = x
|
||||
} else {
|
||||
e.Extensions = append(e.Extensions, x)
|
||||
addToSlice(&e.Extensions, x)
|
||||
e.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteExtension deletes the Extension at index from the Entry. It return an
|
||||
// error.
|
||||
func (e *Entry) DeleteExtension(index int) error {
|
||||
if err := deleteFromSlice(&e.Extensions, index); err != nil {
|
||||
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, e.ID.URI, err)
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
e.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Entry for incompatibilities with RFC4287. It returns an
|
||||
@ -138,64 +161,64 @@ func (e *Entry) Check() error {
|
||||
}
|
||||
|
||||
if err := e.checkAuthors(true); err != nil {
|
||||
return fmt.Errorf("entry %v: %v", e, err)
|
||||
return fmt.Errorf("entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
|
||||
for i, c := range e.Categories {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("category element %v of entry %v: %v", i, e, err)
|
||||
return fmt.Errorf("category element %v of entry %v: %v", i, e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Content != nil {
|
||||
if err := e.Content.Check(); err != nil {
|
||||
return fmt.Errorf("content element of entry %v: %v", e, err)
|
||||
return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
} else {
|
||||
// atom:entry elements that contain no child atom:content element MUST
|
||||
// contain at least one atom:link element with a rel attribute value of
|
||||
// "alternate".
|
||||
if !alternateRelExists(e.Links) {
|
||||
return fmt.Errorf("no content element of entry %v and no link element with rel \"alternate\"", e)
|
||||
return fmt.Errorf("no content element of entry %v and no link element with rel \"alternate\"", e.ID.URI)
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range e.Contributors {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("contributor element %v of entry %v: %v", i, e, err)
|
||||
return fmt.Errorf("contributor element %v of entry %v: %v", i, e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, l := range e.Links {
|
||||
if err := l.Check(); err != nil {
|
||||
return fmt.Errorf("link element %v of entry %v: %v", i, e, err)
|
||||
return fmt.Errorf("link element %v of entry %v: %v", i, e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
if hasAlternateDuplicateLinks(e.Links) {
|
||||
return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in entry %v", e)
|
||||
return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in entry %v", e.ID.URI)
|
||||
}
|
||||
|
||||
if e.Published != nil {
|
||||
if err := e.Published.Check(); err != nil {
|
||||
return fmt.Errorf("published element of entry %v: %v", e, err)
|
||||
return fmt.Errorf("published element of entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Rights != nil {
|
||||
if err := e.Rights.Check(); err != nil {
|
||||
return fmt.Errorf("rights element of entry %v: %v", e, err)
|
||||
return fmt.Errorf("rights element of entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Source != nil {
|
||||
if err := e.Source.Check(); err != nil {
|
||||
return fmt.Errorf("source element of entry %v: %v", e, err)
|
||||
return fmt.Errorf("source element of entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Summary != nil {
|
||||
if err := e.Summary.Check(); err != nil {
|
||||
return fmt.Errorf("summary element of entry %v: %v", e, err)
|
||||
return fmt.Errorf("summary element of entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
} else {
|
||||
// atom:entry elements MUST contain an atom:summary element in either
|
||||
@ -203,7 +226,7 @@ func (e *Entry) Check() error {
|
||||
// the atom:entry contains an atom:content that has a "src" attribute
|
||||
// (and is thus empty).
|
||||
if e.Content.hasSRC() {
|
||||
return fmt.Errorf("no summary element of entry %v but content of type out of line content", e)
|
||||
return fmt.Errorf("no summary element of entry %v but content of type out of line content", e.ID.URI)
|
||||
}
|
||||
// the atom:entry contains content that is encoded in Base64; i.e., the
|
||||
// "type" attribute of atom:content is a MIME media type [MIMEREG], but
|
||||
@ -211,29 +234,29 @@ func (e *Entry) Check() error {
|
||||
// does not end with "/xml" or "+xml".
|
||||
mediaType := e.Content.getType()
|
||||
if isValidMediaType(mediaType) && !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") {
|
||||
return fmt.Errorf("no summary element of entry %v but media type not xml", e)
|
||||
return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Title == nil {
|
||||
return fmt.Errorf("no title element of entry %v", e)
|
||||
return fmt.Errorf("no title element of entry %v", e.ID.URI)
|
||||
} else {
|
||||
if err := e.Title.Check(); err != nil {
|
||||
return fmt.Errorf("title element of entry %v: %v", e, err)
|
||||
return fmt.Errorf("title element of entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Updated == nil {
|
||||
return fmt.Errorf("no updated element of entry %v", e)
|
||||
return fmt.Errorf("no updated element of entry %v", e.ID.URI)
|
||||
} else {
|
||||
if err := e.Updated.Check(); err != nil {
|
||||
return fmt.Errorf("updated element of entry %v: %v", e, err)
|
||||
return fmt.Errorf("updated element of entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, x := range e.Extensions {
|
||||
if err := x.Check(); err != nil {
|
||||
return fmt.Errorf("extension element %v of entry %v: %v", i, e, err)
|
||||
return fmt.Errorf("extension element %v of entry %v: %v", i, e.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -244,7 +267,7 @@ func (e *Entry) Check() error {
|
||||
func (e *Entry) ToXML(encoding string) (string, error) {
|
||||
xml, err := xml.MarshalIndent(e, "", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error xml encoding entry: %v", err)
|
||||
return "", fmt.Errorf("error xml encoding entry %v: %v", e.ID.URI, err)
|
||||
}
|
||||
|
||||
return fmt.Sprintln(`<?xml version="1.0" encoding="`+encoding+`"?>`) + string(xml), nil
|
||||
|
157
feed.go
157
feed.go
@ -37,74 +37,123 @@ func NewFeed(title string) *Feed {
|
||||
|
||||
// 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)
|
||||
addToSlice(&f.Authors, p)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteAuthor deletes the Person at index from the Feed. It return an error.
|
||||
func (f *Feed) DeleteAuthor(index int) error {
|
||||
if err := deleteFromSlice(&f.Authors, index); err != nil {
|
||||
return fmt.Errorf("error deleting author %v from entry %v: %v", index, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
f.Updated = NewDate(time.Now())
|
||||
return 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
|
||||
} else {
|
||||
f.Categories = append(f.Categories, c)
|
||||
addToSlice(&f.Categories, c)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteCategory deletes the Category at index from the Feed. It return an
|
||||
// error.
|
||||
func (f *Feed) DeleteCategory(index int) error {
|
||||
if err := deleteFromSlice(&f.Categories, index); err != nil {
|
||||
return fmt.Errorf("error deleting category %v from entry %v: %v", index, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
f.Updated = NewDate(time.Now())
|
||||
return 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
|
||||
} else {
|
||||
f.Contributors = append(f.Contributors, c)
|
||||
addToSlice(&f.Contributors, c)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteContributor deletes the Person at index from the Feed. It return an
|
||||
// error.
|
||||
func (f *Feed) DeleteContributor(index int) error {
|
||||
if err := deleteFromSlice(&f.Contributors, index); err != nil {
|
||||
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
f.Updated = NewDate(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) {
|
||||
if f.Links == nil {
|
||||
f.Links = make([]*Link, 1)
|
||||
f.Links[0] = l
|
||||
} else {
|
||||
f.Links = append(f.Links, l)
|
||||
addToSlice(&f.Links, l)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteLink deletes the Link at index from the Feed. It return an error.
|
||||
func (f *Feed) DeleteLink(index int) error {
|
||||
if err := deleteFromSlice(&f.Links, index); err != nil {
|
||||
return fmt.Errorf("error deleting link %v from entry %v: %v", index, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
f.Updated = NewDate(time.Now())
|
||||
return 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
|
||||
} else {
|
||||
f.Extensions = append(f.Extensions, e)
|
||||
addToSlice(&f.Extensions, e)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteExtension deletes the Extension at index from the Feed. It return an
|
||||
// error.
|
||||
func (f *Feed) DeleteExtension(index int) error {
|
||||
if err := deleteFromSlice(&f.Extensions, index); err != nil {
|
||||
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
f.Updated = NewDate(time.Now())
|
||||
return 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
|
||||
} else {
|
||||
f.Entries = append(f.Entries, e)
|
||||
addToSlice(&f.Entries, e)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteEntry deletes the Entry at index from the Feed. It return an error.
|
||||
func (f *Feed) DeleteEntry(index int) error {
|
||||
if err := deleteFromSlice(&f.Entries, index); err != nil {
|
||||
return fmt.Errorf("error deleting entry %v from entry %v: %v", index, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
f.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteEntryByURI deletes the Entry from the Feed. It return an error.
|
||||
func (f *Feed) DeleteEntryByURI(uri string) error {
|
||||
if !isValidIRI(uri) {
|
||||
return fmt.Errorf("error deleting entry from feed %v: uri %v invalid", f.ID.URI, uri)
|
||||
}
|
||||
|
||||
index := -1
|
||||
for i, e := range f.Entries {
|
||||
if e.ID.URI == uri {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if index < 0 {
|
||||
return fmt.Errorf("error deleting entry from feed %v: id %v not found", f.ID.URI, uri)
|
||||
}
|
||||
|
||||
f.Entries = append(f.Entries[:index], f.Entries[index+1:]...)
|
||||
f.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Feed for incompatibilities with RFC4287. It returns an
|
||||
@ -124,93 +173,93 @@ func (f *Feed) Check() error {
|
||||
if f.Authors == nil {
|
||||
for _, e := range f.Entries {
|
||||
if err := e.checkAuthors(false); err != nil {
|
||||
return fmt.Errorf("no authors set in feed %v: %v", f, err)
|
||||
return fmt.Errorf("no authors set in feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i, a := range f.Authors {
|
||||
if err := a.Check(); err != nil {
|
||||
return fmt.Errorf("author element %v of feed %v: %v", i, f, err)
|
||||
return fmt.Errorf("author element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range f.Categories {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("category element %v of feed %v: %v", i, f, err)
|
||||
return fmt.Errorf("category element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range f.Contributors {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("contributor element %v of feed %v: %v", i, f, err)
|
||||
return fmt.Errorf("contributor element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Generator != nil {
|
||||
if err := f.Generator.Check(); err != nil {
|
||||
return fmt.Errorf("generator element of feed %v: %v", f, err)
|
||||
return fmt.Errorf("generator element of feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Icon != nil {
|
||||
if err := f.Icon.Check(); err != nil {
|
||||
return fmt.Errorf("icon element of feed %v: %v", f, err)
|
||||
return fmt.Errorf("icon element of feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, l := range f.Links {
|
||||
if err := l.Check(); err != nil {
|
||||
return fmt.Errorf("link element %v of feed %v: %v", i, f, err)
|
||||
return fmt.Errorf("link element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
if hasAlternateDuplicateLinks(f.Links) {
|
||||
return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in feed %v", f)
|
||||
return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in feed %v", f.ID.URI)
|
||||
}
|
||||
|
||||
if f.Logo != nil {
|
||||
if err := f.Logo.Check(); err != nil {
|
||||
return fmt.Errorf("logo element of feed %v: %v", f, err)
|
||||
return fmt.Errorf("logo element of feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Rights != nil {
|
||||
if err := f.Rights.Check(); err != nil {
|
||||
return fmt.Errorf("rights element of feed %v: %v", f, err)
|
||||
return fmt.Errorf("rights element of feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Subtitle != nil {
|
||||
if err := f.Subtitle.Check(); err != nil {
|
||||
return fmt.Errorf("subtitle element of feed %v: %v", f, err)
|
||||
return fmt.Errorf("subtitle element of feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Title == nil {
|
||||
return fmt.Errorf("no title element of feed %v", f)
|
||||
return fmt.Errorf("no title element of feed %v", f.ID.URI)
|
||||
} else {
|
||||
if err := f.Title.Check(); err != nil {
|
||||
return fmt.Errorf("title element of feed %v: %v", f, err)
|
||||
return fmt.Errorf("title element of feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Updated == nil {
|
||||
return fmt.Errorf("no updated element of feed %v", f)
|
||||
return fmt.Errorf("no updated element of feed %v", f.ID.URI)
|
||||
} else {
|
||||
if err := f.Updated.Check(); err != nil {
|
||||
return fmt.Errorf("updated element of feed %v: %v", f, err)
|
||||
return fmt.Errorf("updated element of feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, x := range f.Extensions {
|
||||
if err := x.Check(); err != nil {
|
||||
return fmt.Errorf("extension element %v of feed %v: %v", i, f, err)
|
||||
return fmt.Errorf("extension element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, n := range f.Entries {
|
||||
if err := n.Check(); err != nil {
|
||||
return fmt.Errorf("entry element %v of feed %v: %v", i, f, err)
|
||||
return fmt.Errorf("entry element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -221,7 +270,7 @@ func (f *Feed) Check() error {
|
||||
func (f *Feed) ToXML(encoding string) (string, error) {
|
||||
xml, err := xml.MarshalIndent(f, "", " ")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error xml encoding feed: %v", err)
|
||||
return "", fmt.Errorf("error xml encoding feed %v: %v", f.ID.URI, err)
|
||||
}
|
||||
|
||||
return fmt.Sprintln(`<?xml version="1.0" encoding="`+encoding+`"?>`) + string(xml), nil
|
||||
|
22
person.go
22
person.go
@ -23,12 +23,16 @@ func NewPerson(name string) *Person {
|
||||
|
||||
// AddExtension adds the Extension to the Person.
|
||||
func (p *Person) AddExtension(e *ExtensionElement) {
|
||||
if p.Extensions == nil {
|
||||
p.Extensions = make([]*ExtensionElement, 1)
|
||||
p.Extensions[0] = e
|
||||
} else {
|
||||
p.Extensions = append(p.Extensions, e)
|
||||
addToSlice(&p.Extensions, e)
|
||||
}
|
||||
|
||||
// DeleteExtension deletes the Extension at index from the Person. It return an
|
||||
// error.
|
||||
func (p *Person) DeleteExtension(index int) error {
|
||||
if err := deleteFromSlice(&p.Extensions, index); err != nil {
|
||||
return fmt.Errorf("error deleting extension %v from person %v: %v", index, p, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Person for incompatibilities with RFC4287. It returns an
|
||||
@ -50,11 +54,9 @@ func (p *Person) Check() error {
|
||||
}
|
||||
}
|
||||
|
||||
if p.Extensions != nil {
|
||||
for i, e := range p.Extensions {
|
||||
if err := e.Check(); err != nil {
|
||||
return fmt.Errorf("extension element %v of person %v: %v", i, p, err)
|
||||
}
|
||||
for i, e := range p.Extensions {
|
||||
if err := e.Check(); err != nil {
|
||||
return fmt.Errorf("extension element %v of person %v: %v", i, p, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
68
source.go
68
source.go
@ -28,6 +28,74 @@ func NewSource() *Source {
|
||||
return &Source{CommonAttributes: newCommonAttributes()}
|
||||
}
|
||||
|
||||
// AddAuthor adds the Person as an author to the Source.
|
||||
func (s *Source) AddAuthor(p *Person) {
|
||||
addToSlice(&s.Authors, p)
|
||||
}
|
||||
|
||||
// DeleteAuthor deletes the Person at index from the Source. It return an error.
|
||||
func (s *Source) DeleteAuthor(index int) error {
|
||||
if err := deleteFromSlice(&s.Authors, index); err != nil {
|
||||
return fmt.Errorf("error deleting author %v from source %v: %v", index, s, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddCategory adds the Category to the Source.
|
||||
func (s *Source) AddCategory(c *Category) {
|
||||
addToSlice(&s.Categories, c)
|
||||
}
|
||||
|
||||
// DeleteCategory deletes the Category at index from the Source. It return an
|
||||
// error.
|
||||
func (s *Source) DeleteCategory(index int) error {
|
||||
if err := deleteFromSlice(&s.Categories, index); err != nil {
|
||||
return fmt.Errorf("error deleting category %v from source %v: %v", index, s, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddContributor adds the Person as a contributor to the Source.
|
||||
func (s *Source) AddContributor(c *Person) {
|
||||
addToSlice(&s.Contributors, c)
|
||||
}
|
||||
|
||||
// DeleteContributor deletes the Person at index from the Source. It return an
|
||||
// error.
|
||||
func (s *Source) DeleteContributor(index int) error {
|
||||
if err := deleteFromSlice(&s.Contributors, index); err != nil {
|
||||
return fmt.Errorf("error deleting contributor %v from source %v: %v", index, s, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddLink adds the Link to the Source.
|
||||
func (s *Source) AddLink(l *Link) {
|
||||
addToSlice(&s.Links, l)
|
||||
}
|
||||
|
||||
// DeleteLink deletes the Link at index from the Source. It return an error.
|
||||
func (s *Source) DeleteLink(index int) error {
|
||||
if err := deleteFromSlice(&s.Links, index); err != nil {
|
||||
return fmt.Errorf("error deleting link %v from source %v: %v", index, s, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddExtension adds the ExtensionElement to the Source.
|
||||
func (s *Source) AddExtension(e *ExtensionElement) {
|
||||
addToSlice(&s.Extensions, e)
|
||||
}
|
||||
|
||||
// DeleteExtension deletes the Extension at index from the Source. It return an
|
||||
// error.
|
||||
func (s *Source) DeleteExtension(index int) error {
|
||||
if err := deleteFromSlice(&s.Extensions, index); err != nil {
|
||||
return fmt.Errorf("error deleting extension %v from source %v: %v", index, s, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Source for incompatibilities with RFC4287. It returns an
|
||||
// error.
|
||||
func (s *Source) Check() error {
|
||||
|
Loading…
x
Reference in New Issue
Block a user