Skip to content

Commit 3ebe5eb

Browse files
committed
version 1.2.0 change project logic
1 parent 91d75f6 commit 3ebe5eb

13 files changed

+89
-127
lines changed

.github/workflows/release.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- "v*"
77

88
jobs:
9-
goreleaser:
9+
release:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Checkout
@@ -36,7 +36,5 @@ jobs:
3636

3737
- name: Refresh Proxy Go Package
3838
run: |
39-
echo "Repository name: ${{ github.repository }}"
4039
TAG_NAME=${GITHUB_REF#refs/tags/}
41-
echo "Tag name: $TAG_NAME"
4240
curl -v https://proxy.golang.org/${{ github.repository }}/goctlx/@v/$TAG_NAME.info

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44

55
go install github.com/dxc0522/goctlx@latest
66

7-
## update proxy package
7+
## 差异
88

9-
访问 https://proxy.golang.org/github.com/dxc0522/goctlx/@v/v1.0.2.info
9+
修改逻辑及部分页面结构
10+
剔除单独的context设置
11+
12+
### api
13+
14+
* 生成api以当前父级文件夹的名称读取.api文件
15+
* 导出文件夹默认为当前文件夹
1016

1117
## document
1218

api/gogen/default-logic.tpl

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package logic
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"github.com/zeromicro/go-zero/core/logx"
7+
{{.imports}}
8+
)
9+
10+
type {{.logic}} struct {
11+
logx.Logger
12+
*svc.ServiceContext
13+
ctx context.Context
14+
reqCtx *http.Request
15+
respWriter *http.ResponseWriter
16+
}
17+
18+
func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext, reqCtx *http.Request, respWriter *http.ResponseWriter) *{{.logic}} {
19+
return &{{.logic}}{
20+
Logger: logx.WithContext(ctx),
21+
ServiceContext: svcCtx,
22+
ctx: ctx,
23+
reqCtx: reqCtx,
24+
respWriter: respWriter,
25+
}
26+
}

api/gogen/genhandlers.go

+11-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package gogen
33
import (
44
_ "embed"
55
"fmt"
6-
"path"
76
"strings"
87

98
"github.com/dxc0522/goctlx/api/spec"
@@ -13,40 +12,31 @@ import (
1312
"github.com/dxc0522/goctlx/util/pathx"
1413
)
1514

16-
const defaultLogicPackage = "logic"
17-
1815
//go:embed handler.tpl
1916
var handlerTemplate string
2017

21-
func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
18+
func genHandler(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec, group spec.Group, route spec.Route) error {
2219
handler := getHandlerName(route)
23-
handlerPath := getHandlerFolderPath(group, route)
24-
pkgName := handlerPath[strings.LastIndex(handlerPath, "/")+1:]
25-
logicName := defaultLogicPackage
26-
if handlerPath != handlerDir {
27-
handler = strings.Title(handler)
28-
logicName = pkgName
29-
}
3020
filename, err := format.FileNamingFormat(cfg.NamingFormat, handler)
3121
if err != nil {
3222
return err
3323
}
3424

3525
return genFile(fileGenConfig{
3626
dir: dir,
37-
subdir: getHandlerFolderPath(group, route),
38-
filename: filename + ".go",
27+
subdir: handlerDir,
28+
filename: filename + "_gen.go",
3929
templateName: "handlerTemplate",
4030
category: category,
4131
templateFile: handlerTemplateFile,
4232
builtinTemplate: handlerTemplate,
4333
data: map[string]any{
44-
"PkgName": pkgName,
45-
"ImportPackages": genHandlerImports(group, route, rootPkg),
46-
"HandlerName": handler,
34+
"PkgName": handlerDir,
35+
"ImportPackages": genHandlerImports(route, rootPkg),
36+
"HandlerName": strings.Title(handler),
4737
"RequestType": util.Title(route.RequestTypeName()),
48-
"LogicName": logicName,
49-
"LogicType": strings.Title(getLogicName(route)),
38+
"LogicName": logicDir,
39+
"LogicType": strings.Title(api.Service.Name + "Logic"),
5040
"Call": strings.Title(strings.TrimSuffix(handler, "Handler")),
5141
"HasResp": len(route.ResponseTypeName()) > 0,
5242
"HasRequest": len(route.RequestTypeName()) > 0,
@@ -59,7 +49,7 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
5949
func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
6050
for _, group := range api.Service.Groups {
6151
for _, route := range group.Routes {
62-
if err := genHandler(dir, rootPkg, cfg, group, route); err != nil {
52+
if err := genHandler(dir, rootPkg, cfg, api, group, route); err != nil {
6353
return err
6454
}
6555
}
@@ -68,9 +58,9 @@ func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) err
6858
return nil
6959
}
7060

71-
func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
61+
func genHandlerImports(route spec.Route, parentPkg string) string {
7262
imports := []string{
73-
fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, getLogicFolderPath(group, route))),
63+
fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, logicDir)),
7464
fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)),
7565
}
7666
if len(route.RequestTypeName()) > 0 {
@@ -89,21 +79,6 @@ func getHandlerBaseName(route spec.Route) (string, error) {
8979
return handler, nil
9080
}
9181

92-
func getHandlerFolderPath(group spec.Group, route spec.Route) string {
93-
folder := route.GetAnnotation(groupProperty)
94-
if len(folder) == 0 {
95-
folder = group.GetAnnotation(groupProperty)
96-
if len(folder) == 0 {
97-
return handlerDir
98-
}
99-
}
100-
101-
folder = strings.TrimPrefix(folder, "/")
102-
folder = strings.TrimSuffix(folder, "/")
103-
104-
return path.Join(handlerDir, folder)
105-
}
106-
10782
func getHandlerName(route spec.Route) string {
10883
handler, err := getHandlerBaseName(route)
10984
if err != nil {

api/gogen/genlogic.go

+27-24
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package gogen
33
import (
44
_ "embed"
55
"fmt"
6-
"path"
76
"strconv"
87
"strings"
98

@@ -12,16 +11,37 @@ import (
1211
"github.com/dxc0522/goctlx/config"
1312
"github.com/dxc0522/goctlx/util/format"
1413
"github.com/dxc0522/goctlx/util/pathx"
15-
"github.com/dxc0522/goctlx/vars"
1614
)
1715

1816
//go:embed logic.tpl
1917
var logicTemplate string
2018

19+
//go:embed default-logic.tpl
20+
var defaultLogicTemplate string
21+
2122
func genLogic(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
23+
//generate default logic file
24+
imports := fmt.Sprintf("\"%s\"", pathx.JoinPackages(rootPkg, contextDir))
25+
logicName := strings.Title(api.Service.Name + "Logic")
26+
err := genFile(fileGenConfig{
27+
dir: dir,
28+
subdir: logicDir,
29+
filename: logicDir + ".go",
30+
templateName: "logicDefaultTemplate",
31+
category: category,
32+
templateFile: defaultLogicTemplateFile,
33+
builtinTemplate: defaultLogicTemplate,
34+
data: map[string]any{
35+
"logic": logicName,
36+
"imports": imports,
37+
},
38+
})
39+
if err != nil {
40+
return err
41+
}
2242
for _, g := range api.Service.Groups {
2343
for _, r := range g.Routes {
24-
err := genLogicByRoute(dir, rootPkg, cfg, g, r)
44+
err := genLogicByRoute(dir, rootPkg, cfg, logicName, r)
2545
if err != nil {
2646
return err
2747
}
@@ -30,7 +50,7 @@ func genLogic(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
3050
return nil
3151
}
3252

33-
func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
53+
func genLogicByRoute(dir, rootPkg string, cfg *config.Config, logicName string, route spec.Route) error {
3454
logic := getLogicName(route)
3555
goFile, err := format.FileNamingFormat(cfg.NamingFormat, logic)
3656
if err != nil {
@@ -53,19 +73,18 @@ func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group,
5373
requestString = "req *" + requestGoTypeName(route, typesPacket)
5474
}
5575

56-
subDir := getLogicFolderPath(group, route)
5776
return genFile(fileGenConfig{
5877
dir: dir,
59-
subdir: subDir,
78+
subdir: logicDir,
6079
filename: goFile + ".go",
6180
templateName: "logicTemplate",
6281
category: category,
6382
templateFile: logicTemplateFile,
6483
builtinTemplate: logicTemplate,
6584
data: map[string]any{
66-
"pkgName": subDir[strings.LastIndex(subDir, "/")+1:],
85+
"pkgName": logicDir,
6786
"imports": imports,
68-
"logic": strings.Title(logic),
87+
"logic": logicName,
6988
"function": strings.Title(strings.TrimSuffix(logic, "Logic")),
7089
"responseType": responseString,
7190
"returnString": returnString,
@@ -76,27 +95,11 @@ func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group,
7695
})
7796
}
7897

79-
func getLogicFolderPath(group spec.Group, route spec.Route) string {
80-
folder := route.GetAnnotation(groupProperty)
81-
if len(folder) == 0 {
82-
folder = group.GetAnnotation(groupProperty)
83-
if len(folder) == 0 {
84-
return logicDir
85-
}
86-
}
87-
folder = strings.TrimPrefix(folder, "/")
88-
folder = strings.TrimSuffix(folder, "/")
89-
return path.Join(logicDir, folder)
90-
}
91-
9298
func genLogicImports(route spec.Route, parentPkg string) string {
9399
var imports []string
94-
imports = append(imports, `"context"`+"\n")
95-
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
96100
if shallImportTypesPackage(route) {
97101
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir)))
98102
}
99-
imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceURL))
100103
return strings.Join(imports, "\n\t")
101104
}
102105

api/gogen/genroutes.go

+3-34
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,22 @@ import (
44
"fmt"
55
"os"
66
"path"
7-
"sort"
87
"strconv"
98
"strings"
109
"text/template"
1110
"time"
1211

1312
"github.com/dxc0522/goctlx/api/spec"
1413
"github.com/dxc0522/goctlx/config"
15-
"github.com/dxc0522/goctlx/internal/version"
1614
"github.com/dxc0522/goctlx/util/format"
1715
"github.com/dxc0522/goctlx/util/pathx"
1816
"github.com/dxc0522/goctlx/vars"
19-
"github.com/zeromicro/go-zero/core/collection"
2017
)
2118

2219
const (
2320
jwtTransKey = "jwtTransition"
2421
routesFilename = "routes"
2522
routesTemplate = `// Code generated by goctl. DO NOT EDIT.
26-
// goctl {{.version}}
2723
2824
package handler
2925
@@ -185,7 +181,7 @@ rest.WithPrefix("%s"),`, g.prefix)
185181
return err
186182
}
187183

188-
routeFilename = routeFilename + ".go"
184+
routeFilename = routeFilename + "_gen.go"
189185
filename := path.Join(dir, handlerDir, routeFilename)
190186
os.Remove(filename)
191187

@@ -201,7 +197,6 @@ rest.WithPrefix("%s"),`, g.prefix)
201197
"hasTimeout": hasTimeout,
202198
"importPackages": genRouteImports(rootPkg, api),
203199
"routesAdditions": strings.TrimSpace(builder.String()),
204-
"version": version.BuildVersion,
205200
},
206201
})
207202
}
@@ -217,24 +212,7 @@ func formatDuration(duration time.Duration) string {
217212
}
218213

219214
func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
220-
importSet := collection.NewSet()
221-
importSet.AddStr(fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
222-
for _, group := range api.Service.Groups {
223-
for _, route := range group.Routes {
224-
folder := route.GetAnnotation(groupProperty)
225-
if len(folder) == 0 {
226-
folder = group.GetAnnotation(groupProperty)
227-
if len(folder) == 0 {
228-
continue
229-
}
230-
}
231-
importSet.AddStr(fmt.Sprintf("%s \"%s\"", toPrefix(folder),
232-
pathx.JoinPackages(parentPkg, handlerDir, folder)))
233-
}
234-
}
235-
imports := importSet.KeysStr()
236-
sort.Strings(imports)
237-
projectSection := strings.Join(imports, "\n\t")
215+
projectSection := fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir))
238216
depSection := fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL)
239217
return fmt.Sprintf("%s\n\n\t%s", projectSection, depSection)
240218
}
@@ -245,17 +223,8 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
245223
for _, g := range api.Service.Groups {
246224
var groupedRoutes group
247225
for _, r := range g.Routes {
248-
handler := getHandlerName(r)
226+
handler := strings.Title(getHandlerName(r))
249227
handler = handler + "(serverCtx)"
250-
folder := r.GetAnnotation(groupProperty)
251-
if len(folder) > 0 {
252-
handler = toPrefix(folder) + "." + strings.ToUpper(handler[:1]) + handler[1:]
253-
} else {
254-
folder = g.GetAnnotation(groupProperty)
255-
if len(folder) > 0 {
256-
handler = toPrefix(folder) + "." + strings.ToUpper(handler[:1]) + handler[1:]
257-
}
258-
}
259228
groupedRoutes.routes = append(groupedRoutes.routes, route{
260229
method: mapping[r.Method],
261230
path: r.Path,

api/gogen/gentypes.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/dxc0522/goctlx/api/spec"
1212
apiutil "github.com/dxc0522/goctlx/api/util"
1313
"github.com/dxc0522/goctlx/config"
14-
"github.com/dxc0522/goctlx/internal/version"
1514
"github.com/dxc0522/goctlx/util"
1615
"github.com/dxc0522/goctlx/util/format"
1716
)
@@ -50,7 +49,7 @@ func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
5049
return err
5150
}
5251

53-
typeFilename = typeFilename + ".go"
52+
typeFilename = typeFilename + "_gen.go"
5453
filename := path.Join(dir, typesDir, typeFilename)
5554
os.Remove(filename)
5655

@@ -65,7 +64,6 @@ func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
6564
data: map[string]any{
6665
"types": val,
6766
"containsTime": false,
68-
"version": version.BuildVersion,
6967
},
7068
})
7169
}

api/gogen/handler.tpl

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Code generated by goctlx. DO NOT EDIT.
12
package {{.PkgName}}
23

34
import (
@@ -19,6 +20,7 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
1920
{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx, r, &w)
2021
{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
2122
if err != nil {
23+
l.Error(err)
2224
httpx.ErrorCtx(r.Context(), w, err)
2325
} else {
2426
{{if .HasResp}}httpx.OkJsonCtx(r.Context(), w, resp){{else}}httpx.Ok(w){{end}}

0 commit comments

Comments
 (0)