-
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
Dec 2, 2024
1 parent
6341621
commit 7b02ffc
Showing
60 changed files
with
982 additions
and
711 deletions.
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 yangyile-yyle88 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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/sortslice/blob/5f56c911501ffcef244e46d7c9f96b2ca60e5b16/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,103 +1,63 @@ | ||
# sure | ||
在我们开发golang代码时,经常会遇到需要判断 err 非空的情况,但这有点麻烦,因此我发明了这个包,能够在确定err不会触发时直接碾过错误。 | ||
|
||
比如在 | ||
``` | ||
res, err := a.Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
``` | ||
这个场景里,假如主逻辑不能运行,panic 有助于定位问题,让服务快速崩溃有助于外部检测,即时重启。 | ||
|
||
或者在 | ||
``` | ||
cfg, err := config.LoadFromFile(path) | ||
if err != nil { | ||
panic(err) | ||
} | ||
``` | ||
这个场景里,假如读取配置报错,则系统已无法靠自身逻辑恢复,直接 panic,以便于运维同事发现问题。 | ||
|
||
这时假如使用 | ||
``` | ||
res := a_must.Run() //假设 a 是个包,而通过 a 包能得到 a_must 包,在里面自带出错时 panic 的函数。 | ||
``` | ||
或者 | ||
``` | ||
res := a.Must().Run() //假设 a 是个对象,能通过 A 类得到 AMust 类,里面自带出错时 panic 的方法。 | ||
``` | ||
就能避免频繁的判断 if err != nil 让程序变得更丝滑。 | ||
|
||
这种丝滑是指可以让代码维持链式调用。 | ||
|
||
比如原本的: | ||
``` | ||
res, err := opt.GetR() | ||
if err != nil { | ||
panic(err) | ||
} | ||
abc, err := res.GetA() | ||
if err != nil { | ||
panic(err) | ||
} | ||
xyz, err := abc.GetX() | ||
if err != nil { | ||
panic(err) | ||
} | ||
``` | ||
就可以写成这样的语句: | ||
``` | ||
xyz := opt.Must().GetR().Must().GetA().Must().GetX() | ||
``` | ||
这就比每次调用完判断是否有 error 简单些,在略微非正式的情况下是无妨的。 | ||
|
||
这个包的目的就是提供这样的便利。 | ||
|
||
# 提供类和包两种情况下的 sure 操作 | ||
有的方法是某个类的成员方法,比如 `param.Check()`,当参数不正确时报错,当联调结束以后参数基本都是对的,即使出错 panic 也没问题。 | ||
|
||
而有的函数是某个包的小函数,比如 `json.Marshal` 函数,它就几乎不会出错(除非传个接口给它),经常需要判断err是否非空,其实没必要。 | ||
|
||
因此对于类和包,两种情况我做了两个生成器。 | ||
|
||
## 类操作代码生成器 | ||
假设我们封装了个类 A 它有: | ||
``` | ||
GetConfig(path string) (Config, error) | ||
``` | ||
就简单地封装这个操作为这样: | ||
``` | ||
cfg := a.Must().GetConfig(cfgPath) | ||
``` | ||
这样岂不是非常方便,这就是“类操作生成器”的基本逻辑,就是把类中所有导出方法都在遇到 err 时 panic,在调用时就能省去判断逻辑。 | ||
|
||
详情见 demos: | ||
|
||
[Demo1](/internal/examples/example1) | ||
|
||
[Demo4](/internal/examples/example4) | ||
|
||
[Demo5](/internal/examples/example5) | ||
|
||
## 包操作代码生成器 | ||
假如封装的函数在 `utils` 包里,常规的调用是这样的: | ||
``` | ||
cfg, err := utils.GetConfig(cfgPath) | ||
``` | ||
经过代码生成以后会得到 `utils_must` 新包,调用就被简化为这样: | ||
``` | ||
cfg := utils_must.GetConfig(cfgPath) | ||
``` | ||
|
||
详情见 demos: | ||
|
||
[Demo2](/internal/examples/example2) | ||
|
||
[Demo3](/internal/examples/example3) | ||
|
||
## 思路 | ||
[创作背景](/internal/docs/CREATION_IDEAS.md) | ||
|
||
## 最终: | ||
[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/yyle88/sure/release.yml?branch=main&label=BUILD)](https://github.com/yyle88/sure/actions/workflows/release.yml?query=branch%3Amain) | ||
[![GoDoc](https://pkg.go.dev/badge/github.com/yyle88/sure)](https://pkg.go.dev/github.com/yyle88/sure) | ||
[![Coverage Status](https://img.shields.io/coveralls/github/yyle88/sure/master.svg)](https://coveralls.io/github/yyle88/sure?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/sure.svg)](https://github.com/yyle88/sure/releases) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/yyle88/sure)](https://goreportcard.com/report/github.com/yyle88/sure) | ||
|
||
# sure: Add Assertions and Crash Handling to Existing Go Code | ||
|
||
`sure` enhances your existing Go code by adding assertions and crash handling. It automatically asserts conditions and crashes when errors occur, allowing you to improve error handling in legacy code without needing to manually add repetitive checks. | ||
|
||
## CHINESE README | ||
|
||
[中文说明](README.zh.md) | ||
|
||
## CREATION_IDEAS | ||
|
||
[CREATION_IDEAS](internal/docs/CREATION_IDEAS.en.md) | ||
|
||
## Packages Overview | ||
|
||
### `sure_cls_gen`: **Generates Go Classes with Assertions** | ||
|
||
Generates Go classes from predefined objects, embedding assertion logic to prevent common errors. | ||
|
||
### `sure_pkg_gen`: **Generates Go Packages with Error Handling** | ||
|
||
Extracts functions from existing code and generates Go packages, integrating assertion and crash handling. | ||
|
||
### `cls_stub_gen`: **Generates Go Method Stubs with Assertions** | ||
|
||
Generates method stubs for Go objects, embedding assertions for proper error handling. | ||
|
||
## Usage | ||
|
||
### Examples: | ||
|
||
- [sure_cls_gen](internal/examples/example_sure_cls_gen) | ||
- [sure_pkg_gen](internal/examples/example_sure_pkg_gen) | ||
- [cls_stub_gen](internal/examples/example_cls_stub_gen) | ||
|
||
--- | ||
|
||
## License | ||
|
||
`sure` is open-source and released under the MIT License. See the LICENSE file for more information. | ||
|
||
--- | ||
|
||
## 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 `sure`!** 🎉 | ||
|
||
Give me stars. Thank you!!! | ||
|
||
## See stars | ||
[![see stars](https://starchart.cc/yyle88/sure.svg?variant=adaptive)](https://starchart.cc/yyle88/sure) |
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,51 @@ | ||
# sure: 给现有 Go 代码添加断言和崩溃处理 | ||
|
||
`sure` 通过为现有的 Go 代码添加断言和崩溃处理功能来增强代码。它自动断言条件并在发生错误时崩溃,从而帮助你在不需要手动添加重复检查的情况下改善遗留代码的错误处理。 | ||
|
||
## 英文文档 | ||
|
||
[English README](README.md) | ||
|
||
## 创作背景 | ||
|
||
[CREATION_IDEAS](internal/docs/CREATION_IDEAS.zh.md) | ||
|
||
## 模块概述 | ||
|
||
### `sure_cls_gen`: **生成带有断言的 Go 类** | ||
|
||
从预定义的对象生成 Go 类,再嵌入断言逻辑以防止常见错误。 | ||
|
||
### `sure_pkg_gen`: **生成带有错误处理的 Go 包** | ||
|
||
从现有代码中提取函数并生成 Go 包,同时集成断言和崩溃处理。 | ||
|
||
### `cls_stub_gen`: **生成带有断言的 Go 方法存根** | ||
|
||
给 Go 对象生成方法存根,再嵌入断言以确保适当的错误处理。 | ||
|
||
## 使用示例 | ||
|
||
### 示例: | ||
|
||
- [sure_cls_gen](internal/examples/example_sure_cls_gen) | ||
- [sure_pkg_gen](internal/examples/example_sure_pkg_gen) | ||
- [cls_stub_gen](internal/examples/example_cls_stub_gen) | ||
|
||
--- | ||
|
||
## 许可 | ||
|
||
`sure` 是一个开源项目,发布于 MIT 许可证下。有关更多信息,请参阅 LICENSE 文件。 | ||
|
||
## 贡献与支持 | ||
|
||
欢迎通过提交 pull request 或报告问题来贡献此项目。 | ||
|
||
如果你觉得这个包对你有帮助,请在 GitHub 上给个 ⭐,感谢支持!!! | ||
|
||
**感谢你的支持!** | ||
|
||
**祝编程愉快!** 🎉 | ||
|
||
Give me stars. Thank you!!! |
Oops, something went wrong.