addChild appends the new `child` node to the tree using the `pattern` as the trie key. For a URL router like chi's, we split the static, param, regexp and wildcard segments into different nodes. In addition, addChild will recursively call itself until every pattern segment is added to the url patter
(child *node, prefix string)
| 233 | // into different nodes. In addition, addChild will recursively call itself until every |
| 234 | // pattern segment is added to the url pattern tree as individual nodes, depending on type. |
| 235 | func (n *node) addChild(child *node, prefix string) *node { |
| 236 | search := prefix |
| 237 | |
| 238 | // handler leaf node added to the tree is the child. |
| 239 | // this may be overridden later down the flow |
| 240 | hn := child |
| 241 | |
| 242 | // Parse next segment |
| 243 | segTyp, _, segRexpat, segTail, segStartIdx, segEndIdx := patNextSegment(search) |
| 244 | |
| 245 | // Add child depending on next up segment |
| 246 | switch segTyp { |
| 247 | |
| 248 | case ntStatic: |
| 249 | // Search prefix is all static (that is, has no params in path) |
| 250 | // noop |
| 251 | |
| 252 | default: |
| 253 | // Search prefix contains a param, regexp or wildcard |
| 254 | |
| 255 | if segTyp == ntRegexp { |
| 256 | rex, err := regexp.Compile(segRexpat) |
| 257 | if err != nil { |
| 258 | panic(fmt.Sprintf("chi: invalid regexp pattern '%s' in route param", segRexpat)) |
| 259 | } |
| 260 | child.prefix = segRexpat |
| 261 | child.rex = rex |
| 262 | } |
| 263 | |
| 264 | if segStartIdx == 0 { |
| 265 | // Route starts with a param |
| 266 | child.typ = segTyp |
| 267 | |
| 268 | if segTyp == ntCatchAll { |
| 269 | segStartIdx = -1 |
| 270 | } else { |
| 271 | segStartIdx = segEndIdx |
| 272 | } |
| 273 | if segStartIdx < 0 { |
| 274 | segStartIdx = len(search) |
| 275 | } |
| 276 | child.tail = segTail // for params, we set the tail |
| 277 | |
| 278 | if segStartIdx != len(search) { |
| 279 | // add static edge for the remaining part, split the end. |
| 280 | // its not possible to have adjacent param nodes, so its certainly |
| 281 | // going to be a static node next. |
| 282 | |
| 283 | search = search[segStartIdx:] // advance search position |
| 284 | |
| 285 | nn := &node{ |
| 286 | typ: ntStatic, |
| 287 | label: search[0], |
| 288 | prefix: search, |
| 289 | } |
| 290 | hn = child.addChild(nn, search) |
| 291 | } |
| 292 |
no test coverage detected