Use generics for Add and Delete Methods

This commit is contained in:
Jason Streifling 2024-10-20 12:03:26 +02:00
parent e0384904b4
commit a49e853efb
6 changed files with 78 additions and 198 deletions

24
atom.go
View File

@ -1,6 +1,7 @@
package atom package atom
import ( import (
"encoding/xml"
"fmt" "fmt"
"html" "html"
"mime" "mime"
@ -11,6 +12,29 @@ import (
"golang.org/x/text/language" "golang.org/x/text/language"
) )
type Countable interface {
*xml.Attr | *Person | *Category | *Link | *ExtensionElement | *Entry
}
func addToSlice[C Countable](slice []C, countable C) {
if slice == nil {
slice = make([]C, 1)
slice[0] = countable
} else {
slice = append(slice, countable)
}
}
func deleteFromSlice[C Countable](slice []C, id int) error {
length := len(slice)
if id > length {
return fmt.Errorf("id %v out of range %v", id, length)
}
slice = append(slice[:id], slice[id+1:]...)
return nil
}
// isValidIRI checks whether an IRI is valid or not. It returns a bool. // isValidIRI checks whether an IRI is valid or not. It returns a bool.
// https://www.w3.org/2011/04/XMLSchema/TypeLibrary-IRI-RFC3987.xsd // https://www.w3.org/2011/04/XMLSchema/TypeLibrary-IRI-RFC3987.xsd
func isValidIRI(iri string) bool { func isValidIRI(iri string) bool {

View File

@ -19,23 +19,15 @@ func newCommonAttributes() *CommonAttributes {
// AddAttribute adds the attribute to the CommonAttributes. // AddAttribute adds the attribute to the CommonAttributes.
func (c *CommonAttributes) AddAttribute(name, value string) { func (c *CommonAttributes) AddAttribute(name, value string) {
if c.UndefinedAttributes == nil { addToSlice(c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
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})
}
} }
// DeleteAttribute deletes the attribute from the CommonAttributes. It return an // DeleteAttribute deletes the attribute from the CommonAttributes. It return an
// error. // error.
func (c *CommonAttributes) DeleteAttribute(id int) error { func (c *CommonAttributes) DeleteAttribute(id int) error {
length := len(c.UndefinedAttributes) if err := deleteFromSlice(c.UndefinedAttributes, id); err != nil {
if id > length { return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", id, c, err)
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 return nil
} }

View File

@ -68,120 +68,80 @@ 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.
func (e *Entry) AddAuthor(p *Person) { func (e *Entry) AddAuthor(p *Person) {
if e.Authors == nil { addToSlice(e.Authors, p)
e.Authors = make([]*Person, 1)
e.Authors[0] = p
} else {
e.Authors = append(e.Authors, p)
}
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
} }
// DeleteAuthor deletes the Person from the Entry. It return an error. // DeleteAuthor deletes the Person from the Entry. It return an error.
func (e *Entry) DeleteAuthor(id int) error { func (e *Entry) DeleteAuthor(id int) error {
length := len(e.Authors) if err := deleteFromSlice(e.Authors, id); err != nil {
if id > length { return fmt.Errorf("error deleting author %v from entry %v: %v", id, e.ID.URI, err)
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()) e.Updated = NewDate(time.Now())
return nil return nil
} }
// AddCategory adds the Category to the Entry. // AddCategory adds the Category to the Entry.
func (e *Entry) AddCategory(c *Category) { func (e *Entry) AddCategory(c *Category) {
if e.Categories == nil { addToSlice(e.Categories, c)
e.Categories = make([]*Category, 1)
e.Categories[0] = c
} else {
e.Categories = append(e.Categories, c)
}
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
} }
// DeleteCategory deletes the Category from the Entry. It return an error. // DeleteCategory deletes the Category from the Entry. It return an error.
func (e *Entry) DeleteCategory(id int) error { func (e *Entry) DeleteCategory(id int) error {
length := len(e.Categories) if err := deleteFromSlice(e.Categories, id); err != nil {
if id > length { return fmt.Errorf("error deleting category %v from entry %v: %v", id, e.ID.URI, err)
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()) 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.
func (e *Entry) AddContributor(c *Person) { func (e *Entry) AddContributor(c *Person) {
if e.Contributors == nil { addToSlice(e.Contributors, c)
e.Contributors = make([]*Person, 1)
e.Contributors[0] = c
} else {
e.Contributors = append(e.Contributors, c)
}
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
} }
// DeleteContributor deletes the Person from the Entry. It return an error. // DeleteContributor deletes the Person from the Entry. It return an error.
func (e *Entry) DeleteContributor(id int) error { func (e *Entry) DeleteContributor(id int) error {
length := len(e.Contributors) if err := deleteFromSlice(e.Contributors, id); err != nil {
if id > length { return fmt.Errorf("error deleting contributor %v from entry %v: %v", id, e.ID.URI, err)
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()) e.Updated = NewDate(time.Now())
return nil return nil
} }
// AddLink adds the Link to the Entry. // AddLink adds the Link to the Entry.
func (e *Entry) AddLink(l *Link) { func (e *Entry) AddLink(l *Link) {
if e.Links == nil { addToSlice(e.Links, l)
e.Links = make([]*Link, 1)
e.Links[0] = l
} else {
e.Links = append(e.Links, l)
}
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
} }
// DeleteLink deletes the Link from the Entry. It return an error. // DeleteLink deletes the Link from the Entry. It return an error.
func (e *Entry) DeleteLink(id int) error { func (e *Entry) DeleteLink(id int) error {
length := len(e.Links) if err := deleteFromSlice(e.Links, id); err != nil {
if id > length { return fmt.Errorf("error deleting link %v from entry %v: %v", id, e.ID.URI, err)
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()) e.Updated = NewDate(time.Now())
return nil return nil
} }
// AddExtension adds the ExtensionElement to the Entry. // AddExtension adds the ExtensionElement to the Entry.
func (e *Entry) AddExtension(x *ExtensionElement) { func (e *Entry) AddExtension(x *ExtensionElement) {
if e.Extensions == nil { addToSlice(e.Extensions, x)
e.Extensions = make([]*ExtensionElement, 1)
e.Extensions[0] = x
} else {
e.Extensions = append(e.Extensions, x)
}
e.Updated = NewDate(time.Now()) e.Updated = NewDate(time.Now())
} }
// DeleteExtension deletes the Extension from the Entry. It return an error. // DeleteExtension deletes the Extension from the Entry. It return an error.
func (e *Entry) DeleteExtension(id int) error { func (e *Entry) DeleteExtension(id int) error {
length := len(e.Extensions) if err := deleteFromSlice(e.Extensions, id); err != nil {
if id > length { return fmt.Errorf("error deleting extension %v from entry %v: %v", id, e.ID.URI, err)
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()) e.Updated = NewDate(time.Now())
return nil return nil
} }

84
feed.go
View File

@ -37,144 +37,96 @@ 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.
func (f *Feed) AddAuthor(p *Person) { func (f *Feed) AddAuthor(p *Person) {
if f.Authors == nil { addToSlice(f.Authors, p)
f.Authors = make([]*Person, 1)
f.Authors[0] = p
} else {
f.Authors = append(f.Authors, p)
}
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
} }
// DeleteAuthor deletes the Person from the Feed. It return an error. // DeleteAuthor deletes the Person from the Feed. It return an error.
func (f *Feed) DeleteAuthor(id int) error { func (f *Feed) DeleteAuthor(id int) error {
length := len(f.Authors) if err := deleteFromSlice(f.Authors, id); err != nil {
if id > length { return fmt.Errorf("error deleting author %v from entry %v: %v", id, f.ID.URI, err)
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()) f.Updated = NewDate(time.Now())
return nil return nil
} }
// AddCategory adds the Category to the Feed. // AddCategory adds the Category to the Feed.
func (f *Feed) AddCategory(c *Category) { func (f *Feed) AddCategory(c *Category) {
if f.Categories == nil { addToSlice(f.Categories, c)
f.Categories = make([]*Category, 1)
f.Categories[0] = c
} else {
f.Categories = append(f.Categories, c)
}
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
} }
// DeleteCategory deletes the Category from the Feed. It return an error. // DeleteCategory deletes the Category from the Feed. It return an error.
func (f *Feed) DeleteCategory(id int) error { func (f *Feed) DeleteCategory(id int) error {
length := len(f.Categories) if err := deleteFromSlice(f.Categories, id); err != nil {
if id > length { return fmt.Errorf("error deleting category %v from entry %v: %v", id, f.ID.URI, err)
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()) 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.
func (f *Feed) AddContributor(c *Person) { func (f *Feed) AddContributor(c *Person) {
if f.Contributors == nil { addToSlice(f.Contributors, c)
f.Contributors = make([]*Person, 1)
f.Contributors[0] = c
} else {
f.Contributors = append(f.Contributors, c)
}
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
} }
// DeleteContributor deletes the Person from the Feed. It return an error. // DeleteContributor deletes the Person from the Feed. It return an error.
func (f *Feed) DeleteContributor(id int) error { func (f *Feed) DeleteContributor(id int) error {
length := len(f.Contributors) if err := deleteFromSlice(f.Contributors, id); err != nil {
if id > length { return fmt.Errorf("error deleting contributor %v from entry %v: %v", id, f.ID.URI, err)
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()) f.Updated = NewDate(time.Now())
return nil return nil
} }
// 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) { func (f *Feed) AddLink(l *Link) {
if f.Links == nil { addToSlice(f.Links, l)
f.Links = make([]*Link, 1)
f.Links[0] = l
} else {
f.Links = append(f.Links, l)
}
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
} }
// DeleteLink deletes the Link from the Feed. It return an error. // DeleteLink deletes the Link from the Feed. It return an error.
func (f *Feed) DeleteLink(id int) error { func (f *Feed) DeleteLink(id int) error {
length := len(f.Links) if err := deleteFromSlice(f.Links, id); err != nil {
if id > length { return fmt.Errorf("error deleting link %v from entry %v: %v", id, f.ID.URI, err)
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()) f.Updated = NewDate(time.Now())
return nil return nil
} }
// AddExtension adds the Extension to the Feed. // AddExtension adds the Extension to the Feed.
func (f *Feed) AddExtension(e *ExtensionElement) { func (f *Feed) AddExtension(e *ExtensionElement) {
if f.Extensions == nil { addToSlice(f.Extensions, e)
f.Extensions = make([]*ExtensionElement, 1)
f.Extensions[0] = e
} else {
f.Extensions = append(f.Extensions, e)
}
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
} }
// DeleteExtension deletes the Extension from the Feed. It return an error. // DeleteExtension deletes the Extension from the Feed. It return an error.
func (f *Feed) DeleteExtension(id int) error { func (f *Feed) DeleteExtension(id int) error {
length := len(f.Extensions) if err := deleteFromSlice(f.Extensions, id); err != nil {
if id > length { return fmt.Errorf("error deleting extension %v from entry %v: %v", id, f.ID.URI, err)
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()) f.Updated = NewDate(time.Now())
return nil return nil
} }
// AddEntry adds the Entry to the Feed. // AddEntry adds the Entry to the Feed.
func (f *Feed) AddEntry(e *Entry) { func (f *Feed) AddEntry(e *Entry) {
if f.Entries == nil { addToSlice(f.Entries, e)
f.Entries = make([]*Entry, 1)
f.Entries[0] = e
} else {
f.Entries = append(f.Entries, e)
}
f.Updated = NewDate(time.Now()) f.Updated = NewDate(time.Now())
} }
// DeleteEntry deletes the Entry from the Feed. It return an error. // DeleteEntry deletes the Entry from the Feed. It return an error.
func (f *Feed) DeleteEntry(id int) error { func (f *Feed) DeleteEntry(id int) error {
length := len(f.Entries) if err := deleteFromSlice(f.Entries, id); err != nil {
if id > length { return fmt.Errorf("error deleting entry %v from entry %v: %v", id, f.ID.URI, err)
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()) f.Updated = NewDate(time.Now())
return nil return nil
} }

View File

@ -23,22 +23,14 @@ func NewPerson(name string) *Person {
// AddExtension adds the Extension to the Person. // AddExtension adds the Extension to the Person.
func (p *Person) AddExtension(e *ExtensionElement) { func (p *Person) AddExtension(e *ExtensionElement) {
if p.Extensions == nil { addToSlice(p.Extensions, e)
p.Extensions = make([]*ExtensionElement, 1)
p.Extensions[0] = e
} else {
p.Extensions = append(p.Extensions, e)
}
} }
// DeleteExtension deletes the Extension from the Person. It return an error. // DeleteExtension deletes the Extension from the Person. It return an error.
func (p *Person) DeleteExtension(id int) error { func (p *Person) DeleteExtension(id int) error {
length := len(p.Extensions) if err := deleteFromSlice(p.Extensions, id); err != nil {
if id > length { return fmt.Errorf("error deleting extension %v from person %v: %v", id, p, err)
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 return nil
} }

View File

@ -30,106 +30,66 @@ func NewSource() *Source {
// AddAuthor adds the Person as an author to the Source. // AddAuthor adds the Person as an author to the Source.
func (s *Source) AddAuthor(p *Person) { func (s *Source) AddAuthor(p *Person) {
if s.Authors == nil { addToSlice(s.Authors, p)
s.Authors = make([]*Person, 1)
s.Authors[0] = p
} else {
s.Authors = append(s.Authors, p)
}
} }
// DeleteAuthor deletes the Person from the Source. It return an error. // DeleteAuthor deletes the Person from the Source. It return an error.
func (s *Source) DeleteAuthor(id int) error { func (s *Source) DeleteAuthor(id int) error {
length := len(s.Authors) if err := deleteFromSlice(s.Authors, id); err != nil {
if id > length { return fmt.Errorf("error deleting author %v from source %v: %v", id, s, err)
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 return nil
} }
// AddCategory adds the Category to the Source. // AddCategory adds the Category to the Source.
func (s *Source) AddCategory(c *Category) { func (s *Source) AddCategory(c *Category) {
if s.Categories == nil { addToSlice(s.Categories, c)
s.Categories = make([]*Category, 1)
s.Categories[0] = c
} else {
s.Categories = append(s.Categories, c)
}
} }
// DeleteCategory deletes the Category from the Source. It return an error. // DeleteCategory deletes the Category from the Source. It return an error.
func (s *Source) DeleteCategory(id int) error { func (s *Source) DeleteCategory(id int) error {
length := len(s.Categories) if err := deleteFromSlice(s.Categories, id); err != nil {
if id > length { return fmt.Errorf("error deleting category %v from source %v: %v", id, s, err)
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 return nil
} }
// AddContributor adds the Person as a contributor to the Source. // AddContributor adds the Person as a contributor to the Source.
func (s *Source) AddContributor(c *Person) { func (s *Source) AddContributor(c *Person) {
if s.Contributors == nil { addToSlice(s.Contributors, c)
s.Contributors = make([]*Person, 1)
s.Contributors[0] = c
} else {
s.Contributors = append(s.Contributors, c)
}
} }
// DeleteContributor deletes the Person from the Source. It return an error. // DeleteContributor deletes the Person from the Source. It return an error.
func (s *Source) DeleteContributor(id int) error { func (s *Source) DeleteContributor(id int) error {
length := len(s.Contributors) if err := deleteFromSlice(s.Contributors, id); err != nil {
if id > length { return fmt.Errorf("error deleting contributor %v from source %v: %v", id, s, err)
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 return nil
} }
// AddLink adds the Link to the Source. // AddLink adds the Link to the Source.
func (s *Source) AddLink(l *Link) { func (s *Source) AddLink(l *Link) {
if s.Links == nil { addToSlice(s.Links, l)
s.Links = make([]*Link, 1)
s.Links[0] = l
} else {
s.Links = append(s.Links, l)
}
} }
// DeleteLink deletes the Link from the Source. It return an error. // DeleteLink deletes the Link from the Source. It return an error.
func (s *Source) DeleteLink(id int) error { func (s *Source) DeleteLink(id int) error {
length := len(s.Links) if err := deleteFromSlice(s.Links, id); err != nil {
if id > length { return fmt.Errorf("error deleting link %v from source %v: %v", id, s, err)
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 return nil
} }
// AddExtension adds the ExtensionElement to the Source. // AddExtension adds the ExtensionElement to the Source.
func (s *Source) AddExtension(e *ExtensionElement) { func (s *Source) AddExtension(e *ExtensionElement) {
if s.Extensions == nil { addToSlice(s.Extensions, e)
s.Extensions = make([]*ExtensionElement, 1)
s.Extensions[0] = e
} else {
s.Extensions = append(s.Extensions, e)
}
} }
// DeleteExtension deletes the Extension from the Source. It return an error. // DeleteExtension deletes the Extension from the Source. It return an error.
func (s *Source) DeleteExtension(id int) error { func (s *Source) DeleteExtension(id int) error {
length := len(s.Extensions) if err := deleteFromSlice(s.Extensions, id); err != nil {
if id > length { return fmt.Errorf("error deleting extension %v from source %v: %v", id, s, err)
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 return nil
} }