/
main_generator.go
83 lines (76 loc) · 2.23 KB
/
main_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
package generator
import (
"fmt"
. "github.com/dave/jennifer/jen"
"go/format"
"os"
)
func GenerateMain(parsedStruct *Config, modelnames []string, outputDir string) {
f := NewFile("main")
f.Var().Id("db").Op("*").Qual("gorm.io/gorm", "DB")
// Generate the main function
f.Func().Id("main").Params().BlockFunc(func(mainBlock *Group) {
mainBlock.Var().Id("err").Error()
mainBlock.Line()
// Open the database connection
mainBlock.List(Id("db"), Id("err")).Op("=").Qual("gorm.io/gorm", "Open").Call(
Qual("gorm.io/driver/sqlite", "Open").Call(
Lit(parsedStruct.camelCase+".db")),
Op("&").Qual("gorm.io/gorm", "Config").Values(),
)
mainBlock.If(Id("err").Op("!=").Nil()).Block(
Panic(Lit("failed to connect database")),
)
mainBlock.Defer().Func().Params().Block(
Id("err").Op("=").Qual("os", "RemoveAll").Call(
Lit(parsedStruct.camelCase+".db"),
),
If(Id("err").Op("!=").Nil()).Block(
Return(),
),
).Call()
mainBlock.Line()
// Auto-migrate models
for _, model := range modelnames {
mainBlock.Id("db").Dot("AutoMigrate").Call(Op("&").Id(model + "{}"))
}
mainBlock.Line()
// Handle HTTP routes
mainBlock.Qual("net/http", "HandleFunc").Call(
Lit("/"+ToSnakeCase(parsedStruct.Name)+"/read"),
Id("Read"+parsedStruct.camelCase),
)
mainBlock.Qual("net/http", "HandleFunc").Call(
Lit("/"+ToSnakeCase(parsedStruct.Name)+"/create"),
Id("Create"+parsedStruct.camelCase),
)
mainBlock.Qual("net/http", "HandleFunc").Call(
Lit("/"+ToSnakeCase(parsedStruct.Name)+"/update"),
Id("Update"+parsedStruct.camelCase),
)
mainBlock.Qual("net/http", "HandleFunc").Call(
Lit("/"+ToSnakeCase(parsedStruct.Name)+"/delete"),
Id("Delete"+parsedStruct.camelCase),
)
mainBlock.Err().Op("=").Qual("net/http", "ListenAndServe").Call(
Lit(":3333"),
Nil(),
)
mainBlock.If(Err().Op("!=").Nil()).Block(
Panic(Err()),
)
})
output := fmt.Sprintf("%#v", f)
outputBytes, err := format.Source([]byte(output))
if err != nil {
fmt.Println(output)
fmt.Println("Error:" + err.Error())
os.Exit(1)
}
// write rawOutput to file
err = os.WriteFile(outputDir+ToSnakeCase(parsedStruct.Name)+"/main.go", outputBytes, 0644)
if err != nil {
fmt.Println("Error:" + err.Error())
os.Exit(1)
}
}