Use generics for Add and Delete Methods
This commit is contained in:
24
atom.go
24
atom.go
@@ -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 {
|
||||
|
Reference in New Issue
Block a user