MCPcopy
hub / github.com/julienschmidt/httprouter / addRoute

Method addRoute

tree.go:83–215  ·  tree.go::node.addRoute

addRoute adds a node with the given handle to the path. Not concurrency-safe!

(path string, handle Handle)

Source from the content-addressed store, hash-verified

81// addRoute adds a node with the given handle to the path.
82// Not concurrency-safe!
83func (n *node) addRoute(path string, handle Handle) {
84 fullPath := path
85 n.priority++
86 numParams := countParams(path)
87
88 // non-empty tree
89 if len(n.path) > 0 || len(n.children) > 0 {
90 walk:
91 for {
92 // Update maxParams of the current node
93 if numParams > n.maxParams {
94 n.maxParams = numParams
95 }
96
97 // Find the longest common prefix.
98 // This also implies that the common prefix contains no ':' or '*'
99 // since the existing key can't contain those chars.
100 i := 0
101 max := min(len(path), len(n.path))
102 for i < max && path[i] == n.path[i] {
103 i++
104 }
105
106 // Split edge
107 if i < len(n.path) {
108 child := node{
109 path: n.path[i:],
110 wildChild: n.wildChild,
111 nType: static,
112 indices: n.indices,
113 children: n.children,
114 handle: n.handle,
115 priority: n.priority - 1,
116 }
117
118 // Update maxParams (max of all children)
119 for i := range child.children {
120 if child.children[i].maxParams > child.maxParams {
121 child.maxParams = child.children[i].maxParams
122 }
123 }
124
125 n.children = []*node{&child}
126 // []byte for proper unicode char conversion, see #65
127 n.indices = string([]byte{n.path[i]})
128 n.path = path[:i]
129 n.handle = nil
130 n.wildChild = false
131 }
132
133 // Make new node a child of this node
134 if i < len(path) {
135 path = path[i:]
136
137 if n.wildChild {
138 n = n.children[0]
139 n.priority++
140

Callers 13

TestTreeAddAndGetFunction · 0.95
TestTreeWildcardFunction · 0.95
testRoutesFunction · 0.95
TestTreeDupliatePathFunction · 0.95
TestEmptyWildcardNameFunction · 0.95
TestTreeCatchMaxParamsFunction · 0.95
TestTreeDoubleWildcardFunction · 0.95
TestTreeInvalidNodeTypeFunction · 0.95

Calls 4

incrementChildPrioMethod · 0.95
insertChildMethod · 0.95
countParamsFunction · 0.85
minFunction · 0.85

Tested by 12

TestTreeAddAndGetFunction · 0.76
TestTreeWildcardFunction · 0.76
testRoutesFunction · 0.76
TestTreeDupliatePathFunction · 0.76
TestEmptyWildcardNameFunction · 0.76
TestTreeCatchMaxParamsFunction · 0.76
TestTreeDoubleWildcardFunction · 0.76
TestTreeInvalidNodeTypeFunction · 0.76