Skip to content

Commit 4ff34a8

Browse files
committed
refactor: adjust generated api code
1 parent e6d2afc commit 4ff34a8

File tree

5 files changed

+88
-71
lines changed

5 files changed

+88
-71
lines changed

README_zh-CN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ route_style: "kebab"
102102
query_style: "value"
103103
# 是否需要路由单词复数
104104
enable_plural: true
105+
# 是否启用结构体缩写,如 Request -> Req,Response -> Resp 等
106+
enable_struct_abbr: true
105107

106108
# 代码生成相关配置
107109
# 将要生成的文件所在目录,如果为相对路径,则相对于命令执行路径生成,必填
@@ -191,6 +193,8 @@ route_style: "kebab"
191193
query_style: "value"
192194
# 是否需要路由单词复数
193195
enable_plural: true
196+
# 是否启用结构体缩写,如 Request -> Req,Response -> Resp 等
197+
enable_struct_abbr: true
194198

195199
# 代码生成相关配置
196200
# 将要生成的文件所在目录,如果为相对路径,则相对于命令执行路径生成,必填

pkg/api/cmd/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ route_style: "kebab"
4747
query_style: "value"
4848
# 是否需要路由单词复数
4949
enable_plural: true
50+
# 是否启用结构体缩写,如 Request -> Req,Response -> Resp 等
51+
enable_struct_abbr: true
5052

5153
# 代码生成相关配置
5254
# 将要生成的文件所在目录,如果为相对路径,则相对于命令执行路径生成,必填

pkg/api/generate.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,20 @@ func GenerateAPI(c Config, fs []*util.StructField) (string, error) {
258258
if c.EnablePlural {
259259
routeName = inflection.Plural(routeName)
260260
}
261+
reqName, respName := "Req", "Resp"
262+
if !c.EnableStructAbbr {
263+
reqName, respName = "Request", "Response"
264+
}
261265
structGetInfo := buildStructGetInfo(gc.StructFields, c.QueryStyle == QueryStylePointer)
262266
err := generator.ExecuteTemplate(buffer, outTplName, struct {
267+
ReqName string
268+
RespName string
263269
TableComment string
264270
StructName string // camel
265271
StructNamePlural string // camel plural
266272
RouteName string // snake or kebab
267273
GroupName string // lower
274+
Delimiter string
268275
APIInfo string
269276
ServiceName string
270277
RoutePrefix string
@@ -283,11 +290,14 @@ func GenerateAPI(c Config, fs []*util.StructField) (string, error) {
283290
StructFilterInfo string
284291
StructBatchUpdateInfo string
285292
}{
293+
ReqName: reqName,
294+
RespName: respName,
286295
TableComment: c.TableComment,
287296
StructName: c.StructName,
288297
StructNamePlural: inflection.Plural(c.StructName),
289298
RouteName: routeName,
290299
GroupName: gc.GroupName,
300+
Delimiter: string(c.GetDelimiter()),
291301
APIInfo: buildAPIInfo(c),
292302
ServiceName: c.ServiceName,
293303
RoutePrefix: strings.Trim(c.RoutePrefix, `/`),

pkg/api/tpl/out.tpl

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,64 @@ syntax = "v1"
1010
)
1111
service {{ .ServiceName }} {
1212
@doc (
13-
summary: "查询{{ .TableComment }}"
13+
summary: "获取{{ .TableComment }}"
1414
)
1515
@handler Get{{ .StructName }}
16-
get /{{ .RouteName }}/:{{ .IdRawName }} (Get{{ .StructName }}Req) returns (Get{{ .StructName }}Resp)
16+
get /{{ .RouteName }}/:{{ .IdRawName }} (Get{{ .StructName }}{{ .ReqName }}) returns (Get{{ .StructName }}{{ .RespName }})
1717

1818
@doc (
19-
summary: "获取{{ .TableComment }}分页"
19+
summary: "列出{{ .TableComment }}"
2020
)
21-
@handler Paginate{{ .StructName }}
22-
get /{{ .RouteName }} (Paginate{{ .StructName }}Req) returns (Paginate{{ .StructName }}Resp)
21+
@handler List{{ .StructNamePlural }}
22+
get /{{ .RouteName }} (List{{ .StructNamePlural }}{{ .ReqName }}) returns (List{{ .StructNamePlural }}{{ .RespName }})
2323

2424
@doc (
2525
summary: "创建{{ .TableComment }}"
2626
)
2727
@handler Create{{ .StructName }}
28-
post /{{ .RouteName }} (Create{{ .StructName }}Req) returns (Create{{ .StructName }}Resp)
28+
post /{{ .RouteName }} (Create{{ .StructName }}{{ .ReqName }}) returns (Create{{ .StructName }}{{ .RespName }})
2929

3030
@doc (
3131
summary: "更新{{ .TableComment }}"
3232
)
3333
@handler Update{{ .StructName }}
34-
put /{{ .RouteName }}/:{{ .IdRawName }} (Update{{ .StructName }}Req) returns (Update{{ .StructName }}Resp)
34+
put /{{ .RouteName }}/:{{ .IdRawName }} (Update{{ .StructName }}{{ .ReqName }}) returns (Update{{ .StructName }}{{ .RespName }})
3535

3636
@doc (
3737
summary: "删除{{ .TableComment }}"
3838
)
3939
@handler Delete{{ .StructName }}
40-
delete /{{ .RouteName }}/:{{ .IdRawName }} (Delete{{ .StructName }}Req) returns (Delete{{ .StructName }}Resp)
40+
delete /{{ .RouteName }}/:{{ .IdRawName }} (Delete{{ .StructName }}{{ .ReqName }}) returns (Delete{{ .StructName }}{{ .RespName }})
4141

4242
@doc (
43-
summary: "部分更新{{ .TableComment }}"
43+
summary: "修补{{ .TableComment }}"
4444
)
4545
@handler Patch{{ .StructName }}
46-
patch /{{ .RouteName }}/:{{ .IdRawName }} (Patch{{ .StructName }}Req) returns (Patch{{ .StructName }}Resp)
46+
patch /{{ .RouteName }}/:{{ .IdRawName }} (Patch{{ .StructName }}{{ .ReqName }}) returns (Patch{{ .StructName }}{{ .RespName }})
4747

4848
@doc (
49-
summary: "获取{{ .TableComment }}列表"
49+
summary: "批量获取{{ .TableComment }}"
5050
)
51-
@handler List{{ .StructName }}
52-
post /{{ .RouteName }}/list (List{{ .StructName }}Req) returns (List{{ .StructName }}Resp)
51+
@handler BatchGet{{ .StructNamePlural }}
52+
post /{{ .RouteName }}/batch{{ .Delimiter }}get (BatchGet{{ .StructNamePlural }}{{ .ReqName }}) returns (BatchGet{{ .StructNamePlural }}{{ .RespName }})
5353

5454
@doc (
5555
summary: "批量创建{{ .TableComment }}"
5656
)
57-
@handler Create{{ .StructNamePlural }}
58-
post /{{ .RouteName }}/batch/create (Create{{ .StructNamePlural }}Req) returns (Create{{ .StructNamePlural }}Resp)
57+
@handler BatchCreate{{ .StructNamePlural }}
58+
post /{{ .RouteName }}/batch{{ .Delimiter }}create (BatchCreate{{ .StructNamePlural }}{{ .ReqName }}) returns (BatchCreate{{ .StructNamePlural }}{{ .RespName }})
5959

6060
@doc (
6161
summary: "批量更新{{ .TableComment }}"
6262
)
63-
@handler Update{{ .StructNamePlural }}
64-
post /{{ .RouteName }}/batch/update (Update{{ .StructNamePlural }}Req) returns (Update{{ .StructNamePlural }}Resp)
63+
@handler BatchUpdate{{ .StructNamePlural }}
64+
post /{{ .RouteName }}/batch{{ .Delimiter }}update (BatchUpdate{{ .StructNamePlural }}{{ .ReqName }}) returns (BatchUpdate{{ .StructNamePlural }}{{ .RespName }})
6565

6666
@doc (
6767
summary: "批量删除{{ .TableComment }}"
6868
)
69-
@handler Delete{{ .StructNamePlural }}
70-
post /{{ .RouteName }}/batch/delete (Delete{{ .StructNamePlural }}Req) returns (Delete{{ .StructNamePlural }}Resp)
69+
@handler BatchDelete{{ .StructNamePlural }}
70+
post /{{ .RouteName }}/batch{{ .Delimiter }}delete (BatchDelete{{ .StructNamePlural }}{{ .ReqName }}) returns (BatchDelete{{ .StructNamePlural }}{{ .RespName }})
7171
}
7272

7373
// -------------------- {{ .TableComment }} {{ .StructName }} -------------------- //
@@ -76,113 +76,113 @@ type {{ .StructName }} {
7676
{{ .StructInfo }}
7777
}
7878

79-
// Get{{ .StructName }}Req 查询{{ .TableComment }}请求
80-
type Get{{ .StructName }}Req {
79+
// Get{{ .StructName }}{{ .ReqName }} 获取{{ .TableComment }}请求
80+
type Get{{ .StructName }}{{ .ReqName }} {
8181
{{ .IdName }} {{ .IdType }} `path:"{{ .IdRawName }}" validate:"required" label:"{{ .IdLabel }}"` // {{ .IdComment }}
8282
}
8383

84-
// Get{{ .StructName }}Resp 查询{{ .TableComment }}响应
85-
type Get{{ .StructName }}Resp {
84+
// Get{{ .StructName }}{{ .RespName }} 获取{{ .TableComment }}响应
85+
type Get{{ .StructName }}{{ .RespName }} {
8686
{{ .StructName }}
8787
}
8888

89-
// Paginate{{ .StructName }}Req 获取{{ .TableComment }}分页请求
90-
type Paginate{{ .StructName }}Req {
89+
// List{{ .StructNamePlural }}{{ .ReqName }} 列出{{ .TableComment }}请求
90+
type List{{ .StructNamePlural }}{{ .ReqName }} {
9191
{{ .StructGetInfo }}
9292
Page int64 `form:"page" validate:"required" label:"页数"` // 页数
9393
PageSize int64 `form:"page_size" validate:"required" label:"每条页数"` // 每条页数
9494
}
9595

96-
// Paginate{{ .StructName }}Resp 获取{{ .TableComment }}分页响应
97-
type Paginate{{ .StructName }}Resp {
96+
// List{{ .StructNamePlural }}{{ .RespName }} 列出{{ .TableComment }}响应
97+
type List{{ .StructNamePlural }}{{ .RespName }} {
9898
Count int64 `json:"count"` // 总数
9999
PageCount int64 `json:"page_count"` // 页数
100100
Results []*{{ .StructName }} `json:"results"` // 结果
101101
}
102102

103-
// Create{{ .StructName }}Req 创建{{ .TableComment }}请求
104-
type Create{{ .StructName }}Req {
103+
// Create{{ .StructName }}{{ .ReqName }} 创建{{ .TableComment }}请求
104+
type Create{{ .StructName }}{{ .ReqName }} {
105105
{{ .StructCreateInfo }}
106106
}
107107

108-
// Create{{ .StructName }}Resp 创建{{ .TableComment }}响应
109-
type Create{{ .StructName }}Resp {
108+
// Create{{ .StructName }}{{ .RespName }} 创建{{ .TableComment }}响应
109+
type Create{{ .StructName }}{{ .RespName }} {
110110
{{ .StructName }}
111111
}
112112

113-
// Update{{ .StructName }}Req 更新{{ .TableComment }}请求
114-
type Update{{ .StructName }}Req {
113+
// Update{{ .StructName }}{{ .ReqName }} 更新{{ .TableComment }}请求
114+
type Update{{ .StructName }}{{ .ReqName }} {
115115
{{ .StructUpdateInfo }}
116116
}
117117

118-
// Update{{ .StructName }}Resp 更新{{ .TableComment }}响应
119-
type Update{{ .StructName }}Resp {
118+
// Update{{ .StructName }}{{ .RespName }} 更新{{ .TableComment }}响应
119+
type Update{{ .StructName }}{{ .RespName }} {
120120
{{ .StructName }}
121121
}
122122

123-
// Delete{{ .StructName }}Req 删除{{ .TableComment }}请求
124-
type Delete{{ .StructName }}Req {
123+
// Delete{{ .StructName }}{{ .ReqName }} 删除{{ .TableComment }}请求
124+
type Delete{{ .StructName }}{{ .ReqName }} {
125125
{{ .IdName }} {{ .IdType }} `path:"{{ .IdRawName }}" validate:"required" label:"{{ .IdLabel }}"` // {{ .IdComment }}
126126
}
127127

128-
// Delete{{ .StructName }}Resp 删除{{ .TableComment }}响应
129-
type Delete{{ .StructName }}Resp {
128+
// Delete{{ .StructName }}{{ .RespName }} 删除{{ .TableComment }}响应
129+
type Delete{{ .StructName }}{{ .RespName }} {
130130
{{ .IdName }} {{ .IdType }} `json:"{{ .IdRawName }}"` // {{ .IdComment }}
131131
}
132132

133-
// Patch{{ .StructName }}Req 部分更新{{ .TableComment }}请求
134-
type Patch{{ .StructName }}Req {
133+
// Patch{{ .StructName }}{{ .ReqName }} 修补{{ .TableComment }}请求
134+
type Patch{{ .StructName }}{{ .ReqName }} {
135135
{{ .StructUpdateInfo }}
136136
}
137137

138-
// Patch{{ .StructName }}Resp 部分更新{{ .TableComment }}响应
139-
type Patch{{ .StructName }}Resp {
138+
// Patch{{ .StructName }}{{ .RespName }} 修补{{ .TableComment }}响应
139+
type Patch{{ .StructName }}{{ .RespName }} {
140140
{{ .StructName }}
141141
}
142142

143-
// {{ .StructName }}Filter {{ .TableComment }}筛选参数
143+
// {{ .StructName }}Filter {{ .TableComment }}过滤参数
144144
type {{ .StructName }}Filter {
145145
{{ .IdNamePlural }} []{{ .IdType }} `json:"{{ .IdRawNamePlural }},optional"` // {{ .IdComment }}列表
146146
{{ .StructFilterInfo }}
147147
}
148148

149-
// List{{ .StructName }}Req 获取{{ .TableComment }}列表请求
150-
type List{{ .StructName }}Req {
151-
Filter {{ .StructName }}Filter `json:"filter"` // {{ .TableComment }}筛选参数
149+
// BatchGet{{ .StructNamePlural }}{{ .ReqName }} 批量获取{{ .TableComment }}请求
150+
type BatchGet{{ .StructNamePlural }}{{ .ReqName }} {
151+
Filter {{ .StructName }}Filter `json:"filter"` // {{ .TableComment }}过滤参数
152152
}
153153

154-
// List{{ .StructName }}Resp 获取{{ .TableComment }}列表响应
155-
type List{{ .StructName }}Resp {
154+
// BatchGet{{ .StructNamePlural }}{{ .RespName }} 批量获取{{ .TableComment }}响应
155+
type BatchGet{{ .StructNamePlural }}{{ .RespName }} {
156156
Results []*{{ .StructName }} `json:"results"` // 结果
157157
}
158158

159-
// Create{{ .StructNamePlural }}Req 批量创建{{ .TableComment }}请求
160-
type Create{{ .StructNamePlural }}Req {
161-
Objects []*Create{{ .StructName }}Req `json:"objects" validate:"gt=0,dive" label:"{{ .TableComment }}列表"` // {{ .TableComment }}列表
159+
// BatchCreate{{ .StructNamePlural }}{{ .ReqName }} 批量创建{{ .TableComment }}请求
160+
type BatchCreate{{ .StructNamePlural }}{{ .ReqName }} {
161+
Objects []*Create{{ .StructName }}{{ .ReqName }} `json:"objects" validate:"gt=0,dive" label:"{{ .TableComment }}列表"` // {{ .TableComment }}列表
162162
}
163163

164-
// Create{{ .StructNamePlural }}Resp 批量创建{{ .TableComment }}响应
165-
type Create{{ .StructNamePlural }}Resp {
164+
// BatchCreate{{ .StructNamePlural }}{{ .RespName }} 批量创建{{ .TableComment }}响应
165+
type BatchCreate{{ .StructNamePlural }}{{ .RespName }} {
166166
Results []*{{ .StructName }} `json:"results"` // 结果
167167
}
168168

169-
// Update{{ .StructNamePlural }}Req 批量更新{{ .TableComment }}请求
170-
type Update{{ .StructNamePlural }}Req {
171-
Filter {{ .StructName }}Filter `json:"filter"` // {{ .TableComment }}筛选参数
169+
// BatchUpdate{{ .StructNamePlural }}{{ .ReqName }} 批量更新{{ .TableComment }}请求
170+
type BatchUpdate{{ .StructNamePlural }}{{ .ReqName }} {
171+
Filter {{ .StructName }}Filter `json:"filter"` // {{ .TableComment }}过滤参数
172172
{{ .StructBatchUpdateInfo }}
173173
}
174174

175-
// Update{{ .StructNamePlural }}Resp 批量更新{{ .TableComment }}响应
176-
type Update{{ .StructNamePlural }}Resp {
175+
// BatchUpdate{{ .StructNamePlural }}{{ .RespName }} 批量更新{{ .TableComment }}响应
176+
type BatchUpdate{{ .StructNamePlural }}{{ .RespName }} {
177177
Affected int64 `json:"affected"` // 影响数量
178178
}
179179

180-
// Delete{{ .StructNamePlural }}Req 批量删除{{ .TableComment }}请求
181-
type Delete{{ .StructNamePlural }}Req {
182-
Filter {{ .StructName }}Filter `json:"filter"` // {{ .TableComment }}筛选参数
180+
// BatchDelete{{ .StructNamePlural }}{{ .ReqName }} 批量删除{{ .TableComment }}请求
181+
type BatchDelete{{ .StructNamePlural }}{{ .ReqName }} {
182+
Filter {{ .StructName }}Filter `json:"filter"` // {{ .TableComment }}过滤参数
183183
}
184184

185-
// Delete{{ .StructNamePlural }}Resp 批量删除{{ .TableComment }}响应
186-
type Delete{{ .StructNamePlural }}Resp {
185+
// BatchDelete{{ .StructNamePlural }}{{ .RespName }} 批量删除{{ .TableComment }}响应
186+
type BatchDelete{{ .StructNamePlural }}{{ .RespName }} {
187187
Affected int64 `json:"affected"` // 影响数量
188188
}

pkg/api/types.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ type Config struct {
2525
Email string `json:"email"`
2626
Version string `json:"version"`
2727

28-
ServiceName string `json:"service_name"` // camel
29-
RoutePrefix string `json:"route_prefix"` // lower
30-
GroupPrefix string `json:"group_prefix"` // lower
31-
RouteStyle string `json:"route_style"` // one of [snake, kebab], default is kebab
32-
QueryStyle string `json:"query_style"` // one of [value, pointer], default is pointer
33-
EnablePlural bool `json:"enable_plural"`
34-
EnableModel bool `json:"enable_model"`
28+
ServiceName string `json:"service_name"` // camel
29+
RoutePrefix string `json:"route_prefix"` // lower
30+
GroupPrefix string `json:"group_prefix"` // lower
31+
RouteStyle string `json:"route_style"` // one of [snake, kebab], default is kebab
32+
QueryStyle string `json:"query_style"` // one of [value, pointer], default is pointer
33+
EnablePlural bool `json:"enable_plural"`
34+
EnableModel bool `json:"enable_model"`
35+
EnableStructAbbr bool `json:"enable_struct_abbr"`
3536
}
3637

3738
// GetCmdConfig gets the *util.CmdConfig.

0 commit comments

Comments
 (0)