/
package.go
89 lines (79 loc) · 1.68 KB
/
package.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package typeutil
import (
"go/types"
"path"
"strings"
"github.com/hori-ryota/go-codegen/util/gocodeutil"
"golang.org/x/tools/go/loader"
)
func SamePackage(a *types.Package, b *types.Package) bool {
if a == b {
return true
}
if a == nil || b == nil {
return false
}
return a.Path() == b.Path()
}
func IsContainedInPackage(pkg *types.Package, typ types.Type) bool {
p := TypeToPackage(typ)
return SamePackage(pkg, p)
}
func TypeToPackage(typ types.Type) *types.Package {
if named, ok := typ.(*types.Named); ok {
return named.Obj().Pkg()
}
type pkger interface {
Pkg() *types.Package
}
if t, ok := typ.(pkger); ok {
return t.Pkg()
}
return nil
}
func FmtImports(pkgs []*types.Package, current *types.Package) string {
m := make(map[string]string, len(pkgs))
for _, pkg := range pkgs {
if pkg == nil {
continue
}
if current != nil && pkg.Path() == current.Path() {
continue
}
m[pkg.Name()] = pkg.Path()
}
if _, ok := m["fmt"]; !ok {
m["fmt"] = "fmt"
}
if _, ok := m["runtime"]; !ok {
m["runtime"] = "runtime"
}
return gocodeutil.FmtImports(m)
}
func AllImported(pkg *loader.PackageInfo) []*types.Package {
type hasPkg interface {
Pkg() *types.Package
}
dst := make([]*types.Package, 0, 100)
for _, file := range pkg.Files {
for _, is := range file.Imports {
pat := strings.Trim(is.Path.Value, `"`)
var name string
if is.Name != nil {
name = is.Name.Name
} else {
name = path.Base(pat)
}
dst = append(dst, types.NewPackage(pat, name))
}
}
return dst
}
func SearchPackage(pkg *loader.PackageInfo, name string) *types.Package {
for _, p := range AllImported(pkg) {
if p.Name() == name {
return p
}
}
return nil
}