Remove unregisters registered route
(method string, path string)
| 322 | |
| 323 | // Remove unregisters registered route |
| 324 | func (r *DefaultRouter) Remove(method string, path string) error { |
| 325 | currentNode := r.tree |
| 326 | if currentNode == nil || (currentNode.isLeaf && !currentNode.isHandler) { |
| 327 | return errors.New("router has no routes to remove") |
| 328 | } |
| 329 | |
| 330 | if path == "" { |
| 331 | path = "/" |
| 332 | } |
| 333 | if path[0] != '/' { |
| 334 | path = "/" + path |
| 335 | } |
| 336 | |
| 337 | var nodeToRemove *node |
| 338 | prefixLen := 0 |
| 339 | for { |
| 340 | if currentNode.originalPath == path && currentNode.isHandler { |
| 341 | nodeToRemove = currentNode |
| 342 | break |
| 343 | } |
| 344 | if currentNode.kind == staticKind { |
| 345 | prefixLen = prefixLen + len(currentNode.prefix) |
| 346 | } else { |
| 347 | prefixLen = len(currentNode.originalPath) |
| 348 | } |
| 349 | |
| 350 | if prefixLen >= len(path) { |
| 351 | break |
| 352 | } |
| 353 | |
| 354 | next := path[prefixLen] |
| 355 | switch next { |
| 356 | case paramLabel: |
| 357 | currentNode = currentNode.paramChild |
| 358 | case anyLabel: |
| 359 | currentNode = currentNode.anyChild |
| 360 | default: |
| 361 | currentNode = currentNode.findStaticChild(next) |
| 362 | } |
| 363 | |
| 364 | if currentNode == nil { |
| 365 | break |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if nodeToRemove == nil { |
| 370 | return errors.New("could not find route to remove by given path") |
| 371 | } |
| 372 | |
| 373 | if !nodeToRemove.isHandler { |
| 374 | return errors.New("could not find route to remove by given path") |
| 375 | } |
| 376 | |
| 377 | if mh := nodeToRemove.methods.find(method, false); mh == nil { |
| 378 | return errors.New("could not find route to remove by given path and method") |
| 379 | } |
| 380 | nodeToRemove.setHandler(method, nil) |
| 381 |
nothing calls this directly
no test coverage detected