Skip to content

Commit 4e98fa9

Browse files
committed
Fixed #500
Signed-off-by: Vishal Rana <[email protected]>
1 parent d0ed583 commit 4e98fa9

File tree

3 files changed

+12
-44
lines changed

3 files changed

+12
-44
lines changed

context.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ type (
2525
Context interface {
2626
context.Context
2727

28-
// StdContext returns `net/context.Context`. By default it is set the background
29-
// context. To change the context, use SetStdContext().
30-
StdContext() context.Context
31-
32-
// SetStdContext sets `net/context.Context`.
33-
SetStdContext(context.Context)
28+
// SetContext sets `net/context.Context`.
29+
SetContext(context.Context)
3430

3531
// Request returns `engine.Request` interface.
3632
Request() engine.Request
@@ -104,12 +100,6 @@ type (
104100
// Set saves data in the context.
105101
Set(string, interface{})
106102

107-
// Del deletes data from the context.
108-
Del(string)
109-
110-
// Contains checks if the key exists in the context.
111-
Contains(string) bool
112-
113103
// Bind binds the request body into provided type `i`. The default binder
114104
// does it based on Content-Type header.
115105
Bind(interface{}) error
@@ -186,23 +176,16 @@ type (
186176
path string
187177
pnames []string
188178
pvalues []string
189-
store store
190179
handler HandlerFunc
191180
echo *Echo
192181
}
193-
194-
store map[string]interface{}
195182
)
196183

197184
const (
198185
indexPage = "index.html"
199186
)
200187

201-
func (c *echoContext) StdContext() context.Context {
202-
return c.Context
203-
}
204-
205-
func (c *echoContext) SetStdContext(ctx context.Context) {
188+
func (c *echoContext) SetContext(ctx context.Context) {
206189
c.Context = ctx
207190
}
208191

@@ -310,23 +293,11 @@ func (c *echoContext) Cookies() []engine.Cookie {
310293
}
311294

312295
func (c *echoContext) Set(key string, val interface{}) {
313-
if c.store == nil {
314-
c.store = make(store)
315-
}
316-
c.store[key] = val
296+
c.Context = context.WithValue(c, key, val)
317297
}
318298

319299
func (c *echoContext) Get(key string) interface{} {
320-
return c.store[key]
321-
}
322-
323-
func (c *echoContext) Del(key string) {
324-
delete(c.store, key)
325-
}
326-
327-
func (c *echoContext) Contains(key string) bool {
328-
_, ok := c.store[key]
329-
return ok
300+
return c.Context.Value(key)
330301
}
331302

332303
func (c *echoContext) Bind(i interface{}) error {
@@ -511,6 +482,5 @@ func (c *echoContext) Reset(req engine.Request, res engine.Response) {
511482
c.Context = nil
512483
c.request = req
513484
c.response = res
514-
c.store = nil
515485
c.handler = notFoundHandler
516486
}

context_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,24 +327,23 @@ func TestContextRedirect(t *testing.T) {
327327
assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo"))
328328
}
329329

330-
func TestContextStdContext(t *testing.T) {
330+
func TestContextEmbedded(t *testing.T) {
331331
c := new(echoContext)
332-
c.SetStdContext(context.WithValue(c.StdContext(), "key", "val"))
332+
c.SetContext(context.WithValue(c, "key", "val"))
333333
assert.Equal(t, "val", c.Value("key"))
334-
ctx, _ := context.WithDeadline(context.Background(), time.Now())
335-
c.SetStdContext(ctx)
334+
now := time.Now()
335+
ctx, _ := context.WithDeadline(context.Background(), now)
336+
c.SetContext(ctx)
337+
n, _ := ctx.Deadline()
338+
assert.Equal(t, now, n)
336339
assert.Equal(t, context.DeadlineExceeded, c.Err())
337340
assert.NotNil(t, c.Done())
338341
}
339342

340343
func TestContextStore(t *testing.T) {
341344
c := new(echoContext)
342-
c.store = nil
343345
c.Set("name", "Jon Snow")
344346
assert.Equal(t, "Jon Snow", c.Get("name"))
345-
assert.True(t, c.Contains("name"))
346-
c.Del("name")
347-
assert.Empty(t, c.Get("name"))
348347
}
349348

350349
func TestContextServeContent(t *testing.T) {

echo.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
243243
response: res,
244244
echo: e,
245245
pvalues: make([]string, *e.maxParam),
246-
store: make(store),
247246
handler: notFoundHandler,
248247
}
249248
}

0 commit comments

Comments
 (0)