Skip to content

Commit

Permalink
Merge pull request #68 from xushiwei/req
Browse files Browse the repository at this point in the history
gopmod: PkgId
  • Loading branch information
xushiwei committed Feb 12, 2024
2 parents 39dcecd + bfcfcf4 commit 7a437d7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
24 changes: 24 additions & 0 deletions gopmod/gopmod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package gopmod

import (
"log"
"os"
"path/filepath"
"runtime"
"testing"
Expand All @@ -29,6 +30,29 @@ import (
"golang.org/x/mod/module"
)

func TestPkgId(t *testing.T) {
mod := New(modtest.GopClass(t))
if id, err := mod.PkgId(""); err != ErrInvalidPkgPath || id != "" {
t.Fatal("mod.PkgId:", id, err)
}
if id, err := mod.PkgId("."); err != ErrInvalidPkgPath || id != "" {
t.Fatal("mod.PkgId:", id, err)
}
if id, err := mod.PkgId("fmt"); err != nil || id != "fmt" {
t.Fatal("mod.PkgId fmt:", id, err)
}
if id, err := mod.PkgId("github.com/goplus/community/bar"); err != nil || id != string(os.PathSeparator)+"foo/bar" {
t.Fatal("mod.PkgId github.com/goplus/community/bar:", id, err)
}
xpath, _ := modcache.Path(module.Version{Path: "github.com/qiniu/x", Version: "v1.13.2"})
if id, err := mod.PkgId("github.com/qiniu/x/mockhttp"); err != nil || id != xpath+"/mockhttp" {
t.Fatal("mod.PkgId github.com/qiniu/x/mockhttp:", err)
}
if _, err := mod.PkgId("github.com/qiniu/y/mockhttp"); err == nil {
t.Fatal("mod.PkgId github.com/qiniu/y/mockhttp: no error?")
}
}

func TestLookup(t *testing.T) {
mod := New(modtest.GopClass(t))
if modv, ok := mod.LookupDepMod("github.com/qiniu/x"); !ok || modv.Version != "v1.13.2" {
Expand Down
36 changes: 35 additions & 1 deletion gopmod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import (
"golang.org/x/mod/module"
)

var (
ErrInvalidPkgPath = errors.New("invalid package path")
)

// -----------------------------------------------------------------------------

type Module struct {
Expand Down Expand Up @@ -84,6 +88,34 @@ func (p *Module) PkgType(pkgPath string) PkgType {
return PkgtStandard
}

// PkgId returns an unique package id of specified package.
// PkgId of a Go standard package is its package path.
// ie. PkgId("fmt") == "fmt"
func (p *Module) PkgId(pkgPath string) (string, error) {
if pkgPath == "" {
return "", ErrInvalidPkgPath
}
modPath := p.Path()
if isPkgInMod(pkgPath, modPath) {
return p.Root() + pkgPath[len(modPath):], nil
}
if pkgPath[0] == '.' { // local package: please convert it first
return "", ErrInvalidPkgPath
}
domain := pkgPath
if pos := strings.Index(pkgPath, "/"); pos > 0 {
domain = pkgPath[:pos]
}
if strings.Contains(domain, ".") {
pkg, err := p.lookupExternPkg(pkgPath)
if err != nil {
return "", err
}
return pkg.Dir, nil
}
return pkgPath, nil
}

func isPkgInMod(pkgPath, modPath string) bool {
if modPath != "" && strings.HasPrefix(pkgPath, modPath) {
suffix := pkgPath[len(modPath):]
Expand All @@ -103,7 +135,7 @@ type Package struct {
func (p *Module) Lookup(pkgPath string) (pkg *Package, err error) {
switch pt := p.PkgType(pkgPath); pt {
case PkgtStandard:
modDir := runtime.GOROOT() + "/src"
modDir := goroot + "/src"
pkg = &Package{Type: PkgtStandard, ModDir: modDir, Dir: filepath.Join(modDir, pkgPath)}
case PkgtModule:
modPath := p.Path()
Expand Down Expand Up @@ -204,4 +236,6 @@ func (e *MissingError) Error() string {
// Default represents the default gop.mod object.
var Default = New(modload.Default)

var goroot = runtime.GOROOT()

// -----------------------------------------------------------------------------

0 comments on commit 7a437d7

Please sign in to comment.