Skip to content

Enhancement: sliceutil - New function to convert a slice to any #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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

@@ -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: |
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 (
5 changes: 3 additions & 2 deletions goutil/goutil_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
"",
},
9 changes: 9 additions & 0 deletions sliceutil/sliceutil.go
Original file line number Diff line number Diff line change
@@ -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))
10 changes: 10 additions & 0 deletions sliceutil/sliceutil_test.go
Original file line number Diff line number Diff line change
@@ -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