MCPcopy Create free account
hub / github.com/BryanMwangi/pine / insert

Method insert

router.go:66–150  ·  view source on GitHub ↗

insert walks the tree and registers handlers at the terminal node.

(method, path string, handlers []Handler, paramNames []string)

Source from the content-addressed store, hash-verified

64
65// insert walks the tree and registers handlers at the terminal node.
66func (n *node) insert(method, path string, handlers []Handler, paramNames []string) {
67 // Reached the end of the path — this is the leaf.
68 if path == "" {
69 if n.handlers == nil {
70 n.handlers = make(map[string][]Handler)
71 }
72 n.handlers[method] = handlers
73 n.paramNames = paramNames
74 return
75 }
76
77 // Consume the next segment (up to the next '/' or end of string).
78 seg, rest := nextSegment(path)
79
80 if seg == "" {
81 // Consecutive or trailing slash — skip and continue.
82 n.insert(method, rest, handlers, paramNames)
83 return
84 }
85
86 firstByte := seg[0]
87
88 switch firstByte {
89 case ':':
90 // Parametric segment.
91 paramName := seg[1:]
92 if n.paramChild == nil {
93 n.paramChild = &node{}
94 }
95 n.paramChild.insert(method, rest, handlers, append(paramNames, paramName))
96
97 case '*':
98 // Catch-all wildcard. Remaining path captured as one param.
99 paramName := seg[1:]
100 if paramName == "" {
101 paramName = "wildcard"
102 }
103 if n.wildcard == nil {
104 n.wildcard = &node{}
105 }
106 if n.wildcard.handlers == nil {
107 n.wildcard.handlers = make(map[string][]Handler)
108 }
109 n.wildcard.handlers[method] = handlers
110 n.wildcard.paramNames = append(paramNames, paramName)
111
112 default:
113 // Static segment.
114 if n.children == nil {
115 n.children = make(map[byte]*node)
116 }
117 child, ok := n.children[firstByte]
118 if !ok {
119 child = &node{path: seg}
120 n.children[firstByte] = child
121 child.insert(method, rest, handlers, paramNames)
122 return
123 }

Callers 1

InsertMethod · 0.80

Calls 3

nextSegmentFunction · 0.85
longestCommonPrefixFunction · 0.85
segSepFunction · 0.85

Tested by

no test coverage detected