76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
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)
|
|
}
|