/
types.go
173 lines (141 loc) · 3.52 KB
/
types.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package types
import (
"errors"
"flag"
"fmt"
"go/build"
"go/types"
"reflect"
"strings"
"github.com/nu50218/impls/command"
"github.com/nu50218/impls/impls"
"golang.org/x/tools/go/packages"
)
const name = "types"
var Command (command.Command) = &c{}
var flagSet = flag.NewFlagSet(name, flag.ExitOnError)
// オプション
var (
exported bool
flagIncludeTest bool
)
func init() {
flagSet.Usage = func() {
fmt.Printf("Usage of %s:\n", name)
fmt.Println()
fmt.Println("Examples:")
fmt.Println(" $ impls types go/ast.Expr")
fmt.Println(" $ impls types io.Writer net/http")
fmt.Println()
fmt.Println("Options:")
flagSet.PrintDefaults()
}
flagSet.BoolVar(&exported, "exported", false, "only exported")
flagSet.BoolVar(&flagIncludeTest, "t", false, "include test package")
}
type c struct{}
func (*c) Name() string {
return name
}
func (*c) Description() string {
return "find all types by interface"
}
func (*c) FlagSet() *flag.FlagSet {
return flagSet
}
// Run 下のような感じで動かしたい
// $ impls types [options] io.Writer
// $ impls types [options] io.Writer io
// $ impls types [options] io.Writer ./... io
func (*c) Run(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
flagArgs := flagSet.Args()
if len(flagArgs) < 1 {
return errors.New("invalid arguments")
}
target := flagArgs[0]
var targetPkgPath string
loadPkgs := flagArgs[1:]
if strings.Contains(target, ".") {
targetPkgPath = target[:strings.LastIndex(target, ".")]
loadPkgs = append(loadPkgs, targetPkgPath)
}
type pkgPathsResponse struct {
paths map[string]struct{}
err error
}
pkgPathsChan := make(chan *pkgPathsResponse, 1)
go func() {
paths, err := impls.PkgPaths(flagArgs[1:]...)
pkgPathsChan <- &pkgPathsResponse{paths: paths, err: err}
}()
pkgs, err := impls.LoadPkgs(flagIncludeTest, loadPkgs...)
if err != nil {
return err
}
checkPkgIncludedResp := <-pkgPathsChan
paths, err := checkPkgIncludedResp.paths, checkPkgIncludedResp.err
if err != nil {
return err
}
i, err := findInterface(target, pkgs)
if err != nil {
return err
}
covered := map[string]struct{}{}
for _, pkg := range pkgs {
if _, ok := paths[pkg.Types.Path()]; !ok && len(flagArgs) != 1 {
continue
}
scope := pkg.Types.Scope()
for _, n := range scope.Names() {
obj := scope.Lookup(n)
if exported && !obj.Exported() {
continue
}
t, _ := obj.(*types.TypeName)
if t == nil {
continue
}
pos := pkg.Fset.Position(obj.Pos()).String()
if _, ok := covered[pos]; ok {
continue
}
if impls.Implements(obj.Type(), i) {
covered[pos] = struct{}{}
fmt.Println(pos, pkg.Types.Name()+"."+obj.Name())
}
}
}
return nil
}
func findInterface(s string, pkgs []*packages.Package) (*types.Interface, error) {
if s == "error" {
errType, _ := types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
return errType, nil
}
if !strings.Contains(s, ".") {
return nil, errors.New("invalid syntax")
}
lastComma := strings.LastIndex(s, ".")
ifacePath := s[:lastComma]
ifaceName := s[lastComma+1:]
buildPkg, err := build.Default.Import(ifacePath, ".", build.ImportMode(0))
if err != nil {
return nil, err
}
for _, pkg := range pkgs {
obj := pkg.Types.Scope().Lookup(ifaceName)
if obj == nil || reflect.ValueOf(obj).IsNil() || obj.Pkg().Path() != buildPkg.ImportPath {
continue
}
i, err := impls.UnderlyingInterface(obj.Type())
if err != nil {
return nil, err
}
return i, nil
}
return nil, errors.New("not found")
}