-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathmain.go
224 lines (193 loc) · 5.4 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//go:build exclude
package main
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"go/types"
"io"
"log"
"os"
"path/filepath"
"strings"
"unicode/utf8"
)
func main() {
filename := os.Getenv("GOFILE")
fmt.Printf("Running generator on %s with args %#v\n", filename, os.Args[1:])
astFile := parseSourceFile(filename)
gen := setUpGenerator(astFile)
fmt.Printf("Generator set up for package \"%s\" with output name \"%s\".\n", gen.outputPackage, gen.outputName)
gen.addFilePreamble()
gen.addImports()
gen.addConstructorsAndBuilderMethods()
src, errSrcFormat := format.Source(gen.buffer.Bytes())
if errSrcFormat != nil {
log.Panicln(errSrcFormat)
}
if err := os.WriteFile(gen.outputName, src, 0o600); err != nil {
log.Panicln(err)
}
}
func parseSourceFile(filename string) *ast.File {
astFile, err := parser.ParseFile(token.NewFileSet(), filename, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
return astFile
}
type Generator struct {
astFile *ast.File
buffer bytes.Buffer
outputPackage string
outputName string
}
func setUpGenerator(astFile *ast.File) *Generator {
wd, errWd := os.Getwd()
if errWd != nil {
log.Panicln(errWd)
}
file := os.Getenv("GOFILE")
fileWithoutSuffix, _ := strings.CutSuffix(file, ".go")
fileWithoutSuffix, _ = strings.CutSuffix(fileWithoutSuffix, "_gen")
baseName := fmt.Sprintf("%s_builders_gen.go", fileWithoutSuffix)
outputName := filepath.Join(wd, baseName)
return &Generator{
astFile: astFile,
buffer: bytes.Buffer{},
outputPackage: os.Getenv("GOPACKAGE"),
outputName: outputName,
}
}
func (gen *Generator) printf(format string, args ...any) {
printf(&gen.buffer, format, args...)
}
func printf(w io.Writer, format string, args ...any) {
_, err := fmt.Fprintf(w, format, args...)
if err != nil {
log.Panicln(err)
}
}
func (gen *Generator) addFilePreamble() {
gen.printf("// Code generated by dto builder generator; DO NOT EDIT.\n")
gen.printf("\n")
gen.printf("package %s\n", gen.outputPackage)
gen.printf("\n")
}
func (gen *Generator) addImports() {
imports := make([]string, 0, len(gen.astFile.Imports))
for _, importNode := range gen.astFile.Imports {
imports = append(imports, types.ExprString(importNode.Path))
}
gen.generateImports(imports...)
}
func (gen *Generator) generateImports(imports ...string) {
gen.printf("import (\n")
for _, i := range imports {
gen.printf("%s\n", i)
}
gen.printf(")\n")
gen.printf("\n")
}
func (gen *Generator) addConstructorsAndBuilderMethods() {
for _, node := range gen.astFile.Decls {
if _, ok := node.(*ast.GenDecl); ok {
for _, spec := range node.(*ast.GenDecl).Specs {
if _, ok := spec.(*ast.TypeSpec); ok {
if _, ok := spec.(*ast.TypeSpec).Type.(*ast.StructType); ok {
var fields []*fieldDef
for _, field := range spec.(*ast.TypeSpec).Type.(*ast.StructType).Fields.List {
for _, name := range field.Names {
fs := newFieldDef(name, field)
fields = append(fields, fs)
}
}
def := newStructDef(spec.(*ast.TypeSpec), fields)
gen.generateConstructor(def)
gen.generateBuilderMethods(def)
}
}
}
}
}
}
type structDef struct {
name string
fields []*fieldDef
}
func newStructDef(ts *ast.TypeSpec, fields []*fieldDef) *structDef {
return &structDef{
name: ts.Name.Name,
fields: fields,
}
}
type fieldDef struct {
name string
typeString string
isRequired bool
}
func (fs *fieldDef) String() string {
return fmt.Sprintf("Field: name=%s type=%s is required=%t", fs.name, fs.typeString, fs.isRequired)
}
func newFieldDef(name *ast.Ident, field *ast.Field) *fieldDef {
return &fieldDef{
name: name.Name,
typeString: types.ExprString(field.Type),
isRequired: strings.TrimSpace(field.Comment.Text()) == "required",
}
}
func (gen *Generator) generateConstructor(d *structDef) {
gen.printf("func New%s(", d.name)
var requiredFields []*fieldDef
for _, field := range d.fields {
if field.isRequired {
requiredFields = append(requiredFields, field)
}
}
if len(requiredFields) != 0 {
gen.printf("\n")
for _, field := range requiredFields {
gen.printf("%s %s,\n", field.name, field.typeString)
}
}
gen.printf(") *%s {\n", d.name)
var returnStatement string
if len(requiredFields) != 0 {
gen.printf("s := %s{}\n", d.name)
for _, field := range requiredFields {
gen.printf("s.%s = %s\n", field.name, field.name)
}
returnStatement = "&s"
} else {
returnStatement = fmt.Sprintf("&%s{}", d.name)
}
gen.printf("return %s\n", returnStatement)
gen.printf("}\n\n")
}
func (gen *Generator) generateBuilderMethods(d *structDef) {
var optionalFields []*fieldDef
for _, field := range d.fields {
if !field.isRequired {
optionalFields = append(optionalFields, field)
}
}
for _, field := range optionalFields {
gen.printf("func (s *%s) With%s(%s %s) *%s {\n", d.name, toTitle(field.name), field.name, strings.TrimLeft(field.typeString, "*"), d.name)
switch {
case strings.HasPrefix(field.typeString, "*"):
// If the target field is a pointer, assign the address of input field because right now we always pass them by value
gen.printf("s.%s = &%s\n", field.name, field.name)
default:
gen.printf("s.%s = %s\n", field.name, field.name)
}
gen.printf("return s\n")
gen.printf("}\n\n")
}
}
func toTitle(s string) string {
firstLetter, _ := utf8.DecodeRuneInString(s)
return strings.ToUpper(string(firstLetter)) + s[1:]
}