-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.go
40 lines (33 loc) · 902 Bytes
/
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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
const generatorTemplate = `// Code generated by "enumall -type={{.TypeName}}"; DO NOT EDIT.
package {{.PackageName}}
var All{{.TypeName}} = []{{.TypeName}}{
{{range .Values}} {{.}},
{{end}}}`
// generator holds information for generating all enum variable.
type generator struct {
PackageName string
TypeName string
Values []string
}
// generate creates the output file containing values of given type name.
func (g *generator) generate() {
if len(g.Values) > 0 {
baseName := fmt.Sprintf("%s_all.go", strings.ToLower(g.TypeName))
wd, _ := os.Getwd()
outputName := filepath.Join(wd, baseName)
f, err := os.Create(outputName)
handleError(err)
// parsing well known template should not fail
t, _ := tmpl.Parse(generatorTemplate)
// write to the output file
err = t.Execute(f, g)
handleError(err)
}
}