forked from ernesto-jimenez/gogen
/
generator.go
312 lines (278 loc) · 7.2 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
// Package exportdefault provides the functionality to automatically generate
// package-level exported functions wrapping calls to a package-level default
// instance of a type.
//
// This helps auto-generating code for the common use case where a package
// implements certain information as methods within a stub and, for
// convenience, exports functions that wrap calls to those methods on a default
// variable.
//
// Some examples of that behaviour in the stdlib:
//
// - `net/http` has `http.DefaultClient` and functions like `http.Get` just
// call the default `http.DefaultClient.Get`
// - `log` has `log.Logger` and functions like `log.Print` just call the
// default `log.std.Print`
package exportdefault
import (
"bytes"
"fmt"
"go/ast"
"go/build"
"go/doc"
"go/importer"
"go/parser"
"go/token"
"go/types"
"io"
"io/ioutil"
"path"
"regexp"
"text/template"
"github.com/ernesto-jimenez/gogen/cleanimports"
"github.com/ernesto-jimenez/gogen/imports"
)
// Generator contains the metadata needed to generate all the function wrappers
// arround methods from a package variable
type Generator struct {
Name string
Imports map[string]string
funcs []fn
FuncNamePrefix string
Include *regexp.Regexp
Exclude *regexp.Regexp
}
// New initialises a new Generator for the corresponding package's variable
//
// Returns an error if the package or variable are invalid
func New(pkg string, variable string) (*Generator, error) {
scope, docs, err := parsePackageSource(pkg)
if err != nil {
return nil, err
}
importer, funcs, err := analyzeCode(scope, docs, variable)
if err != nil {
return nil, err
}
return &Generator{
Name: docs.Name,
Imports: importer.Imports(),
funcs: funcs,
}, nil
}
// Write the generated code into the given io.Writer
//
// Returns an error if there is a problem generating the code
func (g *Generator) Write(w io.Writer) error {
buff := bytes.NewBuffer(nil)
// Generate header
if err := headerTpl.Execute(buff, g); err != nil {
return err
}
// Generate funcs
for _, fn := range g.funcs {
if g.Include != nil && !g.Include.MatchString(fn.Name) {
continue
}
if g.Exclude != nil && g.Exclude.MatchString(fn.Name) {
continue
}
fn.FuncNamePrefix = g.FuncNamePrefix
buff.Write([]byte("\n\n"))
if err := funcTpl.Execute(buff, &fn); err != nil {
return err
}
}
return cleanimports.Clean(w, buff.Bytes())
}
type fn struct {
FuncNamePrefix string
WrappedVar string
Name string
CurrentPkg string
TypeInfo *types.Func
}
func (f *fn) Qualifier(p *types.Package) string {
if p == nil || p.Name() == f.CurrentPkg {
return ""
}
return p.Name()
}
func (f *fn) Params() string {
sig := f.TypeInfo.Type().(*types.Signature)
params := sig.Params()
p := ""
comma := ""
to := params.Len()
var i int
if sig.Variadic() {
to--
}
for i = 0; i < to; i++ {
param := params.At(i)
name := param.Name()
if name == "" {
name = fmt.Sprintf("p%d", i)
}
p += fmt.Sprintf("%s%s %s", comma, name, types.TypeString(param.Type(), f.Qualifier))
comma = ", "
}
if sig.Variadic() {
param := params.At(params.Len() - 1)
name := param.Name()
if name == "" {
name = fmt.Sprintf("p%d", to)
}
p += fmt.Sprintf("%s%s ...%s", comma, name, types.TypeString(param.Type().(*types.Slice).Elem(), f.Qualifier))
}
return p
}
func (f *fn) ReturnsAnything() bool {
sig := f.TypeInfo.Type().(*types.Signature)
params := sig.Results()
return params.Len() > 0
}
func (f *fn) ReturnTypes() string {
sig := f.TypeInfo.Type().(*types.Signature)
params := sig.Results()
p := ""
comma := ""
to := params.Len()
var i int
for i = 0; i < to; i++ {
param := params.At(i)
p += fmt.Sprintf("%s %s", comma, types.TypeString(param.Type(), f.Qualifier))
comma = ", "
}
if to > 1 {
p = fmt.Sprintf("(%s)", p)
}
return p
}
func (f *fn) ForwardedParams() string {
sig := f.TypeInfo.Type().(*types.Signature)
params := sig.Params()
p := ""
comma := ""
to := params.Len()
var i int
if sig.Variadic() {
to--
}
for i = 0; i < to; i++ {
param := params.At(i)
name := param.Name()
if name == "" {
name = fmt.Sprintf("p%d", i)
}
p += fmt.Sprintf("%s%s", comma, name)
comma = ", "
}
if sig.Variadic() {
param := params.At(params.Len() - 1)
name := param.Name()
if name == "" {
name = fmt.Sprintf("p%d", to)
}
p += fmt.Sprintf("%s%s...", comma, name)
}
return p
}
// parsePackageSource returns the types scope and the package documentation from the specified package
func parsePackageSource(pkg string) (*types.Scope, *doc.Package, error) {
pd, err := build.Import(pkg, ".", 0)
if err != nil {
return nil, nil, err
}
fset := token.NewFileSet()
files := make(map[string]*ast.File)
fileList := make([]*ast.File, len(pd.GoFiles))
for i, fname := range pd.GoFiles {
src, err := ioutil.ReadFile(path.Join(pd.SrcRoot, pd.ImportPath, fname))
if err != nil {
return nil, nil, err
}
f, err := parser.ParseFile(fset, fname, src, parser.ParseComments|parser.AllErrors)
if err != nil {
return nil, nil, err
}
files[fname] = f
fileList[i] = f
}
cfg := types.Config{
Importer: importer.Default(),
}
info := types.Info{
Defs: make(map[*ast.Ident]types.Object),
}
tp, err := cfg.Check(pkg, fset, fileList, &info)
if err != nil {
return nil, nil, err
}
scope := tp.Scope()
ap, _ := ast.NewPackage(fset, files, nil, nil)
docs := doc.New(ap, pkg, doc.AllDecls|doc.AllMethods)
return scope, docs, nil
}
func analyzeCode(scope *types.Scope, docs *doc.Package, variable string) (imports.Importer, []fn, error) {
pkg := docs.Name
v, ok := scope.Lookup(variable).(*types.Var)
if v == nil {
return nil, nil, fmt.Errorf("impossible to find variable %s", variable)
}
if !ok {
return nil, nil, fmt.Errorf("%s must be a variable", variable)
}
var vType interface {
NumMethods() int
Method(int) *types.Func
}
switch t := v.Type().(type) {
case *types.Interface:
vType = t
case *types.Pointer:
vType = t.Elem().(*types.Named)
case *types.Named:
vType = t
if t, ok := t.Underlying().(*types.Interface); ok {
vType = t
}
default:
return nil, nil, fmt.Errorf("variable is of an invalid type: %T", v.Type().Underlying())
}
importer := imports.New(pkg)
var funcs []fn
for i := 0; i < vType.NumMethods(); i++ {
f := vType.Method(i)
if !f.Exported() {
continue
}
sig := f.Type().(*types.Signature)
funcs = append(funcs, fn{
WrappedVar: variable,
Name: f.Name(),
CurrentPkg: pkg,
TypeInfo: f,
})
importer.AddImportsFrom(sig.Params())
importer.AddImportsFrom(sig.Results())
}
return importer, funcs, nil
}
var headerTpl = template.Must(template.New("header").Parse(`/*
* CODE GENERATED AUTOMATICALLY WITH goexportdefault
* THIS FILE MUST NOT BE EDITED BY HAND
*
* Install goexportdefault with:
* go get github.com/ernesto-jimenez/gogen/cmd/goexportdefault
*/
package {{.Name}}
import (
{{range $path, $name := .Imports}}
{{$name}} "{{$path}}"{{end}}
)
`))
var funcTpl = template.Must(template.New("func").Parse(`// {{.FuncNamePrefix}}{{.Name}} is a wrapper around {{.WrappedVar}}.{{.Name}}
func {{.FuncNamePrefix}}{{.Name}}({{.Params}}) {{.ReturnTypes}} {
{{if .ReturnsAnything}}return {{end}}{{.WrappedVar}}.{{.Name}}({{.ForwardedParams}})
}`))