Skip to content

Commit 64f50e6

Browse files
authored
Add sliceutil package (#53)
1 parent 184e4b0 commit 64f50e6

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ linters:
3030
- varcheck
3131

3232
linters-settings:
33+
ireturn:
34+
allow:
35+
- anon
36+
- empty
37+
- error
38+
- generic
39+
- stdlib
3340
lll:
3441
tab-width: 4
3542
tagliatelle:

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/prometheus/client_golang v1.14.0
1212
github.com/sirupsen/logrus v1.9.3
1313
github.com/spf13/viper v1.14.0
14+
github.com/stretchr/testify v1.8.3
1415
github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f
1516
gotest.tools v2.2.0+incompatible
1617
)
@@ -21,6 +22,7 @@ require (
2122
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2223
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
2324
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
25+
github.com/davecgh/go-spew v1.1.1 // indirect
2426
github.com/fsnotify/fsnotify v1.6.0 // indirect
2527
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
2628
github.com/gin-contrib/sse v0.1.0 // indirect
@@ -43,6 +45,7 @@ require (
4345
github.com/pelletier/go-toml v1.9.5 // indirect
4446
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
4547
github.com/pkg/errors v0.9.1 // indirect
48+
github.com/pmezard/go-difflib v1.0.0 // indirect
4649
github.com/prometheus/client_model v0.3.0 // indirect
4750
github.com/prometheus/common v0.37.0 // indirect
4851
github.com/prometheus/procfs v0.8.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
7979
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
8080
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
8181
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
82+
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
8283
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
8384
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
8485
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
@@ -105,6 +106,7 @@ github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab h1:xveKWz2iauee
105106
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8=
106107
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
107108
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
109+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
108110
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
109111
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
110112
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=

pkg/sliceutil/sliceutil.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Package sliceutil provides useful functions for working with slices not provided by the standard library.
2+
package sliceutil
3+
4+
import (
5+
"reflect"
6+
"strings"
7+
)
8+
9+
// RemoveZeroValues returns a copy of s with all zero-valued elements removed.
10+
func RemoveZeroValues[S ~[]E, E any](s S) S {
11+
updatedSlice := make([]E, 0)
12+
for _, v := range s {
13+
if !reflect.ValueOf(v).IsZero() {
14+
updatedSlice = append(updatedSlice, v)
15+
}
16+
}
17+
18+
return updatedSlice
19+
}
20+
21+
// ToLower returns a copy of s with all letters in all elements mapped to lower case.
22+
func ToLower(s []string) []string {
23+
lowerCasedSlice := make([]string, 0, len(s))
24+
for _, v := range s {
25+
lowerCased := strings.ToLower(v)
26+
lowerCasedSlice = append(lowerCasedSlice, lowerCased)
27+
}
28+
29+
return lowerCasedSlice
30+
}
31+
32+
// Trim returns a copy of s with all leading and trailing whitespace in all elements removed.
33+
func Trim(s []string) []string {
34+
trimmedSlice := make([]string, 0, len(s))
35+
for _, v := range s {
36+
trimmed := strings.TrimSpace(v)
37+
trimmedSlice = append(trimmedSlice, trimmed)
38+
}
39+
40+
return trimmedSlice
41+
}

pkg/sliceutil/sliceutil_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package sliceutil
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestRemoveZeroValues(t *testing.T) {
10+
boolSlice := []bool{false, false, false}
11+
require.Equal(t, []bool{}, RemoveZeroValues(boolSlice))
12+
13+
intSlice := []int{1, 987, 0}
14+
require.Equal(t, []int{1, 987}, RemoveZeroValues(intSlice))
15+
16+
strSlice := []string{"hello", "world", ""}
17+
require.Equal(t, []string{"hello", "world"}, RemoveZeroValues(strSlice))
18+
19+
str1 := "hello"
20+
str2 := "world"
21+
str1Ptr := &str1
22+
str2Ptr := &str2
23+
var nilStrPtr *string
24+
25+
strPtrSlice := []*string{&str1, &str2, nilStrPtr, nil}
26+
require.Equal(t, []*string{str1Ptr, str2Ptr}, RemoveZeroValues(strPtrSlice))
27+
28+
type testStruct struct {
29+
flag bool
30+
}
31+
32+
ts := []testStruct{{flag: true}, {flag: false}}
33+
require.Equal(t, []testStruct{{flag: true}}, RemoveZeroValues(ts))
34+
}
35+
36+
func TestToLower(t *testing.T) {
37+
s := []string{"apples", "ORANGES", "**Pears**", "bAnAnAs-123"}
38+
require.Equal(t, []string{"apples", "oranges", "**pears**", "bananas-123"}, ToLower(s))
39+
}
40+
41+
func TestTrim(t *testing.T) {
42+
s := []string{"no-space", "middle space", " left-space", "right-space ", " surrounding-space "}
43+
require.Equal(t, []string{"no-space", "middle space", "left-space", "right-space", "surrounding-space"}, Trim(s))
44+
}

0 commit comments

Comments
 (0)