Home / Function/ addRoute() — gin Function Reference

addRoute() — gin Function Reference

Architecture documentation for the addRoute() function in tree.go from the gin codebase.

Entity Profile

Dependency Diagram

graph TD
  1e254bd3_e110_d850_2675_5cdfb2b748aa["addRoute()"]
  1a47c43a_b5cc_8545_a924_5d7bc35fb4bc["insertChild()"]
  1e254bd3_e110_d850_2675_5cdfb2b748aa -->|calls| 1a47c43a_b5cc_8545_a924_5d7bc35fb4bc
  d5014715_c0ae_9699_24c7_2e76e5802f19["longestCommonPrefix()"]
  1e254bd3_e110_d850_2675_5cdfb2b748aa -->|calls| d5014715_c0ae_9699_24c7_2e76e5802f19
  56fd4895_ab64_982a_9ad0_06c8140ee252["incrementChildPrio()"]
  1e254bd3_e110_d850_2675_5cdfb2b748aa -->|calls| 56fd4895_ab64_982a_9ad0_06c8140ee252
  52b021ff_6220_b2f1_d6cf_3a67e18c9f64["addChild()"]
  1e254bd3_e110_d850_2675_5cdfb2b748aa -->|calls| 52b021ff_6220_b2f1_d6cf_3a67e18c9f64
  style 1e254bd3_e110_d850_2675_5cdfb2b748aa fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

tree.go lines 135–249

func (n *node) addRoute(path string, handlers HandlersChain) {
	fullPath := path
	n.priority++

	// Empty tree
	if len(n.path) == 0 && len(n.children) == 0 {
		n.insertChild(path, fullPath, handlers)
		n.nType = root
		return
	}

	parentFullPathIndex := 0

walk:
	for {
		// Find the longest common prefix.
		// This also implies that the common prefix contains no ':' or '*'
		// since the existing key can't contain those chars.
		i := longestCommonPrefix(path, n.path)

		// Split edge
		if i < len(n.path) {
			child := node{
				path:      n.path[i:],
				wildChild: n.wildChild,
				nType:     static,
				indices:   n.indices,
				children:  n.children,
				handlers:  n.handlers,
				priority:  n.priority - 1,
				fullPath:  n.fullPath,
			}

			n.children = []*node{&child}
			// []byte for proper unicode char conversion, see #65
			n.indices = bytesconv.BytesToString([]byte{n.path[i]})
			n.path = path[:i]
			n.handlers = nil
			n.wildChild = false
			n.fullPath = fullPath[:parentFullPathIndex+i]
		}

		// Make new node a child of this node
		if i < len(path) {
			path = path[i:]
			c := path[0]

			// '/' after param
			if n.nType == param && c == '/' && len(n.children) == 1 {
				parentFullPathIndex += len(n.path)
				n = n.children[0]
				n.priority++
				continue walk
			}

			// Check if a child with the next path byte exists
			for i, max_ := 0, len(n.indices); i < max_; i++ {
				if c == n.indices[i] {
					parentFullPathIndex += len(n.path)
					i = n.incrementChildPrio(i)
					n = n.children[i]
					continue walk
				}
			}

			// Otherwise insert it
			if c != ':' && c != '*' && n.nType != catchAll {
				// []byte for proper unicode char conversion, see #65
				n.indices += bytesconv.BytesToString([]byte{c})
				child := &node{
					fullPath: fullPath,
				}
				n.addChild(child)
				n.incrementChildPrio(len(n.indices) - 1)
				n = child
			} else if n.wildChild {
				// inserting a wildcard node, need to check if it conflicts with the existing wildcard
				n = n.children[len(n.children)-1]
				n.priority++

				// Check if the wildcard matches
				if len(path) >= len(n.path) && n.path == path[:len(n.path)] &&
					// Adding a child to a catchAll is not possible
					n.nType != catchAll &&
					// Check for longer wildcard, e.g. :name and :names
					(len(n.path) >= len(path) || path[len(n.path)] == '/') {
					continue walk
				}

				// Wildcard conflict
				pathSeg := path
				if n.nType != catchAll {
					pathSeg, _, _ = strings.Cut(pathSeg, "/")
				}
				prefix := fullPath[:strings.Index(fullPath, pathSeg)] + n.path
				panic("'" + pathSeg +
					"' in new path '" + fullPath +
					"' conflicts with existing wildcard '" + n.path +
					"' in existing prefix '" + prefix +
					"'")
			}

			n.insertChild(path, fullPath, handlers)
			return
		}

		// Otherwise add handle to current node
		if n.handlers != nil {
			panic("handlers are already registered for path '" + fullPath + "'")
		}
		n.handlers = handlers
		n.fullPath = fullPath
		return
	}
}

Domain

Subdomains

Frequently Asked Questions

What does addRoute() do?
addRoute() is a function in the gin codebase.
What does addRoute() call?
addRoute() calls 4 function(s): addChild, incrementChildPrio, insertChild, longestCommonPrefix.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free