-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
180 lines (163 loc) · 3.07 KB
/
tree.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
package flex
import (
"fmt"
"regexp"
"strings"
)
//
//todo 参数和正则现在还不支持
const (
root byte = iota
static
named
pattern
)
const (
start = '{'
end = '}'
separator = ':'
)
const (
defaultKey = "key"
defaultExpr = "[0-9a-zA-Z_.]+"
)
type node struct {
key string
typ byte
expr *regexp.Regexp //编译后的正则表达式
regNode *node
children map[string]*node
params Param
handler HandlerFunc
}
type tree struct {
root *node
}
func newTree() *tree {
return &tree{root: &node{
key: "/",
typ: root,
children: make(map[string]*node),
}}
}
func (t *tree) insert(path string, handler HandlerFunc) {
n := t.root
if path != n.key {
path = trimPathPrefix(path)
keys := splitPath(path)
for _, s := range keys {
typ := parseNodeType(s)
var (
key string
reg string
tn *node
ok bool
)
if typ == static {
key = s
tn, ok = n.children[key]
if !ok {
tn = &node{
key: key,
typ: static,
children: make(map[string]*node),
}
n.children[key] = tn
}
} else {
//如果是正则节点,那么这部分就只能有一个节点,否则就会冲突
if len(n.children) > 0 {
panic(fmt.Sprintf("<%s> conflict with static path", path))
}
ln := len(s)
if typ == pattern {
i := strings.IndexByte(s, separator)
if i < 0 {
// /post/{id}/hello => /post/{id:[0-9a-zA-Z_.]+}/hello
key = s[1 : ln-1]
reg = defaultExpr
} else if i == 1 {
// /post/{:\\d+}/hello => /post/{id:\\d+}/hello
key = defaultKey
reg = s[i+1 : ln-1]
} else {
// /post/{id:\\d+}/hello => /post/{id:\\d+}/hello
key = s[1:i]
reg = s[i+1 : ln-1]
}
} else {
// /post/:id/hello => /post/{id:[0-9a-zA-Z_.]+}/hello
key = s[1 : ln-1]
reg = defaultExpr
}
tn = n.regNode
if tn == nil {
tn = &node{
key: key,
typ: typ,
expr: regexp.MustCompile(reg),
children: make(map[string]*node),
}
n.regNode = tn
}
}
n = tn
}
}
if n.handler != nil {
panic(fmt.Sprintf("<%s> route exists", path))
}
n.handler = handler
}
func (t *tree) find(path string) *node {
n := t.root
if path == n.key {
return n
}
params := make(map[string][]byte)
path = trimPathPrefix(path)
ln := len(path)
if path[ln-1] == '/' {
path = path[:ln-1]
}
keys := splitPath(path)
for _, key := range keys {
var (
child *node
ok bool
)
if n.regNode != nil {
child = n.regNode
val := child.expr.Find([]byte(key))
//如果不匹配就是没有找到对应的路由
if val == nil {
return nil
}
params[child.key] = val
} else {
child, ok = n.children[key]
if !ok {
return nil
}
}
n = child
}
n.params = params
return n
}
func parseNodeType(s string) byte {
n := len(s)
if n == 0 {
panic("route key empty")
}
if s[0] == start {
if s[n-1] == end {
return pattern
} else {
panic("router syntax error")
}
} else if s[0] == separator {
return named
}
return static
}