diff --git a/commonAttributes.go b/commonAttributes.go index 477ede5..875eea7 100644 --- a/commonAttributes.go +++ b/commonAttributes.go @@ -17,7 +17,7 @@ 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) @@ -27,6 +27,18 @@ func (c *CommonAttributes) AddAttribute(name, value string) { } } +// DeleteAttribute deletes the attribute from the CommonAttributes. It return an +// error. +func (c *CommonAttributes) DeleteAttribute(id int) error { + length := len(c.UndefinedAttributes) + if id > length { + return fmt.Errorf("error deleting undefined attribute from common attributes %v: id %v out of range %v", c, id, length) + } + + c.UndefinedAttributes = append(c.UndefinedAttributes[:id], c.UndefinedAttributes[id+1:]...) + return nil +} + // Check checks the CommonAttributes for incompatibilities with RFC4287. It // returns an error. func (c *CommonAttributes) Check() error { diff --git a/entry.go b/entry.go index 802be20..6b74f5f 100644 --- a/entry.go +++ b/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) } } } @@ -75,7 +75,19 @@ func (e *Entry) AddAuthor(p *Person) { e.Authors = append(e.Authors, p) } - e.Updated.DateTime = DateTime(time.Now()) + e.Updated = NewDate(time.Now()) +} + +// DeleteAuthor deletes the Person from the Entry. It return an error. +func (e *Entry) DeleteAuthor(id int) error { + length := len(e.Authors) + if id > length { + return fmt.Errorf("error deleting author from entry %v: id %v out of range %v", e.ID.URI, id, length) + } + + e.Authors = append(e.Authors[:id], e.Authors[id+1:]...) + e.Updated = NewDate(time.Now()) + return nil } // AddCategory adds the Category to the Entry. @@ -87,7 +99,19 @@ func (e *Entry) AddCategory(c *Category) { e.Categories = append(e.Categories, c) } - e.Updated.DateTime = DateTime(time.Now()) + e.Updated = NewDate(time.Now()) +} + +// DeleteCategory deletes the Category from the Entry. It return an error. +func (e *Entry) DeleteCategory(id int) error { + length := len(e.Categories) + if id > length { + return fmt.Errorf("error deleting category from entry %v: id %v out of range %v", e.ID.URI, id, length) + } + + e.Categories = append(e.Categories[:id], e.Categories[id+1:]...) + e.Updated = NewDate(time.Now()) + return nil } // AddContributor adds the Person as a contributor to the Entry. @@ -99,7 +123,19 @@ func (e *Entry) AddContributor(c *Person) { e.Contributors = append(e.Contributors, c) } - e.Updated.DateTime = DateTime(time.Now()) + e.Updated = NewDate(time.Now()) +} + +// DeleteContributor deletes the Person from the Entry. It return an error. +func (e *Entry) DeleteContributor(id int) error { + length := len(e.Contributors) + if id > length { + return fmt.Errorf("error deleting contributor from entry %v: id %v out of range %v", e.ID.URI, id, length) + } + + e.Contributors = append(e.Contributors[:id], e.Contributors[id+1:]...) + e.Updated = NewDate(time.Now()) + return nil } // AddLink adds the Link to the Entry. @@ -111,7 +147,19 @@ func (e *Entry) AddLink(l *Link) { e.Links = append(e.Links, l) } - e.Updated.DateTime = DateTime(time.Now()) + e.Updated = NewDate(time.Now()) +} + +// DeleteLink deletes the Link from the Entry. It return an error. +func (e *Entry) DeleteLink(id int) error { + length := len(e.Links) + if id > length { + return fmt.Errorf("error deleting link from entry %v: id %v out of range %v", e.ID.URI, id, length) + } + + e.Links = append(e.Links[:id], e.Links[id+1:]...) + e.Updated = NewDate(time.Now()) + return nil } // AddExtension adds the ExtensionElement to the Entry. @@ -123,7 +171,19 @@ func (e *Entry) AddExtension(x *ExtensionElement) { e.Extensions = append(e.Extensions, x) } - e.Updated.DateTime = DateTime(time.Now()) + e.Updated = NewDate(time.Now()) +} + +// DeleteExtension deletes the Extension from the Entry. It return an error. +func (e *Entry) DeleteExtension(id int) error { + length := len(e.Extensions) + if id > length { + return fmt.Errorf("error deleting extension from entry %v: id %v out of range %v", e.ID.URI, id, length) + } + + e.Extensions = append(e.Extensions[:id], e.Extensions[id+1:]...) + e.Updated = NewDate(time.Now()) + return nil } // Check checks the Entry for incompatibilities with RFC4287. It returns an @@ -138,64 +198,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 +263,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 +271,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 +304,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(``) + string(xml), nil diff --git a/feed.go b/feed.go index 22d72fb..1ba637d 100644 --- a/feed.go +++ b/feed.go @@ -44,7 +44,19 @@ func (f *Feed) AddAuthor(p *Person) { f.Authors = append(f.Authors, p) } - f.Updated.DateTime = DateTime(time.Now()) + f.Updated = NewDate(time.Now()) +} + +// DeleteAuthor deletes the Person from the Feed. It return an error. +func (f *Feed) DeleteAuthor(id int) error { + length := len(f.Authors) + if id > length { + return fmt.Errorf("error deleting author from feed %v: id %v out of range %v", f.ID.URI, id, length) + } + + f.Authors = append(f.Authors[:id], f.Authors[id+1:]...) + f.Updated = NewDate(time.Now()) + return nil } // AddCategory adds the Category to the Feed. @@ -56,7 +68,19 @@ func (f *Feed) AddCategory(c *Category) { f.Categories = append(f.Categories, c) } - f.Updated.DateTime = DateTime(time.Now()) + f.Updated = NewDate(time.Now()) +} + +// DeleteCategory deletes the Category from the Feed. It return an error. +func (f *Feed) DeleteCategory(id int) error { + length := len(f.Categories) + if id > length { + return fmt.Errorf("error deleting category from feed %v: id %v out of range %v", f.ID.URI, id, length) + } + + f.Categories = append(f.Categories[:id], f.Categories[id+1:]...) + f.Updated = NewDate(time.Now()) + return nil } // AddContributor adds the Person as a contributor to the Feed. @@ -68,7 +92,19 @@ func (f *Feed) AddContributor(c *Person) { f.Contributors = append(f.Contributors, c) } - f.Updated.DateTime = DateTime(time.Now()) + f.Updated = NewDate(time.Now()) +} + +// DeleteContributor deletes the Person from the Feed. It return an error. +func (f *Feed) DeleteContributor(id int) error { + length := len(f.Contributors) + if id > length { + return fmt.Errorf("error deleting contributor from feed %v: id %v out of range %v", f.ID.URI, id, length) + } + + f.Contributors = append(f.Contributors[:id], f.Contributors[id+1:]...) + f.Updated = NewDate(time.Now()) + return nil } // AddLink adds the Link to the Feed. There should be one Link with Rel "self". @@ -80,7 +116,19 @@ func (f *Feed) AddLink(l *Link) { f.Links = append(f.Links, l) } - f.Updated.DateTime = DateTime(time.Now()) + f.Updated = NewDate(time.Now()) +} + +// DeleteLink deletes the Link from the Feed. It return an error. +func (f *Feed) DeleteLink(id int) error { + length := len(f.Links) + if id > length { + return fmt.Errorf("error deleting link from feed %v: id %v out of range %v", f.ID.URI, id, length) + } + + f.Links = append(f.Links[:id], f.Links[id+1:]...) + f.Updated = NewDate(time.Now()) + return nil } // AddExtension adds the Extension to the Feed. @@ -92,7 +140,19 @@ func (f *Feed) AddExtension(e *ExtensionElement) { f.Extensions = append(f.Extensions, e) } - f.Updated.DateTime = DateTime(time.Now()) + f.Updated = NewDate(time.Now()) +} + +// DeleteExtension deletes the Extension from the Feed. It return an error. +func (f *Feed) DeleteExtension(id int) error { + length := len(f.Extensions) + if id > length { + return fmt.Errorf("error deleting extension from feed %v: id %v out of range %v", f.ID.URI, id, length) + } + + f.Extensions = append(f.Extensions[:id], f.Extensions[id+1:]...) + f.Updated = NewDate(time.Now()) + return nil } // AddEntry adds the Entry to the Feed. @@ -104,7 +164,41 @@ func (f *Feed) AddEntry(e *Entry) { f.Entries = append(f.Entries, e) } - f.Updated.DateTime = DateTime(time.Now()) + f.Updated = NewDate(time.Now()) +} + +// DeleteEntry deletes the Entry from the Feed. It return an error. +func (f *Feed) DeleteEntry(id int) error { + length := len(f.Entries) + if id > length { + return fmt.Errorf("error deleting entry from feed %v: id %v out of range %v", f.ID.URI, id, length) + } + + f.Entries = append(f.Entries[:id], f.Entries[id+1:]...) + 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 +218,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 +315,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(``) + string(xml), nil diff --git a/person.go b/person.go index 133e30b..2864681 100644 --- a/person.go +++ b/person.go @@ -31,6 +31,17 @@ func (p *Person) AddExtension(e *ExtensionElement) { } } +// DeleteExtension deletes the Extension from the Person. It return an error. +func (p *Person) DeleteExtension(id int) error { + length := len(p.Extensions) + if id > length { + return fmt.Errorf("error deleting extension from person %v: id %v out of range %v", p, id, length) + } + + p.Extensions = append(p.Extensions[:id], p.Extensions[id+1:]...) + return nil +} + // Check checks the Person for incompatibilities with RFC4287. It returns an // error. func (p *Person) Check() error { @@ -50,11 +61,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) } } diff --git a/source.go b/source.go index 1be908a..3af7601 100644 --- a/source.go +++ b/source.go @@ -28,7 +28,7 @@ func NewSource() *Source { return &Source{CommonAttributes: newCommonAttributes()} } -// AddAuthor adds the Person as an author to the Entry. +// AddAuthor adds the Person as an author to the Source. func (s *Source) AddAuthor(p *Person) { if s.Authors == nil { s.Authors = make([]*Person, 1) @@ -38,7 +38,18 @@ func (s *Source) AddAuthor(p *Person) { } } -// AddCategory adds the Category to the Entry. +// DeleteAuthor deletes the Person from the Source. It return an error. +func (s *Source) DeleteAuthor(id int) error { + length := len(s.Authors) + if id > length { + return fmt.Errorf("error deleting author from source %v: id %v out of range %v", s, id, length) + } + + s.Authors = append(s.Authors[:id], s.Authors[id+1:]...) + return nil +} + +// AddCategory adds the Category to the Source. func (s *Source) AddCategory(c *Category) { if s.Categories == nil { s.Categories = make([]*Category, 1) @@ -48,7 +59,18 @@ func (s *Source) AddCategory(c *Category) { } } -// AddContributor adds the Person as a contributor to the Entry. +// DeleteCategory deletes the Category from the Source. It return an error. +func (s *Source) DeleteCategory(id int) error { + length := len(s.Categories) + if id > length { + return fmt.Errorf("error deleting category from source %v: id %v out of range %v", s, id, length) + } + + s.Categories = append(s.Categories[:id], s.Categories[id+1:]...) + return nil +} + +// AddContributor adds the Person as a contributor to the Source. func (s *Source) AddContributor(c *Person) { if s.Contributors == nil { s.Contributors = make([]*Person, 1) @@ -58,7 +80,18 @@ func (s *Source) AddContributor(c *Person) { } } -// AddLink adds the Link to the Entry. +// DeleteContributor deletes the Person from the Source. It return an error. +func (s *Source) DeleteContributor(id int) error { + length := len(s.Contributors) + if id > length { + return fmt.Errorf("error deleting contributor from source %v: id %v out of range %v", s, id, length) + } + + s.Contributors = append(s.Contributors[:id], s.Contributors[id+1:]...) + return nil +} + +// AddLink adds the Link to the Source. func (s *Source) AddLink(l *Link) { if s.Links == nil { s.Links = make([]*Link, 1) @@ -68,7 +101,18 @@ func (s *Source) AddLink(l *Link) { } } -// AddExtension adds the ExtensionElement to the Entry. +// DeleteLink deletes the Link from the Source. It return an error. +func (s *Source) DeleteLink(id int) error { + length := len(s.Links) + if id > length { + return fmt.Errorf("error deleting link from source %v: id %v out of range %v", s, id, length) + } + + s.Links = append(s.Links[:id], s.Links[id+1:]...) + return nil +} + +// AddExtension adds the ExtensionElement to the Source. func (s *Source) AddExtension(e *ExtensionElement) { if s.Extensions == nil { s.Extensions = make([]*ExtensionElement, 1) @@ -78,6 +122,17 @@ func (s *Source) AddExtension(e *ExtensionElement) { } } +// DeleteExtension deletes the Extension from the Source. It return an error. +func (s *Source) DeleteExtension(id int) error { + length := len(s.Extensions) + if id > length { + return fmt.Errorf("error deleting extension from source %v: id %v out of range %v", s, id, length) + } + + s.Extensions = append(s.Extensions[:id], s.Extensions[id+1:]...) + return nil +} + // Check checks the Source for incompatibilities with RFC4287. It returns an // error. func (s *Source) Check() error {