Skip to content

Commit

Permalink
Merge pull request #83 from Teamwork/enhancement/sliceutil-toany
Browse files Browse the repository at this point in the history
Enhancement: sliceutil - New function to convert a slice to any
  • Loading branch information
rafaeljusto authored Oct 17, 2024
2 parents d630e9a + 6b62a48 commit 53670b9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v4

- name: Lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
only-new-issues: true

Expand All @@ -22,9 +22,9 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: '1.20'
go-version: '1.23'

- name: Test
run: |
Expand Down
2 changes: 2 additions & 0 deletions goutil/goutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Package goutil provides functions to work with Go source files.
//
//nolint:staticcheck
package goutil // import "github.com/teamwork/utils/v2/goutil"

import (
Expand Down
5 changes: 3 additions & 2 deletions goutil/goutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ func TestExpand(t *testing.T) {
"net/http/fcgi", "net/http/httptest", "net/http/httptrace",
"net/http/httputil", "net/http/internal", "net/http/internal/ascii",
"net/http/internal/testcert", "net/http/pprof",
"net/internal/socktest", "net/mail", "net/netip", "net/rpc", "net/rpc/jsonrpc",
"net/smtp", "net/textproto", "net/url",
"net/internal/cgotest", "net/internal/socktest", "net/mail",
"net/netip", "net/rpc", "net/rpc/jsonrpc", "net/smtp", "net/textproto",
"net/url",
},
"",
},
Expand Down
9 changes: 9 additions & 0 deletions sliceutil/sliceutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ func InterfaceSliceTo(src []interface{}, dst interface{}) interface{} {
return dstV.Interface()
}

// ToAnySlice converts a slice of T to a slice of any.
func ToAnySlice[T any](tt []T) []any {
ret := make([]any, len(tt))
for i, t := range tt {
ret[i] = t
}
return ret
}

// Values returns a list of items extracted from T, see test file for example
func Values[T comparable, N any](tt []T, fn func(T) N) []N {
ret := make([]N, len(tt))
Expand Down
10 changes: 10 additions & 0 deletions sliceutil/sliceutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,16 @@ func TestInterfaceSliceTo(t *testing.T) {
}
}

func TestToAnySlice(t *testing.T) {
in := []int{1, 2, 3, 4, 5}
out := ToAnySlice(in)
for i := range out {
if !reflect.DeepEqual(out[i], in[i]) {
t.Errorf("want %[1]v(%[1]T),\tgot %[2]v(%[2]T)", in[i], out[i])
}
}
}

func TestValues(t *testing.T) {
type testStruct struct {
Name string
Expand Down

0 comments on commit 53670b9

Please sign in to comment.