From 54f62c6d18b3ec13e4af48a55ff1472ca1accd5b Mon Sep 17 00:00:00 2001 From: Sterling Hanenkamp Date: Thu, 13 Jun 2024 17:00:55 -0500 Subject: [PATCH 1/6] feat: adding deferred.Error() --- Changes.md | 4 ++++ README.md | 7 ++++++ deferred/error.go | 33 +++++++++++++++++++++++++++ deferred/error_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 deferred/error.go create mode 100644 deferred/error_test.go diff --git a/Changes.md b/Changes.md index cca7006..99e0af7 100644 --- a/Changes.md +++ b/Changes.md @@ -1,3 +1,7 @@ +WIP TBD + + * Adding deferred.Error for helping with deferred error handling. + v0.6.0 2023-08-12 * Adding slices.Insert diff --git a/README.md b/README.md index e403e6d..b931dd1 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,13 @@ There are a lot of common operations simply missing from the Golang builtin library. This makes up for that deficiency. Here's a summary of some of the provided tools: +## Deferred Handling + +Handling deferred functions is a bit of a pain. This provides a way to handle +certain deferred calls with a little less hassle: + +* `Error` (allows capturing both errors that may occur during defer) + ## FileSystem Operations The built-in `io/fs` package is fine for reading files, but is missing write interfaces. This provides them: diff --git a/deferred/error.go b/deferred/error.go new file mode 100644 index 0000000..dd590b5 --- /dev/null +++ b/deferred/error.go @@ -0,0 +1,33 @@ +package deferred + +import "errors" + +// Error is intended to be used in cases where you have a deferred function that +// returns an error and you want to capture that error, but you may have an error +// that has already occurred and need to capture that too: +// +// func ProcessFile(name string) (err error) { +// var r io.Reader +// r, err = os.Open(name) +// if err != nil { +// return +// } +// defer deferred.Error(&err, r.Close()) +// +// // process file +// return +// } +// +// Now, both errors can be captured without a lot gymnastics. +func Error(err *error, deferErr error) { + if deferErr == nil { + return + } + + if *err == nil { + *err = deferErr + return + } + + *err = errors.Join(*err, deferErr) +} diff --git a/deferred/error_test.go b/deferred/error_test.go new file mode 100644 index 0000000..fec0431 --- /dev/null +++ b/deferred/error_test.go @@ -0,0 +1,52 @@ +package deferred_test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/zostay/go-std/deferred" +) + +func TestError(t *testing.T) { + t.Parallel() + + errors := []error{ + fmt.Errorf("error 1"), + fmt.Errorf("error 2"), + } + + cases := []struct { + first, second error + name string + }{ + {nil, nil, "nil-nil"}, + {errors[0], nil, "error-nil"}, + {nil, errors[0], "nil-error"}, + {errors[0], errors[1], "error-error"}, + } + + for _, c := range cases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + + err := c.first + deferred.Error(&err, c.second) + + if c.first == nil && c.second == nil { + assert.NoError(t, err, c.name) + return + } + + assert.Error(t, err) + if c.first != nil { + assert.ErrorIs(t, err, c.first) + } + if c.second != nil { + assert.ErrorIs(t, err, c.second) + } + }) + } +} From 9fc927dcf26fbae7f187d07357e728f01282b07a Mon Sep 17 00:00:00 2001 From: Sterling Hanenkamp Date: Thu, 13 Jun 2024 18:05:50 -0500 Subject: [PATCH 2/6] fix: upgrade to Go 1.20 --- .github/workflows/prepare.yaml | 2 +- .github/workflows/release.yaml | 2 +- .github/workflows/test.yaml | 2 +- Changes.md | 1 + deferred/error.go | 2 +- go.mod | 2 +- 6 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/prepare.yaml b/.github/workflows/prepare.yaml index 539db34..c93dfee 100644 --- a/.github/workflows/prepare.yaml +++ b/.github/workflows/prepare.yaml @@ -12,7 +12,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: 1.19 + go-version: 1.20 - name: Release Verseion run: echo $RELEASE_VERSION=$(echo $GITHUB_REF_NAME | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+.*$') >> $GITHUB_ENV - name: Version Check diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index f8effff..9f895ff 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -15,7 +15,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: 1.19 + go-version: 1.20 - name: Release Verseion run: echo $RELEASE_VERSION=$(echo $GITHUB_REF_NAME | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+.*$') >> $GITHUB_ENV - name: Version Check diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 99a843c..32b9062 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: 1.19 + go-version: 1.20 - uses: golangci/golangci-lint-action@v6 with: version: v1.59.0 diff --git a/Changes.md b/Changes.md index 9be9531..545b651 100644 --- a/Changes.md +++ b/Changes.md @@ -1,5 +1,6 @@ ## WIP TBD + * :boom: Breaking Change :boom:: Now requires Go 1.20. * Adding deferred.Error for helping with deferred error handling. ## v0.6.0 2023-08-12 diff --git a/deferred/error.go b/deferred/error.go index dd590b5..be269b4 100644 --- a/deferred/error.go +++ b/deferred/error.go @@ -18,7 +18,7 @@ import "errors" // return // } // -// Now, both errors can be captured without a lot gymnastics. +// Now, both errors can be captured without a lot of gymnastics. func Error(err *error, deferErr error) { if deferErr == nil { return diff --git a/go.mod b/go.mod index 8c2c7a4..5e9d258 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/zostay/go-std -go 1.19 +go 1.20 require ( github.com/stretchr/testify v1.8.2 From d284f6c749beebdfdab316caa939d72d17f1b889 Mon Sep 17 00:00:00 2001 From: Sterling Hanenkamp Date: Thu, 13 Jun 2024 22:32:24 -0500 Subject: [PATCH 3/6] fix: linter --- .github/workflows/test.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 32b9062..01d0d63 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -11,6 +11,7 @@ jobs: - uses: actions/setup-go@v5 with: go-version: 1.20 + cache: false - uses: golangci/golangci-lint-action@v6 with: version: v1.59.0 From 796026c029abd92fee6098bc5524908b28df98fb Mon Sep 17 00:00:00 2001 From: Sterling Hanenkamp Date: Thu, 13 Jun 2024 22:53:04 -0500 Subject: [PATCH 4/6] fix: cicd --- .github/workflows/test.yaml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 01d0d63..8f75621 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -14,16 +14,15 @@ jobs: cache: false - uses: golangci/golangci-lint-action@v6 with: - version: v1.59.0 - - run: go test ./... - name: Run All Tests - - run: go test ./... -coverprofile=coverage.out - name: Run Tests with Coverage + version: v1.59 + - run: go test -v ./... + - run: | + coveredFiles=$(go list ./... | grep -v 'github.com/zostay/today/\(cmd\|tools/gen/verses\)') + go test $coveredFiles -coverprofile=coverage.out - run: go tool cover -func=coverage.out - name: Display Coverage - name: Coverage Quality Check env: - REQUIRED_COVERAGE: 90 + REQUIRED_COVERAGE: 95 run: | totalCoverage=$(go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+') echo "Total Coverage: $totalCoverage %" From 53ab997fd208ab06d7ca54b9b8cf5de99cb3fdc2 Mon Sep 17 00:00:00 2001 From: Sterling Hanenkamp Date: Thu, 13 Jun 2024 23:30:30 -0500 Subject: [PATCH 5/6] fix: linter --- .github/workflows/test.yaml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 8f75621..f6eae63 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -12,9 +12,6 @@ jobs: with: go-version: 1.20 cache: false - - uses: golangci/golangci-lint-action@v6 - with: - version: v1.59 - run: go test -v ./... - run: | coveredFiles=$(go list ./... | grep -v 'github.com/zostay/today/\(cmd\|tools/gen/verses\)') @@ -30,3 +27,10 @@ jobs: echo "Coverage is less than $REQUIRED_COVERAGE %" exit 1 fi + - uses: actions/setup-go@v5 + with: + go-version: 1.21 + cache: false + - uses: golangci/golangci-lint-action@v6 + with: + version: v1.59 From f8de641da759a15f728285b615bf7c03c9cb17b3 Mon Sep 17 00:00:00 2001 From: Sterling Hanenkamp Date: Thu, 13 Jun 2024 23:31:52 -0500 Subject: [PATCH 6/6] fix: linter --- .github/workflows/test.yaml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f6eae63..752fab0 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,8 +10,11 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: 1.20 + go-version: '1.20' cache: false + - uses: golangci/golangci-lint-action@v6 + with: + version: v1.59 - run: go test -v ./... - run: | coveredFiles=$(go list ./... | grep -v 'github.com/zostay/today/\(cmd\|tools/gen/verses\)') @@ -27,10 +30,3 @@ jobs: echo "Coverage is less than $REQUIRED_COVERAGE %" exit 1 fi - - uses: actions/setup-go@v5 - with: - go-version: 1.21 - cache: false - - uses: golangci/golangci-lint-action@v6 - with: - version: v1.59