From 7fdca516bab719da889066eaa539235e16a43ee3 Mon Sep 17 00:00:00 2001 From: Florent Messa Date: Tue, 3 Aug 2021 14:22:23 +0200 Subject: [PATCH] feat: linter --- .github/workflows/go.yml | 2 +- .github/workflows/golangci-lint.yml | 20 ++++++++++ Makefile | 8 +++- intersection.go | 16 ++++---- join_primitives.go | 24 +++--------- short_if_test.go | 59 +++++++++++++++-------------- 6 files changed, 72 insertions(+), 57 deletions(-) create mode 100644 .github/workflows/golangci-lint.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a4021c0..da02b82 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -22,4 +22,4 @@ jobs: run: go build -v ./... - name: Test - run: go test -v ./... + run: make test diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..4c97987 --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,20 @@ +name: golangci-lint +on: + push: + tags: + - v* + branches: + - master + - main + pull_request: +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version + version: v1.29 diff --git a/Makefile b/Makefile index 2fed2b2..59ae860 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,11 @@ +build: + go build -v ./... + test: - go test -v + go test -v ./... + +lint: + golangci-lint run bench: go test -benchmem -bench . diff --git a/intersection.go b/intersection.go index 17ec69e..a275ef0 100644 --- a/intersection.go +++ b/intersection.go @@ -97,14 +97,14 @@ func Difference(x interface{}, y interface{}) (interface{}, interface{}) { for i := 0; i < xValue.Len(); i++ { v := xValue.Index(i).Interface() - if Contains(y, v) == false { + if !Contains(y, v) { leftSlice = reflect.Append(leftSlice, xValue.Index(i)) } } for i := 0; i < yValue.Len(); i++ { v := yValue.Index(i).Interface() - if Contains(x, v) == false { + if !Contains(x, v) { rightSlice = reflect.Append(rightSlice, yValue.Index(i)) } } @@ -118,13 +118,13 @@ func DifferenceString(x []string, y []string) ([]string, []string) { rightSlice := []string{} for _, v := range x { - if ContainsString(y, v) == false { + if !ContainsString(y, v) { leftSlice = append(leftSlice, v) } } for _, v := range y { - if ContainsString(x, v) == false { + if !ContainsString(x, v) { rightSlice = append(rightSlice, v) } } @@ -138,13 +138,13 @@ func DifferenceInt64(x []int64, y []int64) ([]int64, []int64) { rightSlice := []int64{} for _, v := range x { - if ContainsInt64(y, v) == false { + if !ContainsInt64(y, v) { leftSlice = append(leftSlice, v) } } for _, v := range y { - if ContainsInt64(x, v) == false { + if !ContainsInt64(x, v) { rightSlice = append(rightSlice, v) } } @@ -178,13 +178,13 @@ func DifferenceInt(x []int, y []int) ([]int, []int) { rightSlice := []int{} for _, v := range x { - if ContainsInt(y, v) == false { + if !ContainsInt(y, v) { leftSlice = append(leftSlice, v) } } for _, v := range y { - if ContainsInt(x, v) == false { + if !ContainsInt(x, v) { rightSlice = append(rightSlice, v) } } diff --git a/join_primitives.go b/join_primitives.go index 1e44983..eefcac1 100644 --- a/join_primitives.go +++ b/join_primitives.go @@ -30,9 +30,7 @@ func OuterJoinInt(lx, rx []int) []int { rjoin := RightJoinInt(lx, rx) result := make([]int, len(ljoin)+len(rjoin)) - for i, v := range ljoin { - result[i] = v - } + copy(result, ljoin) for i, v := range rjoin { result[len(ljoin)+i] = v } @@ -94,9 +92,7 @@ func OuterJoinInt32(lx, rx []int32) []int32 { rjoin := RightJoinInt32(lx, rx) result := make([]int32, len(ljoin)+len(rjoin)) - for i, v := range ljoin { - result[i] = v - } + copy(result, ljoin) for i, v := range rjoin { result[len(ljoin)+i] = v } @@ -158,9 +154,7 @@ func OuterJoinInt64(lx, rx []int64) []int64 { rjoin := RightJoinInt64(lx, rx) result := make([]int64, len(ljoin)+len(rjoin)) - for i, v := range ljoin { - result[i] = v - } + copy(result, ljoin) for i, v := range rjoin { result[len(ljoin)+i] = v } @@ -222,9 +216,7 @@ func OuterJoinString(lx, rx []string) []string { rjoin := RightJoinString(lx, rx) result := make([]string, len(ljoin)+len(rjoin)) - for i, v := range ljoin { - result[i] = v - } + copy(result, ljoin) for i, v := range rjoin { result[len(ljoin)+i] = v } @@ -286,9 +278,7 @@ func OuterJoinFloat32(lx, rx []float32) []float32 { rjoin := RightJoinFloat32(lx, rx) result := make([]float32, len(ljoin)+len(rjoin)) - for i, v := range ljoin { - result[i] = v - } + copy(result, ljoin) for i, v := range rjoin { result[len(ljoin)+i] = v } @@ -350,9 +340,7 @@ func OuterJoinFloat64(lx, rx []float64) []float64 { rjoin := RightJoinFloat64(lx, rx) result := make([]float64, len(ljoin)+len(rjoin)) - for i, v := range ljoin { - result[i] = v - } + copy(result, ljoin) for i, v := range rjoin { result[len(ljoin)+i] = v } diff --git a/short_if_test.go b/short_if_test.go index 8f595c3..b09eb22 100644 --- a/short_if_test.go +++ b/short_if_test.go @@ -1,29 +1,30 @@ -package funk - -import ( - "testing" - "github.com/stretchr/testify/assert" -) - -func TestShortIf(t *testing.T) { - is := assert.New(t) - - r := ShortIf(10>5 , 10, 5) - is.Equal(r,10) - - r = ShortIf(10.0 == 10 , "yes", "no") - is.Equal(r,"yes") - - r = ShortIf('a'=='b',"equal chars","unequal chars") - is.Equal(r,"unequal chars") - - r = ShortIf("abc"=="abc","Same string","Different strings") - is.Equal(r,"Same string") - - type testStruct struct{} - a := testStruct{} - b := testStruct{} - r = ShortIf(a==b , &a, &b) - is.Equal(r,&b) - -} +package funk + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestShortIf(t *testing.T) { + is := assert.New(t) + + r := ShortIf(10 > 5, 10, 5) + is.Equal(r, 10) + + r = ShortIf(10.0 == 10, "yes", "no") + is.Equal(r, "yes") + + r = ShortIf('a' == 'b', "equal chars", "unequal chars") + is.Equal(r, "unequal chars") + + r = ShortIf(true, "Same string", "Different strings") + is.Equal(r, "Same string") + + type testStruct struct{} + a := testStruct{} + b := testStruct{} + r = ShortIf(a == b, &a, &b) + is.Equal(r, &b) + +}