forked from sasha-s/goimpl
/
goimpl.go
378 lines (352 loc) · 9.7 KB
/
goimpl.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
// A tool to generate stub implementation of an interface.
package goimpl
import (
"bytes"
"errors"
"fmt"
"go/parser"
"go/printer"
"go/token"
"io"
"reflect"
"strings"
"text/template"
"unicode"
"golang.org/x/net/context"
"golang.org/x/tools/imports"
)
// GenOpts specifies code generation options.
type GenOpts struct {
PkgName string // target package.
ImplName string // type (struct) that would implement the interface.
Inter reflect.Type // Interface to implement.
Existing interface{} // Existing type that we want to implement the interface.
NoNamedReturnValues bool // Do not generate named return values. The generated code might not compiple if this is set.
MethodBlacklist map[string]struct{} // Would not generate the code for those methods.
Comments map[string]string // Add comments to those methods in generated code.
NoGoImports bool // No goimports if set. Faster. The generated code might not compile.
Extra []string // Extra imports.
}
// Generate an empty implementation of the interface as specified in opts and write the result to out.
func Generate(opts *GenOpts, out io.Writer) error {
if opts.MethodBlacklist == nil {
opts.MethodBlacklist = map[string]struct{}{}
}
if opts.Comments == nil {
opts.Comments = map[string]string{}
}
if err := opts.handleExisting(); err != nil {
return err
}
if opts.PkgName == "" {
opts.PkgName, _ = packageAndName(opts.Inter)
}
buf := new(bytes.Buffer)
if err := tm.Execute(buf, opts); err != nil {
return err
}
// Parse it back.
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, "dummy.go", buf, parser.ParseComments)
if err != nil {
return fmt.Errorf("Error parsing generated code: %s", err.Error())
}
b := bytes.NewBuffer([]byte{})
// Print.
cfg := &printer.Config{
Mode: printer.UseSpaces | printer.TabIndent,
Tabwidth: 8,
}
if err = cfg.Fprint(b, fset, astFile); err != nil {
return err
}
var bts []byte
if opts.NoGoImports {
bts = b.Bytes()
} else if bts, err = imports.Process("dummy.go", b.Bytes(), nil); err != nil {
return errors.New("Error fixing imports: " + err.Error())
}
_, err = out.Write(bts)
return err
}
func (opts *GenOpts) handleExisting() error {
if opts.Existing == nil {
return nil
}
if opts.ImplName != "" {
return errors.New("only one of ImplName and existing should be set.")
}
if opts.PkgName != "" {
return errors.New("only one of PkgName and existing should be set.")
}
et := reflect.TypeOf(opts.Existing)
em := opts.Methods(et)
for i, mtd := range em {
em[i].Inputs = mtd.Inputs[1:]
}
mtds := opts.Methods(opts.Inter)
eMap := toMap(em)
rMap := toMap(mtds)
for k, v := range rMap {
w, ok := eMap[v.Name]
if !ok {
continue
}
d := w.Diff(*v)
if d != "" {
comm, _ := opts.Comments[k]
if comm != "" {
comm = comm + " "
}
opts.Comments[k] = comm + d
} else {
// Method exists and has a right signature.
opts.MethodBlacklist[k] = struct{}{}
}
}
if et.Kind() == reflect.Ptr {
var name string
opts.PkgName, name = packageAndName(et.Elem())
opts.ImplName = "*" + name
} else {
opts.PkgName, opts.ImplName = packageAndName(et)
}
return nil
}
// Arg describes an argument of a method: either in or out.
type Arg struct {
reflect.Type
ArgName string // Name for a variable for this arg.
Sep string // Separator - empty if it the last arg in a list, comma otherwise.
}
// Method.
type Method struct {
reflect.Method
Inputs []Arg
Outputs []Arg
Comment string
}
func toMap(m []Method) map[string]*Method {
r := map[string]*Method{}
for i := range m {
r[m[i].Name] = &m[i]
}
return r
}
// Diff returns an empty string if the methods have same signature.
// Returns a description of the diff oftherwise.
func (m Method) Diff(other Method) string {
if m.Name != other.Name {
// Does not really happen.
return fmt.Sprintf("names are different: %s != %s", m.Name, other.Name)
}
a := diff("input", m.Inputs, other.Inputs)
b := diff("output", m.Outputs, other.Outputs)
if a != "" && b != "" {
return a + "; " + b
}
return a + b
}
func diff(n string, a, b []Arg) string {
if len(a) != len(b) {
return fmt.Sprintf("number of %ss: had %d, want %d", n, len(a), len(b))
}
ds := []string{}
for i := range a {
if a[i].String() != b[i].String() {
ds = append(ds, fmt.Sprintf("%ss[%d]: had `%s` want `%s`", n, i, a[i].String(), b[i].String()))
}
}
return strings.Join(ds, "; ")
}
// Methods populates a list of methods for a given reflect type (which is supposed to be an interface).
func (opts *GenOpts) Methods(it reflect.Type) []Method {
m := make([]Method, 0, it.NumMethod())
rec := opts.First(opts.ImplName)
for i := 0; i < it.NumMethod(); i++ {
name := it.Method(i).Name
if _, ok := opts.MethodBlacklist[name]; !ok {
mtd := opts.Method(rec, it.Method(i))
if c, ok := opts.Comments[name]; ok {
mtd.Comment = c
}
m = append(m, mtd)
}
}
return m
}
// Method populates the Method struct.
// recName is a name of the receiver in the generated code.
func (opts *GenOpts) Method(recName string, ft reflect.Method) Method {
cur := map[string]struct{}{recName: struct{}{}} // Current names. Start with the name of receiver.
inp := make([]Arg, ft.Type.NumIn())
last := len(inp) - 1
for i := range inp {
t := ft.Type.In(i)
sep := ", "
if i == last {
sep = ""
}
inp[i] = Arg{Type: t, ArgName: opts.Short(t, cur), Sep: sep}
}
out := make([]Arg, ft.Type.NumOut())
last = len(out) - 1
for i := range out {
t := ft.Type.Out(i)
sep := ", "
if i == last {
sep = ""
}
out[i] = Arg{Type: t, ArgName: opts.Short(t, cur), Sep: sep}
}
return Method{Inputs: inp, Outputs: out, Method: ft}
}
// Clean keeps only letters.
func (opts *GenOpts) Clean(s string) string {
rs := []rune(s)
res := make([]rune, 0, len(rs))
for _, r := range rs {
if !unicode.IsLetter(r) {
continue
}
res = append(res, r)
}
return string(res)
}
// Short returns a unique (in the current scope) name for the argument of type t.
func (opts *GenOpts) Short(t reflect.Type, cur map[string]struct{}) string {
tt := t
for tt.Kind() == reflect.Ptr || tt.Kind() == reflect.Slice {
tt = tt.Elem()
}
pkg, name := packageAndName(tt)
f := opts.First(name) // First letter.
// Handle common types.
switch {
case t.ConvertibleTo(errorType):
f = "err"
case t.ConvertibleTo(ctxType):
f = "ctx"
default:
n, clean := opts.lowerName(name)
// Very short names.
if len(n) <= 3 && string(n) != clean && string(n) != pkg {
f = string(n)
}
}
// Make sure the name is unique.
name = f
for c := 1; ; c++ {
if _, ok := cur[name]; !ok {
// Update the set of currently used names.
cur[name] = struct{}{}
return name
}
name = fmt.Sprintf("%s%d", f, c)
}
}
func (opts *GenOpts) lowerName(s string) ([]rune, string) {
parts := strings.Split(s, ".")
if len(parts) == 0 {
return []rune("u"), ""
}
clean := opts.Clean(parts[len(parts)-1])
rs := []rune(clean)
for i, r := range rs {
rs[i] = unicode.ToLower(r)
}
return rs, clean
}
// First returns a first letter of s in lowercase.
func (GenOpts) First(s string) string {
parts := strings.Split(s, ".")
if len(parts) == 0 {
return "u"
}
for _, r := range []rune(parts[len(parts)-1]) {
if unicode.IsLetter(r) {
return string([]rune{unicode.ToLower(r)})
}
}
return "z"
}
// GetName of a type.
func (opts *GenOpts) GetName(t reflect.Type) string {
name := t.Name()
if name != "" {
pkg, _ := packageAndName(t)
// Handle the case the type is in the package we are generating code for.
if pkg == "" || pkg == opts.PkgName {
return name
}
return fmt.Sprintf("%s.%s", pkg, name)
}
switch t.Kind() {
case reflect.Ptr:
return fmt.Sprintf("*%s", opts.GetName(t.Elem()))
case reflect.Map:
return fmt.Sprintf("map[%s]%s", opts.GetName(t.Key()), opts.GetName(t.Elem()))
case reflect.Slice:
return fmt.Sprintf("[]%s", opts.GetName(t.Elem()))
case reflect.Chan:
return fmt.Sprintf("%s %s", t.ChanDir().String(), opts.GetName(t.Elem()))
case reflect.Array:
return fmt.Sprintf("[%d]%s", t.Len(), opts.GetName(t.Elem()))
case reflect.Func:
inputs := make([]string, t.NumIn())
for i := range inputs {
inputs[i] = opts.GetName(t.In(i))
}
outputs := make([]string, t.NumOut())
for i := range outputs {
outputs[i] = opts.GetName(t.Out(i))
}
out := strings.Join(outputs, ", ")
if len(outputs) > 1 {
out = fmt.Sprintf("(%s)", out)
}
return fmt.Sprintf("func (%s) %s", strings.Join(inputs, ", "), out)
default:
return t.String()
}
}
func packageAndName(t reflect.Type) (pkgName, name string) {
pkgPath := t.PkgPath()
if pkgPath == "" {
return "", t.String()
}
parts := strings.Split(t.String(), ".")
if len(parts) == 0 {
return
}
if len(parts) > 1 {
name = parts[1]
}
return parts[0], name
}
var errorType = reflect.TypeOf((*error)(nil)).Elem()
var ctxType = reflect.TypeOf((*context.Context)(nil)).Elem()
const templateS = `
{{$R := .}}
package {{.PkgName}}
import (
"errors"
{{range .Extra}}"{{.}}"
{{end}})
type {{.Clean .ImplName}} struct{}
{{$rec := .First .ImplName}}
{{range $R.Methods .Inter}}
{{if .Comment}}// {{ .Comment}} {{end}}
func ({{$rec}} {{$R.ImplName}}) {{.Name}} ({{range .Inputs}} {{.ArgName}} {{$R.GetName .}} {{.Sep}} {{end}}) ({{range .Outputs}} {{if not $R.NoNamedReturnValues}} {{.ArgName}} {{end}} {{$R.GetName .}} {{.Sep}} {{end}}) {
panic(errors.New("Not implemented")) }
{{end}}
`
var _ = `{{if $R.NoNamedReturnValues}} {{range .Inputs}} _ {{if eq .Sep ""}} = {{else}} {{.Sep}} {{end}} {{end}} {{range .Inputs}} {{.ArgName}} {{.Sep}} {{end}}
{{end}}`
var tm = template.New("impl")
func init() {
_, err := tm.Parse(templateS)
if err != nil {
panic(err)
}
}