Comments (6)
带参数的装饰器调用,目前有以下几种实现方式供大家讨论参考(正式上线该Feature时仅支持一种):
一、类似函数调用
//go:decor Logging("keyName", true)
二、类似函数调用,但需指定形参名字
//go:decor Logging(key = "keyName", autoBr = true)
三、类似初始化结构体
//go:decor Logging{key: "keyName", autoBr: true}
四、类似初始化结构体,但使用 =
代替 :
//go:decor Logging{key = "keyName", autoBr = true}
from go-decorator.
上面的方式类似函数调用或结构体的初始化,容易产生一些误解和疑惑,可以考虑额外加一个符号来解决这个问题。
比如:
//go:decor Logging#(key = "keyName", autoBr = true)
//go:decor Logging@(key = "keyName", autoBr = true)
//go:decor Logging#{key = "keyName", autoBr = true}
//go:decor Logging@{key = "keyName", autoBr = true}
增加一个 #
或者 @
符号来区分。
from go-decorator.
PR #4
from go-decorator.
正式版带参数的装饰器调用,实现方式已确定,语法为:
装饰器名
+ #
+ {}
。{}
为 key: value 键值对。
如:
//go:decor Logging#{key: "keyName", autoBr: true}
为更方便开发者约束装饰器的调用,增加了装饰器 lint
,它可以让开发者添加一些规则应用在装饰器上,当别处使用这些装饰器时,如果不满足约束条件,编译会出错终止。比如我们想让调用方调用装饰器时必传一些参数,可以使用 required
lint 约束;想让某个字段不能为零值,可以使用 nonzero
约束。
例如:
//go:decor-lint required: {name, age}
//go:decor-lint nonzero: {count}
func catch(ctx *decor.Context, name string, age int, count int) {
// code ...
}
下面的代码就在编译时报错,因为不能满足 catch
装饰器的 lint 约束条件( name 必传、count 不能为零值 ):
//go:decor catch#{age: 22}
func sth() {
// code...
}
from go-decorator.
特性已准备就绪,现在可以切到 feat/parameters
分支,通过 go install ./cmd/decorator/
来体验全新功能!
from go-decorator.
Relesaed v0.10.0
from go-decorator.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-decorator.