match on package substring
This allows godoc.org to work on nested packages. As it stood gddo would fail trying to find nested packages. Let's say we have a vaind with this route: go.mcquay.me/vain -> https://s.mcquay.me/sm/vain Asking godoc for go.mcquay.me/vain would work fine. However trying to get documentation for go.mcquay.me/vain/errors (nested package) would return 404 since it wouldn't match known paths exactly. Since now we match and return "go.mcquay.me/vain" for the full path, gddo is able to use local caches for its information. Change-Id: I599a75898493734fc652e507f477c11b1b1b13e8
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -75,16 +76,30 @@ func (m *MemDB) NSForToken(ns namespace, tok Token) error {
|
||||
// Package fetches the package associated with path.
|
||||
func (m *MemDB) Package(pth string) (Package, error) {
|
||||
m.l.RLock()
|
||||
defer m.l.RUnlock()
|
||||
|
||||
pkg, ok := m.Packages[path(pth)]
|
||||
m.l.RUnlock()
|
||||
if ok {
|
||||
return pkg, nil
|
||||
}
|
||||
|
||||
var longest Package
|
||||
for _, p := range m.Packages {
|
||||
if splitPathHasPrefix(strings.Split(pth, "/"), strings.Split(p.Path, "/")) {
|
||||
if len(p.Path) > len(longest.Path) {
|
||||
longest = p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
if !ok {
|
||||
if longest.Path == "" {
|
||||
err = verrors.HTTP{
|
||||
Message: fmt.Sprintf("couldn't find package %q", pth),
|
||||
Code: http.StatusNotFound,
|
||||
}
|
||||
}
|
||||
return pkg, err
|
||||
return longest, err
|
||||
}
|
||||
|
||||
// AddPackage adds p into packages table.
|
||||
|
||||
Reference in New Issue
Block a user