MCPcopy Create free account
hub / github.com/devfeel/dotweb / addRoute

Method addRoute

tree.go:125–267  ·  view source on GitHub ↗

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

(path string, handle RouterHandle, m ...Middleware)

Source from the content-addressed store, hash-verified

123// addRoute adds a node with the given handle to the path.
124// Not concurrency-safe!
125func (n *Node) addRoute(path string, handle RouterHandle, m ...Middleware) (outnode *Node) {
126 outnode = n
127
128 fullPath := path
129 n.priority++
130 numParams := countParams(path)
131
132 // Non-empty tree
133 if len(n.path) > 0 || len(n.children) > 0 {
134 walk:
135 for {
136 // Update maxParams of the current node
137 if numParams > n.maxParams {
138 n.maxParams = numParams
139 }
140
141 // Find the longest common prefix.
142 // This also implies that the common prefix contains no ':' or '*'
143 // since the existing key can't contain those chars.
144 i := 0
145 max := min(len(path), len(n.path))
146 for i < max && path[i] == n.path[i] {
147 i++
148 }
149
150 // Split edge
151 if i < len(n.path) {
152 child := Node{
153 path: n.path[i:],
154 fullPath: n.fullPath,
155 wildChild: n.wildChild,
156 nType: static,
157 indices: n.indices,
158 children: n.children,
159 handle: n.handle,
160 priority: n.priority - 1,
161 middlewares: n.middlewares,
162 groupMiddlewares: n.groupMiddlewares,
163 appMiddlewares: n.appMiddlewares,
164 }
165
166 // Update maxParams (max of all children)
167 for i := range child.children {
168 if child.children[i].maxParams > child.maxParams {
169 child.maxParams = child.children[i].maxParams
170 }
171 }
172
173 n.children = []*Node{&child}
174 // []byte for proper unicode char conversion, see #65
175 n.indices = string([]byte{n.path[i]})
176 n.path = path[:i]
177 n.handle = nil
178 n.wildChild = false
179 n.middlewares = nil
180 n.groupMiddlewares = nil
181 n.appMiddlewares = nil
182 }

Callers 1

addMethod · 0.80

Calls 5

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

Tested by

no test coverage detected