diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..7af6886 --- /dev/null +++ b/.github/workflows/release.yml @@ -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 }} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3b3f66e --- /dev/null +++ b/Makefile @@ -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) ./... diff --git a/README.md b/README.md index e877185..5155f91 100644 --- a/README.md +++ b/README.md @@ -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`!** 🎉 diff --git a/README.zh.md b/README.zh.md new file mode 100644 index 0000000..4906fcf --- /dev/null +++ b/README.zh.md @@ -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 上给个 ⭐,感谢支持!!! + +**感谢你的支持!** + +**祝编程愉快!** 🎉 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8a8ecd6 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1e7b3d1 --- /dev/null +++ b/go.sum @@ -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= diff --git a/rese.go b/rese.go new file mode 100644 index 0000000..27375c4 --- /dev/null +++ b/rese.go @@ -0,0 +1,243 @@ +package rese + +import ( + "github.com/yyle88/done" + "github.com/yyle88/must" +) + +func V0(err error) { + done.Done(err) +} + +func V1[T1 any](v1 T1, err error) T1 { + done.Done(err) + return v1 +} + +func V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2) { + done.Done(err) + return v1, v2 +} + +func V3[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) { + done.Done(err) + return v1, v2, v3 +} + +func V4[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4) { + done.Done(err) + return v1, v2, v3, v4 +} + +func V5[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5) { + done.Done(err) + return v1, v2, v3, v4, v5 +} + +func V6[T1, T2, T3, T4, T5, T6 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, err error) (T1, T2, T3, T4, T5, T6) { + done.Done(err) + return v1, v2, v3, v4, v5, v6 +} + +func V7[T1, T2, T3, T4, T5, T6, T7 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, err error) (T1, T2, T3, T4, T5, T6, T7) { + done.Done(err) + return v1, v2, v3, v4, v5, v6, v7 +} + +func V8[T1, T2, T3, T4, T5, T6, T7, T8 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, err error) (T1, T2, T3, T4, T5, T6, T7, T8) { + done.Done(err) + return v1, v2, v3, v4, v5, v6, v7, v8 +} + +func V9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, v9 T9, err error) (T1, T2, T3, T4, T5, T6, T7, T8, T9) { + done.Done(err) + return v1, v2, v3, v4, v5, v6, v7, v8, v9 +} + +func P0(err error) { + done.Done(err) +} + +func P1[T1 any](v1 *T1, err error) *T1 { + done.Done(err) + must.Full(v1) + return v1 +} + +func P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2) { + done.Done(err) + must.Full(v1) + must.Full(v2) + return v1, v2 +} + +func P3[T1, T2, T3 any](v1 *T1, v2 *T2, v3 *T3, err error) (*T1, *T2, *T3) { + done.Done(err) + must.Full(v1) + must.Full(v2) + must.Full(v3) + return v1, v2, v3 +} + +func P4[T1, T2, T3, T4 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, err error) (*T1, *T2, *T3, *T4) { + done.Done(err) + must.Full(v1) + must.Full(v2) + must.Full(v3) + must.Full(v4) + return v1, v2, v3, v4 +} + +func P5[T1, T2, T3, T4, T5 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, err error) (*T1, *T2, *T3, *T4, *T5) { + done.Done(err) + must.Full(v1) + must.Full(v2) + must.Full(v3) + must.Full(v4) + must.Full(v5) + return v1, v2, v3, v4, v5 +} + +func P6[T1, T2, T3, T4, T5, T6 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, v6 *T6, err error) (*T1, *T2, *T3, *T4, *T5, *T6) { + done.Done(err) + must.Full(v1) + must.Full(v2) + must.Full(v3) + must.Full(v4) + must.Full(v5) + must.Full(v6) + return v1, v2, v3, v4, v5, v6 +} + +func P7[T1, T2, T3, T4, T5, T6, T7 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, v6 *T6, v7 *T7, err error) (*T1, *T2, *T3, *T4, *T5, *T6, *T7) { + done.Done(err) + must.Full(v1) + must.Full(v2) + must.Full(v3) + must.Full(v4) + must.Full(v5) + must.Full(v6) + must.Full(v7) + return v1, v2, v3, v4, v5, v6, v7 +} + +func P8[T1, T2, T3, T4, T5, T6, T7, T8 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, v6 *T6, v7 *T7, v8 *T8, err error) (*T1, *T2, *T3, *T4, *T5, *T6, *T7, *T8) { + done.Done(err) + must.Full(v1) + must.Full(v2) + must.Full(v3) + must.Full(v4) + must.Full(v5) + must.Full(v6) + must.Full(v7) + must.Full(v8) + return v1, v2, v3, v4, v5, v6, v7, v8 +} + +func P9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, v6 *T6, v7 *T7, v8 *T8, v9 *T9, err error) (*T1, *T2, *T3, *T4, *T5, *T6, *T7, *T8, *T9) { + done.Done(err) + must.Full(v1) + must.Full(v2) + must.Full(v3) + must.Full(v4) + must.Full(v5) + must.Full(v6) + must.Full(v7) + must.Full(v8) + must.Full(v9) + return v1, v2, v3, v4, v5, v6, v7, v8, v9 +} + +func C0(err error) { + done.Done(err) +} + +func C1[T1 comparable](v1 T1, err error) T1 { + done.Done(err) + must.Nice(v1) + return v1 +} + +func C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2) { + done.Done(err) + must.Nice(v1) + must.Nice(v2) + return v1, v2 +} + +func C3[T1, T2, T3 comparable](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) { + done.Done(err) + must.Nice(v1) + must.Nice(v2) + must.Nice(v3) + return v1, v2, v3 +} + +func C4[T1, T2, T3, T4 comparable](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4) { + done.Done(err) + must.Nice(v1) + must.Nice(v2) + must.Nice(v3) + must.Nice(v4) + return v1, v2, v3, v4 +} + +func C5[T1, T2, T3, T4, T5 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5) { + done.Done(err) + must.Nice(v1) + must.Nice(v2) + must.Nice(v3) + must.Nice(v4) + must.Nice(v5) + return v1, v2, v3, v4, v5 +} + +func C6[T1, T2, T3, T4, T5, T6 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, err error) (T1, T2, T3, T4, T5, T6) { + done.Done(err) + must.Nice(v1) + must.Nice(v2) + must.Nice(v3) + must.Nice(v4) + must.Nice(v5) + must.Nice(v6) + return v1, v2, v3, v4, v5, v6 +} + +func C7[T1, T2, T3, T4, T5, T6, T7 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, err error) (T1, T2, T3, T4, T5, T6, T7) { + done.Done(err) + must.Nice(v1) + must.Nice(v2) + must.Nice(v3) + must.Nice(v4) + must.Nice(v5) + must.Nice(v6) + must.Nice(v7) + return v1, v2, v3, v4, v5, v6, v7 +} + +func C8[T1, T2, T3, T4, T5, T6, T7, T8 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, err error) (T1, T2, T3, T4, T5, T6, T7, T8) { + done.Done(err) + must.Nice(v1) + must.Nice(v2) + must.Nice(v3) + must.Nice(v4) + must.Nice(v5) + must.Nice(v6) + must.Nice(v7) + must.Nice(v8) + return v1, v2, v3, v4, v5, v6, v7, v8 +} + +func C9[T1, T2, T3, T4, T5, T6, T7, T8, T9 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, v9 T9, err error) (T1, T2, T3, T4, T5, T6, T7, T8, T9) { + done.Done(err) + must.Nice(v1) + must.Nice(v2) + must.Nice(v3) + must.Nice(v4) + must.Nice(v5) + must.Nice(v6) + must.Nice(v7) + must.Nice(v8) + must.Nice(v9) + return v1, v2, v3, v4, v5, v6, v7, v8, v9 +} diff --git a/rese_test.go b/rese_test.go new file mode 100644 index 0000000..501ae39 --- /dev/null +++ b/rese_test.go @@ -0,0 +1,428 @@ +package rese_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/yyle88/rese" +) + +func TestV0(t *testing.T) { + run := func() error { + return nil + } + + rese.V0(run()) +} + +func TestV1(t *testing.T) { + run := func() (string, error) { + return "a", nil + } + + v1 := rese.V1(run()) + require.Equal(t, "a", v1) +} + +func TestV2(t *testing.T) { + run := func() (string, uint64, error) { + return "a", 2, nil + } + + v1, v2 := rese.V2(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) +} + +func TestV3(t *testing.T) { + run := func() (string, uint64, rune, error) { + return "a", 2, 'x', nil + } + + v1, v2, v3 := rese.V3(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) +} + +func TestV4(t *testing.T) { + run := func() (string, uint64, rune, int32, error) { + return "a", 2, 'x', 42, nil + } + + v1, v2, v3, v4 := rese.V4(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) +} + +func TestV5(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, error) { + return "a", 2, 'x', 42, 3.14, nil + } + + v1, v2, v3, v4, v5 := rese.V5(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) +} + +func TestV6(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, bool, error) { + return "a", 2, 'x', 42, 3.14, true, nil + } + + v1, v2, v3, v4, v5, v6 := rese.V6(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) + require.Equal(t, true, v6) +} + +func TestV7(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, bool, byte, error) { + return "a", 2, 'x', 42, 3.14, true, byte(255), nil + } + + v1, v2, v3, v4, v5, v6, v7 := rese.V7(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) + require.Equal(t, true, v6) + require.Equal(t, byte(255), v7) +} + +func TestV8(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, bool, byte, int64, error) { + return "a", 2, 'x', 42, 3.14, true, byte(255), int64(123456789), nil + } + + v1, v2, v3, v4, v5, v6, v7, v8 := rese.V8(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) + require.Equal(t, true, v6) + require.Equal(t, byte(255), v7) + require.Equal(t, int64(123456789), v8) +} + +func TestV9(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, bool, byte, int64, uint32, error) { + return "a", 2, 'x', 42, 3.14, true, byte(255), int64(123456789), uint32(987654321), nil + } + + v1, v2, v3, v4, v5, v6, v7, v8, v9 := rese.V9(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) + require.Equal(t, true, v6) + require.Equal(t, byte(255), v7) + require.Equal(t, int64(123456789), v8) + require.Equal(t, uint32(987654321), v9) +} + +func TestP0(t *testing.T) { + run := func() error { + return nil + } + + rese.P0(run()) +} + +func TestP1(t *testing.T) { + run := func() (*string, error) { + v1 := "hello" + return &v1, nil + } + + p1 := rese.P1(run()) + require.Equal(t, "hello", *p1) +} + +func TestP2(t *testing.T) { + run := func() (*int, *float64, error) { + v1 := 42 + v2 := 3.14 + return &v1, &v2, nil + } + + v1, v2 := rese.P2(run()) + require.Equal(t, 42, *v1) + require.Equal(t, 3.14, *v2) +} + +func TestP3(t *testing.T) { + run := func() (*string, *int, *bool, error) { + v1 := "test" + v2 := 100 + v3 := true + return &v1, &v2, &v3, nil + } + + v1, v2, v3 := rese.P3(run()) + require.Equal(t, "test", *v1) + require.Equal(t, 100, *v2) + require.Equal(t, true, *v3) +} + +func TestP4(t *testing.T) { + run := func() (*int, *float64, *bool, *string, error) { + v1 := 42 + v2 := 3.14 + v3 := false + v4 := "world" + return &v1, &v2, &v3, &v4, nil + } + + v1, v2, v3, v4 := rese.P4(run()) + require.Equal(t, 42, *v1) + require.Equal(t, 3.14, *v2) + require.Equal(t, false, *v3) + require.Equal(t, "world", *v4) +} + +func TestP5(t *testing.T) { + run := func() (*string, *int, *bool, *float64, *rune, error) { + v1 := "test" + v2 := 123 + v3 := true + v4 := 2.718 + v5 := 'a' + return &v1, &v2, &v3, &v4, &v5, nil + } + + v1, v2, v3, v4, v5 := rese.P5(run()) + require.Equal(t, "test", *v1) + require.Equal(t, 123, *v2) + require.Equal(t, true, *v3) + require.Equal(t, 2.718, *v4) + require.Equal(t, 'a', *v5) +} + +func TestP6(t *testing.T) { + run := func() (*float64, *int, *string, *bool, *rune, *int64, error) { + v1 := 3.14 + v2 := 42 + v3 := "hello" + v4 := true + v5 := 'x' + v6 := int64(100000) + return &v1, &v2, &v3, &v4, &v5, &v6, nil + } + + v1, v2, v3, v4, v5, v6 := rese.P6(run()) + require.Equal(t, 3.14, *v1) + require.Equal(t, 42, *v2) + require.Equal(t, "hello", *v3) + require.Equal(t, true, *v4) + require.Equal(t, 'x', *v5) + require.Equal(t, int64(100000), *v6) +} + +func TestP7(t *testing.T) { + run := func() (*bool, *string, *int, *float64, *rune, *int64, *uint32, error) { + v1 := true + v2 := "world" + v3 := 50 + v4 := 1.618 + v5 := 'y' + v6 := int64(200000) + v7 := uint32(300) + return &v1, &v2, &v3, &v4, &v5, &v6, &v7, nil + } + + v1, v2, v3, v4, v5, v6, v7 := rese.P7(run()) + require.Equal(t, true, *v1) + require.Equal(t, "world", *v2) + require.Equal(t, 50, *v3) + require.Equal(t, 1.618, *v4) + require.Equal(t, 'y', *v5) + require.Equal(t, int64(200000), *v6) + require.Equal(t, uint32(300), *v7) +} + +func TestP8(t *testing.T) { + run := func() (*string, *int, *bool, *float64, *rune, *int64, *uint32, *uint8, error) { + v1 := "abc" + v2 := 999 + v3 := false + v4 := 0.577 + v5 := 'z' + v6 := int64(500000) + v7 := uint32(400) + v8 := uint8(255) + return &v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, nil + } + + v1, v2, v3, v4, v5, v6, v7, v8 := rese.P8(run()) + require.Equal(t, "abc", *v1) + require.Equal(t, 999, *v2) + require.Equal(t, false, *v3) + require.Equal(t, 0.577, *v4) + require.Equal(t, 'z', *v5) + require.Equal(t, int64(500000), *v6) + require.Equal(t, uint32(400), *v7) + require.Equal(t, uint8(255), *v8) +} + +func TestP9(t *testing.T) { + run := func() (*string, *int, *bool, *float64, *rune, *int64, *uint32, *uint8, *uint64, error) { + v1 := "final" + v2 := 888 + v3 := true + v4 := 2.718 + v5 := 'p' + v6 := int64(900000) + v7 := uint32(500) + v8 := uint8(100) + v9 := uint64(1000000) + return &v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9, nil + } + + v1, v2, v3, v4, v5, v6, v7, v8, v9 := rese.P9(run()) + require.Equal(t, "final", *v1) + require.Equal(t, 888, *v2) + require.Equal(t, true, *v3) + require.Equal(t, 2.718, *v4) + require.Equal(t, 'p', *v5) + require.Equal(t, int64(900000), *v6) + require.Equal(t, uint32(500), *v7) + require.Equal(t, uint8(100), *v8) + require.Equal(t, uint64(1000000), *v9) +} + +func TestC0(t *testing.T) { + run := func() error { + return nil + } + + rese.C0(run()) +} + +func TestC1(t *testing.T) { + run := func() (string, error) { + return "hello", nil + } + + v1 := rese.C1(run()) + require.Equal(t, "hello", v1) +} + +func TestC2(t *testing.T) { + run := func() (int, float64, error) { + return 42, 3.14, nil + } + + v1, v2 := rese.C2(run()) + require.Equal(t, 42, v1) + require.Equal(t, 3.14, v2) +} + +func TestC3(t *testing.T) { + run := func() (bool, byte, rune, error) { + return true, 'A', '中', nil + } + + v1, v2, v3 := rese.C3(run()) + require.True(t, v1) + require.Equal(t, byte('A'), v2) + require.Equal(t, '中', v3) +} + +func TestC4(t *testing.T) { + run := func() (string, int, uint, float32, error) { + return "world", -7, 7, 1.23, nil + } + + v1, v2, v3, v4 := rese.C4(run()) + require.Equal(t, "world", v1) + require.Equal(t, -7, v2) + require.Equal(t, uint(7), v3) + require.Equal(t, float32(1.23), v4) +} + +func TestC5(t *testing.T) { + run := func() (uint8, uint16, uint32, uint64, string, error) { + return 8, 16, 32, 64, "test", nil + } + + v1, v2, v3, v4, v5 := rese.C5(run()) + require.Equal(t, uint8(8), v1) + require.Equal(t, uint16(16), v2) + require.Equal(t, uint32(32), v3) + require.Equal(t, uint64(64), v4) + require.Equal(t, "test", v5) +} + +func TestC6(t *testing.T) { + run := func() (float64, complex64, complex128, bool, string, int, error) { + return 6.28, complex(1, 2), complex(3, 4), true, "check", -1, nil + } + + v1, v2, v3, v4, v5, v6 := rese.C6(run()) + require.Equal(t, 6.28, v1) + require.Equal(t, complex64(complex(1, 2)), v2) + require.Equal(t, complex(3, 4), v3) + require.True(t, v4) + require.Equal(t, "check", v5) + require.Equal(t, -1, v6) +} + +func TestC7(t *testing.T) { + run := func() (uint, uint8, uint16, uint32, uint64, int, int8, error) { + return 1, 2, 3, 4, 5, 6, 7, nil + } + + v1, v2, v3, v4, v5, v6, v7 := rese.C7(run()) + require.Equal(t, uint(1), v1) + require.Equal(t, uint8(2), v2) + require.Equal(t, uint16(3), v3) + require.Equal(t, uint32(4), v4) + require.Equal(t, uint64(5), v5) + require.Equal(t, 6, v6) + require.Equal(t, int8(7), v7) +} + +func TestC8(t *testing.T) { + run := func() (string, rune, byte, bool, float32, float64, int32, int64, error) { + return "go", '中', 'B', true, 2.71, 1.618, -32, -64, nil + } + + v1, v2, v3, v4, v5, v6, v7, v8 := rese.C8(run()) + require.Equal(t, "go", v1) + require.Equal(t, '中', v2) + require.Equal(t, byte('B'), v3) + require.True(t, v4) + require.Equal(t, float32(2.71), v5) + require.Equal(t, 1.618, v6) + require.Equal(t, int32(-32), v7) + require.Equal(t, int64(-64), v8) +} + +func TestC9(t *testing.T) { + run := func() (string, uint8, int16, uint32, int64, float32, float64, complex64, complex128, error) { + return "done", 255, -16, 32, -64, 3.14, 2.71, complex(5, 6), complex(7, 8), nil + } + + v1, v2, v3, v4, v5, v6, v7, v8, v9 := rese.C9(run()) + require.Equal(t, "done", v1) + require.Equal(t, uint8(255), v2) + require.Equal(t, int16(-16), v3) + require.Equal(t, uint32(32), v4) + require.Equal(t, int64(-64), v5) + require.Equal(t, float32(3.14), v6) + require.Equal(t, 2.71, v7) + require.Equal(t, complex64(complex(5, 6)), v8) + require.Equal(t, complex(7, 8), v9) +}