Use generics for Add and Delete Methods

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

View File

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