-
Notifications
You must be signed in to change notification settings - Fork 10
/
generator.go
276 lines (238 loc) · 6.42 KB
/
generator.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"flag"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"go/types"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
// Generator holds the state of the analysis
type Generator struct {
// PackageOverride can be set to control the package for the output file. The default is the same package as the input interface(s).
PackageOverride string
packageName string
imports *ImportSet
interfaces map[string]*Interface
}
// LoadPackageDir parses a package in the given directory.
func LoadPackageDir(directory string) (*Generator, error) {
pkg, err := build.Default.ImportDir(directory, 0)
if err != nil {
return nil, fmt.Errorf("cannot process directory %s: %s", directory, err)
}
names := make([]string, 0, len(pkg.GoFiles)+len(pkg.CgoFiles))
names = append(names, pkg.GoFiles...)
names = append(names, pkg.CgoFiles...)
if directory != "." {
for i, name := range names {
names[i] = filepath.Join(directory, name)
}
}
return parsePackage(directory, names)
}
func parsePackage(directory string, filenames []string) (*Generator, error) {
generator := &Generator{
imports: new(ImportSet),
interfaces: make(map[string]*Interface),
}
files := make([]*ast.File, 0, len(filenames))
fileset := token.NewFileSet()
importer := defaultImporter()
for _, filename := range filenames {
if !strings.HasSuffix(filename, ".go") {
continue
}
file, err := parser.ParseFile(fileset, filename, nil, 0)
if err != nil {
return nil, fmt.Errorf("syntax error: %s", err)
}
if err := generator.processImports(file, importer); err != nil {
return nil, err
}
if err := generator.processInterfaces(file); err != nil {
return nil, err
}
files = append(files, file)
}
if len(files) == 0 {
return nil, fmt.Errorf("error: no Go files found in %s", directory)
}
// N.B. - type check the package
config := types.Config{Importer: importer, Error: func(err error) { fmt.Fprintln(os.Stderr, err) }}
pkg, err := config.Check(directory, fileset, files, nil)
if err != nil {
return nil, fmt.Errorf("type check failed")
}
generator.packageName = pkg.Name()
return generator, nil
}
func (g *Generator) processImports(file *ast.File, importer types.Importer) error {
for _, spec := range file.Imports {
path, err := strconv.Unquote(spec.Path.Value)
if err != nil {
return err
}
pkg, err := importer.Import(path)
if err != nil {
return err
}
g.processImport(spec, pkg)
if err := g.processImportInterfaces(pkg); err != nil {
return err
}
}
return nil
}
func (g *Generator) processImport(spec *ast.ImportSpec, pkg *types.Package) {
decl := &Import{
Name: pkg.Name(),
Path: spec.Path.Value,
}
if spec.Name == nil {
g.imports.Add(decl)
return
}
switch spec.Name.Name {
case "_":
break
case ".":
decl.Required = true
decl.Alias = "."
default:
decl.Alias = spec.Name.Name
}
g.imports.Add(decl)
}
func (g *Generator) processImportInterfaces(pkg *types.Package) error {
for _, name := range pkg.Scope().Names() {
obj := pkg.Scope().Lookup(name)
qname := fmt.Sprintf("%s.%s", pkg.Name(), obj.Name())
if _, exists := g.interfaces[qname]; exists {
continue
}
if _, isType := obj.(*types.TypeName); !isType || !obj.Exported() || !types.IsInterface(obj.Type()) {
continue
}
ifType := obj.Type().Underlying().(*types.Interface)
decl := &Interface{
Name: obj.Name(),
}
for i := 0; i < ifType.NumMethods(); i++ {
m := ifType.Method(i)
if !m.Exported() {
continue
}
if err := decl.addMethodFromType(m, g.imports); err != nil {
return err
}
}
g.interfaces[qname] = decl
}
return nil
}
func (g *Generator) processInterfaces(file *ast.File) error {
for _, node := range file.Decls {
gen, ok := node.(*ast.GenDecl)
if !ok || gen.Tok != token.TYPE {
continue
}
spec := gen.Specs[0].(*ast.TypeSpec)
ifType, ok := spec.Type.(*ast.InterfaceType)
if !ok {
continue
}
decl, err := g.processInterface(spec.Name.Name, ifType)
if err != nil {
return err
}
g.interfaces[spec.Name.Name] = decl
}
return nil
}
func (g *Generator) processInterface(name string, ifType *ast.InterfaceType) (*Interface, error) {
decl := &Interface{
Name: name,
}
for _, field := range ifType.Methods.List {
switch f := field.Type.(type) {
case *ast.FuncType:
if err := decl.addMethodFromField(field, g.imports); err != nil {
return nil, err
}
case *ast.Ident:
// N.B. - embedded interface from current package
decl.embeds = append(decl.embeds, f.Name)
case *ast.SelectorExpr:
// N.B. - embedded interface from imported package
decl.embeds = append(decl.embeds, fmt.Sprintf("%s.%s", f.X.(*ast.Ident).String(), f.Sel.String()))
default:
return nil, fmt.Errorf("internal error: unsupported interface field: %#v", field.Type)
}
}
return decl, nil
}
// Generate produces the charlatan source file data for the named interfaces.
func (g *Generator) Generate(interfaceNames []string) ([]byte, error) {
decls := make([]*Interface, 0, len(interfaceNames))
for _, name := range interfaceNames {
decl, ok := g.interfaces[name]
if !ok {
return nil, fmt.Errorf("error: interface %q not found", name)
}
if len(decl.Methods) == 0 {
log.Printf("warning: ignoring empty interface %q\n", decl.Name)
continue
}
if decl.Name == "_" {
log.Println(`warning: ignorning interface named "_"`)
continue
}
decls = append(decls, decl)
if len(decl.embeds) == 0 {
continue
}
embeddedMethods := []*Method{}
for _, embedName := range decl.embeds {
embed, ok := g.interfaces[embedName]
if !ok {
return nil, fmt.Errorf("error: interface %q embedded in %s not found", embedName, name)
}
for _, m := range embed.Methods {
c := *m
c.Interface = decl.Name
embeddedMethods = append(embeddedMethods, &c)
}
}
decl.Methods = append(embeddedMethods, decl.Methods...)
}
if len(decls) == 0 {
return nil, fmt.Errorf("error: no valid interface names provided")
}
packageName := g.packageName
if g.PackageOverride != "" {
packageName = g.PackageOverride
}
var argv strings.Builder
argv.WriteString("charlatan")
flag.Visit(func(f *flag.Flag) {
fmt.Fprintf(&argv, " -%s=%s", f.Name, f.Value)
})
if flag.NArg() > 0 {
argv.WriteByte(' ')
argv.WriteString(strings.Join(flag.Args(), " "))
}
tmpl := charlatanTemplate{
CommandLine: argv.String(),
PackageName: packageName,
Imports: g.imports.GetRequired(),
Interfaces: decls,
}
return tmpl.execute()
}