3 Commits

Author SHA1 Message Date
8ce7d54d00 Return the index of the added element 2024-10-20 12:57:24 +02:00
764b143ff8 Changed comments a bit 2024-10-20 12:41:09 +02:00
bcf2532372 Rename id in Delete methods to index 2024-10-20 12:35:26 +02:00
7 changed files with 152 additions and 136 deletions

View File

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

14
atom.go
View File

@ -16,23 +16,25 @@ type Countable interface {
*xml.Attr | *Person | *Category | *Link | *ExtensionElement | *Entry *xml.Attr | *Person | *Category | *Link | *ExtensionElement | *Entry
} }
// addToSlice adds a Countable to to a *[]Countable // addToSlice adds a Countable to to a *[]Countable. It returns an int.
func addToSlice[C Countable](slice *[]C, countable C) { func addToSlice[C Countable](slice *[]C, countable C) int {
if *slice == nil { if *slice == nil {
*slice = make([]C, 0) *slice = make([]C, 0)
} }
*slice = append(*slice, countable) *slice = append(*slice, countable)
return len(*slice) - 1
} }
// deleteFromSlice deletes the Countable with the index from the *[]Countable. // deleteFromSlice deletes the Countable with the index from the *[]Countable.
// It return an error. // It return an error.
func deleteFromSlice[C Countable](slice *[]C, id int) error { func deleteFromSlice[C Countable](slice *[]C, index int) error {
length := len(*slice) length := len(*slice)
if id > length { if index > length {
return fmt.Errorf("id %v out of range %v", id, length) return fmt.Errorf("id %v out of range %v", index, length)
} }
*slice = append((*slice)[:id], (*slice)[id+1:]...) *slice = append((*slice)[:index], (*slice)[index+1:]...)
return nil return nil
} }

View File

@ -17,16 +17,16 @@ func newCommonAttributes() *CommonAttributes {
return new(CommonAttributes) return new(CommonAttributes)
} }
// AddAttribute adds the attribute to the CommonAttributes. // AddAttribute adds the attribute to the CommonAttributes. It returns an int.
func (c *CommonAttributes) AddAttribute(name, value string) { func (c *CommonAttributes) AddAttribute(name, value string) int {
addToSlice(&c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value}) return addToSlice(&c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
} }
// DeleteAttribute deletes the attribute from the CommonAttributes. It return an // DeleteAttribute deletes the attribute at index from the CommonAttributes. It
// error. // return an error.
func (c *CommonAttributes) DeleteAttribute(id int) error { func (c *CommonAttributes) DeleteAttribute(index int) error {
if err := deleteFromSlice(&c.UndefinedAttributes, id); err != nil { if err := deleteFromSlice(&c.UndefinedAttributes, index); err != nil {
return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", id, c, err) return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", index, c, err)
} }
return nil return nil
} }

View File

@ -66,80 +66,84 @@ func NewEntry(title string) *Entry {
} }
} }
// AddAuthor adds the Person as an author to the Entry. // AddAuthor adds the Person as an author to the Entry. It returns an int.
func (e *Entry) AddAuthor(p *Person) { func (e *Entry) AddAuthor(p *Person) int {
addToSlice(&e.Authors, p)
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
return addToSlice(&e.Authors, p)
} }
// DeleteAuthor deletes the Person from the Entry. It return an error. // DeleteAuthor deletes the Person at index from the Entry. It return an error.
func (e *Entry) DeleteAuthor(id int) error { func (e *Entry) DeleteAuthor(index int) error {
if err := deleteFromSlice(&e.Authors, id); err != nil { if err := deleteFromSlice(&e.Authors, index); err != nil {
return fmt.Errorf("error deleting author %v from entry %v: %v", id, e.ID.URI, err) return fmt.Errorf("error deleting author %v from entry %v: %v", index, e.ID.URI, err)
} }
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
return nil return nil
} }
// AddCategory adds the Category to the Entry. // AddCategory adds the Category to the Entry. It returns an int.
func (e *Entry) AddCategory(c *Category) { func (e *Entry) AddCategory(c *Category) int {
addToSlice(&e.Categories, c)
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
return addToSlice(&e.Categories, c)
} }
// DeleteCategory deletes the Category from the Entry. It return an error. // DeleteCategory deletes the Category at index from the Entry. It return an
func (e *Entry) DeleteCategory(id int) error { // error.
if err := deleteFromSlice(&e.Categories, id); err != nil { func (e *Entry) DeleteCategory(index int) error {
return fmt.Errorf("error deleting category %v from entry %v: %v", id, e.ID.URI, err) 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 = NewDate(time.Now()) e.Updated = NewDate(time.Now())
return nil return nil
} }
// AddContributor adds the Person as a contributor to the Entry. // AddContributor adds the Person as a contributor to the Entry. It returns an
func (e *Entry) AddContributor(c *Person) { // int.
addToSlice(&e.Contributors, c) func (e *Entry) AddContributor(c *Person) int {
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
return addToSlice(&e.Contributors, c)
} }
// DeleteContributor deletes the Person from the Entry. It return an error. // DeleteContributor deletes the Person at index from the Entry. It return an
func (e *Entry) DeleteContributor(id int) error { // error.
if err := deleteFromSlice(&e.Contributors, id); err != nil { func (e *Entry) DeleteContributor(index int) error {
return fmt.Errorf("error deleting contributor %v from entry %v: %v", id, e.ID.URI, err) 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 = NewDate(time.Now()) e.Updated = NewDate(time.Now())
return nil return nil
} }
// AddLink adds the Link to the Entry. // AddLink adds the Link to the Entry. It returns an int.
func (e *Entry) AddLink(l *Link) { func (e *Entry) AddLink(l *Link) int {
addToSlice(&e.Links, l)
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
return addToSlice(&e.Links, l)
} }
// DeleteLink deletes the Link from the Entry. It return an error. // DeleteLink deletes the Link at index from the Entry. It return an error.
func (e *Entry) DeleteLink(id int) error { func (e *Entry) DeleteLink(index int) error {
if err := deleteFromSlice(&e.Links, id); err != nil { if err := deleteFromSlice(&e.Links, index); err != nil {
return fmt.Errorf("error deleting link %v from entry %v: %v", id, e.ID.URI, err) return fmt.Errorf("error deleting link %v from entry %v: %v", index, e.ID.URI, err)
} }
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
return nil return nil
} }
// AddExtension adds the ExtensionElement to the Entry. // AddExtension adds the ExtensionElement to the Entry. It returns an int.
func (e *Entry) AddExtension(x *ExtensionElement) { func (e *Entry) AddExtension(x *ExtensionElement) int {
addToSlice(&e.Extensions, x)
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
return addToSlice(&e.Extensions, x)
} }
// DeleteExtension deletes the Extension from the Entry. It return an error. // DeleteExtension deletes the Extension at index from the Entry. It return an
func (e *Entry) DeleteExtension(id int) error { // error.
if err := deleteFromSlice(&e.Extensions, id); err != nil { func (e *Entry) DeleteExtension(index int) error {
return fmt.Errorf("error deleting extension %v from entry %v: %v", id, e.ID.URI, err) 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 = NewDate(time.Now()) e.Updated = NewDate(time.Now())

87
feed.go
View File

@ -35,48 +35,51 @@ func NewFeed(title string) *Feed {
} }
} }
// AddAuthor adds the Person as an author to the Feed. // AddAuthor adds the Person as an author to the Feed. It returns an int.
func (f *Feed) AddAuthor(p *Person) { func (f *Feed) AddAuthor(p *Person) int {
addToSlice(&f.Authors, p)
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return addToSlice(&f.Authors, p)
} }
// DeleteAuthor deletes the Person from the Feed. It return an error. // DeleteAuthor deletes the Person at index from the Feed. It return an error.
func (f *Feed) DeleteAuthor(id int) error { func (f *Feed) DeleteAuthor(index int) error {
if err := deleteFromSlice(&f.Authors, id); err != nil { if err := deleteFromSlice(&f.Authors, index); err != nil {
return fmt.Errorf("error deleting author %v from entry %v: %v", id, f.ID.URI, err) return fmt.Errorf("error deleting author %v from entry %v: %v", index, f.ID.URI, err)
} }
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return nil return nil
} }
// AddCategory adds the Category to the Feed. // AddCategory adds the Category to the Feed. It returns an int.
func (f *Feed) AddCategory(c *Category) { func (f *Feed) AddCategory(c *Category) int {
addToSlice(&f.Categories, c)
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return addToSlice(&f.Categories, c)
} }
// DeleteCategory deletes the Category from the Feed. It return an error. // DeleteCategory deletes the Category at index from the Feed. It return an
func (f *Feed) DeleteCategory(id int) error { // error.
if err := deleteFromSlice(&f.Categories, id); err != nil { func (f *Feed) DeleteCategory(index int) error {
return fmt.Errorf("error deleting category %v from entry %v: %v", id, f.ID.URI, err) 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 = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return nil return nil
} }
// AddContributor adds the Person as a contributor to the Feed. // AddContributor adds the Person as a contributor to the Feed. It returns an
func (f *Feed) AddContributor(c *Person) { // int.
addToSlice(&f.Contributors, c) func (f *Feed) AddContributor(c *Person) int {
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return addToSlice(&f.Contributors, c)
} }
// DeleteContributor deletes the Person from the Feed. It return an error. // DeleteContributor deletes the Person at index from the Feed. It return an
func (f *Feed) DeleteContributor(id int) error { // error.
if err := deleteFromSlice(&f.Contributors, id); err != nil { func (f *Feed) DeleteContributor(index int) error {
return fmt.Errorf("error deleting contributor %v from entry %v: %v", id, f.ID.URI, err) 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 = NewDate(time.Now()) f.Updated = NewDate(time.Now())
@ -84,47 +87,49 @@ func (f *Feed) DeleteContributor(id int) error {
} }
// AddLink adds the Link to the Feed. There should be one Link with Rel "self". // AddLink adds the Link to the Feed. There should be one Link with Rel "self".
func (f *Feed) AddLink(l *Link) { // It returns an int.
addToSlice(&f.Links, l) func (f *Feed) AddLink(l *Link) int {
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return addToSlice(&f.Links, l)
} }
// DeleteLink deletes the Link from the Feed. It return an error. // DeleteLink deletes the Link at index from the Feed. It return an error.
func (f *Feed) DeleteLink(id int) error { func (f *Feed) DeleteLink(index int) error {
if err := deleteFromSlice(&f.Links, id); err != nil { if err := deleteFromSlice(&f.Links, index); err != nil {
return fmt.Errorf("error deleting link %v from entry %v: %v", id, f.ID.URI, err) return fmt.Errorf("error deleting link %v from entry %v: %v", index, f.ID.URI, err)
} }
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return nil return nil
} }
// AddExtension adds the Extension to the Feed. // AddExtension adds the Extension to the Feed. It returns an int.
func (f *Feed) AddExtension(e *ExtensionElement) { func (f *Feed) AddExtension(e *ExtensionElement) int {
addToSlice(&f.Extensions, e)
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return addToSlice(&f.Extensions, e)
} }
// DeleteExtension deletes the Extension from the Feed. It return an error. // DeleteExtension deletes the Extension at index from the Feed. It return an
func (f *Feed) DeleteExtension(id int) error { // error.
if err := deleteFromSlice(&f.Extensions, id); err != nil { func (f *Feed) DeleteExtension(index int) error {
return fmt.Errorf("error deleting extension %v from entry %v: %v", id, f.ID.URI, err) 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 = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return nil return nil
} }
// AddEntry adds the Entry to the Feed. // AddEntry adds the Entry to the Feed. It returns an int.
func (f *Feed) AddEntry(e *Entry) { func (f *Feed) AddEntry(e *Entry) int {
addToSlice(&f.Entries, e)
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
return addToSlice(&f.Entries, e)
} }
// DeleteEntry deletes the Entry from the Feed. It return an error. // DeleteEntry deletes the Entry at index from the Feed. It return an error.
func (f *Feed) DeleteEntry(id int) error { func (f *Feed) DeleteEntry(index int) error {
if err := deleteFromSlice(&f.Entries, id); err != nil { if err := deleteFromSlice(&f.Entries, index); err != nil {
return fmt.Errorf("error deleting entry %v from entry %v: %v", id, f.ID.URI, err) return fmt.Errorf("error deleting entry %v from entry %v: %v", index, f.ID.URI, err)
} }
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())

View File

@ -21,15 +21,16 @@ func NewPerson(name string) *Person {
} }
} }
// AddExtension adds the Extension to the Person. // AddExtension adds the Extension to the Person. It returns an int.
func (p *Person) AddExtension(e *ExtensionElement) { func (p *Person) AddExtension(e *ExtensionElement) int {
addToSlice(&p.Extensions, e) return addToSlice(&p.Extensions, e)
} }
// DeleteExtension deletes the Extension from the Person. It return an error. // DeleteExtension deletes the Extension at index from the Person. It return an
func (p *Person) DeleteExtension(id int) error { // error.
if err := deleteFromSlice(&p.Extensions, id); err != nil { func (p *Person) DeleteExtension(index int) error {
return fmt.Errorf("error deleting extension %v from person %v: %v", id, p, err) if err := deleteFromSlice(&p.Extensions, index); err != nil {
return fmt.Errorf("error deleting extension %v from person %v: %v", index, p, err)
} }
return nil return nil
} }

View File

@ -28,67 +28,71 @@ func NewSource() *Source {
return &Source{CommonAttributes: newCommonAttributes()} return &Source{CommonAttributes: newCommonAttributes()}
} }
// AddAuthor adds the Person as an author to the Source. // AddAuthor adds the Person as an author to the Source. It returns an int.
func (s *Source) AddAuthor(p *Person) { func (s *Source) AddAuthor(p *Person) int {
addToSlice(&s.Authors, p) return addToSlice(&s.Authors, p)
} }
// DeleteAuthor deletes the Person from the Source. It return an error. // DeleteAuthor deletes the Person at index from the Source. It return an error.
func (s *Source) DeleteAuthor(id int) error { func (s *Source) DeleteAuthor(index int) error {
if err := deleteFromSlice(&s.Authors, id); err != nil { if err := deleteFromSlice(&s.Authors, index); err != nil {
return fmt.Errorf("error deleting author %v from source %v: %v", id, s, err) return fmt.Errorf("error deleting author %v from source %v: %v", index, s, err)
} }
return nil return nil
} }
// AddCategory adds the Category to the Source. // AddCategory adds the Category to the Source. It returns an int.
func (s *Source) AddCategory(c *Category) { func (s *Source) AddCategory(c *Category) int {
addToSlice(&s.Categories, c) return addToSlice(&s.Categories, c)
} }
// DeleteCategory deletes the Category from the Source. It return an error. // DeleteCategory deletes the Category at index from the Source. It return an
func (s *Source) DeleteCategory(id int) error { // error.
if err := deleteFromSlice(&s.Categories, id); err != nil { func (s *Source) DeleteCategory(index int) error {
return fmt.Errorf("error deleting category %v from source %v: %v", id, s, err) if err := deleteFromSlice(&s.Categories, index); err != nil {
return fmt.Errorf("error deleting category %v from source %v: %v", index, s, err)
} }
return nil return nil
} }
// AddContributor adds the Person as a contributor to the Source. // AddContributor adds the Person as a contributor to the Source. It returns an
func (s *Source) AddContributor(c *Person) { // int.
addToSlice(&s.Contributors, c) func (s *Source) AddContributor(c *Person) int {
return addToSlice(&s.Contributors, c)
} }
// DeleteContributor deletes the Person from the Source. It return an error. // DeleteContributor deletes the Person at index from the Source. It return an
func (s *Source) DeleteContributor(id int) error { // error.
if err := deleteFromSlice(&s.Contributors, id); err != nil { func (s *Source) DeleteContributor(index int) error {
return fmt.Errorf("error deleting contributor %v from source %v: %v", id, s, err) if err := deleteFromSlice(&s.Contributors, index); err != nil {
return fmt.Errorf("error deleting contributor %v from source %v: %v", index, s, err)
} }
return nil return nil
} }
// AddLink adds the Link to the Source. // AddLink adds the Link to the Source. It returns an int.
func (s *Source) AddLink(l *Link) { func (s *Source) AddLink(l *Link) int {
addToSlice(&s.Links, l) return addToSlice(&s.Links, l)
} }
// DeleteLink deletes the Link from the Source. It return an error. // DeleteLink deletes the Link at index from the Source. It return an error.
func (s *Source) DeleteLink(id int) error { func (s *Source) DeleteLink(index int) error {
if err := deleteFromSlice(&s.Links, id); err != nil { if err := deleteFromSlice(&s.Links, index); err != nil {
return fmt.Errorf("error deleting link %v from source %v: %v", id, s, err) return fmt.Errorf("error deleting link %v from source %v: %v", index, s, err)
} }
return nil return nil
} }
// AddExtension adds the ExtensionElement to the Source. // AddExtension adds the ExtensionElement to the Source. It returns an int.
func (s *Source) AddExtension(e *ExtensionElement) { func (s *Source) AddExtension(e *ExtensionElement) int {
addToSlice(&s.Extensions, e) return addToSlice(&s.Extensions, e)
} }
// DeleteExtension deletes the Extension from the Source. It return an error. // DeleteExtension deletes the Extension at index from the Source. It return an
func (s *Source) DeleteExtension(id int) error { // error.
if err := deleteFromSlice(&s.Extensions, id); err != nil { func (s *Source) DeleteExtension(index int) error {
return fmt.Errorf("error deleting extension %v from source %v: %v", id, s, err) if err := deleteFromSlice(&s.Extensions, index); err != nil {
return fmt.Errorf("error deleting extension %v from source %v: %v", index, s, err)
} }
return nil return nil
} }