forked from kop/gowrap
/
generator.go
453 lines (365 loc) · 11.6 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package generator
import (
"bytes"
"path/filepath"
"strings"
"go/ast"
"go/token"
"io"
"text/template"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
"github.com/SergiiShapoval/gowrap/pkg"
"github.com/SergiiShapoval/gowrap/printer"
)
//Generator generates decorators for the interface types
type Generator struct {
Options
headerTemplate *template.Template
bodyTemplate *template.Template
srcPackage *packages.Package
dstPackage *packages.Package
methods methodsList
interfaceType string
}
// TemplateInputs information passed to template for generation
type TemplateInputs struct {
// Interface information for template
Interface TemplateInputInterface
// Vars additional vars to pass to the template, see Options.Vars
Vars map[string]interface{}
Imports []string
}
// RenderImports outputs a list of imports, combining the ones from
// the source file with any provided for the template itself.
func (t TemplateInputs) RenderImports(imports ...string) string {
allImports := make(map[string]struct{})
for _, i := range t.Imports {
allImports[strings.TrimSpace(i)] = struct{}{}
}
for _, i := range imports {
if i[len(i)-1] != '"' {
i = `"` + i + `"`
}
allImports[strings.TrimSpace(i)] = struct{}{}
}
var out []string
for i := range allImports {
out = append(out, i)
}
return strings.Join(out, "\n")
}
// TemplateInputInterface subset of interface information used for template generation
type TemplateInputInterface struct {
Name string
// Type of the interface, with package name qualifier (e.g. sort.Interface)
Type string
// Methods name keyed map of method information
Methods map[string]Method
}
type methodsList map[string]Method
//Options of the NewGenerator constructor
type Options struct {
//InterfaceName is a name of interface type
InterfaceName string
//Imports from the file with interface definition
Imports []string
//SourcePackage is an import path or a relative path of the package that contains the source interface
SourcePackage string
//SourcePackageAlias is an import selector defauls is source package name
SourcePackageAlias string
//OutputFile name which is used to detect destination package name and also to fix imports in the resulting source
OutputFile string
//HeaderTemplate is used to generate package clause and comment over the generated source
HeaderTemplate string
//BodyTemplate generates import section, decorator constructor and methods
BodyTemplate string
//Vars additional vars that are passed to the templates from the command line
Vars map[string]interface{}
//HeaderVars header specific variables
HeaderVars map[string]interface{}
//Funcs is a map of helper functions that can be used within a template
Funcs template.FuncMap
}
var errEmptyInterface = errors.New("interface has no methods")
var errUnexportedMethod = errors.New("unexported method")
//NewGenerator returns Generator initialized with options
func NewGenerator(options Options) (*Generator, error) {
if options.Funcs == nil {
options.Funcs = make(template.FuncMap)
}
headerTemplate, err := template.New("header").Funcs(options.Funcs).Parse(options.HeaderTemplate)
if err != nil {
return nil, errors.Wrap(err, "failed to parse header template")
}
bodyTemplate, err := template.New("body").Funcs(options.Funcs).Parse(options.BodyTemplate)
if err != nil {
return nil, errors.Wrap(err, "failed to parse body template")
}
if options.Vars == nil {
options.Vars = make(map[string]interface{})
}
fs := token.NewFileSet()
srcPackage, err := pkg.Load(options.SourcePackage)
if err != nil {
return nil, errors.Wrap(err, "failed to load source package")
}
dstPackagePath := filepath.Dir(options.OutputFile)
if !strings.HasPrefix(dstPackagePath, "/") && !strings.HasPrefix(dstPackagePath, "./") {
dstPackagePath = "./" + dstPackagePath
}
dstPackage, err := loadDestinationPackage(dstPackagePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to load destination package: %s", dstPackagePath)
}
srcPackageAST, err := pkg.AST(fs, srcPackage)
if err != nil {
return nil, errors.Wrap(err, "failed to parse source package")
}
interfaceType := srcPackage.Name + "." + options.InterfaceName
if srcPackage.PkgPath == dstPackage.PkgPath {
interfaceType = options.InterfaceName
srcPackageAST.Name = ""
} else if options.SourcePackageAlias != "" {
srcPackageAST.Name = options.SourcePackageAlias
}
methods, imports, err := findInterface(fs, srcPackageAST, options.InterfaceName)
if err != nil {
return nil, errors.Wrap(err, "failed to parse interface declaration")
}
if len(methods) == 0 {
return nil, errEmptyInterface
}
for _, m := range methods {
if srcPackageAST.Name != "" && []rune(m.Name)[0] == []rune(strings.ToLower(m.Name))[0] {
return nil, errors.Wrap(errUnexportedMethod, m.Name)
}
}
options.Imports = makeImports(imports)
return &Generator{
Options: options,
headerTemplate: headerTemplate,
bodyTemplate: bodyTemplate,
srcPackage: srcPackage,
dstPackage: dstPackage,
interfaceType: interfaceType,
methods: methods,
}, nil
}
func makeImports(imports []*ast.ImportSpec) []string {
result := make([]string, len(imports))
for _, i := range imports {
var name string
if i.Name != nil {
name = i.Name.Name
}
result = append(result, name+" "+i.Path.Value)
}
return result
}
func loadDestinationPackage(path string) (*packages.Package, error) {
dstPackage, err := pkg.Load(path)
if err != nil {
//using directory name as a package name
dstPackage, err = makePackage(path)
}
return dstPackage, err
}
var errNoPackageName = errors.New("failed to determine the destination package name")
func makePackage(path string) (*packages.Package, error) {
name := filepath.Base(path)
if name == string(filepath.Separator) || name == "." {
return nil, errNoPackageName
}
return &packages.Package{
Name: name,
}, nil
}
//Generate generates code using header and body templates
func (g Generator) Generate(w io.Writer) error {
buf := bytes.NewBuffer([]byte{})
err := g.headerTemplate.Execute(buf, map[string]interface{}{
"SourcePackage": g.srcPackage,
"Package": g.dstPackage,
"Vars": g.Options.Vars,
"Options": g.Options,
})
if err != nil {
return err
}
err = g.bodyTemplate.Execute(buf, TemplateInputs{
Interface: TemplateInputInterface{
Name: g.Options.InterfaceName,
Type: g.interfaceType,
Methods: g.methods,
},
Imports: g.Options.Imports,
Vars: g.Options.Vars,
})
if err != nil {
return err
}
processedSource, err := imports.Process(g.Options.OutputFile, buf.Bytes(), nil)
if err != nil {
return errors.Wrapf(err, "failed to format generated code:\n%s", buf)
}
_, err = w.Write(processedSource)
return err
}
var errInterfaceNotFound = errors.New("interface type declaration not found")
// findInterface looks for the interface declaration in the given directory
// and returns a list of the interface's methods and a list of imports from the file
// where interface type declaration was found
func findInterface(fs *token.FileSet, p *ast.Package, interfaceName string) (methods methodsList, imports []*ast.ImportSpec, err error) {
var found bool
var types []*ast.TypeSpec
var it *ast.InterfaceType
//looking for the source interface declaration in all files in the dir
//while doing this we also store all found type declarations to check if some of the
//interface methods use unexported types
for _, f := range p.Files {
for _, ts := range typeSpecs(f) {
types = append(types, ts)
if i, ok := ts.Type.(*ast.InterfaceType); ok {
if ts.Name.Name == interfaceName && !found {
imports = f.Imports
it = i
found = true
}
}
}
}
if !found {
return nil, nil, errors.Wrap(errInterfaceNotFound, interfaceName)
}
methods, err = processInterface(fs, it, types, p.Name, imports)
if err != nil {
return nil, nil, err
}
return methods, imports, err
}
func typeSpecs(f *ast.File) []*ast.TypeSpec {
result := []*ast.TypeSpec{}
for _, decl := range f.Decls {
if gd, ok := decl.(*ast.GenDecl); ok && gd.Tok == token.TYPE {
for _, spec := range gd.Specs {
if ts, ok := spec.(*ast.TypeSpec); ok {
result = append(result, ts)
}
}
}
}
return result
}
func processInterface(fs *token.FileSet, it *ast.InterfaceType, types []*ast.TypeSpec, typesPrefix string, imports []*ast.ImportSpec) (methods methodsList, err error) {
if it.Methods == nil {
return nil, nil
}
methods = make(methodsList, len(it.Methods.List))
for _, field := range it.Methods.List {
var embeddedMethods methodsList
var err error
switch v := field.Type.(type) {
case *ast.FuncType:
var method *Method
method, err = NewMethod(field.Names[0].Name, v, printer.New(fs, types, typesPrefix))
if err == nil {
methods[field.Names[0].Name] = *method
continue
}
case *ast.SelectorExpr:
embeddedMethods, err = processSelector(fs, v, imports)
case *ast.Ident:
embeddedMethods, err = processIdent(fs, v, types, typesPrefix, imports)
}
if err != nil {
return nil, err
}
methods, err = mergeMethods(methods, embeddedMethods)
if err != nil {
return nil, err
}
}
return methods, nil
}
func processSelector(fs *token.FileSet, se *ast.SelectorExpr, imports []*ast.ImportSpec) (methodsList, error) {
interfaceName := se.Sel.Name
packageSelector := se.X.(*ast.Ident).Name
importPath, err := importPathByPrefix(imports, packageSelector)
if err != nil {
return nil, errors.Wrapf(err, "unable to load %s.%s", packageSelector, interfaceName)
}
p, err := pkg.Load(importPath)
if err != nil {
return nil, errors.Wrap(err, "failed to load imported package")
}
astPkg, err := pkg.AST(fs, p)
if err != nil {
return nil, errors.Wrap(err, "failed to import package")
}
methods, _, err := findInterface(fs, astPkg, interfaceName)
return methods, err
}
var errDuplicateMethod = errors.New("embedded interface has same method")
//mergeMethods merges two methods list, if there is a duplicate method name
//errDuplicateMethod is returned
func mergeMethods(ml1, ml2 methodsList) (methodsList, error) {
if ml1 == nil || ml2 == nil {
return ml1, nil
}
result := make(methodsList, len(ml1)+len(ml2))
for k, v := range ml1 {
result[k] = v
}
for name, signature := range ml2 {
if _, ok := ml1[name]; ok {
return nil, errors.Wrap(errDuplicateMethod, name)
}
result[name] = signature
}
return result, nil
}
var errEmbeddedInterfaceNotFound = errors.New("embedded interface not found")
var errNotAnInterface = errors.New("embedded type is not an interface")
func processIdent(fs *token.FileSet, i *ast.Ident, types []*ast.TypeSpec, typesPrefix string, imports []*ast.ImportSpec) (methodsList, error) {
var embeddedInterface *ast.InterfaceType
for _, t := range types {
if t.Name.Name == i.Name {
var ok bool
embeddedInterface, ok = t.Type.(*ast.InterfaceType)
if !ok {
return nil, errors.Wrap(errNotAnInterface, t.Name.Name)
}
break
}
}
if embeddedInterface == nil {
return nil, errors.Wrap(errEmbeddedInterfaceNotFound, i.Name)
}
return processInterface(fs, embeddedInterface, types, typesPrefix, imports)
}
var errUnknownSelector = errors.New("unknown selector")
func importPathByPrefix(imports []*ast.ImportSpec, prefix string) (string, error) {
for _, i := range imports {
if i.Name != nil && i.Name.Name == prefix {
return unquote(i.Path.Value), nil
}
}
for _, i := range imports {
p, err := pkg.Load(unquote(i.Path.Value))
if err == nil && p.Name == prefix {
return p.PkgPath, nil
}
}
return "", errUnknownSelector
}
func unquote(s string) string {
if s[0] == '"' {
s = s[1:]
}
if s[len(s)-1] == '"' {
s = s[0 : len(s)-1]
}
return s
}