MCPcopy
hub / github.com/gin-gonic/gin / addRoute

Method addRoute

tree.go:135–249  ·  view source on GitHub ↗

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

(path string, handlers HandlersChain)

Source from the content-addressed store, hash-verified

133// addRoute adds a node with the given handle to the path.
134// Not concurrency-safe!
135func (n *node) addRoute(path string, handlers HandlersChain) {
136 fullPath := path
137 n.priority++
138
139 // Empty tree
140 if len(n.path) == 0 && len(n.children) == 0 {
141 n.insertChild(path, fullPath, handlers)
142 n.nType = root
143 return
144 }
145
146 parentFullPathIndex := 0
147
148walk:
149 for {
150 // Find the longest common prefix.
151 // This also implies that the common prefix contains no ':' or '*'
152 // since the existing key can't contain those chars.
153 i := longestCommonPrefix(path, n.path)
154
155 // Split edge
156 if i < len(n.path) {
157 child := node{
158 path: n.path[i:],
159 wildChild: n.wildChild,
160 nType: static,
161 indices: n.indices,
162 children: n.children,
163 handlers: n.handlers,
164 priority: n.priority - 1,
165 fullPath: n.fullPath,
166 }
167
168 n.children = []*node{&child}
169 // []byte for proper unicode char conversion, see #65
170 n.indices = bytesconv.BytesToString([]byte{n.path[i]})
171 n.path = path[:i]
172 n.handlers = nil
173 n.wildChild = false
174 n.fullPath = fullPath[:parentFullPathIndex+i]
175 }
176
177 // Make new node a child of this node
178 if i < len(path) {
179 path = path[i:]
180 c := path[0]
181
182 // '/' after param
183 if n.nType == param && c == '/' && len(n.children) == 1 {
184 parentFullPathIndex += len(n.path)
185 n = n.children[0]
186 n.priority++
187 continue walk
188 }
189
190 // Check if a child with the next path byte exists
191 for i, max_ := 0, len(n.indices); i < max_; i++ {
192 if c == n.indices[i] {

Callers 15

TestTreeAddAndGetFunction · 0.95
TestTreeWildcardFunction · 0.95
TestUnescapeParametersFunction · 0.95
testRoutesFunction · 0.95
TestTreeDuplicatePathFunction · 0.95
TestEmptyWildcardNameFunction · 0.95
TestTreeCatchMaxParamsFunction · 0.95
TestTreeDoubleWildcardFunction · 0.95

Calls 5

insertChildMethod · 0.95
incrementChildPrioMethod · 0.95
addChildMethod · 0.95
BytesToStringFunction · 0.92
longestCommonPrefixFunction · 0.85

Tested by 15

TestTreeAddAndGetFunction · 0.76
TestTreeWildcardFunction · 0.76
TestUnescapeParametersFunction · 0.76
testRoutesFunction · 0.76
TestTreeDuplicatePathFunction · 0.76
TestEmptyWildcardNameFunction · 0.76
TestTreeCatchMaxParamsFunction · 0.76
TestTreeDoubleWildcardFunction · 0.76