Skip to content
This repository was archived by the owner on Dec 9, 2022. It is now read-only.

Commit d333a02

Browse files
committed
feat: golam.Context add value getter, setter
1 parent 63a4dab commit d333a02

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

context.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const (
1616
type Context interface {
1717
Ctx() context.Context
1818

19+
Set(key interface{}, val interface{})
20+
21+
Get(key interface{}) interface{}
22+
1923
Request() *Req
2024

2125
Response() *Resp
@@ -88,6 +92,7 @@ type Context interface {
8892
var _ Context = (*contextImpl)(nil)
8993

9094
type contextImpl struct {
95+
ctx context.Context
9196
request *Req
9297
response *Resp
9398
path string
@@ -104,7 +109,18 @@ type contextImpl struct {
104109
}
105110

106111
func (c *contextImpl) Ctx() context.Context {
107-
return c.Request().Context()
112+
if c.ctx == nil {
113+
c.ctx = c.Request().Context()
114+
}
115+
return c.ctx
116+
}
117+
118+
func (c *contextImpl) Set(key interface{}, val interface{}) {
119+
c.ctx = context.WithValue(c.Ctx(), key, val)
120+
}
121+
122+
func (c *contextImpl) Get(key interface{}) interface{} {
123+
return c.Ctx().Value(key)
108124
}
109125

110126
func (c *contextImpl) writeContentType(value string) {

0 commit comments

Comments
 (0)