From 5d4902e4e7e3afd5374a8a7931b2af87b9b55c2b Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Thu, 17 Oct 2024 12:37:44 -0300 Subject: [PATCH 1/4] Enhancement: sliceutil - New function to convert any slice to any Some libraries require providing `[]any` as input, and this function would be handy for dropping boring loop conversions. --- sliceutil/sliceutil.go | 9 +++++++++ sliceutil/sliceutil_test.go | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/sliceutil/sliceutil.go b/sliceutil/sliceutil.go index 0b24043..a4f1a40 100644 --- a/sliceutil/sliceutil.go +++ b/sliceutil/sliceutil.go @@ -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)) diff --git a/sliceutil/sliceutil_test.go b/sliceutil/sliceutil_test.go index caa9162..29443c5 100644 --- a/sliceutil/sliceutil_test.go +++ b/sliceutil/sliceutil_test.go @@ -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], in[i]) + } + } +} + func TestValues(t *testing.T) { type testStruct struct { Name string From 6b7e63d9341335de028b1a7f18bf86934b1f5a94 Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Thu, 17 Oct 2024 12:50:20 -0300 Subject: [PATCH 2/4] Fix test Co-authored-by: Marc --- sliceutil/sliceutil_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sliceutil/sliceutil_test.go b/sliceutil/sliceutil_test.go index 29443c5..6234e7a 100644 --- a/sliceutil/sliceutil_test.go +++ b/sliceutil/sliceutil_test.go @@ -627,7 +627,7 @@ func TestToAnySlice(t *testing.T) { 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], in[i]) + t.Errorf("want %[1]v(%[1]T),\tgot %[2]v(%[2]T)", in[i], out[i]) } } } From 03b83f5941e461bdb56edc06060827405e046d91 Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Thu, 17 Oct 2024 13:05:39 -0300 Subject: [PATCH 3/4] Allow use of deprecated go/ast package for now Linter was complaining about use of the `go/ast` package. Disabling the check for the moment as this will require extra changes. --- .github/workflows/build.yml | 6 +++--- goutil/goutil.go | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 965d093..a4b28f2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: | diff --git a/goutil/goutil.go b/goutil/goutil.go index 7d5d142..0214518 100644 --- a/goutil/goutil.go +++ b/goutil/goutil.go @@ -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 ( From 6b62a4877f7da2afda2dc65ba4f71d05bbef11e6 Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Thu, 17 Oct 2024 13:33:13 -0300 Subject: [PATCH 4/4] Update tests after compiler upgrade --- goutil/goutil_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/goutil/goutil_test.go b/goutil/goutil_test.go index 65a33c0..28b42ff 100644 --- a/goutil/goutil_test.go +++ b/goutil/goutil_test.go @@ -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", }, "", },