Compare commits

...

2 Commits

Author SHA1 Message Date
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
6 changed files with 87 additions and 77 deletions

View File

@ -26,13 +26,13 @@ func addToSlice[C Countable](slice *[]C, countable C) {
// deleteFromSlice deletes the Countable with the index from the *[]Countable.
// 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)
if id > length {
return fmt.Errorf("id %v out of range %v", id, length)
if index > 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
}

View File

@ -22,11 +22,11 @@ func (c *CommonAttributes) AddAttribute(name, value string) {
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 {
return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", id, c, err)
// DeleteAttribute deletes the attribute at index from the CommonAttributes. It
// return an error.
func (c *CommonAttributes) DeleteAttribute(index int) error {
if err := deleteFromSlice(&c.UndefinedAttributes, index); err != nil {
return fmt.Errorf("error deleting undefined attribute %v from common attributes %v: %v", index, c, err)
}
return nil
}

View File

@ -72,10 +72,10 @@ func (e *Entry) AddAuthor(p *Person) {
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 {
return fmt.Errorf("error deleting author %v from entry %v: %v", id, e.ID.URI, err)
// DeleteAuthor deletes the Person at index from the Entry. It return an error.
func (e *Entry) DeleteAuthor(index int) error {
if err := deleteFromSlice(&e.Authors, index); err != nil {
return fmt.Errorf("error deleting author %v from entry %v: %v", index, e.ID.URI, err)
}
e.Updated = NewDate(time.Now())
@ -88,10 +88,11 @@ func (e *Entry) AddCategory(c *Category) {
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 {
return fmt.Errorf("error deleting category %v from entry %v: %v", id, e.ID.URI, err)
// DeleteCategory deletes the Category at index from the Entry. It return an
// error.
func (e *Entry) DeleteCategory(index int) error {
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())
@ -104,10 +105,11 @@ func (e *Entry) AddContributor(c *Person) {
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 {
return fmt.Errorf("error deleting contributor %v from entry %v: %v", id, e.ID.URI, err)
// DeleteContributor deletes the Person at index from the Entry. It return an
// error.
func (e *Entry) DeleteContributor(index int) error {
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())
@ -120,10 +122,10 @@ func (e *Entry) AddLink(l *Link) {
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 {
return fmt.Errorf("error deleting link %v from entry %v: %v", id, e.ID.URI, err)
// DeleteLink deletes the Link at index from the Entry. It return an error.
func (e *Entry) DeleteLink(index int) error {
if err := deleteFromSlice(&e.Links, index); err != nil {
return fmt.Errorf("error deleting link %v from entry %v: %v", index, e.ID.URI, err)
}
e.Updated = NewDate(time.Now())
@ -136,10 +138,11 @@ func (e *Entry) AddExtension(x *ExtensionElement) {
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 {
return fmt.Errorf("error deleting extension %v from entry %v: %v", id, e.ID.URI, err)
// DeleteExtension deletes the Extension at index from the Entry. It return an
// error.
func (e *Entry) DeleteExtension(index int) error {
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())

51
feed.go
View File

@ -41,10 +41,10 @@ func (f *Feed) AddAuthor(p *Person) {
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 {
return fmt.Errorf("error deleting author %v from entry %v: %v", id, f.ID.URI, err)
// DeleteAuthor deletes the Person at index from the Feed. It return an error.
func (f *Feed) DeleteAuthor(index int) error {
if err := deleteFromSlice(&f.Authors, index); err != nil {
return fmt.Errorf("error deleting author %v from entry %v: %v", index, f.ID.URI, err)
}
f.Updated = NewDate(time.Now())
@ -57,10 +57,11 @@ func (f *Feed) AddCategory(c *Category) {
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 {
return fmt.Errorf("error deleting category %v from entry %v: %v", id, f.ID.URI, err)
// DeleteCategory deletes the Category at index from the Feed. It return an
// error.
func (f *Feed) DeleteCategory(index int) error {
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())
@ -73,10 +74,11 @@ func (f *Feed) AddContributor(c *Person) {
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 {
return fmt.Errorf("error deleting contributor %v from entry %v: %v", id, f.ID.URI, err)
// DeleteContributor deletes the Person at index from the Feed. It return an
// error.
func (f *Feed) DeleteContributor(index int) error {
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())
@ -89,10 +91,10 @@ func (f *Feed) AddLink(l *Link) {
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 {
return fmt.Errorf("error deleting link %v from entry %v: %v", id, f.ID.URI, err)
// DeleteLink deletes the Link at index from the Feed. It return an error.
func (f *Feed) DeleteLink(index int) error {
if err := deleteFromSlice(&f.Links, index); err != nil {
return fmt.Errorf("error deleting link %v from entry %v: %v", index, f.ID.URI, err)
}
f.Updated = NewDate(time.Now())
@ -105,10 +107,11 @@ func (f *Feed) AddExtension(e *ExtensionElement) {
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 {
return fmt.Errorf("error deleting extension %v from entry %v: %v", id, f.ID.URI, err)
// DeleteExtension deletes the Extension at index from the Feed. It return an
// error.
func (f *Feed) DeleteExtension(index int) error {
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())
@ -121,10 +124,10 @@ func (f *Feed) AddEntry(e *Entry) {
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 {
return fmt.Errorf("error deleting entry %v from entry %v: %v", id, f.ID.URI, err)
// DeleteEntry deletes the Entry at index from the Feed. It return an error.
func (f *Feed) DeleteEntry(index int) error {
if err := deleteFromSlice(&f.Entries, index); err != nil {
return fmt.Errorf("error deleting entry %v from entry %v: %v", index, f.ID.URI, err)
}
f.Updated = NewDate(time.Now())

View File

@ -26,10 +26,11 @@ func (p *Person) AddExtension(e *ExtensionElement) {
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 {
return fmt.Errorf("error deleting extension %v from person %v: %v", id, p, err)
// DeleteExtension deletes the Extension at index from the Person. It return an
// error.
func (p *Person) DeleteExtension(index int) error {
if err := deleteFromSlice(&p.Extensions, index); err != nil {
return fmt.Errorf("error deleting extension %v from person %v: %v", index, p, err)
}
return nil
}

View File

@ -33,10 +33,10 @@ func (s *Source) AddAuthor(p *Person) {
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 {
return fmt.Errorf("error deleting author %v from source %v: %v", id, s, err)
// DeleteAuthor deletes the Person at index from the Source. It return an error.
func (s *Source) DeleteAuthor(index int) error {
if err := deleteFromSlice(&s.Authors, index); err != nil {
return fmt.Errorf("error deleting author %v from source %v: %v", index, s, err)
}
return nil
}
@ -46,10 +46,11 @@ func (s *Source) AddCategory(c *Category) {
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 {
return fmt.Errorf("error deleting category %v from source %v: %v", id, s, err)
// DeleteCategory deletes the Category at index from the Source. It return an
// error.
func (s *Source) DeleteCategory(index int) error {
if err := deleteFromSlice(&s.Categories, index); err != nil {
return fmt.Errorf("error deleting category %v from source %v: %v", index, s, err)
}
return nil
}
@ -59,10 +60,11 @@ func (s *Source) AddContributor(c *Person) {
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 {
return fmt.Errorf("error deleting contributor %v from source %v: %v", id, s, err)
// DeleteContributor deletes the Person at index from the Source. It return an
// error.
func (s *Source) DeleteContributor(index int) error {
if err := deleteFromSlice(&s.Contributors, index); err != nil {
return fmt.Errorf("error deleting contributor %v from source %v: %v", index, s, err)
}
return nil
}
@ -72,10 +74,10 @@ func (s *Source) AddLink(l *Link) {
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 {
return fmt.Errorf("error deleting link %v from source %v: %v", id, s, err)
// DeleteLink deletes the Link at index from the Source. It return an error.
func (s *Source) DeleteLink(index int) error {
if err := deleteFromSlice(&s.Links, index); err != nil {
return fmt.Errorf("error deleting link %v from source %v: %v", index, s, err)
}
return nil
}
@ -85,10 +87,11 @@ func (s *Source) AddExtension(e *ExtensionElement) {
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 {
return fmt.Errorf("error deleting extension %v from source %v: %v", id, s, err)
// DeleteExtension deletes the Extension at index from the Source. It return an
// error.
func (s *Source) DeleteExtension(index int) error {
if err := deleteFromSlice(&s.Extensions, index); err != nil {
return fmt.Errorf("error deleting extension %v from source %v: %v", index, s, err)
}
return nil
}