Skip to content

Commit 7cb5b70

Browse files
committed
refactor: optimize generate api logic
1 parent d55db7f commit 7cb5b70

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

pkg/api/generate.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ func GenerateAPI(c Config, fs []*util.StructField) (string, error) {
264264
err := generator.ExecuteTemplate(buffer, outTplName, struct {
265265
TableComment string
266266
StructName string // camel
267+
StructNamePlural string // camel plural
267268
RouteName string // snake or kebab
268269
GroupName string // lower
269270
APIInfo string
@@ -286,6 +287,7 @@ func GenerateAPI(c Config, fs []*util.StructField) (string, error) {
286287
}{
287288
TableComment: c.TableComment,
288289
StructName: c.StructName,
290+
StructNamePlural: inflection.Plural(c.StructName),
289291
RouteName: routeName,
290292
GroupName: gc.GroupName,
291293
APIInfo: buildAPIInfo(c),

pkg/api/tpl/out.tpl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ service {{ .ServiceName }} {
5454
@doc (
5555
summary: "批量创建{{ .TableComment }}"
5656
)
57-
@handler Create{{ .StructName }}s
58-
post /{{ .RouteName }}/batch/create (Create{{ .StructName }}sReq) returns (Create{{ .StructName }}sResp)
57+
@handler Create{{ .StructNamePlural }}
58+
post /{{ .RouteName }}/batch/create (Create{{ .StructNamePlural }}Req) returns (Create{{ .StructNamePlural }}Resp)
5959

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

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

7373
// -------------------- {{ .TableComment }} {{ .StructName }} -------------------- //
@@ -156,33 +156,33 @@ type List{{ .StructName }}Resp {
156156
Results []*{{ .StructName }} `json:"results"` // 结果
157157
}
158158

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

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

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

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

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

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

0 commit comments

Comments
 (0)