/
model_generator.go
88 lines (84 loc) · 3.02 KB
/
model_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
package generator
import (
"fmt"
. "github.com/dave/jennifer/jen"
"go/format"
"os"
)
func GenerateModel(s *Config, outputDir string) (string, []string) {
f := NewFile("main")
f.ImportName("github.com/divakarmanoj/go-scaffolding/imports", "imports")
modelNames := GormStruct(s, f)
GenerateModelToResponse(s, f)
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(s.Name)+"/models.go", outputBytes, 0644)
if err != nil {
fmt.Println("Error:" + err.Error())
os.Exit(1)
}
return output, modelNames
}
func GormStruct(s *Config, f *File) []string {
modelNames := []string{s.camelCase + "Model"}
f.Type().Id(s.camelCase + "Model").StructFunc(func(g *Group) {
g.Qual("github.com/divakarmanoj/go-scaffolding/imports", "Model")
for _, attr := range s.Attributes {
if !isValidType(attr.Type) {
fmt.Printf("Error: Invalid type %s\n", attr.Type)
os.Exit(1)
}
if attr.Type != "struct" {
if attr.IsRequired {
g.Id(attr.camelCase).Id(attr.Type).Tag(map[string]string{"json": ToSnakeCase(attr.Name)})
} else {
g.Id(attr.camelCase).Qual("database/sql", "Null"+toTitleCase(attr.Type)).Tag(map[string]string{"json": ToSnakeCase(attr.Name)})
}
} else {
f.Line()
NestedModelNames := GormStruct(&Config{Name: attr.Name, camelCase: attr.camelCase, Attributes: attr.Attributes}, f)
modelNames = append(modelNames, NestedModelNames...)
g.Id(attr.camelCase + "ID").Id("uint").Tag(map[string]string{"json": ToSnakeCase(attr.Name) + "_id"})
g.Id(attr.camelCase).Id("*" + attr.camelCase + "Model").Tag(map[string]string{"json": ToSnakeCase(attr.Name)})
}
}
})
f.Line()
return modelNames
}
func GenerateModelToResponse(s *Config, f *File) {
f.Func().Params(
Id("model").Id("*" + s.camelCase + "Model"),
).Id("ToResponse").Params().Id("*" + s.camelCase + "Response").BlockFunc(func(g *Group) {
g.If(Id("model").Op("==").Nil()).Block(
Return(Nil()),
)
g.Return(Op("&").Id(s.camelCase + "Response").ValuesFunc(func(g *Group) {
g.Id("ID").Op(":").Id("model").Dot("Model").Dot("ID")
g.Id("CreatedAt").Op(":").Id("model").Dot("Model").Dot("CreatedAt")
g.Id("UpdatedAt").Op(":").Id("model").Dot("Model").Dot("UpdatedAt")
for _, attr := range s.Attributes {
if !isValidType(attr.Type) {
fmt.Println("Error: invalid type " + attr.Type)
os.Exit(1)
}
if attr.Type != "struct" {
if !attr.IsRequired {
g.Id(attr.camelCase).Op(":").Qual("github.com/divakarmanoj/go-scaffolding/imports", "Null"+toTitleCase(attr.Type)+"ToPtr").Call(Id("model").Dot(attr.camelCase))
} else {
g.Id(attr.camelCase).Op(":").Id("model").Dot(attr.camelCase)
}
} else {
GenerateModelToResponse(&Config{Name: attr.Name, camelCase: attr.camelCase, Attributes: attr.Attributes}, f)
g.Id(attr.camelCase).Op(":").Id("model").Dot(attr.camelCase).Dot("ToResponse").Call()
}
}
}))
})
}