-
Notifications
You must be signed in to change notification settings - Fork 4
/
generator.go
160 lines (136 loc) · 3.54 KB
/
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
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
package main
import (
"bytes"
"go/format"
"html/template"
"io"
"log"
"os"
"path/filepath"
"sort"
"github.com/gojuno/genval/types"
)
type generator struct {
structs []StructDef
imports map[string]bool
}
func NewGenerator(structs []StructDef) generator {
return generator{
structs: structs,
imports: make(map[string]bool),
}
}
func (g generator) Generate(validatorsCfg config, needCheck bool) error {
cfg := types.GenConfig{
SupportedTags: validatorsCfg.supportedTags,
NeedValidatableCheck: needCheck,
AddImport: func(imp string) {
g.imports[imp] = true
},
}
buf, err := g.gen(validatorsCfg.pkg, cfg)
if err != nil {
log.Fatalf("can't generate resulting source: %s", err)
}
formatted, err := format.Source(buf.Bytes())
if err != nil {
log.Fatalf("source: %s\ncan't format resulting source: %s", buf.String(), err)
}
filepath := filepath.Join(validatorsCfg.dir, validatorsCfg.outputFile)
f, err := os.Create(filepath)
if err != nil {
log.Fatalf("can't create file %q: %v", filepath, err)
}
defer f.Close()
if _, err := f.Write(formatted); err != nil {
log.Fatalf("can't write to resulting file %q: %v", filepath, err)
}
return nil
}
func (g generator) gen(pkg string, cfg types.GenConfig) (*bytes.Buffer, error) {
codeBuf := bytes.NewBuffer([]byte{})
importsBuf := bytes.NewBuffer([]byte{})
g.genUtilCode(codeBuf, cfg)
g.genCode(codeBuf, cfg)
g.genImports(importsBuf, pkg)
io.WriteString(importsBuf, codeBuf.String())
return importsBuf, nil
}
func (g generator) genCode(w io.Writer, cfg types.GenConfig) {
for _, s := range sorted(g.structs) {
s.Generate(w, cfg)
}
}
func sorted(structs []StructDef) []StructDef {
sort.Sort(AlpabetSorter(structs))
return structs
}
type AlpabetSorter []StructDef
func (a AlpabetSorter) Len() int { return len(a) }
func (a AlpabetSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a AlpabetSorter) Less(i, j int) bool { return a[i].Name < a[j].Name }
func (g generator) genUtilCode(w io.Writer, cfg types.GenConfig) {
const t = `
{{if .NeedValidatableCheck}}
{{range $tag := .SupportedTags}}
type validatable{{$tag}} interface {
Validate{{$tag}}() error
}
{{ if eq $tag ""}}
func validate(i interface{}) error {
if v, ok := i.(validatable); ok {
return v.Validate()
}
return nil
}
{{else}}
func validate{{$tag}}(i interface{}) error {
if v, ok := i.(validatable{{$tag}}); ok {
return v.Validate{{$tag}}()
} else {
if v, ok := i.(validatable); ok {
return v.Validate()
}
}
return nil
}
{{end}}
{{end}}
{{end}}
`
writeTmpl(t, "utilCode", w, cfg)
}
func (g generator) genImports(w io.Writer, pkg string) {
const t = `
//This file was automatically generated by the genval generator v{{ .Version }}
//Please don't modify it manually. Edit your entity tags and then
//run go generate
package {{ .Pkg }}
{{if .Imports}}
import (
{{ range $imp, $v := .Imports }}
"{{ $imp }}"
{{ end }}
)
{{end}}
`
p := struct {
Imports map[string]bool
Pkg string
Version string
}{
Imports: g.imports,
Pkg: pkg,
Version: version,
}
writeTmpl(t, "imports", w, p)
}
func writeTmpl(t, name string, w io.Writer, data interface{}) {
tmpl, err := template.New("utilCode").Parse(t)
if err != nil {
log.Fatalf("failed to create template for %s: %v", name, err)
}
if err := tmpl.Execute(w, data); err != nil {
log.Fatalf("failed to execute template for %s: %v", name, err)
}
}