Skip to content

Commit 7dca61e

Browse files
authored
Add constructor options, update CI and deps (#14)
1 parent 7ca847b commit 7dca61e

File tree

13 files changed

+105
-47
lines changed

13 files changed

+105
-47
lines changed

.github/workflows/cloc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ jobs:
2424
- name: Count Lines Of Code
2525
id: loc
2626
run: |
27-
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
27+
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.3/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
28+
sccdiff_hash=$(git hash-object ./sccdiff)
29+
[ "$sccdiff_hash" == "ae8a07b687bd3dba60861584efe724351aa7ff63" ] || (echo "::error::unexpected hash for sccdiff, possible tampering: $sccdiff_hash" && exit 1)
2830
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
2931
echo "${OUTPUT}"
3032
OUTPUT="${OUTPUT//$'\n'/%0A}"

.github/workflows/golangci-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ jobs:
2121
steps:
2222
- uses: actions/setup-go@v3
2323
with:
24-
go-version: 1.18.x
24+
go-version: 1.19.x
2525
- uses: actions/checkout@v2
2626
- name: golangci-lint
2727
uses: golangci/[email protected]
2828
with:
2929
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
30-
version: v1.46.2
30+
version: v1.49.0
3131

3232
# Optional: working directory, useful for monorepos
3333
# working-directory: somedir

.github/workflows/gorelease.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ concurrency:
99
cancel-in-progress: true
1010

1111
env:
12-
GO_VERSION: 1.18.x
12+
GO_VERSION: 1.19.x
1313
jobs:
1414
gorelease:
1515
runs-on: ubuntu-latest
@@ -35,11 +35,11 @@ jobs:
3535
with:
3636
path: |
3737
~/go/bin/gorelease
38-
key: ${{ runner.os }}-gorelease-fork
38+
key: ${{ runner.os }}-gorelease-generic
3939
- name: Gorelease
4040
id: gorelease
4141
run: |
42-
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)
42+
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
4343
OUTPUT=$(gorelease 2>&1 || exit 0)
4444
echo "${OUTPUT}"
4545
OUTPUT="${OUTPUT//$'\n'/%0A}"

.github/workflows/test-unit.yml

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ env:
1616
GO111MODULE: "on"
1717
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
1818
COV_GO_VERSION: 1.18.x # Version of Go to collect coverage
19+
TARGET_DELTA_COV: 90 # Target coverage of changed lines, in percents
1920
jobs:
2021
test:
2122
strategy:
2223
matrix:
23-
go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x ]
24+
go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x ]
2425
runs-on: ubuntu-latest
2526
steps:
2627
- name: Install Go stable
2728
if: matrix.go-version != 'tip'
2829
uses: actions/setup-go@v3
2930
with:
3031
go-version: ${{ matrix.go-version }}
32+
3133
- name: Install Go tip
3234
if: matrix.go-version == 'tip'
3335
run: |
@@ -37,8 +39,10 @@ jobs:
3739
tar -C ~/sdk/gotip -xzf gotip.tar.gz
3840
~/sdk/gotip/bin/go version
3941
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
42+
4043
- name: Checkout code
4144
uses: actions/checkout@v2
45+
4246
- name: Go cache
4347
uses: actions/cache@v2
4448
with:
@@ -51,58 +55,81 @@ jobs:
5155
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
5256
restore-keys: |
5357
${{ runner.os }}-go-cache
58+
5459
- name: Restore base test coverage
5560
id: base-coverage
56-
if: matrix.go-version == env.COV_GO_VERSION
61+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
5762
uses: actions/cache@v2
5863
with:
5964
path: |
6065
unit-base.txt
6166
# Use base sha for PR or new commit hash for master/main push in test result key.
6267
key: ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
63-
- name: Checkout base code
64-
if: matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
65-
uses: actions/checkout@v2
66-
with:
67-
ref: ${{ github.event.pull_request.base.sha }}
68-
path: __base
68+
6969
- name: Run test for base code
7070
if: matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
7171
run: |
72-
cd __base
73-
make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
72+
git fetch origin master ${{ github.event.pull_request.base.sha }}
73+
HEAD=$(git rev-parse HEAD)
74+
git reset --hard ${{ github.event.pull_request.base.sha }}
75+
(make test-unit && go tool cover -func=./unit.coverprofile > unit-base.txt) || echo "No test-unit in base"
76+
git reset --hard $HEAD
77+
7478
- name: Test
7579
id: test
7680
run: |
7781
make test-unit
78-
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > unit.txt
79-
OUTPUT=$(test -e unit-base.txt && (diff unit-base.txt unit.txt || exit 0) || cat unit.txt)
80-
echo "${OUTPUT}"
81-
OUTPUT="${OUTPUT//$'\n'/%0A}"
82+
go tool cover -func=./unit.coverprofile > unit.txt
8283
TOTAL=$(grep 'total:' unit.txt)
8384
echo "${TOTAL}"
84-
echo "::set-output name=diff::$OUTPUT"
8585
echo "::set-output name=total::$TOTAL"
86-
- name: Store base coverage
87-
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
88-
run: cp unit.txt unit-base.txt
86+
87+
- name: Annotate missing test coverage
88+
id: annotate
89+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
90+
run: |
91+
curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.3.6/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
92+
gocovdiff_hash=$(git hash-object ./gocovdiff)
93+
[ "$gocovdiff_hash" == "8e507e0d671d4d6dfb3612309b72b163492f28eb" ] || (echo "::error::unexpected hash for gocovdiff, possible tampering: $gocovdiff_hash" && exit 1)
94+
git fetch origin master ${{ github.event.pull_request.base.sha }}
95+
REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV})
96+
echo "${REP}"
97+
REP="${REP//$'\n'/%0A}"
98+
cat gha-unit.txt
99+
DIFF=$(test -e unit-base.txt && ./gocovdiff -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file")
100+
DIFF="${DIFF//$'\n'/%0A}"
101+
TOTAL=$(cat delta-cov-unit.txt)
102+
echo "::set-output name=rep::$REP"
103+
echo "::set-output name=diff::$DIFF"
104+
echo "::set-output name=total::$TOTAL"
105+
89106
- name: Comment Test Coverage
90107
continue-on-error: true
91-
if: matrix.go-version == env.COV_GO_VERSION
108+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
92109
uses: marocchino/sticky-pull-request-comment@v2
93110
with:
94111
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
95112
header: unit-test
96113
message: |
97114
### Unit Test Coverage
98115
${{ steps.test.outputs.total }}
116+
${{ steps.annotate.outputs.total }}
117+
<details><summary>Coverage of changed lines</summary>
118+
119+
${{ steps.annotate.outputs.rep }}
120+
121+
</details>
122+
99123
<details><summary>Coverage diff with base branch</summary>
100124
101-
```diff
102-
${{ steps.test.outputs.diff }}
103-
```
125+
${{ steps.annotate.outputs.diff }}
126+
104127
</details>
105128
129+
- name: Store base coverage
130+
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
131+
run: cp unit.txt unit-base.txt
132+
106133
- name: Upload code coverage
107134
if: matrix.go-version == env.COV_GO_VERSION
108135
uses: codecov/codecov-action@v1

.golangci.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,31 @@ linters:
3939
- ireturn
4040
- exhaustruct
4141
- nonamedreturns
42+
- nosnakecase
43+
- structcheck
44+
- varcheck
45+
- deadcode
4246

4347
issues:
4448
exclude-use-default: false
4549
exclude-rules:
4650
- linters:
51+
- gocritic
52+
text: "newDeref:" # False-positive for generics.
53+
- linters:
54+
- errorlint
4755
- gomnd
4856
- goconst
4957
- goerr113
5058
- noctx
5159
- funlen
5260
- dupl
53-
- errorlint
61+
- structcheck
62+
- unused
63+
- unparam
64+
- nosnakecase
5465
path: "_test.go"
5566
- linters:
56-
- gocritic
57-
text: "newDeref: replace `\\*new"
67+
- errcheck # Error checking omitted for brevity.
68+
path: "example_"
5869

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#GOLANGCI_LINT_VERSION := "v1.46.2" # Optional configuration to pinpoint golangci-lint version.
1+
#GOLANGCI_LINT_VERSION := "v1.49.0" # Optional configuration to pinpoint golangci-lint version.
22

33
# The head of Makefile determines location of dev-go to include standard targets.
44
GO ?= go

generic_go1.18.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type IOInteractorOf[i, o any] struct {
1818
InteractFunc func(ctx context.Context, input i, output *o) error
1919
}
2020

21+
// Invoke calls interact function in a type-safe way.
2122
func (ioi IOInteractorOf[i, o]) Invoke(ctx context.Context, input i, output *o) error {
2223
return ioi.InteractFunc(ctx, input, output)
2324
}
@@ -26,7 +27,7 @@ func (ioi IOInteractorOf[i, o]) Invoke(ctx context.Context, input i, output *o)
2627
//
2728
// It pre-fills name and title with caller function.
2829
// Input is passed by value, while output is passed by pointer to be mutable.
29-
func NewInteractor[i, o any](interact func(ctx context.Context, input i, output *o) error) IOInteractorOf[i, o] {
30+
func NewInteractor[i, o any](interact func(ctx context.Context, input i, output *o) error, options ...func(i *IOInteractor)) IOInteractorOf[i, o] {
3031
u := IOInteractorOf[i, o]{}
3132
u.Input = *new(i)
3233
u.Output = new(o)
@@ -48,5 +49,9 @@ func NewInteractor[i, o any](interact func(ctx context.Context, input i, output
4849
u.name, u.title = callerFunc()
4950
u.name = filterName(u.name)
5051

52+
for _, o := range options {
53+
o(&u.IOInteractor)
54+
}
55+
5156
return u
5257
}

generic_go1.18_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func TestNewInteractor(t *testing.T) {
3838
*output = strconv.Itoa(input)
3939

4040
return nil
41+
}, func(i *usecase.IOInteractor) {
42+
i.SetTags("foo")
4143
})
4244

4345
u.SetDescription("Foo.")
@@ -54,4 +56,6 @@ func TestNewInteractor(t *testing.T) {
5456
assert.Equal(t, "123", out)
5557

5658
assert.Equal(t, "invalid type", usecase.ErrInvalidType.Error())
59+
60+
assert.Equal(t, []string{"foo"}, u.Tags())
5761
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ module github.com/swaggest/usecase
33
go 1.18
44

55
require (
6-
github.com/bool64/dev v0.2.12
7-
github.com/stretchr/testify v1.7.1
6+
github.com/bool64/dev v0.2.20
7+
github.com/stretchr/testify v1.8.0
88
)
99

1010
require (
1111
github.com/davecgh/go-spew v1.1.1 // indirect
1212
github.com/pmezard/go-difflib v1.0.0 // indirect
13-
gopkg.in/yaml.v3 v3.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
1414
)

go.sum

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
github.com/bool64/dev v0.2.12 h1:XXXE1Eh2//Dmp1hr+SqjxIXOEjq6D3Y5SwbGowpIK4I=
2-
github.com/bool64/dev v0.2.12/go.mod h1:/csLrm+4oDSsKJRIVS0mrywAonLnYKFG8RvGT7Jh9b8=
1+
github.com/bool64/dev v0.2.20 h1:9eIRGdcg2kQW2NGza++QbOKidNNaK+KfWuUXcZFDejE=
2+
github.com/bool64/dev v0.2.20/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
77
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
88
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9-
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
9+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
1010
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
12+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1113
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1214
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1315
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14-
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
15-
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)