forked from cloudfoundry-attic/bosh-ext-cli
/
interface_generator.go
81 lines (65 loc) · 1.83 KB
/
interface_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
package generator
import (
"bytes"
"go/ast"
"go/format"
"go/token"
"os"
"strings"
"path/filepath"
"github.com/maxbrunsfeld/counterfeiter/model"
"golang.org/x/tools/imports"
)
type InterfaceGenerator struct {
Model *model.PackageToInterfacify
Package string
DestinationInterface string
DestinationPackageName string
}
func (ig InterfaceGenerator) GenerateInterface() (string, error) {
buf := new(bytes.Buffer)
err := format.Node(buf, token.NewFileSet(), ig.outputAST())
if err != nil {
return "", err
}
code, err := imports.Process("", buf.Bytes(), nil)
if err != nil {
panic(err)
return "", err
}
return commentLine() + "// with command: counterfeiter " + strings.Join(os.Args[1:], " ") + "\n" + prettifyCode(string(code)), nil
}
func (ig InterfaceGenerator) outputAST() *ast.File {
declarations := []ast.Decl{}
declarations = append(declarations, ig.interfaceDecl())
return &ast.File{
Name: &ast.Ident{Name: ig.DestinationPackageName},
Decls: declarations,
}
}
func (ig InterfaceGenerator) interfaceDecl() *ast.GenDecl {
fakeFilePath := filepath.Join(strings.ToLower(ig.Model.Name)+"_fake", "fake_"+strings.ToLower(ig.Model.Name)+".go")
return &ast.GenDecl{
Tok: token.TYPE,
Doc: &ast.CommentGroup{[]*ast.Comment{{Text: "//go:generate counterfeiter -o " + fakeFilePath + " . " + ig.DestinationInterface}}},
Specs: []ast.Spec{
&ast.TypeSpec{
Name: &ast.Ident{Name: ig.DestinationInterface},
Type: &ast.InterfaceType{
Methods: ig.methods(),
},
},
},
}
}
func (ig InterfaceGenerator) methods() *ast.FieldList {
fieldList := []*ast.Field{}
for _, funcDecl := range ig.Model.Funcs {
field := &ast.Field{
Names: []*ast.Ident{funcDecl.Name},
Type: funcDecl.Type,
}
fieldList = append(fieldList, field)
}
return &ast.FieldList{List: fieldList}
}