Skip to content
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

Golang v1.21-22 #30

Merged
merged 7 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
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
44 changes: 8 additions & 36 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,56 +1,28 @@
name: Go Tests
on: [push, pull_request]
jobs:
test-1_18:
name: Test 1.18
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.18
uses: actions/setup-go@v5
with:
go-version: '1.18'
id: go
- name: Check out code
uses: actions/checkout@v4
- name: Test
run: go test -v ./...

test-1_19:
name: Test 1.19
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.19
uses: actions/setup-go@v5
with:
go-version: '1.19'
id: go
- name: Check out code
uses: actions/checkout@v4
- name: Test
run: go test -v ./...

test-1_20:
name: Test 1.20
test-1_21:
name: Test 1.21
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.20
- name: Set up Go 1.21
uses: actions/setup-go@v5
with:
go-version: '1.20'
go-version: '1.21'
id: go
- name: Check out code
uses: actions/checkout@v4
- name: Test
run: go test -v ./...

test-1_21:
name: Test 1.21
test-1_22:
name: Test 1.22
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.21
- name: Set up Go 1.22
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.22'
id: go
- name: Check out code
uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.22'
id: go
- name: Check out code
uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
version: v1.56
args: -c .golangci.yml -v
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# changelog

* golang 1.21 is now the minimum version
* :warning: `ReverseSlice` function in `basicalter` package is now deprecated
use [slices.Reverse()](https://pkg.go.dev/slices#Reverse) from the standard [slices](https://pkg.go.dev/slices) library instead
* :warning: `EqualSlice` function in `basiccheck` package is now deprecated
use [slices.Equal()](https://pkg.go.dev/slices#Equal) from the standard [slices](https://pkg.go.dev/slices) library instead
* :warning: `InSlice` function in `basiccheck` package is now deprecated
use [slices.Contains()](https://pkg.go.dev/slices#Contains) from the standard [slices](https://pkg.go.dev/slices) library instead
* :warning: `OneInSliceWith` function in `basiccheck` package is now deprecated
use [slices.ContainsFunc()](https://pkg.go.dev/slices#ContainsFunc) from the standard [slices](https://pkg.go.dev/slices) library instead

## v0.11.0

* add `SimilarSlice` function in `basiccheck` package
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import (
)

func main() {
sliceOfString := []string{"foo", "bar"}
s1 := []string{"foo", "bar"}
s2 := []string{"bar", "foo"}

if basiccheck.InSlice("bar", sliceOfString) {
fmt.Printf("bar found in %v", sliceOfString)
// Output: bar found in [foo bar]
if basiccheck.SimilarSlice(s1, s2) {
fmt.Printf("%v =~ %v", s1, s2)
// Output: [foo bar] =~ [bar foo]
} else {
fmt.Printf("bar not found in %v", sliceOfString)
fmt.Printf("%v != %v", s1, s2)
}
}
```
6 changes: 5 additions & 1 deletion basicalter/slice.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package basicalter

import "sort"
import (
"sort"
)

// UniqueInSlice remove duplicate elements in slice
// and return the result with a new slice.
Expand Down Expand Up @@ -69,6 +71,8 @@ func FilterInSliceWith[T any, S ~[]T](list S, filter func(T) bool) S {
}

// ReverseSlice reverse order of a slice last to first, before last to second, etc.
//
// Deprecated: use slices.Reverse() from the standard 'slices' library instead.
func ReverseSlice[T any](list []T) {
for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
list[i], list[j] = list[j], list[i]
Expand Down
20 changes: 10 additions & 10 deletions basicalter/slice_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package basicalter_test

import (
"slices"
"strings"
"testing"

"github.com/jeremmfr/go-utils/basicalter"
"github.com/jeremmfr/go-utils/basiccheck"
)

func TestUniqueInSlice(t *testing.T) {
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestDelEmptyStrings(t *testing.T) {

if v := basicalter.DelEmptyStrings(sliceOfString); len(v) != 2 {
t.Errorf("DelEmptyStrings didn't remove empty string: %v", v)
} else if !basiccheck.EqualSlice(v, []string{"foo", "bar"}) {
} else if !slices.Equal(v, []string{"foo", "bar"}) {
t.Errorf("DelEmptyStrings didn't remove empty string: %v", v)
}
}
Expand All @@ -59,7 +59,7 @@ func TestDelInSlice(t *testing.T) {

if v := basicalter.DelInSlice("baz", sliceOfString); len(v) != 2 {
t.Errorf("DelInSlice didn't remove 'baz': %v", v)
} else if !basiccheck.EqualSlice(v, []string{"foo", "bar"}) {
} else if !slices.Equal(v, []string{"foo", "bar"}) {
t.Errorf("DelInSlice didn't remove 'baz': %v", v)
}
}
Expand All @@ -75,12 +75,12 @@ func TestFilterInSliceWith(t *testing.T) {
return strings.HasPrefix(s, "ba")
}); len(v) != 3 {
t.Errorf("FilterInSliceWith didn't remove foo (without prefix 'ba'): %v", v)
} else if !basiccheck.EqualSlice(v, []string{"baz", "bar", "baz"}) {
} else if !slices.Equal(v, []string{"baz", "bar", "baz"}) {
t.Errorf("FilterInSliceWith didn't remove foo (without prefix 'ba'): %v", v)
}

var nilSlice []string
if v := basicalter.FilterInSliceWith(nilSlice, func(s string) bool {
if v := basicalter.FilterInSliceWith(nilSlice, func(_ string) bool {
return true
}); v != nil {
t.Errorf("FilterInSliceWith didn't return nil slice with nil input slice")
Expand All @@ -93,7 +93,7 @@ func TestReverseSlice(t *testing.T) {
basicalter.ReverseSlice(sliceOfString)

desiredStringSlice := []string{"Hello", "World", "bar", "baz", "foo"}
if !basiccheck.EqualSlice(sliceOfString, desiredStringSlice) {
if !slices.Equal(sliceOfString, desiredStringSlice) {
t.Errorf("ReverseSlice didn't reverse slice: %v expected %v", sliceOfString, desiredStringSlice)
}

Expand All @@ -102,7 +102,7 @@ func TestReverseSlice(t *testing.T) {
basicalter.ReverseSlice(sliceOfInt64)

desiredInt64Slice := []int64{0, 1, 2, 3}
if !basiccheck.EqualSlice(sliceOfInt64, desiredInt64Slice) {
if !slices.Equal(sliceOfInt64, desiredInt64Slice) {
t.Errorf("ReverseSlice didn't reverse slice: %v expected %v", sliceOfInt64, desiredInt64Slice)
}
}
Expand All @@ -113,7 +113,7 @@ func TestSortStringsByLengthInc(t *testing.T) {
basicalter.SortStringsByLengthInc(s)

desiredSlice := []string{"Go", "Grin", "Alpha", "Bravo", "Delta", "Gopher"}
if !basiccheck.EqualSlice(s, desiredSlice) {
if !slices.Equal(s, desiredSlice) {
t.Errorf("SortStringsByLength didn't sort slice with smaller first and lexicographic order")
}
}
Expand All @@ -124,7 +124,7 @@ func TestSortStringsByLengthDec(t *testing.T) {
basicalter.SortStringsByLengthDec(s)

desiredSlice := []string{"Gopher", "Alpha", "Bravo", "Delta", "Grin", "Go"}
if !basiccheck.EqualSlice(s, desiredSlice) {
if !slices.Equal(s, desiredSlice) {
t.Errorf("SortStringsByLength didn't sort slice with smaller last and lexicographic order")
}
}
Expand All @@ -134,7 +134,7 @@ func TestReplaceInSliceWith(t *testing.T) {

basicalter.ReplaceInSliceWith(sliceOfString, strings.ToLower)

if !basiccheck.EqualSlice(sliceOfString, []string{"foo", "bar", "baz"}) {
if !slices.Equal(sliceOfString, []string{"foo", "bar", "baz"}) {
t.Errorf("ReplaceInSliceWith didn't replace all strings in slice "+
"with the lowercase version: %v", sliceOfString)
}
Expand Down
11 changes: 6 additions & 5 deletions basiccheck/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
)

func Example() {
input := []string{"foo", "bar"}
if basiccheck.InSlice("bar", input) {
fmt.Printf("bar found in %v", input)
// Output: bar found in [foo bar]
input1 := []string{"foo", "bar"}
input2 := []string{"bar", "foo"}
if basiccheck.SimilarSlice(input1, input2) {
fmt.Printf("%v =~ %v", input1, input2)
// Output: [foo bar] =~ [bar foo]
} else {
fmt.Printf("bar not found in %v", input)
fmt.Printf("%v != %v", input1, input2)
}
}

Expand Down
10 changes: 9 additions & 1 deletion basiccheck/slice.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package basiccheck

import "slices"

// InSlice check if an element is present in a slice.
//
// Deprecated: use slices.Contains() from the standard 'slices' library instead.
func InSlice[T comparable](elem T, list []T) bool {
return OneInSliceWith(list, func(v T) bool {
return v == elem
})
}

// EqualSlice check if two slice is Equal: same length, same element in same order.
//
// Deprecated: use slices.Equal() from the standard 'slices' library instead.
func EqualSlice[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
Expand All @@ -28,7 +34,7 @@ func SimilarSlice[T comparable](a, b []T) bool {
return false
}
for _, v := range a {
if !InSlice(v, b) {
if !slices.Contains(b, v) {
return false
}
}
Expand All @@ -40,6 +46,8 @@ func SimilarSlice[T comparable](a, b []T) bool {
// returns true with the function 'find' passed in arguments.
//
// If 'find' is nil, return false.
//
// Deprecated: use slices.ContainsFunc() from the standard 'slices' library instead.
func OneInSliceWith[T any](list []T, find func(T) bool) bool {
if find == nil {
return false
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/jeremmfr/go-utils

go 1.18
go 1.21
Loading