-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
74 lines (62 loc) · 1.28 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package goa
import (
"context"
"net/http"
)
type ContextBeforeLookup struct {
*http.Request
http.ResponseWriter
data map[string]interface{}
err error
}
type Context struct {
ContextBeforeLookup
handlers []func(c *Context)
params []string
index int
}
// Param returns captured subpatterns.
// index begin at 0, so pass 0 to get the first captured subpatterns.
func (c *Context) Param(i int) string {
if i < len(c.params) {
return c.params[i]
}
return ""
}
// run the next midllware or route handler.
func (c *Context) Next() {
c.index++
if c.index >= len(c.handlers) {
return
}
c.handlers[c.index](c)
}
func (c *ContextBeforeLookup) Context() context.Context {
if data := c.Get("context"); data != nil {
if c, ok := data.(context.Context); ok {
return c
}
}
return c.Request.Context()
}
func (c *ContextBeforeLookup) Get(key string) interface{} {
if c.data == nil {
c.data = make(map[string]interface{})
}
if value, ok := c.data[key]; ok {
return value
}
return nil
}
func (c *ContextBeforeLookup) Set(key string, value interface{}) {
if c.data == nil {
c.data = make(map[string]interface{})
}
c.data[key] = value
}
func (c *ContextBeforeLookup) SetError(err error) {
c.err = err
}
func (c *ContextBeforeLookup) GetError() error {
return c.err
}