// Add creates new node to node tree.
Add creates new node to node tree.
// Add creates new node to node tree .
func (n *Node) Add(p string, httpMethod string, handlerFunc HandlerFunc) error {
	queue := make([]*Node, 0)
	queue = append(queue, n)
	childHasDesiredEndPoint := false
	desiredDepth := strings.Count(p, "/")
	head, p := SeparatePath(p, 1)
	for len(queue) != 0 {
		var node *Node
		node, queue = queue[0], queue[1:]
		if head[1:] == node.Endpoint {
			if strings.Count(p, "/") == 0 {
				if node.Methods == nil {
					node.Methods = make(map[string]HandlerFunc)
				}
				node.Methods[httpMethod] = handlerFunc
				return nil
			}
			head, p = SeparatePath(p, 1)
			childHasDesiredEndPoint = false
		}
		isPathParam := false
		if len(head) > 1 {
			if head[1] == ':' {
				isPathParam = true
				head = head[:1] + head[2:]
			}
		}
		for _, cn := range node.Children {
			queue = append(queue, cn)
			if head[1:] == cn.Endpoint {
				childHasDesiredEndPoint = true
				break
			}
		}
		if childHasDesiredEndPoint {
			continue
		}
		if node.Depth >= desiredDepth {
			continue
		}
		newChild := &Node{
			Depth:       node.Depth + 1,
			Endpoint:    head[1:],
			IsPathParam: isPathParam,
			Parent:      node,
		}
		queue = append(queue, newChild)
		node.Children = append(node.Children, newChild)
	}
	return nil
}
func ( n * Node ) Add ( p string , httpMethod string , handlerFunc HandlerFunc ) error { queue := make ( [ ] * Node , 0 ) queue = append ( queue , n ) childHasDesiredEndPoint := false desiredDepth := strings . Count ( p , " " ) head , p := SeparatePath ( p , 1 ) for len ( queue ) != 0 { var node * Node node , queue = queue [ 0 ] , queue [ 1 : ] if head [ 1 : ] == node . Endpoint { if strings . Count ( p , " " ) == 0 { if node . Methods == nil { node . Methods = make ( map [ string ] HandlerFunc ) } node . Methods [ httpMethod ] = handlerFunc return nil } head , p = SeparatePath ( p , 1 ) childHasDesiredEndPoint = false } isPathParam := false if len ( head ) > 1 { if head [ 1 ] == ':' { isPathParam = true head = head [ : 1 ] + head [ 2 : ] } } for _ , cn := range node . Children { queue = append ( queue , cn ) if head [ 1 : ] == cn . Endpoint { childHasDesiredEndPoint = true break } } if childHasDesiredEndPoint { continue } if node . Depth >= desiredDepth { continue } newChild := & Node { Depth : node . Depth + 1 , Endpoint : head [ 1 : ] , IsPathParam : isPathParam , Parent : node , } queue = append ( queue , newChild ) node . Children = append ( node . Children , newChild ) } return nil }
Add creates new node to node tree.
Add creates new node to node tree .
// initialize search queue for BFS. // start BFS // if current node endpoint is same as head of path // if the path is the end, register the function and end to search // shift paths to search child elements // reset flag // check whether head of path is a path parameter // "/:param" to "/param" // add children nodes to the search queue // if the child node has the desired endpoint, set the flag to search next node // if the flag is true, search next node // if depth of current node is same as the node to add, search next node