diff --git a/.github/workflows/cloc.yml b/.github/workflows/cloc.yml index d6ff20d..4592bb4 100644 --- a/.github/workflows/cloc.yml +++ b/.github/workflows/cloc.yml @@ -24,7 +24,7 @@ jobs: - name: Count Lines Of Code id: loc run: | - curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz + curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.2/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && echo "b17e76bede22af0206b4918d3b3c4e7357f2a21b57f8de9e7c9dc0eb56b676c0 sccdiff" | shasum -c OUTPUT=$(cd pr && ../sccdiff -basedir ../base) echo "${OUTPUT}" OUTPUT="${OUTPUT//$'\n'/%0A}" diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index ab81105..48207f9 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -19,12 +19,15 @@ jobs: name: golangci-lint runs-on: ubuntu-latest steps: + - uses: actions/setup-go@v3 + with: + go-version: 1.18.x - uses: actions/checkout@v2 - name: golangci-lint - uses: golangci/golangci-lint-action@v3.0.0 + uses: golangci/golangci-lint-action@v3.1.0 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.44.2 + version: v1.46.2 # Optional: working directory, useful for monorepos # working-directory: somedir diff --git a/.github/workflows/gorelease.yml b/.github/workflows/gorelease.yml new file mode 100644 index 0000000..0a5a701 --- /dev/null +++ b/.github/workflows/gorelease.yml @@ -0,0 +1,57 @@ +# This script is provided by github.com/bool64/dev. +name: gorelease +on: + pull_request: + +# Cancel the workflow in progress in newer build is about to start. +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +env: + GO_VERSION: 1.18.x +jobs: + gorelease: + runs-on: ubuntu-latest + steps: + - name: Install Go stable + if: env.GO_VERSION != 'tip' + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + - name: Install Go tip + if: env.GO_VERSION == 'tip' + run: | + curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz + ls -lah gotip.tar.gz + mkdir -p ~/sdk/gotip + tar -C ~/sdk/gotip -xzf gotip.tar.gz + ~/sdk/gotip/bin/go version + echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV + - name: Checkout code + uses: actions/checkout@v2 + - name: Gorelease cache + uses: actions/cache@v2 + with: + path: | + ~/go/bin/gorelease + key: ${{ runner.os }}-gorelease-fork + - name: Gorelease + id: gorelease + run: | + test -e ~/go/bin/gorelease || (rm -rf /tmp/gorelease && mkdir -p /tmp/gorelease && cd /tmp/gorelease && go mod init foo && go mod edit -replace golang.org/x/exp=github.com/vearutop/golang-exp@gorelease-generic && go get golang.org/x/exp/cmd/gorelease && go install golang.org/x/exp/cmd/gorelease) + OUTPUT=$(gorelease 2>&1 || exit 0) + echo "${OUTPUT}" + OUTPUT="${OUTPUT//$'\n'/%0A}" + echo "::set-output name=report::$OUTPUT" + - name: Comment Report + continue-on-error: true + uses: marocchino/sticky-pull-request-comment@v2 + with: + header: gorelease + message: | + ### Go API Changes + +
+ ${{ steps.gorelease.outputs.report }} +\ No newline at end of file diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index 0100307..6b6b717 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -20,14 +20,13 @@ jobs: test: strategy: matrix: - go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.0-rc1, tip ] + go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x ] runs-on: ubuntu-latest steps: - - name: Install Go + - name: Install Go stable if: matrix.go-version != 'tip' - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - stable: false go-version: ${{ matrix.go-version }} - name: Install Go tip if: matrix.go-version == 'tip' diff --git a/.golangci.yml b/.golangci.yml index 597ce97..6f5d257 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,10 +1,6 @@ # See https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml run: tests: true - skip-files: - - "go1.18" - skip-dirs: - - "../" linters-settings: errcheck: @@ -41,6 +37,8 @@ linters: - tagliatelle - errname - ireturn + - exhaustruct + - nonamedreturns issues: exclude-use-default: false @@ -54,4 +52,7 @@ issues: - dupl - errorlint path: "_test.go" + - linters: + - gocritic + text: "newDeref: replace `\\*new" diff --git a/Makefile b/Makefile index 996713d..7de96d0 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -#GOLANGCI_LINT_VERSION := "v1.44.2" # Optional configuration to pinpoint golangci-lint version. +#GOLANGCI_LINT_VERSION := "v1.46.2" # Optional configuration to pinpoint golangci-lint version. # The head of Makefile determines location of dev-go to include standard targets. GO ?= go diff --git a/error.go b/error.go index e5774cf..706ce93 100644 --- a/error.go +++ b/error.go @@ -38,3 +38,9 @@ func (e Error) Unwrap() error { return e.Value } + +type sentinelError string + +func (s sentinelError) Error() string { + return string(s) +} diff --git a/generic_go1.18.go b/generic_go1.18.go index 4279d69..d5bb394 100644 --- a/generic_go1.18.go +++ b/generic_go1.18.go @@ -8,6 +8,10 @@ import ( "fmt" ) +// ErrInvalidType is returned on port type assertion error. +const ErrInvalidType = sentinelError("invalid type") + +// IOInteractorOf is an IOInteractor with parametrized input/output types. type IOInteractorOf[i, o any] struct { IOInteractor @@ -30,12 +34,12 @@ func NewInteractor[i, o any](interact func(ctx context.Context, input i, output u.Interactor = Interact(func(ctx context.Context, input, output any) error { inp, ok := input.(i) if !ok { - return fmt.Errorf("invalid input type received: %T, expected: %T", input, u.Input) + return fmt.Errorf("%w of input: %T, expected: %T", ErrInvalidType, input, u.Input) } out, ok := output.(*o) if !ok { - return fmt.Errorf("invalid output type received: %T, expected: %T", output, u.Output) + return fmt.Errorf("%w f output: %T, expected: %T", ErrInvalidType, output, u.Output) } return interact(ctx, inp, out) diff --git a/generic_go1.18_test.go b/generic_go1.18_test.go index 926334b..3e47169 100644 --- a/generic_go1.18_test.go +++ b/generic_go1.18_test.go @@ -13,9 +13,12 @@ import ( ) func TestNewIOI_classic(t *testing.T) { - u := usecase.NewIOI(*new(int), new(string), func(ctx context.Context, input, output interface{}) error { - in := input.(int) - out := output.(*string) + u := usecase.NewIOI(0, new(string), func(ctx context.Context, input, output interface{}) error { + in, ok := input.(int) + assert.True(t, ok) + + out, ok := output.(*string) + assert.True(t, ok) *out = strconv.Itoa(in) @@ -25,6 +28,7 @@ func TestNewIOI_classic(t *testing.T) { ctx := context.Background() var out string + assert.NoError(t, u.Interact(ctx, 123, &out)) assert.Equal(t, "123", out) } @@ -41,10 +45,13 @@ func TestNewInteractor(t *testing.T) { ctx := context.Background() var out string + assert.NoError(t, u.Interact(ctx, 123, &out)) assert.Equal(t, "123", out) out = "" assert.NoError(t, u.Invoke(ctx, 123, &out)) assert.Equal(t, "123", out) + + assert.Equal(t, "invalid type", usecase.ErrInvalidType.Error()) } diff --git a/go.mod b/go.mod index ac1763d..d078bea 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,12 @@ module github.com/swaggest/usecase go 1.18 require ( - github.com/bool64/dev v0.2.7 - github.com/stretchr/testify v1.7.0 + github.com/bool64/dev v0.2.12 + github.com/stretchr/testify v1.7.1 ) require ( - github.com/davecgh/go-spew v1.1.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + gopkg.in/yaml.v3 v3.0.0 // indirect ) diff --git a/go.sum b/go.sum index e6b1fed..a8eed60 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,15 @@ -github.com/bool64/dev v0.2.7 h1:Zs1mCSJJwe8zBGLrlQ6pjuVUJjQtYbQslvxBwlzrYqo= -github.com/bool64/dev v0.2.7/go.mod h1:/csLrm+4oDSsKJRIVS0mrywAonLnYKFG8RvGT7Jh9b8= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/bool64/dev v0.2.12 h1:XXXE1Eh2//Dmp1hr+SqjxIXOEjq6D3Y5SwbGowpIK4I= +github.com/bool64/dev v0.2.12/go.mod h1:/csLrm+4oDSsKJRIVS0mrywAonLnYKFG8RvGT7Jh9b8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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 h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA= +gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=