-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangyile
committed
Nov 28, 2024
1 parent
1dc752a
commit 90d3ef2
Showing
8 changed files
with
1,043 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
name: create-release | ||
|
||
on: | ||
push: | ||
branches: | ||
- main # 监听 main 分支的 push 操作(编译和测试/代码检查) | ||
tags: | ||
- 'v*' # 监听以 'v' 开头的标签的 push 操作(发布 Release) | ||
|
||
jobs: | ||
lint: | ||
name: lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.23.x" | ||
- uses: actions/checkout@v4 | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v6 | ||
with: | ||
version: latest | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
go: [ "1.22.x", "1.23.x" ] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
|
||
- name: Run test | ||
run: make test COVERAGE_DIR=/tmp/coverage | ||
|
||
- name: Send goveralls coverage | ||
uses: shogo82148/actions-goveralls@v1 | ||
with: | ||
path-to-profile: /tmp/coverage/combined.txt | ||
flag-name: Go-${{ matrix.go }} | ||
parallel: true | ||
|
||
check-coverage: | ||
name: Check coverage | ||
needs: [ test ] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: shogo82148/actions-goveralls@v1 | ||
with: | ||
parallel-finished: true | ||
|
||
# 发布 Release | ||
release: | ||
name: Release a new version | ||
needs: [ lint, test ] | ||
runs-on: ubuntu-latest | ||
# 仅在推送标签时执行 | ||
if: ${{ success() && startsWith(github.ref, 'refs/tags/v') }} | ||
steps: | ||
# 1. 检出代码 | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
# 2. 创建 Release 和上传源码包 | ||
- name: Create Release | ||
uses: softprops/action-gh-release@v2 | ||
with: | ||
generate_release_notes: true | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
COVERAGE_DIR ?= .coverage | ||
|
||
# cp from: https://github.com/yyle88/must/blob/fea23ee66c868247a4efff68510f5f2bf26e2546/Makefile#L4 | ||
test: | ||
@-rm -r $(COVERAGE_DIR) | ||
@mkdir $(COVERAGE_DIR) | ||
make test-with-flags TEST_FLAGS='-v -race -covermode atomic -coverprofile $$(COVERAGE_DIR)/combined.txt -bench=. -benchmem -timeout 20m' | ||
|
||
test-with-flags: | ||
@go test $(TEST_FLAGS) ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,121 @@ | ||
[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/yyle88/rese/release.yml?branch=main&label=BUILD)](https://github.com/yyle88/rese/actions/workflows/release.yml?query=branch%3Amain) | ||
[![GoDoc](https://pkg.go.dev/badge/github.com/yyle88/rese)](https://pkg.go.dev/github.com/yyle88/rese) | ||
[![Coverage Status](https://img.shields.io/coveralls/github/yyle88/rese/master.svg)](https://coveralls.io/github/yyle88/rese?branch=main) | ||
![Supported Go Versions](https://img.shields.io/badge/Go-1.22%2C%201.23-lightgrey.svg) | ||
[![GitHub Release](https://img.shields.io/github/release/yyle88/rese.svg)](https://github.com/yyle88/rese/releases) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/yyle88/rese)](https://goreportcard.com/report/github.com/yyle88/rese) | ||
|
||
# rese | ||
**rese** stands for **res** (result) + **err** (error). **rese** simplifies Go error handling and result extraction for multi-value function calls. It combines error and result checks into a single operation. | ||
|
||
**rese** simplifies Go error handling and result extraction for multi-value function calls. It combines error and result checks into a single operation. | ||
|
||
**rese** stands for **res** (result) + **err** (error). | ||
|
||
## CHINESE README | ||
|
||
[中文说明](README.zh.md) | ||
|
||
## Installation | ||
|
||
```bash | ||
go get github.com/yyle88/rese | ||
``` | ||
|
||
--- | ||
|
||
## Functions | ||
|
||
| Function | Purpose | Returns | | ||
|-----------------------------------------------------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------| | ||
| `V0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None | | ||
| `V1[T1 any](v1 T1, err error) T1` | Checks the error, and if no error, returns `v1`. | Returns the value of type `T1` | | ||
| `V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)` | Checks the error, and if no error, returns `v1` and `v2`. | Returns `v1` of type `T1` and `v2` of type `T2` | | ||
| `P0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None | | ||
| `P1[T1 any](v1 *T1, err error) *T1` | Checks the error, checks that `v1` is non-`nil`, and returns `v1`. | Returns a pointer to `v1` of type `T1` | | ||
| `P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)` | Checks the error, checks that `v1` and `v2` are non-`nil`, and returns `v1` and `v2`. | Returns pointers to `v1` and `v2` of types `T1` and `T2` | | ||
| `C0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None | | ||
| `C1[T1 comparable](v1 T1, err error) T1` | Checks the error, checks that `v1` is not a zero value, and returns `v1`. | Returns `v1` of type `T1` | | ||
| `C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)` | Checks the error, checks that `v1` and `v2` are not zero values, and returns `v1` and `v2`. | Returns `v1` of type `T1` and `v2` of type `T2` | | ||
|
||
## Examples | ||
|
||
### Example 1: Simple error and result checking with `V1` | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yyle88/rese" | ||
) | ||
|
||
func run() (string, error) { | ||
return "Hello, World!", nil | ||
} | ||
|
||
func main() { | ||
// Using V1 to check for error and get the result | ||
result := rese.V1(run()) | ||
fmt.Println(result) // Outputs: Hello, World! | ||
} | ||
``` | ||
|
||
### Example 2: Ensuring non-nil pointers with `P1` | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yyle88/rese" | ||
) | ||
|
||
func getSomething() (*int64, error) { | ||
v := int64(42) | ||
return &v, nil | ||
} | ||
|
||
func main() { | ||
// Using P1 to check error and ensure non-nil pointer | ||
ptr := rese.P1(getSomething()) | ||
fmt.Println(*ptr) // Outputs: 42 | ||
} | ||
``` | ||
|
||
### Example 3: Zero-value checking with `C1` | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yyle88/rese" | ||
) | ||
|
||
func getInt() (int, error) { | ||
return 20, nil | ||
} | ||
|
||
func main() { | ||
// Using C1 to check error and ensure non-zero result | ||
num := rese.C1(getInt()) | ||
fmt.Println("Received:", num) // Outputs: 20 | ||
} | ||
``` | ||
|
||
|
||
## License | ||
|
||
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. | ||
|
||
--- | ||
|
||
## Support | ||
|
||
Welcome to contribute to this project by submitting pull requests or reporting issues. | ||
|
||
If you find this package helpful, give it a star on GitHub! | ||
|
||
**Thank you for your support!** | ||
|
||
**Happy Coding with `rese`!** 🎉 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# rese | ||
|
||
**rese** 简化了 Go 中的错误处理和多值函数返回值提取。它将错误检查和结果提取合并为一个操作。 | ||
|
||
**rese** 代表 **res**(结果)+ **err**(错误)。 | ||
|
||
## English README | ||
|
||
[English documentation](README.md) | ||
|
||
## 安装 | ||
|
||
```bash | ||
go get github.com/yyle88/rese | ||
``` | ||
|
||
--- | ||
|
||
## 函数 | ||
|
||
| 函数 | 作用 | 返回值 | | ||
|-----------------------------------------------------------|--------------------------------------------------|-------------------------------------| | ||
| `V0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 | 无 | | ||
| `V1[T1 any](v1 T1, err error) T1` | 检查错误,如果没有错误,返回 `v1`。 | 返回类型为 `T1` 的 `v1` | | ||
| `V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)` | 检查错误,如果没有错误,返回 `v1` 和 `v2`。 | 返回类型为 `T1` 的 `v1` 和 类型为 `T2` 的 `v2` | | ||
| `P0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 | 无 | | ||
| `P1[T1 any](v1 *T1, err error) *T1` | 检查错误,检查 `v1` 是否为非 `nil`,并返回 `v1`。 | 返回类型为 `T1` 的 `v1` 的指针 | | ||
| `P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)` | 检查错误,检查 `v1` 和 `v2` 是否为非 `nil`,并返回 `v1` 和 `v2`。 | 返回类型为 `T1` 和 `T2` 的 `v1` 和 `v2` 的指针 | | ||
| `C0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 | 无 | | ||
| `C1[T1 comparable](v1 T1, err error) T1` | 检查错误,检查 `v1` 是否为零值,如果不是零值,返回 `v1`。 | 返回类型为 `T1` 的 `v1` | | ||
| `C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)` | 检查错误,检查 `v1` 和 `v2` 是否为零值,如果不是零值,返回 `v1` 和 `v2`。 | 返回类型为 `T1` 的 `v1` 和 类型为 `T2` 的 `v2` | | ||
|
||
## 示例 | ||
|
||
### 示例 1: 简单的错误和结果检查,使用 `V1` | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yyle88/rese" | ||
) | ||
|
||
func run() (string, error) { | ||
return "Hello, World!", nil | ||
} | ||
|
||
func main() { | ||
// 使用 V1 来检查错误并获取结果 | ||
result := rese.V1(run()) | ||
fmt.Println(result) // 输出: Hello, World! | ||
} | ||
``` | ||
|
||
### 示例 2: 确保指针非 `nil`,使用 `P1` | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yyle88/rese" | ||
) | ||
|
||
func getSomething() (*int64, error) { | ||
v := int64(42) | ||
return &v, nil | ||
} | ||
|
||
func main() { | ||
// 使用 P1 来检查错误并确保指针非 `nil` | ||
ptr := rese.P1(getSomething()) | ||
fmt.Println(*ptr) // 输出: 42 | ||
} | ||
``` | ||
|
||
### 示例 3: 检查零值,使用 `C1` | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yyle88/rese" | ||
) | ||
|
||
func getInt() (int, error) { | ||
return 20, nil | ||
} | ||
|
||
func main() { | ||
// 使用 C1 来检查错误并确保非零值 | ||
num := rese.C1(getInt()) | ||
fmt.Println("Received:", num) // 输出: 20 | ||
} | ||
``` | ||
|
||
## 许可协议 | ||
|
||
此项目采用 MIT 许可证,详情请参阅 [LICENSE](LICENSE) 文件。 | ||
|
||
--- | ||
|
||
## 贡献与支持 | ||
|
||
欢迎通过提交 pull request 或报告问题来贡献此项目。 | ||
|
||
如果你觉得这个包对你有帮助,请在 GitHub 上给个 ⭐,感谢支持!!! | ||
|
||
**感谢你的支持!** | ||
|
||
**祝编程愉快!** 🎉 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module github.com/yyle88/rese | ||
|
||
go 1.22.8 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.10.0 | ||
github.com/yyle88/done v1.0.18 | ||
github.com/yyle88/must v0.0.9 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||
github.com/yyle88/mutexmap v1.0.8 // indirect | ||
github.com/yyle88/zaplog v0.0.16 // indirect | ||
go.uber.org/multierr v1.11.0 // indirect | ||
go.uber.org/zap v1.27.0 // indirect | ||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= | ||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= | ||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= | ||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= | ||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= | ||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= | ||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
github.com/yyle88/done v1.0.18 h1:O71T+76laNmuY1kYP8PHkp6uceoN6ABTng/8c9KpZts= | ||
github.com/yyle88/done v1.0.18/go.mod h1:32UMgjuZP9LctfNNhBQqTyVbjggPOWpoXn8Mp0VrQbw= | ||
github.com/yyle88/must v0.0.9 h1:LA03i1O4/6Syopma8xqJMiWfAWeehoySeUaRmdQEvKY= | ||
github.com/yyle88/must v0.0.9/go.mod h1:5Ur4BKRx6GuW4gCZNx8Hf+iRS9lVmywSkCpxun/f+Do= | ||
github.com/yyle88/mutexmap v1.0.8 h1:VntAdXID5wbk211LZEPVK96jQBxIcfVIbQuk9cv3P/8= | ||
github.com/yyle88/mutexmap v1.0.8/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc= | ||
github.com/yyle88/zaplog v0.0.16 h1:ZCxQhq3+nWeWMAXIzeA1EA4exRq5Pn8pXTpEw1GjyD4= | ||
github.com/yyle88/zaplog v0.0.16/go.mod h1:0ct8Rh6uE5i9RG+xbH6d4/pyDBt9JmxBqHNCI+T4wiM= | ||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= | ||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= | ||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= | ||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= | ||
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= | ||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= | ||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= | ||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Oops, something went wrong.