From ec26e9101efc98bc99d733d75a6f8cb3fbae5ec1 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 16 Mar 2022 06:08:33 -0400 Subject: [PATCH 1/4] :construction_worker: adding test and push coverage --- .github/workflows/unit-test.yml | 42 +++++++++++++-------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index a025a03..23ccce4 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -5,29 +5,19 @@ jobs: name: Build runs-on: ubuntu-latest steps: - - name: Install gotip - run: | - git clone --depth=1 https://go.googlesource.com/go $HOME/gotip - cd $HOME/gotip/src - ./make.bash - echo "GOROOT=$HOME/gotip" >> $GITHUB_ENV - echo "$HOME/gotip/bin:$PATH" >> $GITHUB_PATH - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - - name: Get dependencies - run: | - go get -v -t -d ./... - if [ -f Gopkg.toml ]; then - curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - dep ensure - fi - - name: Test - run: go test -v ./... -# - name: Upload coverage report -# uses: codecov/codecov-action@v2.1.0 -# with: -# token: 7106da3d-7479-471f-ae37-e3680b62063d -# files: ./coverage.txt -# flags: unittests -# name: codecov-umbrella \ No newline at end of file + - uses: actions/checkout@master + with: + fetch-depth: 2 + - uses: actions/setup-go@v2 + with: + go-version: '1.18' + - name: Run tests with coverage + run: go test -race -coverprofile=coverage.out -covermode=atomic ./... + - uses: codecov/codecov-action@v2 + with: + token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos + files: ./coverage.out # optional + flags: unittests # optional + name: codecov-umbrella # optional + fail_ci_if_error: true # optional (default = false) + verbose: true # optional (default = false) \ No newline at end of file From 36405aff84739d3823ae55e1e9d8bca5dc3d9143 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 16 Mar 2022 07:24:28 -0400 Subject: [PATCH 2/4] :arrow_up: use official Go 1.18 image --- Dockerfile | 2 +- chain/chain.go | 2 +- go.mod | 5 ++++- go.sum | 2 ++ max.go | 2 +- min.go | 2 +- sum.go | 2 +- 7 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 502712b..4974ba9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.18beta1-alpine +FROM golang:1.18-alpine WORKDIR /lib diff --git a/chain/chain.go b/chain/chain.go index 3e81489..71be2c3 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -1,7 +1,7 @@ package chain import ( - "constraints" + "golang.org/x/exp/constraints" u "github.com/rjNemo/underscore" ) diff --git a/go.mod b/go.mod index 98452a0..371f6eb 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module github.com/rjNemo/underscore go 1.18 -require github.com/stretchr/testify v1.7.0 +require ( + github.com/stretchr/testify v1.7.0 + golang.org/x/exp v0.0.0-20220314205449-43aec2f8a4e7 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index c221f64..99f8a6a 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/exp v0.0.0-20220314205449-43aec2f8a4e7 h1:jynE66seADJbyWMUdeOyVTvPtBZt7L6LJHupGwxPZRM= +golang.org/x/exp v0.0.0-20220314205449-43aec2f8a4e7/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/max.go b/max.go index b1e92ac..0799e0b 100644 --- a/max.go +++ b/max.go @@ -1,6 +1,6 @@ package underscore -import "constraints" +import "golang.org/x/exp/constraints" // Max returns the maximum value in the slice. // This function can currently only compare numbers reliably. diff --git a/min.go b/min.go index ce518fa..7789583 100644 --- a/min.go +++ b/min.go @@ -1,6 +1,6 @@ package underscore -import "constraints" +import "golang.org/x/exp/constraints" // Min returns the minimum value in the slice. // This function can currently only compare numbers reliably. diff --git a/sum.go b/sum.go index 278a14f..3d2b5db 100644 --- a/sum.go +++ b/sum.go @@ -1,6 +1,6 @@ package underscore -import "constraints" +import "golang.org/x/exp/constraints" // Sum adds elements of the slice. func Sum[T constraints.Ordered](values []T) (sum T) { From 7ac00e32bcc59e9dd28cb903914b07cb09e27d39 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 16 Mar 2022 13:07:45 -0400 Subject: [PATCH 3/4] :sparkles: result interface --- result.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ result_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 result.go create mode 100644 result_test.go diff --git a/result.go b/result.go new file mode 100644 index 0000000..d6463f4 --- /dev/null +++ b/result.go @@ -0,0 +1,49 @@ +package underscore + +// Result represent the outcome of an operation where failure is possible +type Result[T any] interface { + isResult() //to seal the Result interface + ToValue() (*T, error) + IsSuccess() bool +} + +// Ok is the Result that represents success. +type Ok[T any] struct { + Value *T +} + +func (Ok[T]) isResult() {} + +func (o Ok[T]) ToValue() (*T, error) { + return o.Value, nil +} + +func (o Ok[T]) IsSuccess() bool { + return true +} + +// Err is the Result that represents failure. It implements the error interface +type Err[T any] struct{ Err error } + +func (e Err[T]) ToValue() (*T, error) { + return nil, e.Err +} + +func (e Err[T]) IsSuccess() bool { + return false +} + +func (Err[T]) isResult() {} + +func (e Err[T]) Error() string { + return e.Err.Error() +} + +func ToResult[T any](value *T, err error) Result[T] { + if err != nil { + return Err[T]{ + Err: err, + } + } + return Ok[T]{Value: value} +} diff --git a/result_test.go b/result_test.go new file mode 100644 index 0000000..7eca7a8 --- /dev/null +++ b/result_test.go @@ -0,0 +1,41 @@ +package underscore_test + +import ( + "errors" + "testing" + + u "github.com/rjNemo/underscore" + "github.com/stretchr/testify/assert" +) + +func TestSuccess(t *testing.T) { + res := isAnswerToLife(42) + assert.True(t, res.IsSuccess()) +} + +func TestFailure(t *testing.T) { + res := isAnswerToLife(13) + assert.False(t, res.IsSuccess()) +} + +func TestIsOK(t *testing.T) { + res, err := isAnswerToLife(42).ToValue() + assert.NoError(t, err) + assert.Equal(t, "You get it", *res) +} + +func TestIsError(t *testing.T) { + life := isAnswerToLife(13) + res, err := life.ToValue() + assert.Error(t, err) + assert.Equal(t, "nope", life.(u.Err[string]).Error()) + assert.Nil(t, res) +} + +func isAnswerToLife(num int) u.Result[string] { + if num == 42 { + res := "You get it" + return u.ToResult(&res, nil) + } + return u.ToResult[string](nil, errors.New("nope")) // u.Err[string]{Err: errors.New("nope")} +} From c07554fc700126480087de0893dc0d40923930a3 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 18 Mar 2022 13:32:08 -0400 Subject: [PATCH 4/4] delete commented code --- result_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/result_test.go b/result_test.go index 7eca7a8..75c9398 100644 --- a/result_test.go +++ b/result_test.go @@ -37,5 +37,5 @@ func isAnswerToLife(num int) u.Result[string] { res := "You get it" return u.ToResult(&res, nil) } - return u.ToResult[string](nil, errors.New("nope")) // u.Err[string]{Err: errors.New("nope")} + return u.ToResult[string](nil, errors.New("nope")) }