-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
trie.go
126 lines (110 loc) · 2.48 KB
/
trie.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
package violetear
import (
"errors"
"net/http"
"strings"
)
// MethodHandler keeps HTTP Method and http.handler
type MethodHandler struct {
Method string
Handler http.Handler
}
// Trie data structure
type Trie struct {
Handler []MethodHandler
HasCatchall bool
HasRegex bool
Node []*Trie
name string
path string
version string
}
// contains check if path exists on node
func (t *Trie) contains(path, version string) (*Trie, bool) {
for _, n := range t.Node {
if n.path == path && n.version == version {
return n, true
}
}
return nil, false
}
// Set adds a node (url part) to the Trie
func (t *Trie) Set(path []string, handler http.Handler, method, version string) (*Trie, error) {
if len(path) == 0 {
return nil, errors.New("path cannot be empty")
}
key := path[0]
newpath := path[1:]
node, ok := t.contains(key, version)
if !ok {
node = &Trie{
path: key,
version: version,
}
t.Node = append(t.Node, node)
// check for regex ":"
if strings.HasPrefix(key, ":") {
t.HasRegex = true
}
// check for Catch-all "*"
if key == "*" {
t.HasCatchall = true
}
}
if len(newpath) == 0 {
methods := strings.FieldsFunc(method, func(c rune) bool {
return c == ','
})
for _, v := range methods {
node.Handler = append(node.Handler, MethodHandler{strings.ToUpper(strings.TrimSpace(v)), handler})
}
return node, nil
}
if key == "*" {
return nil, errors.New("catch-all \"*\" must always be the final path element")
}
return node.Set(newpath, handler, method, version)
}
// Get returns a node
func (t *Trie) Get(path, version string) (*Trie, string, string, bool) {
key, path := t.SplitPath(path)
// search the key recursively on the tree
if node, ok := t.contains(key, version); ok {
if path == "" {
return node, key, path, true
}
return node.Get(path, version)
}
// if not fount check for catchall or regex
return t, key, path, false
}
// SplitPath returns first element of path and remaining path
func (t *Trie) SplitPath(path string) (string, string) {
var key string
if path == "" {
return key, path
} else if path == "/" {
return path, ""
}
for i := 0; i < len(path); i++ {
if path[i] == '/' {
if i == 0 {
return t.SplitPath(path[1:])
}
if i > 0 {
key = path[:i]
path = path[i:]
if path == "/" {
return key, ""
}
return key, path
}
}
}
return path, ""
}
// Name add custom name to node
func (t *Trie) Name(name string) *Trie {
t.name = name
return t
}