-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
context.go
213 lines (173 loc) · 4.25 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package context
import (
"fmt"
"github.com/firmeve/firmeve/binding"
"github.com/firmeve/firmeve/kernel"
"github.com/firmeve/firmeve/kernel/contract"
"github.com/firmeve/firmeve/render"
"sync"
"time"
)
const (
abortIndex = -1
)
type (
context struct {
application contract.Application
protocol contract.Protocol
handlers []contract.ContextHandler
entries map[string]*contract.ContextEntity
index int
}
)
var pool = &sync.Pool{
New: func() interface{} {
return new(context)
},
}
func NewContext(application contract.Application, protocol contract.Protocol, handlers ...contract.ContextHandler) contract.Context {
ctx1, _ := application.PoolValue(`context`)
ctx := ctx1.(*context)
ctx.protocol = protocol
ctx.handlers = handlers
return ctx
}
func ReleaseContext(application contract.Application, c contract.Context) {
application.ReleasePool(`context`, func() interface{} {
ctx := c.(*context)
ctx.entries = make(map[string]*contract.ContextEntity, 0)
ctx.index = 0
return ctx
})
}
func (c *context) Application() contract.Application {
return c.application
}
func (c *context) Protocol() contract.Protocol {
return c.protocol
}
func (c *context) Error(status int, params ...interface{}) {
paramsLen := len(params)
var (
err error
)
if paramsLen == 1 {
if v, ok := params[0].(error); ok {
err = v
} else {
err = kernel.Error(fmt.Printf("%v", params[0]))
}
} else { // 固定为2位参数,message,error
err = kernel.Error(params...)
}
// record logger
go c.Resolve(`logger`).(contract.Loggable).Error(
`http error`, "error", err)
// custom error
if v, ok := err.(contract.ErrorRender); ok {
if err2 := v.Render(status, c); err2 != nil {
panic(err2)
}
} else {
if err2 := render.Error.Render(c.protocol, status, err); err2 != nil {
panic(err2)
}
}
c.Abort()
}
func (c *context) Abort() {
c.index = abortIndex
}
func (c *context) Current() int {
return c.index
}
func (c *context) Next() {
if c.index == abortIndex {
return
}
if c.index < len(c.handlers) {
c.index++
c.handlers[c.index-1](c)
}
}
func (c *context) Handlers() []contract.ContextHandler {
return c.handlers
}
func (c *context) AddEntity(key string, value interface{}) {
c.entries[key] = &contract.ContextEntity{
Key: key,
Value: value,
}
}
func (c *context) Entity(key string) *contract.ContextEntity {
if v, ok := c.entries[key]; ok {
return v
}
return nil
}
func (c *context) Get(key string) interface{} {
values := c.protocol.Values()
if value, ok := values[key]; ok {
if len(value) == 1 {
return value[0]
}
return value
}
return nil
}
func (c *context) Bind(v interface{}) error {
return binding.Bind(c.protocol, v)
}
func (c *context) BindValidate(v interface{}) error {
if err := c.Bind(v); err != nil {
return err
}
return c.Resolve(`validator`).(contract.Validator).Validate(v)
}
func (c *context) BindWithValidate(b contract.Binding, v interface{}) error {
if err := c.BindWith(b, v); err != nil {
return err
}
return c.Resolve(`validator`).(contract.Validator).Validate(v)
}
func (c *context) BindWith(b contract.Binding, v interface{}) error {
return b.Protocol(c.protocol, v)
}
func (c *context) RenderWith(status int, r contract.Render, v interface{}) error {
return r.Render(c.protocol, status, v)
}
func (c *context) Render(status int, v interface{}) error {
return render.Render(c.protocol, status, v)
}
func (c *context) Clone() contract.Context {
ctxNew := new(context)
*ctxNew = *c
ctxNew.protocol = c.protocol.Clone()
ctxNew.application = c.application
ctxNew.index = c.index
ctxNew.handlers = c.handlers
ctxNew.entries = make(map[string]*contract.ContextEntity, len(c.entries))
for k, v := range c.entries {
*ctxNew.entries[k] = *v
}
return ctxNew
}
func (c *context) Resolve(abstract interface{}, params ...interface{}) interface{} {
return c.application.Make(abstract, params...)
}
// --------------------------- context.Context -> Base context ------------------------
func (c *context) Deadline() (deadline time.Time, ok bool) {
return
}
func (c *context) Done() <-chan struct{} {
return nil
}
func (c *context) Err() error {
return nil
}
func (c *context) Value(key interface{}) interface{} {
if v, ok := key.(string); ok {
return c.Get(v)
}
return nil
}