-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
66 lines (55 loc) · 1.11 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
package go_aop
import (
"context"
"math"
)
const ABORT = math.MaxInt32 - 10000
type Context struct {
offset int
handlers []func(*Context)
Ctx context.Context
param []CustomizeParam
}
type CustomizeParam interface {
// CustomizeContext 获取参数
CustomizeContext() map[string]interface{}
}
func newContext(ctx context.Context, param ...CustomizeParam) *Context {
return &Context{
offset: -1,
handlers: make([]func(*Context), 0, 10),
Ctx: ctx,
param: param,
}
}
func (ctx *Context) GetParam() []CustomizeParam {
return ctx.param
}
func (ctx *Context) Next() {
ctx.offset++
s := len(ctx.handlers)
for ; ctx.offset < s; ctx.offset++ {
if !ctx.isAbort() {
ctx.handlers[ctx.offset](ctx)
} else {
return
}
}
}
func (ctx *Context) Reset() {
ctx.offset = -1
ctx.handlers = ctx.handlers[:0]
}
// stop middleware chain
func (ctx *Context) Abort() {
ctx.offset = math.MaxInt32 - 10000
}
func (ctx *Context) isAbort() bool {
if ctx.offset >= ABORT {
return true
}
return false
}
func (ctx *Context) addHandler(f func(ctx *Context)) {
ctx.handlers = append(ctx.handlers, f)
}