-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
131 lines (103 loc) · 2.21 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package echotool
import (
"context"
"fmt"
"time"
"github.com/popeyeio/handy"
)
type Context struct {
engine *Engine
handlers HandlerFuncsChain
handlerName string
ok bool
code int
data interface{}
err error
namedValue string
customValues map[string]string
startTime time.Time
}
var _ context.Context = (*Context)(nil)
var _ fmt.Stringer = (*Context)(nil)
func (ec *Context) Deadline() (deadline time.Time, ok bool) {
return
}
func (ec *Context) Done() <-chan struct{} {
return nil
}
func (ec *Context) Err() error {
return nil
}
func (ec *Context) Value(key interface{}) interface{} {
return nil
}
func (ec *Context) String() string {
if ec.ok {
return fmt.Sprintf("code:%d, data:%s", ec.code, handy.Stringify(ec.data))
}
return fmt.Sprintf("code:%d, error:%v", ec.code, ec.err)
}
func (ec *Context) Finish(code int, data interface{}) {
ec.ok = true
ec.code = code
ec.data = data
}
func (ec *Context) Redirect(code int, url string) {
ec.Finish(code, url)
}
func (ec *Context) Abort(code int, err error) {
ec.ok = false
ec.code = code
ec.err = err
}
func (ec *Context) IsOK() bool {
return ec.ok
}
func (ec *Context) GetCode() int {
return ec.code
}
func (ec *Context) GetData() interface{} {
return ec.data
}
func (ec *Context) GetError() error {
return ec.err
}
func (ec *Context) GetNamedValue() string {
return ec.namedValue
}
func (ec *Context) SetNamedValue(value string) {
ec.namedValue = value
}
func (ec *Context) GetCustomValue(key string) (value string, exists bool) {
value, exists = ec.customValues[key]
return
}
func (ec *Context) SetCustomValue(key, value string) {
ec.customValues[key] = value
}
func (ec *Context) GetCustomValues() map[string]string {
return ec.customValues
}
func (ec *Context) GetHandlerName() string {
return ec.handlerName
}
func (ec *Context) GetStartTime() time.Time {
return ec.startTime
}
func (ec *Context) Clone() *Context {
return &Context{
namedValue: ec.namedValue,
customValues: ec.customValues,
}
}
func (ec *Context) reset() {
ec.engine = nil
ec.handlers = ec.handlers[:0]
ec.handlerName = handy.StrEmpty
ec.ok = false
ec.code = 0
ec.data = nil
ec.err = nil
ec.namedValue = handy.StrEmpty
ec.customValues = nil
}