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

24
atom.go
View File

@@ -1,6 +1,7 @@
package atom
import (
"encoding/xml"
"fmt"
"html"
"mime"
@@ -11,6 +12,29 @@ import (
"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.
// https://www.w3.org/2011/04/XMLSchema/TypeLibrary-IRI-RFC3987.xsd
func isValidIRI(iri string) bool {