Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
dengsgo committed Aug 27, 2023
1 parent 574473c commit d1ed6a8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
36 changes: 28 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
> Don`t apply it in production environment !!!
> Project is still under iterative development and is open for testing only.
`go-decorator` is a compilation chaining tool that implements the decorator feature of the Go language.
`go-decorator`, **go language middleware tool using annotation-injected decorators**.

Using this tool, you can use the decorator `decoratorfunctionName` by annotating the function with `//go:decoratorfunctionName`, which allows you to quickly inject sample code, change the behavior of the function, control the flow of logic, and more.
## Feature

The usage scenarios of decorators can be compared to other languages, such as Python and TypeScript.
- Annotating a function with `//go:decoratorfunctionName` allows you to use the decorator `decoratorfunctionName` for quick sample code injection, non-intrusive changes to function behavior, controlling the flow of logic, and more;
- Implement a function of type func(*decor.Context), such as a decorator, that can be used on any top-level function.
- Multiple (line) `//go:decor` decorators are allowed to decorate functions;
- Friendly error hints that can more accurately detect problems at compile time and provide the cause of the error and the line number (e.g. missing decorator or package reference, etc.).
- The target functions only operate at compile time without compromising the performance of the compiled program or causing reflection.
- A basic bootstrap document.

The usage scenarios of decorators can be compared to other languages like Python and TypeScript.

`go-decorator` is a decorator injection performed during compilation, so it does not damage the source files of your project, nor does it generate new `.go` files or other unnecessary files in the project. It is completely different from the `go:generate` generative expression.

Expand All @@ -37,9 +44,9 @@ Note: Please update frequently to install the latest version. Get the best exper

Simply add the `-toolexec decorator` parameter to the `go build` command.

If you usually use `go build`, now all you need to do is add the toolchain parameter to become `go build -toolexec decorator`. Everything else is the same as before, no changes need to be made!
If you usually use `go build`, now all you need to do is add the toolchain parameter to become `go build -toolexec decorator`. Everything remains the same, no changes need to be made!

The other subcommands of go are also used in the same way.
The other subcommands of go are also utilized in the same manner.

## Code

Expand Down Expand Up @@ -103,9 +110,13 @@ func logging(ctx *decor.Context) {

[example](example) This directory demonstrates how to write code correctly to use the `decorator` tool.

[**single**](example/single): This is a single file example where both the decorator definition and the decorated function are located within the same package. In this case, there is no need to consider the issue of importing dependent packages, just use the example code.
| Project | Notes |
|-----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [**single**](example/single) | This is a single file example where both the decorator definition and the decorated function are located within the same package. In this case, there is no need to consider the issue of importing dependent packages, just use the example code. |
| [**packages**](example/packages) | The example of this project is that the decorator definition and the decorated function are not in the same package, and anonymous package import is required. |
| [**datetime**](example/datetime) | The complete code used in the demonstration example in the Guide |
| [**emptyfunc**](example/emptyfunc) | The difference between calling and not calling `TargetDo()` in the demo decorator |

[**packages**](example/packages):The example of this project is that the decorator definition and the decorated function are not in the same package, and anonymous package import is required.

See more [Guide](#guide) .

Expand All @@ -120,4 +131,13 @@ If you find any problems, you can provide feedback here. [GitHub Issues](https:/

## Contribute

The project is still under development and due to frequent changes, external contributions are temporarily not accepted. Welcome to submit a Pull Request after stabilization
The project is still under development and due to frequent changes, external contributions are temporarily not accepted. Welcome to submit a Pull Request after stabilization.

## TODO

- More documents.
- IDE friendly tool support.
- better performance.
- More testing coverage.
- More clear error reminders.
- More...
27 changes: 22 additions & 5 deletions README.zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
[![Actions](https://github.com/dengsgo/go-decorator/workflows/Go/badge.svg)](https://github.com/dengsgo/go-decorator/actions) [![Go Report Card](https://goreportcard.com/badge/github.com/dengsgo/go-decorator)](https://goreportcard.com/report/github.com/dengsgo/go-decorator) [![godoc.org](https://godoc.org/github.com/dengsgo/go-decorator/decor?status.svg)](https://godoc.org/github.com/dengsgo/go-decorator/decor) [![Go doc](https://img.shields.io/badge/go.dev-reference-brightgreen?logo=go&logoColor=white&style=flat)](https://pkg.go.dev/github.com/dengsgo/go-decorator/decor) [![Goproxy.cn](https://goproxy.cn/stats/github.com/dengsgo/go-decorator/badges/download-count.svg)](https://goproxy.cn)


中文 | [English](README.md) (Translated from Chinese document)
中文 | [English](README.md)

> 请勿应用于生产环境!!!
> 项目仍在迭代开发,仅公开测试阶段
`go-decorator` 是实现 Go 语言装饰器特性的编译链工具。
`go-decorator`, **使用注释注入装饰器的 go 语言中间件工具**.

## Feature

- 使用 `//go:decor decoratorfunctionName` 注释函数即可使用装饰器 `decoratorfunctionName`,快速完成样板代码注入、非侵入式改变函数行为、控制逻辑流程等;
- 实现 func(*decor.Context) 类型的函数,即为一个装饰器,可以用在任意的一级函数上(top-level function);
- 允许使用多个(行)`//go:decor`装饰器来装饰函数;
- 友好的错误提示,可以较为正确的在编译时发现问题并给出错误原因和错误行号(比如装饰器未定义/包未引用等);
- 只在编译时增强目标函数,不会降低编译后程序的性能,更没有反射;
- 基础使用的引导文档;

使用该工具,通过 `//go:decor decoratorfunctionName` 来注释函数,即可使用装饰器`decoratorfunctionName`,快速完成样板代码注入、改变函数行为、控制逻辑流程等。

装饰器的使用场景,可以类比其他语言,比如 Python、TypeScript。

Expand Down Expand Up @@ -108,7 +116,7 @@ func logging(ctx *decor.Context) {
| [**single**](example/single) | 这个一个单文件示例,装饰器定义和被装饰的函数都位于一个包内。这种情况无需考虑导入依赖包的问题,按示例代码使用即可。 |
| [**packages**](example/packages) | 该项目示例为装饰器定义和被装饰的函数不在同一个包内,需要使用匿名包导入。 |
| [**datetime**](example/datetime) | Guide 里演示示例所用到的完整代码 |
| [**emptyfunc**](example/emptyfunc) | 演示装饰器中调用和不调用`targetDo()` 的区别 |
| [**emptyfunc**](example/emptyfunc) | 演示装饰器中调用和不调用`TargetDo()` 的区别 |


更多内容查看 [Guide](#guide) .
Expand All @@ -126,4 +134,13 @@ func logging(ctx *decor.Context) {

## Contribute

项目仍在开发中,由于变动频繁,暂时不接受外部贡献。欢迎稳定后再提交 Pull Request .
项目仍在开发中,由于变动频繁,暂时不接受外部贡献。欢迎稳定后再提交 Pull Request .

## TODO

- More documents.
- IDE friendly tool support.
- better performance.
- More testing coverage.
- More clear error reminders.
- More...

0 comments on commit d1ed6a8

Please sign in to comment.