Skip to content

Commit 90d3ef2

Browse files
author
yangyile
committed
把must和done里面的逻辑拷贝过来实现逻辑
1 parent 1dc752a commit 90d3ef2

File tree

8 files changed

+1043
-1
lines changed

8 files changed

+1043
-1
lines changed

.github/workflows/release.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: create-release
2+
3+
on:
4+
push:
5+
branches:
6+
- main # 监听 main 分支的 push 操作(编译和测试/代码检查)
7+
tags:
8+
- 'v*' # 监听以 'v' 开头的标签的 push 操作(发布 Release)
9+
10+
jobs:
11+
lint:
12+
name: lint
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/setup-go@v5
16+
with:
17+
go-version: "1.23.x"
18+
- uses: actions/checkout@v4
19+
- name: golangci-lint
20+
uses: golangci/golangci-lint-action@v6
21+
with:
22+
version: latest
23+
24+
test:
25+
runs-on: ubuntu-latest
26+
strategy:
27+
matrix:
28+
go: [ "1.22.x", "1.23.x" ]
29+
steps:
30+
- uses: actions/checkout@v4
31+
32+
- uses: actions/setup-go@v5
33+
with:
34+
go-version: ${{ matrix.go }}
35+
36+
- name: Run test
37+
run: make test COVERAGE_DIR=/tmp/coverage
38+
39+
- name: Send goveralls coverage
40+
uses: shogo82148/actions-goveralls@v1
41+
with:
42+
path-to-profile: /tmp/coverage/combined.txt
43+
flag-name: Go-${{ matrix.go }}
44+
parallel: true
45+
46+
check-coverage:
47+
name: Check coverage
48+
needs: [ test ]
49+
runs-on: ubuntu-latest
50+
steps:
51+
- uses: shogo82148/actions-goveralls@v1
52+
with:
53+
parallel-finished: true
54+
55+
# 发布 Release
56+
release:
57+
name: Release a new version
58+
needs: [ lint, test ]
59+
runs-on: ubuntu-latest
60+
# 仅在推送标签时执行
61+
if: ${{ success() && startsWith(github.ref, 'refs/tags/v') }}
62+
steps:
63+
# 1. 检出代码
64+
- name: Checkout code
65+
uses: actions/checkout@v4
66+
67+
# 2. 创建 Release 和上传源码包
68+
- name: Create Release
69+
uses: softprops/action-gh-release@v2
70+
with:
71+
generate_release_notes: true
72+
env:
73+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
COVERAGE_DIR ?= .coverage
2+
3+
# cp from: https://github.com/yyle88/must/blob/fea23ee66c868247a4efff68510f5f2bf26e2546/Makefile#L4
4+
test:
5+
@-rm -r $(COVERAGE_DIR)
6+
@mkdir $(COVERAGE_DIR)
7+
make test-with-flags TEST_FLAGS='-v -race -covermode atomic -coverprofile $$(COVERAGE_DIR)/combined.txt -bench=. -benchmem -timeout 20m'
8+
9+
test-with-flags:
10+
@go test $(TEST_FLAGS) ./...

README.md

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,121 @@
1+
[![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)
2+
[![GoDoc](https://pkg.go.dev/badge/github.com/yyle88/rese)](https://pkg.go.dev/github.com/yyle88/rese)
3+
[![Coverage Status](https://img.shields.io/coveralls/github/yyle88/rese/master.svg)](https://coveralls.io/github/yyle88/rese?branch=main)
4+
![Supported Go Versions](https://img.shields.io/badge/Go-1.22%2C%201.23-lightgrey.svg)
5+
[![GitHub Release](https://img.shields.io/github/release/yyle88/rese.svg)](https://github.com/yyle88/rese/releases)
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/yyle88/rese)](https://goreportcard.com/report/github.com/yyle88/rese)
7+
18
# rese
2-
**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.
9+
10+
**rese** simplifies Go error handling and result extraction for multi-value function calls. It combines error and result checks into a single operation.
11+
12+
**rese** stands for **res** (result) + **err** (error).
13+
14+
## CHINESE README
15+
16+
[中文说明](README.zh.md)
17+
18+
## Installation
19+
20+
```bash
21+
go get github.com/yyle88/rese
22+
```
23+
24+
---
25+
26+
## Functions
27+
28+
| Function | Purpose | Returns |
29+
|-----------------------------------------------------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------|
30+
| `V0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None |
31+
| `V1[T1 any](v1 T1, err error) T1` | Checks the error, and if no error, returns `v1`. | Returns the value of type `T1` |
32+
| `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` |
33+
| `P0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None |
34+
| `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` |
35+
| `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` |
36+
| `C0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None |
37+
| `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` |
38+
| `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` |
39+
40+
## Examples
41+
42+
### Example 1: Simple error and result checking with `V1`
43+
44+
```go
45+
package main
46+
47+
import (
48+
"fmt"
49+
"github.com/yyle88/rese"
50+
)
51+
52+
func run() (string, error) {
53+
return "Hello, World!", nil
54+
}
55+
56+
func main() {
57+
// Using V1 to check for error and get the result
58+
result := rese.V1(run())
59+
fmt.Println(result) // Outputs: Hello, World!
60+
}
61+
```
62+
63+
### Example 2: Ensuring non-nil pointers with `P1`
64+
65+
```go
66+
package main
67+
68+
import (
69+
"fmt"
70+
"github.com/yyle88/rese"
71+
)
72+
73+
func getSomething() (*int64, error) {
74+
v := int64(42)
75+
return &v, nil
76+
}
77+
78+
func main() {
79+
// Using P1 to check error and ensure non-nil pointer
80+
ptr := rese.P1(getSomething())
81+
fmt.Println(*ptr) // Outputs: 42
82+
}
83+
```
84+
85+
### Example 3: Zero-value checking with `C1`
86+
87+
```go
88+
package main
89+
90+
import (
91+
"fmt"
92+
"github.com/yyle88/rese"
93+
)
94+
95+
func getInt() (int, error) {
96+
return 20, nil
97+
}
98+
99+
func main() {
100+
// Using C1 to check error and ensure non-zero result
101+
num := rese.C1(getInt())
102+
fmt.Println("Received:", num) // Outputs: 20
103+
}
104+
```
105+
106+
107+
## License
108+
109+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
110+
111+
---
112+
113+
## Support
114+
115+
Welcome to contribute to this project by submitting pull requests or reporting issues.
116+
117+
If you find this package helpful, give it a star on GitHub!
118+
119+
**Thank you for your support!**
120+
121+
**Happy Coding with `rese`!** 🎉

README.zh.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# rese
2+
3+
**rese** 简化了 Go 中的错误处理和多值函数返回值提取。它将错误检查和结果提取合并为一个操作。
4+
5+
**rese** 代表 **res**(结果)+ **err**(错误)。
6+
7+
## English README
8+
9+
[English documentation](README.md)
10+
11+
## 安装
12+
13+
```bash
14+
go get github.com/yyle88/rese
15+
```
16+
17+
---
18+
19+
## 函数
20+
21+
| 函数 | 作用 | 返回值 |
22+
|-----------------------------------------------------------|--------------------------------------------------|-------------------------------------|
23+
| `V0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 ||
24+
| `V1[T1 any](v1 T1, err error) T1` | 检查错误,如果没有错误,返回 `v1`| 返回类型为 `T1``v1` |
25+
| `V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)` | 检查错误,如果没有错误,返回 `v1``v2`| 返回类型为 `T1``v1` 和 类型为 `T2``v2` |
26+
| `P0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 ||
27+
| `P1[T1 any](v1 *T1, err error) *T1` | 检查错误,检查 `v1` 是否为非 `nil`,并返回 `v1`| 返回类型为 `T1``v1` 的指针 |
28+
| `P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)` | 检查错误,检查 `v1``v2` 是否为非 `nil`,并返回 `v1``v2`| 返回类型为 `T1``T2``v1``v2` 的指针 |
29+
| `C0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 ||
30+
| `C1[T1 comparable](v1 T1, err error) T1` | 检查错误,检查 `v1` 是否为零值,如果不是零值,返回 `v1`| 返回类型为 `T1``v1` |
31+
| `C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)` | 检查错误,检查 `v1``v2` 是否为零值,如果不是零值,返回 `v1``v2`| 返回类型为 `T1``v1` 和 类型为 `T2``v2` |
32+
33+
## 示例
34+
35+
### 示例 1: 简单的错误和结果检查,使用 `V1`
36+
37+
```go
38+
package main
39+
40+
import (
41+
"fmt"
42+
"github.com/yyle88/rese"
43+
)
44+
45+
func run() (string, error) {
46+
return "Hello, World!", nil
47+
}
48+
49+
func main() {
50+
// 使用 V1 来检查错误并获取结果
51+
result := rese.V1(run())
52+
fmt.Println(result) // 输出: Hello, World!
53+
}
54+
```
55+
56+
### 示例 2: 确保指针非 `nil`,使用 `P1`
57+
58+
```go
59+
package main
60+
61+
import (
62+
"fmt"
63+
"github.com/yyle88/rese"
64+
)
65+
66+
func getSomething() (*int64, error) {
67+
v := int64(42)
68+
return &v, nil
69+
}
70+
71+
func main() {
72+
// 使用 P1 来检查错误并确保指针非 `nil`
73+
ptr := rese.P1(getSomething())
74+
fmt.Println(*ptr) // 输出: 42
75+
}
76+
```
77+
78+
### 示例 3: 检查零值,使用 `C1`
79+
80+
```go
81+
package main
82+
83+
import (
84+
"fmt"
85+
"github.com/yyle88/rese"
86+
)
87+
88+
func getInt() (int, error) {
89+
return 20, nil
90+
}
91+
92+
func main() {
93+
// 使用 C1 来检查错误并确保非零值
94+
num := rese.C1(getInt())
95+
fmt.Println("Received:", num) // 输出: 20
96+
}
97+
```
98+
99+
## 许可协议
100+
101+
此项目采用 MIT 许可证,详情请参阅 [LICENSE](LICENSE) 文件。
102+
103+
---
104+
105+
## 贡献与支持
106+
107+
欢迎通过提交 pull request 或报告问题来贡献此项目。
108+
109+
如果你觉得这个包对你有帮助,请在 GitHub 上给个 ⭐,感谢支持!!!
110+
111+
**感谢你的支持!**
112+
113+
**祝编程愉快!** 🎉

go.mod

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module github.com/yyle88/rese
2+
3+
go 1.22.8
4+
5+
require (
6+
github.com/stretchr/testify v1.10.0
7+
github.com/yyle88/done v1.0.18
8+
github.com/yyle88/must v0.0.9
9+
)
10+
11+
require (
12+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
13+
github.com/pkg/errors v0.9.1 // indirect
14+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
15+
github.com/yyle88/mutexmap v1.0.8 // indirect
16+
github.com/yyle88/zaplog v0.0.16 // indirect
17+
go.uber.org/multierr v1.11.0 // indirect
18+
go.uber.org/zap v1.27.0 // indirect
19+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
20+
gopkg.in/yaml.v3 v3.0.1 // indirect
21+
)

go.sum

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
2+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
4+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
5+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
6+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
7+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
8+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
9+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
10+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
12+
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
13+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
14+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
15+
github.com/yyle88/done v1.0.18 h1:O71T+76laNmuY1kYP8PHkp6uceoN6ABTng/8c9KpZts=
16+
github.com/yyle88/done v1.0.18/go.mod h1:32UMgjuZP9LctfNNhBQqTyVbjggPOWpoXn8Mp0VrQbw=
17+
github.com/yyle88/must v0.0.9 h1:LA03i1O4/6Syopma8xqJMiWfAWeehoySeUaRmdQEvKY=
18+
github.com/yyle88/must v0.0.9/go.mod h1:5Ur4BKRx6GuW4gCZNx8Hf+iRS9lVmywSkCpxun/f+Do=
19+
github.com/yyle88/mutexmap v1.0.8 h1:VntAdXID5wbk211LZEPVK96jQBxIcfVIbQuk9cv3P/8=
20+
github.com/yyle88/mutexmap v1.0.8/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc=
21+
github.com/yyle88/zaplog v0.0.16 h1:ZCxQhq3+nWeWMAXIzeA1EA4exRq5Pn8pXTpEw1GjyD4=
22+
github.com/yyle88/zaplog v0.0.16/go.mod h1:0ct8Rh6uE5i9RG+xbH6d4/pyDBt9JmxBqHNCI+T4wiM=
23+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
24+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
25+
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
26+
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
27+
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
28+
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
29+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
30+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
31+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
32+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
33+
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
34+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
35+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)