-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
179 lines (159 loc) · 3.76 KB
/
node.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
package gRouter
import (
"errors"
"strings"
)
var (
errorUrlNotFound = errors.New("url not found")
errorUrlFormat = errors.New("url must start with /")
)
type nodeType uint8
const (
nodeTypeNormal nodeType = 0 //uri普通节点
nodeTypeParam nodeType = 1 //uri中restful参数
nodeTypeAll nodeType = 2 //uri中*全部匹配
)
//路径按照这个顺序匹配
var nodeTypeList = [3]nodeType{
nodeTypeNormal,
nodeTypeParam,
nodeTypeAll,
}
type node struct {
path string
pathMatch string
isRoot bool
isLeaf bool
nType nodeType
children []*node
parent *node
handlers HandlersChain
}
func newNode(path string, isRoot bool) *node {
return &node{
path: path,
isRoot: isRoot,
}
}
func (node *node) Find(url string) (*node, error) {
url = strings.TrimSpace(url)
if url == "" || url[0] != '/' {
return nil, errorUrlFormat
}
if url == "/" {
return node, nil
}
paths := node.removeEmptyPath(strings.Split(url, "/"))
return node.find(paths)
}
//删除为空的路径,第0个元素是空的,且不删除
func (node *node) removeEmptyPath(paths []string) []string {
var list []string
for i := 1; i < len(paths); i++ {
if paths[i] == "" {
if list == nil {
list = make([]string, i, len(paths)-1)
copy(list, paths[:i])
}
} else if list != nil {
list = append(list, paths[i])
}
}
if len(list) == 0 {
return paths
}
return list
}
func (node *node) find(paths []string) (*node, error) {
if len(paths) == 0 {
return nil, errorUrlNotFound
}
if err := node.checkPath(paths[0]); err != nil {
return nil, err
}
if len(paths) == 1 {
if node.isLeaf {
return node, nil
}
//当前节点不是叶子节点,说明路径没有完整匹配,地址没找到
return nil, errorUrlNotFound
}
//1:nodeTypeNormal 优先级高
for i := 0; i < len(node.children); i++ {
if node.children[i].path == paths[1] && node.children[i].nType == nodeTypeNormal {
return node.children[i].find(paths[1:])
}
}
//2:nodeTypeParam 优先级中
//3:nodeTypeAll 优先级低
for _, nType := range []nodeType{nodeTypeParam, nodeTypeAll} {
for i := 0; i < len(node.children); i++ {
if node.children[i].nType == nType {
result, err := node.children[i].find(paths[1:])
if err == nil {
return result, err
}
}
}
}
return nil, errorUrlNotFound
}
func (node *node) checkPath(path string) error {
if node.isNodeTypeNormal() && node.path != path {
return errorUrlNotFound
}
return nil
}
func (node *node) Add(url string, handlers HandlersChain) error {
url = strings.TrimSpace(url)
if url == "" || url[0] != '/' {
return errorUrlFormat
}
if url == "/" {
node.handlers = append(node.handlers, handlers...)
node.isLeaf = true
return nil
}
paths := node.removeEmptyPath(strings.Split(url, "/"))
return node.add(paths[1:], handlers)
}
func (node *node) isNodeTypeNormal() bool {
return node.nType == nodeTypeNormal
}
func (node *node) getNodeType(path string) nodeType {
nType := nodeTypeNormal
if len(path) > 0 && path[0] == ':' {
nType = nodeTypeParam
} else if path == "*" {
nType = nodeTypeAll
}
return nType
}
func (node *node) add(paths []string, handlers HandlersChain) error {
path := paths[0]
nType := node.getNodeType(path)
if nType == nodeTypeParam {
path = path[1:]
}
for index := 0; index < len(nodeTypeList); index++ {
if nodeTypeList[index] != nType {
continue
}
for i := 0; i < len(node.children); i++ {
if node.children[i].path == path {
return node.children[i].add(paths[1:], handlers)
}
}
}
sonNode := newNode(path, false)
sonNode.parent = node
sonNode.nType = nType
node.children = append(node.children, sonNode)
if len(paths) == 1 {
sonNode.handlers = handlers
sonNode.isLeaf = true
} else {
sonNode.add(paths[1:], handlers)
}
return nil
}