7 Commits

7 changed files with 171 additions and 277 deletions

View File

@ -2,7 +2,7 @@
An extensible Atom feed generator library that aims to be very close to RFC4287.
It diligently checks for compliance with the standard and provides functions for
easy creation and extension of elements.
easy creation, extension and deletion of elements.
## Installation
@ -14,9 +14,9 @@ go get git.streifling.com/jason/atom@latest
## Usage
This library provides convenient functions to safely create and extend elements
and attributes of Atom feeds. It also provides checks for all constructs'
adherence to RFC4287.
This library provides convenient functions to safely create, extend and delete
elements and attributes of Atom feeds. It also provides checks for all
constructs' adherence to RFC4287.
```go
package main
@ -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)

27
atom.go
View File

@ -1,6 +1,7 @@
package atom
import (
"encoding/xml"
"fmt"
"html"
"mime"
@ -11,6 +12,32 @@ import (
"golang.org/x/text/language"
)
type Countable interface {
*xml.Attr | *Person | *Category | *Link | *ExtensionElement | *Entry
}
// addToSlice adds a Countable to to a *[]Countable. It returns an int.
func addToSlice[C Countable](slice *[]C, countable C) int {
if *slice == nil {
*slice = make([]C, 0)
}
*slice = append(*slice, countable)
return len(*slice) - 1
}
// 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 {

View File

@ -17,25 +17,17 @@ func newCommonAttributes() *CommonAttributes {
return new(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})
}
// AddAttribute adds the attribute to the CommonAttributes. It returns an int.
func (c *CommonAttributes) AddAttribute(name, value string) int {
return addToSlice(&c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
}
// 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)
// 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)
}
c.UndefinedAttributes = append(c.UndefinedAttributes[:id], c.UndefinedAttributes[id+1:]...)
return nil
}

114
entry.go
View File

@ -66,122 +66,86 @@ 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)
}
// AddAuthor adds the Person as an author to the Entry. It returns an int.
func (e *Entry) AddAuthor(p *Person) int {
e.Updated = NewDate(time.Now())
return addToSlice(&e.Authors, p)
}
// 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)
// 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.Authors = append(e.Authors[:id], e.Authors[id+1:]...)
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)
}
// AddCategory adds the Category to the Entry. It returns an int.
func (e *Entry) AddCategory(c *Category) int {
e.Updated = NewDate(time.Now())
return addToSlice(&e.Categories, c)
}
// 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)
// 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.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.
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)
}
// AddContributor adds the Person as a contributor to the Entry. It returns an
// int.
func (e *Entry) AddContributor(c *Person) int {
e.Updated = NewDate(time.Now())
return addToSlice(&e.Contributors, c)
}
// 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)
// 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.Contributors = append(e.Contributors[:id], e.Contributors[id+1:]...)
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)
}
// AddLink adds the Link to the Entry. It returns an int.
func (e *Entry) AddLink(l *Link) int {
e.Updated = NewDate(time.Now())
return addToSlice(&e.Links, l)
}
// 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)
// 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.Links = append(e.Links[:id], e.Links[id+1:]...)
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)
}
// AddExtension adds the ExtensionElement to the Entry. It returns an int.
func (e *Entry) AddExtension(x *ExtensionElement) int {
e.Updated = NewDate(time.Now())
return addToSlice(&e.Extensions, x)
}
// 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)
// 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.Extensions = append(e.Extensions[:id], e.Extensions[id+1:]...)
e.Updated = NewDate(time.Now())
return nil
}

135
feed.go
View File

@ -35,146 +35,103 @@ 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)
}
// AddAuthor adds the Person as an author to the Feed. It returns an int.
func (f *Feed) AddAuthor(p *Person) int {
f.Updated = NewDate(time.Now())
return addToSlice(&f.Authors, p)
}
// 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)
// 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.Authors = append(f.Authors[:id], f.Authors[id+1:]...)
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)
}
// AddCategory adds the Category to the Feed. It returns an int.
func (f *Feed) AddCategory(c *Category) int {
f.Updated = NewDate(time.Now())
return addToSlice(&f.Categories, c)
}
// 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)
// 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.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.
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)
}
// AddContributor adds the Person as a contributor to the Feed. It returns an
// int.
func (f *Feed) AddContributor(c *Person) int {
f.Updated = NewDate(time.Now())
return addToSlice(&f.Contributors, c)
}
// 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)
// 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.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".
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)
}
// It returns an int.
func (f *Feed) AddLink(l *Link) int {
f.Updated = NewDate(time.Now())
return addToSlice(&f.Links, l)
}
// 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)
// 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.Links = append(f.Links[:id], f.Links[id+1:]...)
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)
}
// AddExtension adds the Extension to the Feed. It returns an int.
func (f *Feed) AddExtension(e *ExtensionElement) int {
f.Updated = NewDate(time.Now())
return addToSlice(&f.Extensions, e)
}
// 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)
// 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.Extensions = append(f.Extensions[:id], f.Extensions[id+1:]...)
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)
}
// AddEntry adds the Entry to the Feed. It returns an int.
func (f *Feed) AddEntry(e *Entry) int {
f.Updated = NewDate(time.Now())
return addToSlice(&f.Entries, e)
}
// 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)
// 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.Entries = append(f.Entries[:id], f.Entries[id+1:]...)
f.Updated = NewDate(time.Now())
return nil
}

View File

@ -21,24 +21,17 @@ 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)
}
// AddExtension adds the Extension to the Person. It returns an int.
func (p *Person) AddExtension(e *ExtensionElement) int {
return addToSlice(&p.Extensions, e)
}
// 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)
// 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)
}
p.Extensions = append(p.Extensions[:id], p.Extensions[id+1:]...)
return nil
}

114
source.go
View File

@ -28,108 +28,72 @@ func NewSource() *Source {
return &Source{CommonAttributes: newCommonAttributes()}
}
// 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)
s.Authors[0] = p
} else {
s.Authors = append(s.Authors, p)
}
// AddAuthor adds the Person as an author to the Source. It returns an int.
func (s *Source) AddAuthor(p *Person) int {
return addToSlice(&s.Authors, p)
}
// 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)
// 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)
}
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)
s.Categories[0] = c
} else {
s.Categories = append(s.Categories, c)
}
// AddCategory adds the Category to the Source. It returns an int.
func (s *Source) AddCategory(c *Category) int {
return addToSlice(&s.Categories, c)
}
// 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)
// 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)
}
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)
s.Contributors[0] = c
} else {
s.Contributors = append(s.Contributors, c)
}
// AddContributor adds the Person as a contributor to the Source. It returns an
// int.
func (s *Source) AddContributor(c *Person) int {
return addToSlice(&s.Contributors, c)
}
// 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)
// 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)
}
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)
s.Links[0] = l
} else {
s.Links = append(s.Links, l)
}
// AddLink adds the Link to the Source. It returns an int.
func (s *Source) AddLink(l *Link) int {
return addToSlice(&s.Links, l)
}
// 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)
// 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)
}
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)
s.Extensions[0] = e
} else {
s.Extensions = append(s.Extensions, e)
}
// AddExtension adds the ExtensionElement to the Source. It returns an int.
func (s *Source) AddExtension(e *ExtensionElement) int {
return addToSlice(&s.Extensions, e)
}
// 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)
// 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)
}
s.Extensions = append(s.Extensions[:id], s.Extensions[id+1:]...)
return nil
}