Use pointers to make generic functions work

This commit is contained in:
Jason Streifling 2024-10-20 12:20:25 +02:00
parent 8a00759c4b
commit c0f5306715
6 changed files with 43 additions and 45 deletions

16
atom.go
View File

@ -16,22 +16,20 @@ type Countable interface {
*xml.Attr | *Person | *Category | *Link | *ExtensionElement | *Entry *xml.Attr | *Person | *Category | *Link | *ExtensionElement | *Entry
} }
func addToSlice[C Countable](slice []C, countable C) { func addToSlice[C Countable](slice *[]C, countable C) {
if slice == nil { if *slice == nil {
slice = make([]C, 1) *slice = make([]C, 0)
slice[0] = countable
} else {
slice = append(slice, countable)
} }
*slice = append(*slice, countable)
} }
func deleteFromSlice[C Countable](slice []C, id int) error { func deleteFromSlice[C Countable](slice *[]C, id int) error {
length := len(slice) length := len(*slice)
if id > length { if id > length {
return fmt.Errorf("id %v out of range %v", 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 return nil
} }

View File

@ -19,13 +19,13 @@ 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) {
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 // 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 {
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 fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", id, c, err)
} }
return nil return nil

View File

@ -68,13 +68,13 @@ 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) {
addToSlice(e.Authors, p) addToSlice(&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 {
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) 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. // AddCategory adds the Category to the Entry.
func (e *Entry) AddCategory(c *Category) { func (e *Entry) AddCategory(c *Category) {
addToSlice(e.Categories, c) addToSlice(&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 {
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) 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. // AddContributor adds the Person as a contributor to the Entry.
func (e *Entry) AddContributor(c *Person) { func (e *Entry) AddContributor(c *Person) {
addToSlice(e.Contributors, c) addToSlice(&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 {
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) 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. // AddLink adds the Link to the Entry.
func (e *Entry) AddLink(l *Link) { func (e *Entry) AddLink(l *Link) {
addToSlice(e.Links, l) addToSlice(&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 {
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) 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. // AddExtension adds the ExtensionElement to the Entry.
func (e *Entry) AddExtension(x *ExtensionElement) { func (e *Entry) AddExtension(x *ExtensionElement) {
addToSlice(e.Extensions, x) addToSlice(&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 {
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) return fmt.Errorf("error deleting extension %v from entry %v: %v", id, e.ID.URI, err)
} }

24
feed.go
View File

@ -37,13 +37,13 @@ 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) {
addToSlice(f.Authors, p) addToSlice(&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 {
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) 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. // AddCategory adds the Category to the Feed.
func (f *Feed) AddCategory(c *Category) { func (f *Feed) AddCategory(c *Category) {
addToSlice(f.Categories, c) addToSlice(&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 {
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) 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. // AddContributor adds the Person as a contributor to the Feed.
func (f *Feed) AddContributor(c *Person) { func (f *Feed) AddContributor(c *Person) {
addToSlice(f.Contributors, c) addToSlice(&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 {
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) 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". // 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) {
addToSlice(f.Links, l) addToSlice(&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 {
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) 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. // AddExtension adds the Extension to the Feed.
func (f *Feed) AddExtension(e *ExtensionElement) { func (f *Feed) AddExtension(e *ExtensionElement) {
addToSlice(f.Extensions, e) addToSlice(&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 {
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) 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. // AddEntry adds the Entry to the Feed.
func (f *Feed) AddEntry(e *Entry) { func (f *Feed) AddEntry(e *Entry) {
addToSlice(f.Entries, e) addToSlice(&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 {
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) return fmt.Errorf("error deleting entry %v from entry %v: %v", id, f.ID.URI, err)
} }

View File

@ -23,12 +23,12 @@ 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) {
addToSlice(p.Extensions, e) addToSlice(&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 {
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 fmt.Errorf("error deleting extension %v from person %v: %v", id, p, err)
} }
return nil return nil

View File

@ -30,12 +30,12 @@ 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) {
addToSlice(s.Authors, p) addToSlice(&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 {
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 fmt.Errorf("error deleting author %v from source %v: %v", id, s, err)
} }
return nil return nil
@ -43,12 +43,12 @@ func (s *Source) DeleteAuthor(id int) error {
// 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) {
addToSlice(s.Categories, c) addToSlice(&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 {
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 fmt.Errorf("error deleting category %v from source %v: %v", id, s, err)
} }
return nil return nil
@ -56,12 +56,12 @@ func (s *Source) DeleteCategory(id int) error {
// 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) {
addToSlice(s.Contributors, c) addToSlice(&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 {
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 fmt.Errorf("error deleting contributor %v from source %v: %v", id, s, err)
} }
return nil return nil
@ -69,12 +69,12 @@ func (s *Source) DeleteContributor(id int) error {
// 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) {
addToSlice(s.Links, l) addToSlice(&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 {
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 fmt.Errorf("error deleting link %v from source %v: %v", id, s, err)
} }
return nil return nil
@ -82,12 +82,12 @@ func (s *Source) DeleteLink(id int) error {
// 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) {
addToSlice(s.Extensions, e) addToSlice(&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 {
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 fmt.Errorf("error deleting extension %v from source %v: %v", id, s, err)
} }
return nil return nil