-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
50 lines (42 loc) · 1.19 KB
/
state.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
package router
// state holds a group attributes.
type state struct {
prefix string
middlewares []Middleware
}
// newState creates a new state instance.
func newState(prefix string, middlewares []Middleware) *state {
return &state{prefix, middlewares}
}
// stateStack holds the stack of states (group attributes).
type stateStack struct {
states []*state
}
// prefix returns current state (group) prefix.
func (g *stateStack) prefix() string {
if len(g.states) > 0 {
return g.states[len(g.states)-1].prefix
}
return ""
}
// middlewares returns current state (group) middlewares.
func (g *stateStack) middlewares() []Middleware {
if len(g.states) > 0 {
return g.states[len(g.states)-1].middlewares
}
return []Middleware{}
}
// push adds a new state (group) to the stack.
func (g *stateStack) push(prefix string, middleware []Middleware) {
g.states = append(g.states, newState(g.prefix()+prefix, append(g.middlewares(), middleware...)))
}
// pop removes (closes) the last state (group).
func (g *stateStack) pop() {
if len(g.states) > 0 {
g.states = g.states[:len(g.states)-1]
}
}
// newStateStack creates a new stateStack instance.
func newStateStack() *stateStack {
return &stateStack{}
}