-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.go
98 lines (82 loc) · 2.44 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
package main
import (
"bytes"
_ "embed"
"go/parser"
"go/token"
"log"
"os"
"sort"
"strings"
"text/template"
"golang.org/x/exp/maps"
)
var fileTmpl, entityTmpl *template.Template
//go:embed file.go.tmpl
var fileTmplContent []byte
//go:embed entity.go.tmpl
var entityTmplContent []byte
func init() {
tmplFuncs := template.FuncMap{
"join": func(sep string, s []string) string { return strings.Join(s, sep) },
"add": func(x ...int) (y int) {
for _, v := range x {
y += v
}
return
},
"n_tabs": func(n int) string { return strings.Repeat("\t", n) },
"snake_case_to_camel_case": snakeCaseToCamelCase,
"exists": func(m map[string]any, key string) bool { _, ok := m[key]; return ok },
}
fileTmpl = template.Must(template.New("file").Funcs(tmplFuncs).Parse(string(fileTmplContent)))
entityTmpl = template.Must(template.New("entity").Funcs(tmplFuncs).Parse(string(entityTmplContent)))
}
func generate(packageName string, entities []entity) {
newFileName := "gentity.gen.go"
var entitiesCode []string
var imports map[string]struct{} = make(map[string]struct{})
for _, entity := range entities {
var buf bytes.Buffer
if err := entityTmpl.Execute(&buf, entity); err != nil {
log.Fatalf("Execute template: %v", err)
}
entitiesCode = append(entitiesCode, buf.String())
for _, field := range entity.Fields {
// Import field type need only if it used arguments of methods.
// This is one case: getters.
if len(field.InIndexes) == 0 {
continue
}
t := strings.Split(field.GoType, ".")
if len(t) == 1 {
continue
}
if t[0] == "pgtype" {
imports["github.com/jackc/pgx/v5/pgtype"] = struct{}{}
} else {
imports[t[0]] = struct{}{}
}
}
}
sort.Strings(entitiesCode)
var buf bytes.Buffer
if err := fileTmpl.Execute(&buf, struct {
PackageName string
Entities []string
Imports []string
}{packageName, entitiesCode, maps.Keys(imports)}); err != nil {
log.Fatalf("Execute template: %v", err)
}
if _, err := parser.ParseFile(token.NewFileSet(), newFileName, buf.Bytes(), parser.ParseComments); err != nil {
log.Fatalf("Parse template failed: %v; template below: %s", err, buf.String())
}
outFile, err := os.Create(newFileName)
if err != nil {
log.Fatalf("Create file: %v", err)
}
defer outFile.Close()
if _, err := outFile.WriteString(buf.String()); err != nil {
log.Fatalf("Failed to write generated file %s: %v", newFileName, err)
}
}