-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstack.go
75 lines (62 loc) · 1.76 KB
/
stack.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
package layer
// Priority represents the middleware priority.
type Priority int
const (
// TopHead priority defines the middleware handler
// as first element of the stack head.
TopHead Priority = iota
// Head priority stores the middleware handler
// in the head of the stack.
Head
// Normal priority defines the middleware handler
// in the last stack available.
Normal
// TopTail priority defines the middleware handler
// as fist element of the stack tail.
TopTail
// Tail priority defines the middleware handler
// in the tail of the stack.
Tail
)
// Stack stores the data to show.
type Stack struct {
// memo stores the memorized pre-computed merged stack for better performance.
memo []MiddlewareFunc
// Head stores the head priority handlers.
Head []MiddlewareFunc
// Stack stores the middleware normal priority handlers.
Stack []MiddlewareFunc
// Tail stores the middleware tail priority handlers.
Tail []MiddlewareFunc
}
// Push pushes a new middleware handler to the stack based on the given priority.
func (s *Stack) Push(order Priority, h MiddlewareFunc) {
s.memo = nil // flush the memoized stack
if order == TopHead {
s.Head = append([]MiddlewareFunc{h}, s.Head...)
}
if order == Head {
s.Head = append(s.Head, h)
}
if order == Tail {
s.Tail = append(s.Tail, h)
}
if order == TopTail {
s.Tail = append([]MiddlewareFunc{h}, s.Tail...)
}
if order == Normal {
s.Stack = append(s.Stack, h)
}
}
// Join joins the middleware functions into a unique slice.
func (s *Stack) Join() []MiddlewareFunc {
if s.memo != nil {
return s.memo
}
s.memo = append(append(s.Head, s.Stack...), s.Tail...)
return s.memo
}
// Len returns the middleware stack length.
func (s *Stack) Len() int {
return len(s.Stack) + len(s.Tail) + len(s.Head)
}