forked from MasterJoyHunan/gengin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_logic.go
96 lines (82 loc) · 2.94 KB
/
gen_logic.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
package generator
import (
"fmt"
"sort"
"strings"
. "github.com/MasterJoyHunan/gengin/prepare"
"github.com/MasterJoyHunan/gengin/tpl"
"github.com/zeromicro/go-zero/core/collection"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
func GenLogic() error {
for _, g := range PluginInfo.Api.Service.Groups {
for _, r := range g.Routes {
err := genLogicByRoute(g, r)
if err != nil {
return err
}
}
}
return nil
}
func genLogicByRoute(group spec.Group, route spec.Route) error {
logic := getLogicName(route)
filename, err := format.FileNamingFormat(PluginInfo.Style, logic)
if err != nil {
return err
}
imports := genLogicImports(group, route)
var responseString string
var returnString string
var requestString string
if len(route.RequestTypeName()) > 0 {
groupNameParse := parseGroupName(group.GetAnnotation(groupProperty), typesPacket, typesDir)
requestString = "req *" + groupNameParse.pkgName + "." + strings.Title(route.RequestTypeName()) + " ,"
}
// + svc context
requestString += "ctx *svc.ServiceContext"
if len(route.ResponseTypeName()) > 0 {
groupNameParse := parseGroupName(group.GetAnnotation(groupProperty), typesPacket, typesDir)
responseString = "(resp " + groupNameParse.pkgName + "." + strings.Title(route.ResponseTypeName()) + ", err error)"
returnString = "return"
} else {
responseString = "error"
returnString = "return nil"
}
logicGroupNameParse := parseGroupName(group.GetAnnotation(groupProperty), logicPacket, logicDir)
return genFile(fileGenConfig{
dir: PluginInfo.Dir,
subDir: logicGroupNameParse.dirPath,
filename: filename + ".go",
templateName: "logicTemplate",
builtinTemplate: tpl.LogicTemplate,
data: map[string]interface{}{
"comment": parseComment(route),
"pkgName": logicGroupNameParse.pkgName,
"hasImport": len(imports) > 0,
"imports": imports,
"function": util.Title(strings.TrimSuffix(logic, "Logic")),
"responseType": responseString,
"returnString": returnString,
"request": requestString,
},
})
}
func genLogicImports(group spec.Group, route spec.Route) string {
importSet := collection.NewSet()
if len(route.RequestTypeName()) > 0 {
groupNameParse := parseGroupName(group.GetAnnotation(groupProperty), typesPacket, typesDir)
importSet.AddStr(fmt.Sprintf("\"%s\"", pathx.JoinPackages(RootPkg, groupNameParse.dirPath)))
}
if len(route.ResponseTypeName()) > 0 {
groupNameParse := parseGroupName(group.GetAnnotation(groupProperty), typesPacket, typesDir)
importSet.AddStr(fmt.Sprintf("\"%s\"", pathx.JoinPackages(RootPkg, groupNameParse.dirPath)))
}
importSet.AddStr(fmt.Sprintf("\"%s\"", pathx.JoinPackages(RootPkg, "svc")))
importArr := importSet.KeysStr()
sort.Strings(importArr)
return strings.Join(importArr, "\n\t")
}