:generics-intensifies:

This commit is contained in:
Philipp Heckel 2022-03-22 19:43:08 -04:00
parent d7fa51be2c
commit 5f37741fe3
11 changed files with 38 additions and 44 deletions

View file

@ -31,7 +31,7 @@ func Gzip(next http.Handler) http.Handler {
}
var gzPool = sync.Pool{
New: func() interface{} {
New: func() any {
w := gzip.NewWriter(ioutil.Discard)
return w
},

View file

@ -33,8 +33,8 @@ func FileExists(filename string) bool {
return stat != nil
}
// InStringList returns true if needle is contained in haystack
func InStringList(haystack []string, needle string) bool {
// Contains returns true if needle is contained in haystack
func Contains[T comparable](haystack []T, needle T) bool {
for _, s := range haystack {
if s == needle {
return true
@ -43,8 +43,8 @@ func InStringList(haystack []string, needle string) bool {
return false
}
// InStringListAll returns true if all needles are contained in haystack
func InStringListAll(haystack []string, needles []string) bool {
// ContainsAll returns true if all needles are contained in haystack
func ContainsAll[T comparable](haystack []T, needles []T) bool {
matches := 0
for _, s := range haystack {
for _, needle := range needles {
@ -56,16 +56,6 @@ func InStringListAll(haystack []string, needles []string) bool {
return matches == len(needles)
}
// InIntList returns true if needle is contained in haystack
func InIntList(haystack []int, needle int) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
// SplitNoEmpty splits a string using strings.Split, but filters out empty strings
func SplitNoEmpty(s string, sep string) []string {
res := make([]string, 0)

View file

@ -52,20 +52,20 @@ func TestFileExists(t *testing.T) {
func TestInStringList(t *testing.T) {
s := []string{"one", "two"}
require.True(t, InStringList(s, "two"))
require.False(t, InStringList(s, "three"))
require.True(t, Contains(s, "two"))
require.False(t, Contains(s, "three"))
}
func TestInStringListAll(t *testing.T) {
s := []string{"one", "two", "three", "four"}
require.True(t, InStringListAll(s, []string{"two", "four"}))
require.False(t, InStringListAll(s, []string{"three", "five"}))
require.True(t, ContainsAll(s, []string{"two", "four"}))
require.False(t, ContainsAll(s, []string{"three", "five"}))
}
func TestInIntList(t *testing.T) {
s := []int{1, 2}
require.True(t, InIntList(s, 2))
require.False(t, InIntList(s, 3))
require.True(t, Contains(s, 2))
require.False(t, Contains(s, 3))
}
func TestSplitNoEmpty(t *testing.T) {