建议对外开放Context的定义 #42

Open
opened 2017-06-12 08:41:18 +00:00 by lunny · 3 comments
Owner

Author: @crazystory

想在Context里注入一些自定义的东西.但是目前无法实现.
是否考虑下Context采用interface的方式,默认返回内置的Context

Author: @crazystory 想在Context里注入一些自定义的东西.但是目前无法实现. 是否考虑下Context采用interface的方式,默认返回内置的Context
Author
Owner

Author: @qjebbs

先定义一个自定义的Ctx:

type Ctx struct {
	*tango.Context
	User       *User //自定义的字段
}
//SetContext is the implement for tango interface
func (c *Ctx) SetContext(ctx *tango.Context) {
	c.Context = ctx
	c.User = ... //设置User
}

然后使用这个Ctx替换 tango.Ctx就可以了:

type Action struct {
	Ctx
}
func (a *Action) Get() interface{} {
	return a.User.Name
}
Author: @qjebbs 先定义一个自定义的Ctx: ```Go type Ctx struct { *tango.Context User *User //自定义的字段 } //SetContext is the implement for tango interface func (c *Ctx) SetContext(ctx *tango.Context) { c.Context = ctx c.User = ... //设置User } ``` 然后使用这个Ctx替换 `tango.Ctx`就可以了: ```go type Action struct { Ctx } func (a *Action) Get() interface{} { return a.User.Name } ```
Author
Owner

Author: @lunny

对啊,有Contexter中间件和接口啊

Author: @lunny 对啊,有Contexter中间件和接口啊
Author
Owner

Author: @crazystory

...这个方法不错,可以暂时解决我的问题.
但是这个方法必须使用struct.Get模式
无法支持func(*tango.Context)的模式

Author: @crazystory ...这个方法不错,可以暂时解决我的问题. 但是这个方法必须使用struct.Get模式 无法支持func(*tango.Context)的模式
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: lunny/tango#42
No description provided.