-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
template.go
417 lines (375 loc) · 11.1 KB
/
template.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
// Reads the templates and writes the substituted templates
package main
import (
"bytes"
"fmt"
"go/ast"
"go/build"
"go/format"
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"golang.org/x/tools/imports"
"golang.org/x/tools/go/packages"
)
var testingMode = false
const (
genHeader = "// Code generated by gotemplate. DO NOT EDIT.\n\n"
)
// Holds the desired template
type template struct {
Package string
Name string
Args []string
NewPackage string
Dir string
templateName string
templateArgs []string
templateArgsMap map[string]string
mappings map[types.Object]string
newIsPublic bool
inputFile string
}
// findPackageName reads all the go packages in the curent directory
// and finds which package they are in
func findPackageName() string {
p, err := build.Default.Import(".", ".", build.ImportMode(0))
if err != nil {
fatalf("Failed to read packages in current directory: %v", err)
}
return p.Name
}
// init the template instantiation
func newTemplate(dir, pkg, templateArgsString string) *template {
name, templateArgs := parseTemplateAndArgs(templateArgsString)
return &template{
Package: pkg,
Name: name,
Args: templateArgs,
Dir: dir,
mappings: make(map[types.Object]string),
NewPackage: findPackageName(),
templateArgsMap: make(map[string]string),
}
}
// Add a mapping for identifier
func (t *template) addMapping(object types.Object, name string) {
replacementName := ""
if !strings.Contains(name, t.templateName) {
// If name doesn't contain template name then just prefix it
innerName := strings.ToUpper(t.Name[:1]) + t.Name[1:]
replacementName = name + innerName
debugf("Top level definition '%s' doesn't contain template name '%s', using '%s'", name, t.templateName, replacementName)
} else {
// make sure the new identifier will follow
// Go casing style (newMySet not newmySet).
innerName := t.Name
if strings.Index(name, t.templateName) != 0 {
innerName = strings.ToUpper(innerName[:1]) + innerName[1:]
}
replacementName = strings.Replace(name, t.templateName, innerName, 1)
}
// If new template name is not public then make sure
// the exported name is not public too
if !t.newIsPublic && ast.IsExported(replacementName) {
replacementName = strings.ToLower(replacementName[:1]) + replacementName[1:]
}
t.mappings[object] = replacementName
}
// Parse the arguments string Template(A, B, C)
func parseTemplateAndArgs(s string) (name string, args []string) {
expr, err := parser.ParseExpr(s)
if err != nil {
fatalf("Failed to parse %q: %v", s, err)
}
debugf("expr = %#v\n", expr)
callExpr, ok := expr.(*ast.CallExpr)
if !ok {
fatalf("Failed to parse %q: expecting Identifier(...)", s)
}
debugf("fun = %#v", callExpr.Fun)
fn, ok := callExpr.Fun.(*ast.Ident)
if !ok {
fatalf("Failed to parse %q: expecting Identifier(...)", s)
}
name = fn.Name
for i, arg := range callExpr.Args {
var buf bytes.Buffer
debugf("arg[%d] = %#v", i, arg)
err = format.Node(&buf, token.NewFileSet(), arg)
if err != nil {
fatalf("Failed to format %q: %v", s, err)
}
s := buf.String()
debugf("parsed = %q", s)
args = append(args, s)
}
return
}
// "template type Set(A)"
var matchTemplateType = regexp.MustCompile(`^//\s*template\s+type\s+(\w+\s*.*?)\s*$`)
func (t *template) findTemplateDefinition(f *ast.File) {
// Inspect the comments
t.templateName = ""
t.templateArgs = nil
for _, cg := range f.Comments {
for _, x := range cg.List {
matches := matchTemplateType.FindStringSubmatch(x.Text)
if matches != nil {
if t.templateName != "" {
fatalf("Found multiple template definitions in %s", t.inputFile)
}
t.templateName, t.templateArgs = parseTemplateAndArgs(matches[1])
}
}
}
if t.templateName == "" {
fatalf("Didn't find template definition in %s", t.inputFile)
}
if len(t.templateArgs) != len(t.Args) {
fatalf("Wrong number of arguments - template is expecting %d but %d supplied", len(t.Args), len(t.templateArgs))
}
for i, to := range t.Args {
t.templateArgsMap[t.templateArgs[i]] = to
}
debugf("templateName = %v, templateArgs = %v", t.templateName, t.templateArgs)
}
// Parses a file into a Fileset and Ast
//
// Dies with a fatal error on error
func parseFile(path string, src interface{}) (*token.FileSet, *ast.File) {
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, path, src, parser.ParseComments)
if err != nil {
fatalf("Failed to parse file: %s", err)
}
return fset, f
}
// Replace the identifers in f
func replaceIdentifier(f *ast.File, info *types.Info, old types.Object, new string) {
// We replace the identifier name with a string
// which is a bit untidy if we weren't
// replacing with an identifier
for id, obj := range info.Defs {
if obj == old {
id.Name = new
}
}
for id, obj := range info.Uses {
if obj == old {
id.Name = new
} else {
if var_, ok := obj.(*types.Var); ok && var_.Anonymous() {
// This is an anonymous field in composite literal
// We should replace it if we replace a type it represents
if named, ok := var_.Type().(*types.Named); ok && named.Obj() == old {
id.Name = new
}
}
}
}
}
// Parses the template file
func (t *template) parse(inputFile string) {
t.inputFile = inputFile
// Make the name mappings
t.newIsPublic = ast.IsExported(t.Name)
conf := &packages.Config{
Mode: packages.LoadSyntax,
}
pkgs, err := packages.Load(conf, inputFile)
if err != nil {
fatalf("Type checking error: %v", err)
}
pkg := pkgs[0]
if len(pkg.Errors) > 0 {
fatalf("Type checking error: %v", pkg.Errors[0])
}
info := pkg.TypesInfo
fset := pkg.Fset
f := pkg.Syntax[0]
t.findTemplateDefinition(f)
// debugf("Decls = %#v", f.Decls)
// Find names which need to be adjusted
namesToMangle := map[types.Object]string{}
newDecls := []ast.Decl{}
for _, decl := range f.Decls {
remove := false
switch d := decl.(type) {
case *ast.GenDecl:
// A general definition
switch d.Tok {
case token.IMPORT:
// Ignore imports
case token.CONST, token.VAR:
// Find and remove identifiers found in template
// params
emptySpecs := []int{}
for i, spec := range d.Specs {
namesToRemove := []int{}
v := spec.(*ast.ValueSpec)
for j, name := range v.Names {
debugf("VAR or CONST %v", name.Name)
def := info.Defs[name]
if _, ok := t.templateArgsMap[name.Name]; ok {
namesToRemove = append(namesToRemove, j)
t.mappings[def] = t.templateArgsMap[name.Name]
} else {
namesToMangle[def] = name.Name
}
}
// Shuffle the names to remove out of v.Names and v.Values
for i := len(namesToRemove) - 1; i >= 0; i-- {
p := namesToRemove[i]
v.Names = append(v.Names[:p], v.Names[p+1:]...)
v.Values = append(v.Values[:p], v.Values[p+1:]...)
}
// If empty then add to slice to remove later
if len(v.Names) == 0 {
emptySpecs = append(emptySpecs, i)
}
}
// Remove now-empty specs
for i := len(emptySpecs) - 1; i >= 0; i-- {
p := emptySpecs[i]
d.Specs = append(d.Specs[:p], d.Specs[p+1:]...)
}
remove = len(d.Specs) == 0
case token.TYPE:
namesToRemove := []int{}
for i, spec := range d.Specs {
typeSpec := spec.(*ast.TypeSpec)
debugf("Type %v", typeSpec.Name.Name)
// Remove type A if it is a template definition
def := info.Defs[typeSpec.Name]
if _, ok := t.templateArgsMap[typeSpec.Name.Name]; ok {
namesToRemove = append(namesToRemove, i)
t.mappings[def] = t.templateArgsMap[typeSpec.Name.Name]
} else {
namesToMangle[def] = typeSpec.Name.Name
}
}
for i := len(namesToRemove) - 1; i >= 0; i-- {
p := namesToRemove[i]
d.Specs = append(d.Specs[:p], d.Specs[p+1:]...)
}
remove = len(d.Specs) == 0
default:
logf("Unknown type %s", d.Tok)
}
debugf("GenDecl = %#v", d)
case *ast.FuncDecl:
// A function definition
if d.Recv != nil {
// Has receiver so is a method - ignore this function
} else if d.Name.Name == "init" {
// Init function - ignore this function
} else {
//debugf("FuncDecl = %#v", d)
debugf("FuncDecl = %s", d.Name.Name)
def := info.Defs[d.Name]
// Remove func A() if it is a template definition
if _, ok := t.templateArgsMap[d.Name.Name]; ok {
remove = true
t.mappings[def] = t.templateArgsMap[d.Name.Name]
} else {
namesToMangle[def] = d.Name.Name
}
}
default:
fatalf("Unknown Decl %#v", decl)
}
if !remove {
newDecls = append(newDecls, decl)
}
}
debugf("Names to mangle = %#v", namesToMangle)
// Remove the stub type definitions "type A int" from the package
f.Decls = newDecls
found := false
for obj, name := range namesToMangle {
if name == t.templateName {
found = true
t.addMapping(obj, name)
} else if _, found := t.mappings[obj]; !found {
t.addMapping(obj, name)
}
}
if !found {
fatalf("No definition for template type '%s'", t.templateName)
}
debugf("mappings = %#v", t.mappings)
// Replace the identifiers
for id, replacement := range t.mappings {
replaceIdentifier(f, info, id, replacement)
}
// Change the package to the local package name
f.Name.Name = t.NewPackage
// Output but only if contents have changed from existing file
b := new(bytes.Buffer)
outputFileName := fmt.Sprintf(*outfile+".go", t.Name)
format := func() {
b.Reset()
if err := format.Node(b, fset, f); err != nil {
fatalf("Failed to format output: %v", err)
}
bts, err := imports.Process(outputFileName, b.Bytes(), nil)
if err != nil {
fatalf("Cannot fix imports: %v", err)
}
b.Reset()
if _, err := b.Write(bts); err != nil {
fatalf("Cannot write output: %v", err)
}
}
format()
// bit gross to inject the header this way... but in the spirit of
// minimal changes et al...
fset, f = parseFile(outputFileName, genHeader+b.String())
format()
write := true
var curr []byte
if !testingMode {
var err error
curr, err = ioutil.ReadFile(outputFileName)
if err != nil && !os.IsNotExist(err) {
fatalf("Cannot open existing file: %v", err)
}
}
if bytes.Equal(curr, b.Bytes()) {
write = false
}
if write {
err := ioutil.WriteFile(outputFileName, b.Bytes(), 0666)
if err != nil {
fatalf("Unable to write to %q: %v", outputFileName, err)
}
}
debugf("Written '%s'", outputFileName)
}
// Instantiate the template package
func (t *template) instantiate() {
debugf("Substituting %q with %s(%s) into package %s", t.Package, t.Name, strings.Join(t.Args, ","), t.NewPackage)
p, err := build.Default.Import(t.Package, t.Dir, build.ImportMode(0))
if err != nil {
fatalf("Import %s failed: %s", t.Package, err)
}
//debugf("package = %#v", p)
debugf("Dir = %#v", p.Dir)
// FIXME CgoFiles ?
debugf("Go files = %#v", p.GoFiles)
if len(p.GoFiles) == 0 {
fatalf("No go files found for package '%s'", t.Package)
}
// FIXME
if len(p.GoFiles) != 1 {
fatalf("Found more than one go file in '%s' - can only cope with 1 for the moment, sorry", t.Package)
}
templateFilePath := path.Join(p.Dir, p.GoFiles[0])
t.parse(templateFilePath)
}