Home / Function/ insertChild() — gin Function Reference

insertChild() — gin Function Reference

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

Function go CoreEngine Routing calls 2 called by 1

Entity Profile

Dependency Diagram

graph TD
  1a47c43a_b5cc_8545_a924_5d7bc35fb4bc["insertChild()"]
  1e254bd3_e110_d850_2675_5cdfb2b748aa["addRoute()"]
  1e254bd3_e110_d850_2675_5cdfb2b748aa -->|calls| 1a47c43a_b5cc_8545_a924_5d7bc35fb4bc
  10c537e5_647e_9949_f47a_4a50718bf85b["findWildcard()"]
  1a47c43a_b5cc_8545_a924_5d7bc35fb4bc -->|calls| 10c537e5_647e_9949_f47a_4a50718bf85b
  52b021ff_6220_b2f1_d6cf_3a67e18c9f64["addChild()"]
  1a47c43a_b5cc_8545_a924_5d7bc35fb4bc -->|calls| 52b021ff_6220_b2f1_d6cf_3a67e18c9f64
  style 1a47c43a_b5cc_8545_a924_5d7bc35fb4bc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

tree.go lines 288–397

func (n *node) insertChild(path string, fullPath string, handlers HandlersChain) {
	for {
		// Find prefix until first wildcard
		wildcard, i, valid := findWildcard(path)
		if i < 0 { // No wildcard found
			break
		}

		// The wildcard name must only contain one ':' or '*' character
		if !valid {
			panic("only one wildcard per path segment is allowed, has: '" +
				wildcard + "' in path '" + fullPath + "'")
		}

		// check if the wildcard has a name
		if len(wildcard) < 2 {
			panic("wildcards must be named with a non-empty name in path '" + fullPath + "'")
		}

		if wildcard[0] == ':' { // param
			if i > 0 {
				// Insert prefix before the current wildcard
				n.path = path[:i]
				path = path[i:]
			}

			child := &node{
				nType:    param,
				path:     wildcard,
				fullPath: fullPath,
			}
			n.addChild(child)
			n.wildChild = true
			n = child
			n.priority++

			// if the path doesn't end with the wildcard, then there
			// will be another subpath starting with '/'
			if len(wildcard) < len(path) {
				path = path[len(wildcard):]

				child := &node{
					priority: 1,
					fullPath: fullPath,
				}
				n.addChild(child)
				n = child
				continue
			}

			// Otherwise we're done. Insert the handle in the new leaf
			n.handlers = handlers
			return
		}

		// catchAll
		if i+len(wildcard) != len(path) {
			panic("catch-all routes are only allowed at the end of the path in path '" + fullPath + "'")
		}

		if len(n.path) > 0 && n.path[len(n.path)-1] == '/' {
			pathSeg := ""
			if len(n.children) != 0 {
				pathSeg, _, _ = strings.Cut(n.children[0].path, "/")
			}
			panic("catch-all wildcard '" + path +
				"' in new path '" + fullPath +
				"' conflicts with existing path segment '" + pathSeg +
				"' in existing prefix '" + n.path + pathSeg +
				"'")
		}

		// currently fixed width 1 for '/'
		i--
		if i < 0 || path[i] != '/' {
			panic("no / before catch-all in path '" + fullPath + "'")
		}

		n.path = path[:i]

		// First node: catchAll node with empty path
		child := &node{
			wildChild: true,
			nType:     catchAll,
			fullPath:  fullPath,
		}

		n.addChild(child)
		n.indices = "/"
		n = child
		n.priority++

		// second node: node holding the variable
		child = &node{
			path:     path[i:],
			nType:    catchAll,
			handlers: handlers,
			priority: 1,
			fullPath: fullPath,
		}
		n.children = []*node{child}

		return
	}

	// If no wildcard was found, simply insert the path and handle
	n.path = path
	n.handlers = handlers
	n.fullPath = fullPath
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does insertChild() do?
insertChild() is a function in the gin codebase.
What does insertChild() call?
insertChild() calls 2 function(s): addChild, findWildcard.
What calls insertChild()?
insertChild() is called by 1 function(s): addRoute.

Analyze Your Own Codebase

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

Try Supermodel Free