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

Method getValue

tree.go:339–460  ·  tree.go::node.getValue

Returns the handle registered with the given path (key). The values of wildcards are saved to a map. If no handle can be found, a TSR (trailing slash redirect) recommendation is made if a handle exists with an extra (without the) trailing slash for the given path.

(path string)

Source from the content-addressed store, hash-verified

337// made if a handle exists with an extra (without the) trailing slash for the
338// given path.
339func (n *node) getValue(path string) (handle Handle, p Params, tsr bool) {
340walk: // outer loop for walking the tree
341 for {
342 if len(path) > len(n.path) {
343 if path[:len(n.path)] == n.path {
344 path = path[len(n.path):]
345 // If this node does not have a wildcard (param or catchAll)
346 // child, we can just look up the next child node and continue
347 // to walk down the tree
348 if !n.wildChild {
349 c := path[0]
350 for i := 0; i < len(n.indices); i++ {
351 if c == n.indices[i] {
352 n = n.children[i]
353 continue walk
354 }
355 }
356
357 // Nothing found.
358 // We can recommend to redirect to the same URL without a
359 // trailing slash if a leaf exists for that path.
360 tsr = (path == "/" && n.handle != nil)
361 return
362
363 }
364
365 // handle wildcard child
366 n = n.children[0]
367 switch n.nType {
368 case param:
369 // find param end (either '/' or path end)
370 end := 0
371 for end < len(path) && path[end] != '/' {
372 end++
373 }
374
375 // save param value
376 if p == nil {
377 // lazy allocation
378 p = make(Params, 0, n.maxParams)
379 }
380 i := len(p)
381 p = p[:i+1] // expand slice within preallocated capacity
382 p[i].Key = n.path[1:]
383 p[i].Value = path[:end]
384
385 // we need to go deeper!
386 if end < len(path) {
387 if len(n.children) > 0 {
388 path = path[end:]
389 n = n.children[0]
390 continue walk
391 }
392
393 // ... but we can't
394 tsr = (len(path) == end+1)
395 return
396 }

Callers 7

TestTreeInvalidNodeTypeFunction · 0.95
LookupMethod · 0.80
allowedMethod · 0.80
ServeHTTPMethod · 0.80
checkRequestsFunction · 0.80

Calls

no outgoing calls

Tested by 4

TestTreeInvalidNodeTypeFunction · 0.76
checkRequestsFunction · 0.64