From c0f53067150dad557eeadc4cd6fccd4ec245fe8d Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 20 Oct 2024 12:20:25 +0200 Subject: [PATCH] Use pointers to make generic functions work --- atom.go | 16 +++++++--------- commonAttributes.go | 4 ++-- entry.go | 20 ++++++++++---------- feed.go | 24 ++++++++++++------------ person.go | 4 ++-- source.go | 20 ++++++++++---------- 6 files changed, 43 insertions(+), 45 deletions(-) diff --git a/atom.go b/atom.go index 70d90ed..6049bcb 100644 --- a/atom.go +++ b/atom.go @@ -16,22 +16,20 @@ 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 addToSlice[C Countable](slice *[]C, countable C) { + if *slice == nil { + *slice = make([]C, 0) } + *slice = append(*slice, countable) } -func deleteFromSlice[C Countable](slice []C, id int) error { - length := len(slice) +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:]...) + *slice = append((*slice)[:id], (*slice)[id+1:]...) return nil } diff --git a/commonAttributes.go b/commonAttributes.go index e8d5d2b..6cd6a28 100644 --- a/commonAttributes.go +++ b/commonAttributes.go @@ -19,13 +19,13 @@ func newCommonAttributes() *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}) + 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 { - if err := deleteFromSlice(c.UndefinedAttributes, id); err != nil { + if err := deleteFromSlice(&c.UndefinedAttributes, id); err != nil { return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", id, c, err) } return nil diff --git a/entry.go b/entry.go index b2fe95e..2eddb28 100644 --- a/entry.go +++ b/entry.go @@ -68,13 +68,13 @@ 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) + addToSlice(&e.Authors, p) e.Updated = NewDate(time.Now()) } // DeleteAuthor deletes the Person from the Entry. It return an error. func (e *Entry) DeleteAuthor(id int) error { - if err := deleteFromSlice(e.Authors, id); err != nil { + if err := deleteFromSlice(&e.Authors, id); err != nil { return fmt.Errorf("error deleting author %v from entry %v: %v", id, e.ID.URI, err) } @@ -84,13 +84,13 @@ func (e *Entry) DeleteAuthor(id int) error { // AddCategory adds the Category to the Entry. func (e *Entry) AddCategory(c *Category) { - addToSlice(e.Categories, c) + addToSlice(&e.Categories, c) e.Updated = NewDate(time.Now()) } // DeleteCategory deletes the Category from the Entry. It return an error. func (e *Entry) DeleteCategory(id int) error { - if err := deleteFromSlice(e.Categories, id); err != nil { + if err := deleteFromSlice(&e.Categories, id); err != nil { return fmt.Errorf("error deleting category %v from entry %v: %v", id, e.ID.URI, err) } @@ -100,13 +100,13 @@ func (e *Entry) DeleteCategory(id int) error { // AddContributor adds the Person as a contributor to the Entry. func (e *Entry) AddContributor(c *Person) { - addToSlice(e.Contributors, c) + addToSlice(&e.Contributors, c) e.Updated = NewDate(time.Now()) } // DeleteContributor deletes the Person from the Entry. It return an error. func (e *Entry) DeleteContributor(id int) error { - if err := deleteFromSlice(e.Contributors, id); err != nil { + if err := deleteFromSlice(&e.Contributors, id); err != nil { return fmt.Errorf("error deleting contributor %v from entry %v: %v", id, e.ID.URI, err) } @@ -116,13 +116,13 @@ func (e *Entry) DeleteContributor(id int) error { // AddLink adds the Link to the Entry. func (e *Entry) AddLink(l *Link) { - addToSlice(e.Links, l) + addToSlice(&e.Links, l) e.Updated = NewDate(time.Now()) } // DeleteLink deletes the Link from the Entry. It return an error. func (e *Entry) DeleteLink(id int) error { - if err := deleteFromSlice(e.Links, id); err != nil { + if err := deleteFromSlice(&e.Links, id); err != nil { return fmt.Errorf("error deleting link %v from entry %v: %v", id, e.ID.URI, err) } @@ -132,13 +132,13 @@ func (e *Entry) DeleteLink(id int) error { // AddExtension adds the ExtensionElement to the Entry. func (e *Entry) AddExtension(x *ExtensionElement) { - addToSlice(e.Extensions, x) + addToSlice(&e.Extensions, x) e.Updated = NewDate(time.Now()) } // DeleteExtension deletes the Extension from the Entry. It return an error. func (e *Entry) DeleteExtension(id int) error { - if err := deleteFromSlice(e.Extensions, id); err != nil { + if err := deleteFromSlice(&e.Extensions, id); err != nil { return fmt.Errorf("error deleting extension %v from entry %v: %v", id, e.ID.URI, err) } diff --git a/feed.go b/feed.go index b496192..e4ea920 100644 --- a/feed.go +++ b/feed.go @@ -37,13 +37,13 @@ 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) + addToSlice(&f.Authors, p) f.Updated = NewDate(time.Now()) } // DeleteAuthor deletes the Person from the Feed. It return an error. func (f *Feed) DeleteAuthor(id int) error { - if err := deleteFromSlice(f.Authors, id); err != nil { + if err := deleteFromSlice(&f.Authors, id); err != nil { return fmt.Errorf("error deleting author %v from entry %v: %v", id, f.ID.URI, err) } @@ -53,13 +53,13 @@ func (f *Feed) DeleteAuthor(id int) error { // AddCategory adds the Category to the Feed. func (f *Feed) AddCategory(c *Category) { - addToSlice(f.Categories, c) + addToSlice(&f.Categories, c) f.Updated = NewDate(time.Now()) } // DeleteCategory deletes the Category from the Feed. It return an error. func (f *Feed) DeleteCategory(id int) error { - if err := deleteFromSlice(f.Categories, id); err != nil { + if err := deleteFromSlice(&f.Categories, id); err != nil { return fmt.Errorf("error deleting category %v from entry %v: %v", id, f.ID.URI, err) } @@ -69,13 +69,13 @@ func (f *Feed) DeleteCategory(id int) error { // AddContributor adds the Person as a contributor to the Feed. func (f *Feed) AddContributor(c *Person) { - addToSlice(f.Contributors, c) + addToSlice(&f.Contributors, c) f.Updated = NewDate(time.Now()) } // DeleteContributor deletes the Person from the Feed. It return an error. func (f *Feed) DeleteContributor(id int) error { - if err := deleteFromSlice(f.Contributors, id); err != nil { + if err := deleteFromSlice(&f.Contributors, id); err != nil { return fmt.Errorf("error deleting contributor %v from entry %v: %v", id, f.ID.URI, err) } @@ -85,13 +85,13 @@ func (f *Feed) DeleteContributor(id 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) + addToSlice(&f.Links, l) f.Updated = NewDate(time.Now()) } // DeleteLink deletes the Link from the Feed. It return an error. func (f *Feed) DeleteLink(id int) error { - if err := deleteFromSlice(f.Links, id); err != nil { + if err := deleteFromSlice(&f.Links, id); err != nil { return fmt.Errorf("error deleting link %v from entry %v: %v", id, f.ID.URI, err) } @@ -101,13 +101,13 @@ func (f *Feed) DeleteLink(id int) error { // AddExtension adds the Extension to the Feed. func (f *Feed) AddExtension(e *ExtensionElement) { - addToSlice(f.Extensions, e) + addToSlice(&f.Extensions, e) f.Updated = NewDate(time.Now()) } // DeleteExtension deletes the Extension from the Feed. It return an error. func (f *Feed) DeleteExtension(id int) error { - if err := deleteFromSlice(f.Extensions, id); err != nil { + if err := deleteFromSlice(&f.Extensions, id); err != nil { return fmt.Errorf("error deleting extension %v from entry %v: %v", id, f.ID.URI, err) } @@ -117,13 +117,13 @@ func (f *Feed) DeleteExtension(id int) error { // AddEntry adds the Entry to the Feed. func (f *Feed) AddEntry(e *Entry) { - addToSlice(f.Entries, e) + addToSlice(&f.Entries, e) f.Updated = NewDate(time.Now()) } // DeleteEntry deletes the Entry from the Feed. It return an error. func (f *Feed) DeleteEntry(id int) error { - if err := deleteFromSlice(f.Entries, id); err != nil { + if err := deleteFromSlice(&f.Entries, id); err != nil { return fmt.Errorf("error deleting entry %v from entry %v: %v", id, f.ID.URI, err) } diff --git a/person.go b/person.go index 5505698..1cd928b 100644 --- a/person.go +++ b/person.go @@ -23,12 +23,12 @@ func NewPerson(name string) *Person { // AddExtension adds the Extension to the Person. func (p *Person) AddExtension(e *ExtensionElement) { - addToSlice(p.Extensions, e) + addToSlice(&p.Extensions, e) } // DeleteExtension deletes the Extension from the Person. It return an error. func (p *Person) DeleteExtension(id int) error { - if err := deleteFromSlice(p.Extensions, id); err != nil { + if err := deleteFromSlice(&p.Extensions, id); err != nil { return fmt.Errorf("error deleting extension %v from person %v: %v", id, p, err) } return nil diff --git a/source.go b/source.go index 4f175b9..ee12850 100644 --- a/source.go +++ b/source.go @@ -30,12 +30,12 @@ func NewSource() *Source { // AddAuthor adds the Person as an author to the Source. func (s *Source) AddAuthor(p *Person) { - addToSlice(s.Authors, p) + addToSlice(&s.Authors, p) } // DeleteAuthor deletes the Person from the Source. It return an error. func (s *Source) DeleteAuthor(id int) error { - if err := deleteFromSlice(s.Authors, id); err != nil { + if err := deleteFromSlice(&s.Authors, id); err != nil { return fmt.Errorf("error deleting author %v from source %v: %v", id, s, err) } return nil @@ -43,12 +43,12 @@ func (s *Source) DeleteAuthor(id int) error { // AddCategory adds the Category to the Source. func (s *Source) AddCategory(c *Category) { - addToSlice(s.Categories, c) + addToSlice(&s.Categories, c) } // DeleteCategory deletes the Category from the Source. It return an error. func (s *Source) DeleteCategory(id int) error { - if err := deleteFromSlice(s.Categories, id); err != nil { + if err := deleteFromSlice(&s.Categories, id); err != nil { return fmt.Errorf("error deleting category %v from source %v: %v", id, s, err) } return nil @@ -56,12 +56,12 @@ func (s *Source) DeleteCategory(id int) error { // AddContributor adds the Person as a contributor to the Source. func (s *Source) AddContributor(c *Person) { - addToSlice(s.Contributors, c) + addToSlice(&s.Contributors, c) } // DeleteContributor deletes the Person from the Source. It return an error. func (s *Source) DeleteContributor(id int) error { - if err := deleteFromSlice(s.Contributors, id); err != nil { + if err := deleteFromSlice(&s.Contributors, id); err != nil { return fmt.Errorf("error deleting contributor %v from source %v: %v", id, s, err) } return nil @@ -69,12 +69,12 @@ func (s *Source) DeleteContributor(id int) error { // AddLink adds the Link to the Source. func (s *Source) AddLink(l *Link) { - addToSlice(s.Links, l) + addToSlice(&s.Links, l) } // DeleteLink deletes the Link from the Source. It return an error. func (s *Source) DeleteLink(id int) error { - if err := deleteFromSlice(s.Links, id); err != nil { + if err := deleteFromSlice(&s.Links, id); err != nil { return fmt.Errorf("error deleting link %v from source %v: %v", id, s, err) } return nil @@ -82,12 +82,12 @@ func (s *Source) DeleteLink(id int) error { // AddExtension adds the ExtensionElement to the Source. func (s *Source) AddExtension(e *ExtensionElement) { - addToSlice(s.Extensions, e) + addToSlice(&s.Extensions, e) } // DeleteExtension deletes the Extension from the Source. It return an error. func (s *Source) DeleteExtension(id int) error { - if err := deleteFromSlice(s.Extensions, id); err != nil { + if err := deleteFromSlice(&s.Extensions, id); err != nil { return fmt.Errorf("error deleting extension %v from source %v: %v", id, s, err) } return nil