-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
107 lines (92 loc) · 3.21 KB
/
controller.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
package main
import (
"bufio"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/iancoleman/strcase"
)
// Represents table Entity
type javaClass struct {
PkCamelName string // name of PK in camel Case
PkPascalName string // name of PK in Pascal Case
PkType string // java Type of primary Key
PascalName string // current table name
CamelCase string // current table name
TargetPackage string // e.g. com.example.projectName
FkRelationList []FkRelation // foreign key relations
}
// Mapping of tableType to TemplateFile
const (
NormalTable = "DemoController.txt"
ViewTable = "ViewDemoController.txt"
)
// for testing table type
var viewReg = regexp.MustCompile(`^View[A-Z]+?`)
// GenerateControllers for all tables
func GenerateControllers(baseDir, targetPackage string) {
controllerDir := filepath.Join(baseDir, "controller")
os.Mkdir(controllerDir, os.ModePerm)
modelFiles, err := os.ReadDir(filepath.Join(baseDir, "model"))
check(err)
for _, modelFile := range modelFiles {
writeControllerFile(modelFile, controllerDir, targetPackage)
}
}
// write content and save to file
func writeControllerFile(modelFile os.DirEntry, controllerDir, targetPackage string) {
var fullName = modelFile.Name()
cleanName := fullName[0:strings.Index(fullName, ".")] //without extension
var fkList, exists = fkRelationMap[cleanName]
if exists {
//for each foreign column, attach its Primary key name
for i, relation := range fkList {
foreignPkItem := pkTableMap[relation.RefTablePascal]
relation.RefPkPascal = strcase.ToCamel(foreignPkItem.PkName)
fkList[i] = relation
}
}
var pkItem = pkTableMap[cleanName]
singleClass := &javaClass{
PkType: pkItem.PkType,
PkCamelName: pkItem.PkName,
PkPascalName: strcase.ToCamel(pkItem.PkName),
PascalName: cleanName,
CamelCase: strcase.ToLowerCamel(cleanName),
TargetPackage: targetPackage,
FkRelationList: fkList,
}
var templateFile = NormalTable
if viewReg.MatchString(cleanName) {
templateFile = ViewTable
}
templatePath := filepath.Join(TemplateDir, templateFile)
tc := template.Must(template.New(templateFile).ParseFiles(templatePath))
fout, err := os.Create(filepath.Join(controllerDir, cleanName+"Controller.java"))
check(err)
defer fout.Close()
bw := bufio.NewWriter(fout)
defer bw.Flush()
tc.Execute(bw, singleClass)
}
// GenerateCommonFiles for paging and generic Response
func GenerateCommonFiles(baseDir string, targetPackage string) {
commonDir := filepath.Join(baseDir, "common")
os.Mkdir(commonDir, os.ModePerm)
//write CommonResponse file
tmplResponse := filepath.Join(TemplateDir, "CustomResponse.txt")
tcc := template.Must(template.New("CustomResponse.txt").ParseFiles(tmplResponse))
outResFile, err := os.Create(filepath.Join(commonDir, "CustomResponse.java"))
check(err)
defer outResFile.Close()
tcc.Execute(outResFile, targetPackage)
//write PageInfo file
tmplPageInfo := filepath.Join(TemplateDir, "PageInfo.txt")
tpp := template.Must(template.New("PageInfo.txt").ParseFiles(tmplPageInfo))
outPageInfo, err := os.Create(filepath.Join(commonDir, "PageInfo.java"))
check(err)
defer outPageInfo.Close()
tpp.Execute(outPageInfo, targetPackage)
}