match finds the documented path template for a request. Literal segments beat parameters, so /repos/{owner}/{repo}/issues beats repos/{owner}/{repo}/{x} if both existed. Between templates that tie, such as /orgs/{org}/attestations/{attestation_id} and orgs/{org}/attestations/{subject_digest}, the on
(method, path string)
| 96 | // /orgs/{org}/attestations/{subject_digest}, the one that documents the |
| 97 | // method wins. |
| 98 | func (idx *restIndex) match(method, path string) *restRoute { |
| 99 | parts := splitPath(path) |
| 100 | var best []*restRoute |
| 101 | bestScore := -1 |
| 102 | for i := range idx.routes { |
| 103 | score, ok := idx.routes[i].score(parts) |
| 104 | switch { |
| 105 | case !ok || score < bestScore: |
| 106 | case score > bestScore: |
| 107 | best, bestScore = []*restRoute{&idx.routes[i]}, score |
| 108 | default: |
| 109 | best = append(best, &idx.routes[i]) |
| 110 | } |
| 111 | } |
| 112 | for _, r := range best { |
| 113 | if r.item != nil && r.item.GetOperation(method) != nil { |
| 114 | return r |
| 115 | } |
| 116 | } |
| 117 | if len(best) == 0 { |
| 118 | return nil |
| 119 | } |
| 120 | return best[0] |
| 121 | } |
| 122 | |
| 123 | func (r *restRoute) score(parts []string) (int, bool) { |
| 124 | segs := r.segments |