Skip to content
Permalink
Browse files

internal/{code,mod}: use more packages from golang.org/x/mod

These packages were previously not available in golang.org/x/mod.
By now, they are. Start using them since they're more canonical.

Change internal language describing operations on module paths
and versions from "encode and decode" to "escape and unescape",
to mirror language used in package golang.org/x/mod/module.
  • Loading branch information...
dmitshur committed Nov 3, 2019
1 parent 86354fc commit dce9a6ea048c9ad7a398c1241643d17ec9436c6c
Showing with 18 additions and 18 deletions.
  1. +15 −15 internal/code/module.go
  2. +1 −1 internal/code/module_test.go
  3. +2 −2 internal/mod/mod.go
@@ -17,11 +17,11 @@ import (
"syscall"
"time"

"github.com/rogpeppe/go-internal/modfile"
"github.com/rogpeppe/go-internal/module"
"github.com/shurcooL/go/vfs/godocfs/vfsutil"
"github.com/shurcooL/home/internal/mod"
"github.com/shurcooL/httperror"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"sourcegraph.com/sourcegraph/go-vcs/vcs"
"sourcegraph.com/sourcegraph/go-vcs/vcs/git"
)
@@ -66,11 +66,11 @@ func (h ModuleHandler) ServeModule(w http.ResponseWriter, req *http.Request) err
if !ok {
return os.ErrNotExist
}
dec, ok := r.Decode() // Decode module path and version.
unesc, ok := r.Unescape() // Unescape module path and version.
if !ok {
return httperror.BadRequest{Err: fmt.Errorf("failed to decode module path=%q and/or version=%q", r.Module, r.Version)}
return httperror.BadRequest{Err: fmt.Errorf("failed to unescape module path=%q and/or version=%q", r.Module, r.Version)}
}
modulePath, typ, version := dec.Module, dec.Type, dec.Version
modulePath, typ, version := unesc.Module, unesc.Type, unesc.Version

// Look up code directory by module path.
d, ok := h.Code.Lookup(modulePath)
@@ -257,15 +257,15 @@ func readLine(b *[]byte) string {
}

// moduleProxyRequest represents a module proxy request.
// The Module and Version fields may be encoded or unencoded.
// The Module and Version fields may be escaped or unescaped.
type moduleProxyRequest struct {
Module string // Module path.
Type string // Type of request. One of "list", "info", "mod", or "zip".
Version string // Module version. Applies only when Type is not "list".
}

// parseModuleProxyRequest parses the module proxy request
// from the given URL. It does not attempt to decode the
// from the given URL. It does not attempt to unescape the
// module path and version, the caller is responsible for that.
func parseModuleProxyRequest(url string) (_ moduleProxyRequest, ok bool) {
// Split "<module>/@v/<file>" into module and file.
@@ -296,34 +296,34 @@ func parseModuleProxyRequest(url string) (_ moduleProxyRequest, ok bool) {
}
}

// Decode returns a copy of r with Module and Version fields decoded.
func (r moduleProxyRequest) Decode() (_ moduleProxyRequest, ok bool) {
// Unescape returns a copy of r with Module and Version fields unescaped.
func (r moduleProxyRequest) Unescape() (_ moduleProxyRequest, ok bool) {
var err error
r.Module, err = module.DecodePath(r.Module)
r.Module, err = module.UnescapePath(r.Module)
if err != nil {
return moduleProxyRequest{}, false
}
if r.Type == "list" {
return r, true
}
r.Version, err = module.DecodeVersion(r.Version)
r.Version, err = module.UnescapeVersion(r.Version)
if err != nil {
return moduleProxyRequest{}, false
}
return r, true
}

// Encode returns a copy of r with Module and Version fields encoded.
func (r moduleProxyRequest) Encode() (_ moduleProxyRequest, ok bool) {
// Escape returns a copy of r with Module and Version fields escaped.
func (r moduleProxyRequest) Escape() (_ moduleProxyRequest, ok bool) {
var err error
r.Module, err = module.EncodePath(r.Module)
r.Module, err = module.EscapePath(r.Module)
if err != nil {
return moduleProxyRequest{}, false
}
if r.Type == "list" {
return r, true
}
r.Version, err = module.EncodeVersion(r.Version)
r.Version, err = module.EscapeVersion(r.Version)
if err != nil {
return moduleProxyRequest{}, false
}
@@ -9,10 +9,10 @@ import (
"path/filepath"
"testing"

"github.com/rogpeppe/go-internal/dirhash"
"github.com/shurcooL/home/httputil"
"github.com/shurcooL/home/internal/code"
"github.com/shurcooL/home/internal/mod"
"golang.org/x/mod/sumdb/dirhash"
)

func TestModuleHandler(t *testing.T) {
@@ -11,8 +11,8 @@ import (
"strings"
"time"

"github.com/rogpeppe/go-internal/dirhash"
"github.com/rogpeppe/go-internal/semver"
"golang.org/x/mod/semver"
"golang.org/x/mod/sumdb/dirhash"
)

// A RevInfo describes a single revision in a module repository.

0 comments on commit dce9a6e

Please sign in to comment.
You can’t perform that action at this time.