forked from codetainerapp/codetainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (60 loc) · 1.39 KB
/
main.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
package main
import (
"encoding/json"
"os/exec"
"strings"
"text/template"
"os"
)
const (
tmpl = `package kingpin
// This file is autogenerated by "go generate .". Do not modify.
{{range .}}
// {{if .Comment}}{{.Comment}}{{else}}{{.|Name}} accumulates {{.Type}} values into a slice.{{end}}
func (p *parserMixin) {{.|Name}}() (target *[]{{.Type}}) {
target = new([]{{.Type}})
p.{{.|Name}}Var(target)
return
}
func (p *parserMixin) {{.|Name}}Var(target *[]{{.Type}}) {
p.SetValue(newAccumulator(target, func(v interface{}) Value { return new{{.Name}}Value(v.(*{{.Type}})) }))
}
{{end}}
`
)
type Repeated struct {
Name string `json:"name"`
Comment string `json:"comment"`
Plural string `json:"plural"`
Type string `json:"type"`
}
func fatalIfError(err error) {
if err != nil {
panic(err)
}
}
func main() {
r, err := os.Open("repeated.json")
fatalIfError(err)
defer r.Close()
v := []Repeated{}
err = json.NewDecoder(r).Decode(&v)
fatalIfError(err)
t, err := template.New("genrepeated").Funcs(template.FuncMap{
"Lower": strings.ToLower,
"Name": func(v *Repeated) string {
if v.Plural != "" {
return v.Plural
}
return v.Name + "List"
},
}).Parse(tmpl)
fatalIfError(err)
w, err := os.Create("repeated.go")
fatalIfError(err)
defer w.Close()
err = t.Execute(w, v)
fatalIfError(err)
err = exec.Command("goimports", "-w", "repeated.go").Run()
fatalIfError(err)
}