Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xyz327/sandwich
A Go Struct Decorator base on code generater
https://github.com/xyz327/sandwich
code-generation decorator golang
Last synced: 2 months ago
JSON representation
A Go Struct Decorator base on code generater
- Host: GitHub
- URL: https://github.com/xyz327/sandwich
- Owner: xyz327
- Created: 2024-06-24T12:48:24.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-06-26T07:10:49.000Z (7 months ago)
- Last Synced: 2024-06-27T03:55:55.270Z (7 months ago)
- Topics: code-generation, decorator, golang
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# sandwich
通过定义接口生成代码实现功能扩展(装饰器模式)
## Example 文件说明
- [example/origin.go](example/origin.go) 定义原本的对象
- [example/wrapper.go](example/wrapper.go) 定义包装的对象
- [example/wrapper_gen.go](example/wrapper_gen.go) 生成的代码
- [example/wrapper_test.go](example/wrapper_test.go) 测试代码## 说明
### 假设需求
现有一个 Origin 对象(参考[example/origin.go](example/origin.go)),需要对 Origin 对象的方法进行扩展
1. 对 key 参数增加前缀
2. 统计耗时### 实现
1. 定义包装代码([example/wrapper.go](example/wrapper.go))
2. `执行 go:generate` 生成代码([example/wrapper_gen.go](example/wrapper_gen.go))
3. 执行测试用例([example/wrapper_test.go](example/wrapper_test.go))
最后执行测试代码后会输出```
DoSomething1
start:2024-06-25 12:38:19.014668 +0800 CST m=+0.000469745
WrapperMethod # 包装方法输出
DoSomething1, key-> prefix:key # 改变后的入参,原来的方法输出
cost:0s # 包装方法输出,统计执行耗时DoSomething2
start:2024-06-25 12:38:19.014674 +0800 CST m=+0.000476388
WrapperMethod
DoSomething2, keys-> [prefix:key1 prefix:key2]
cost:0s
```