Return the index of the added element

This commit is contained in:
Jason Streifling 2024-10-20 12:57:24 +02:00
parent 764b143ff8
commit 8ce7d54d00
7 changed files with 65 additions and 59 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

View File

@ -16,12 +16,14 @@ 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) {
// 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.

View File

@ -17,9 +17,9 @@ func newCommonAttributes() *CommonAttributes {
return new(CommonAttributes)
}
// AddAttribute adds the attribute to the CommonAttributes.
func (c *CommonAttributes) AddAttribute(name, value string) {
addToSlice(&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 at index from the CommonAttributes. It

View File

@ -66,10 +66,10 @@ func NewEntry(title string) *Entry {
}
}
// AddAuthor adds the Person as an author to the Entry.
func (e *Entry) AddAuthor(p *Person) {
addToSlice(&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 at index from the Entry. It return an error.
@ -82,10 +82,10 @@ func (e *Entry) DeleteAuthor(index int) error {
return nil
}
// AddCategory adds the Category to the Entry.
func (e *Entry) AddCategory(c *Category) {
addToSlice(&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 at index from the Entry. It return an
@ -99,10 +99,11 @@ func (e *Entry) DeleteCategory(index int) error {
return nil
}
// AddContributor adds the Person as a contributor to the Entry.
func (e *Entry) AddContributor(c *Person) {
addToSlice(&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 at index from the Entry. It return an
@ -116,10 +117,10 @@ func (e *Entry) DeleteContributor(index int) error {
return nil
}
// AddLink adds the Link to the Entry.
func (e *Entry) AddLink(l *Link) {
addToSlice(&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 at index from the Entry. It return an error.
@ -132,10 +133,10 @@ func (e *Entry) DeleteLink(index int) error {
return nil
}
// AddExtension adds the ExtensionElement to the Entry.
func (e *Entry) AddExtension(x *ExtensionElement) {
addToSlice(&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 at index from the Entry. It return an

36
feed.go
View File

@ -35,10 +35,10 @@ func NewFeed(title string) *Feed {
}
}
// AddAuthor adds the Person as an author to the Feed.
func (f *Feed) AddAuthor(p *Person) {
addToSlice(&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 at index from the Feed. It return an error.
@ -51,10 +51,10 @@ func (f *Feed) DeleteAuthor(index int) error {
return nil
}
// AddCategory adds the Category to the Feed.
func (f *Feed) AddCategory(c *Category) {
addToSlice(&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 at index from the Feed. It return an
@ -68,10 +68,11 @@ func (f *Feed) DeleteCategory(index int) error {
return nil
}
// AddContributor adds the Person as a contributor to the Feed.
func (f *Feed) AddContributor(c *Person) {
addToSlice(&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 at index from the Feed. It return an
@ -86,9 +87,10 @@ func (f *Feed) DeleteContributor(index int) error {
}
// AddLink adds the Link to the Feed. There should be one Link with Rel "self".
func (f *Feed) AddLink(l *Link) {
addToSlice(&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 at index from the Feed. It return an error.
@ -101,10 +103,10 @@ func (f *Feed) DeleteLink(index int) error {
return nil
}
// AddExtension adds the Extension to the Feed.
func (f *Feed) AddExtension(e *ExtensionElement) {
addToSlice(&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 at index from the Feed. It return an
@ -118,10 +120,10 @@ func (f *Feed) DeleteExtension(index int) error {
return nil
}
// AddEntry adds the Entry to the Feed.
func (f *Feed) AddEntry(e *Entry) {
addToSlice(&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 at index from the Feed. It return an error.

View File

@ -21,9 +21,9 @@ func NewPerson(name string) *Person {
}
}
// AddExtension adds the Extension to the Person.
func (p *Person) AddExtension(e *ExtensionElement) {
addToSlice(&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 at index from the Person. It return an

View File

@ -28,9 +28,9 @@ 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)
// 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 at index from the Source. It return an error.
@ -41,9 +41,9 @@ func (s *Source) DeleteAuthor(index int) error {
return nil
}
// AddCategory adds the Category to the Source.
func (s *Source) AddCategory(c *Category) {
addToSlice(&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 at index from the Source. It return an
@ -55,9 +55,10 @@ func (s *Source) DeleteCategory(index int) error {
return nil
}
// AddContributor adds the Person as a contributor to the Source.
func (s *Source) AddContributor(c *Person) {
addToSlice(&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 at index from the Source. It return an
@ -69,9 +70,9 @@ func (s *Source) DeleteContributor(index int) error {
return nil
}
// AddLink adds the Link to the Source.
func (s *Source) AddLink(l *Link) {
addToSlice(&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 at index from the Source. It return an error.
@ -82,9 +83,9 @@ func (s *Source) DeleteLink(index int) error {
return nil
}
// AddExtension adds the ExtensionElement to the Source.
func (s *Source) AddExtension(e *ExtensionElement) {
addToSlice(&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 at index from the Source. It return an