-
Notifications
You must be signed in to change notification settings - Fork 98
/
template.go
615 lines (521 loc) · 17.5 KB
/
template.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package parser
import (
"sync"
"text/template"
"github.com/pkg/errors"
)
var (
modelStructTmpl *template.Template
modelStructTmplRaw = `
{{- if .Comment -}}
// {{.TableName}} {{.Comment}}
{{end -}}
type {{.TableName}} struct {
{{- range .Fields}}
{{.Name}} {{.GoType}} {{if .Tag}}` + "`{{.Tag}}`" + `{{end}}{{if .Comment}} // {{.Comment}}{{end}}
{{- end}}
}
{{if .NameFunc}}
// TableName table name
func (m *{{.TableName}}) TableName() string {
return "{{.RawTableName}}"
}
{{end}}
`
modelTmpl *template.Template
modelTmplRaw = `package {{.Package}}
{{if .ImportPath}}
import (
{{- range .ImportPath}}
"{{.}}"
{{- end}}
)
{{- end}}
{{range .StructCode}}
{{.}}
{{end}}`
updateFieldTmpl *template.Template
updateFieldTmplRaw = `
{{- range .Fields}}
if table.{{.Name}}{{.ConditionZero}} {
update["{{.ColName}}"] = table.{{.Name}}
}
{{- end}}`
handlerCreateStructTmpl *template.Template
handlerCreateStructTmplRaw = `
// Create{{.TableName}}Request request params
type Create{{.TableName}}Request struct {
{{- range .Fields}}
{{.Name}} {{.GoType}} ` + "`" + `json:"{{.JSONName}}" binding:""` + "`" + `{{if .Comment}} // {{.Comment}}{{end}}
{{- end}}
}
`
handlerUpdateStructTmpl *template.Template
handlerUpdateStructTmplRaw = `
// Update{{.TableName}}ByIDRequest request params
type Update{{.TableName}}ByIDRequest struct {
{{- range .Fields}}
{{.Name}} {{.GoType}} ` + "`" + `json:"{{.JSONName}}" binding:""` + "`" + `{{if .Comment}} // {{.Comment}}{{end}}
{{- end}}
}
`
handlerDetailStructTmpl *template.Template
handlerDetailStructTmplRaw = `
// {{.TableName}}ObjDetail detail
type {{.TableName}}ObjDetail struct {
{{- range .Fields}}
{{.Name}} {{.GoType}} ` + "`" + `json:"{{.JSONName}}"` + "`" + `{{if .Comment}} // {{.Comment}}{{end}}
{{- end}}
}`
modelJSONTmpl *template.Template
modelJSONTmplRaw = `{
{{- range .Fields}}
"{{.ColName}}" {{.GoZero}}
{{- end}}
}
`
protoFileTmpl *template.Template
protoFileTmplRaw = `syntax = "proto3";
package api.serverNameExample.v1;
import "api/types/types.proto";
import "validate/validate.proto";
option go_package = "github.com/zhufuyi/sponge/api/serverNameExample/v1;v1";
service {{.TName}} {
// create {{.TName}}
rpc Create(Create{{.TableName}}Request) returns (Create{{.TableName}}Reply) {}
// delete {{.TName}} by id
rpc DeleteByID(Delete{{.TableName}}ByIDRequest) returns (Delete{{.TableName}}ByIDReply) {}
// delete {{.TName}} by batch id
rpc DeleteByIDs(Delete{{.TableName}}ByIDsRequest) returns (Delete{{.TableName}}ByIDsReply) {}
// update {{.TName}} by id
rpc UpdateByID(Update{{.TableName}}ByIDRequest) returns (Update{{.TableName}}ByIDReply) {}
// get {{.TName}} by id
rpc GetByID(Get{{.TableName}}ByIDRequest) returns (Get{{.TableName}}ByIDReply) {}
// get {{.TName}} by condition
rpc GetByCondition(Get{{.TableName}}ByConditionRequest) returns (Get{{.TableName}}ByConditionReply) {}
// list of {{.TName}} by batch id
rpc ListByIDs(List{{.TableName}}ByIDsRequest) returns (List{{.TableName}}ByIDsReply) {}
// list {{.TName}} by last id
rpc ListByLastID(List{{.TableName}}ByLastIDRequest) returns (List{{.TableName}}ByLastIDReply) {}
// list of {{.TName}} by query parameters
rpc List(List{{.TableName}}Request) returns (List{{.TableName}}Reply) {}
}
// Some notes on defining fields under message:
// Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
// protoMessageCreateCode
message Create{{.TableName}}Reply {
// createTableReplyFieldCode
}
message Delete{{.TableName}}ByIDRequest {
// deleteTableByIDRequestFieldCode
}
message Delete{{.TableName}}ByIDReply {
}
message Delete{{.TableName}}ByIDsRequest {
// deleteTableByIDsRequestFieldCode
}
message Delete{{.TableName}}ByIDsReply {
}
// protoMessageUpdateCode
message Update{{.TableName}}ByIDReply {
}
// protoMessageDetailCode
message Get{{.TableName}}ByIDRequest {
// getTableByIDRequestFieldCode
}
message Get{{.TableName}}ByIDReply {
{{.TableName}} {{.TName}} = 1;
}
message Get{{.TableName}}ByConditionRequest {
types.Conditions conditions = 1;
}
message Get{{.TableName}}ByConditionReply {
{{.TableName}} {{.TName}} = 1;
}
message List{{.TableName}}ByIDsRequest {
// getTableByIDsRequestFieldCode
}
message List{{.TableName}}ByIDsReply {
repeated {{.TableName}} {{.TName}}s = 1;
}
message List{{.TableName}}ByLastIDRequest {
// listTableByLastIDRequestFieldCode
uint32 limit = 2 [(validate.rules).uint32.gt = 0]; // limit size per page
string sort = 3; // sort by column name of table, default is -id, the - sign indicates descending order.
}
message List{{.TableName}}ByLastIDReply {
repeated {{.TableName}} {{.TName}}s = 1;
}
message List{{.TableName}}Request {
types.Params params = 1;
}
message List{{.TableName}}Reply {
int64 total =1;
repeated {{.TableName}} {{.TName}}s = 2;
}
`
protoFileForWebTmpl *template.Template
protoFileForWebTmplRaw = `syntax = "proto3";
package api.serverNameExample.v1;
import "api/types/types.proto";
import "google/api/annotations.proto";
import "protoc-gen-openapiv2/options/annotations.proto";
import "tagger/tagger.proto";
import "validate/validate.proto";
option go_package = "github.com/zhufuyi/sponge/api/serverNameExample/v1;v1";
// Default settings for generating swagger documents
// NOTE: because json does not support 64 bits, the int64 and uint64 types under *.swagger.json are automatically converted to string types
// Reference https://github.com/grpc-ecosystem/grpc-gateway/blob/db7fbefff7c04877cdb32e16d4a248a024428207/examples/internal/proto/examplepb/a_bit_of_everything.proto
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
host: "localhost:8080"
base_path: ""
info: {
title: "serverNameExample api docs";
version: "2.0";
}
schemes: HTTP;
schemes: HTTPS;
consumes: "application/json";
produces: "application/json";
security_definitions: {
security: {
key: "BearerAuth";
value: {
type: TYPE_API_KEY;
in: IN_HEADER;
name: "Authorization";
description: "Type Bearer your-jwt-token to Value";
}
}
}
};
service {{.TName}} {
// create {{.TName}}
rpc Create(Create{{.TableName}}Request) returns (Create{{.TableName}}Reply) {
option (google.api.http) = {
post: "/api/v1/{{.TName}}"
body: "*"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "create {{.TName}}",
description: "submit information to create {{.TName}}",
};
}
// delete {{.TName}} by id
rpc DeleteByID(Delete{{.TableName}}ByIDRequest) returns (Delete{{.TableName}}ByIDReply) {
option (google.api.http) = {
delete: "/api/v1/{{.TName}}/{id}"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "delete {{.TName}}",
description: "delete {{.TName}} by id",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
// delete {{.TName}} by batch id
rpc DeleteByIDs(Delete{{.TableName}}ByIDsRequest) returns (Delete{{.TableName}}ByIDsReply) {
option (google.api.http) = {
post: "/api/v1/{{.TName}}/delete/ids"
body: "*"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "delete {{.TName}}s by batch id",
description: "delete {{.TName}}s by batch id",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
// update {{.TName}} by id
rpc UpdateByID(Update{{.TableName}}ByIDRequest) returns (Update{{.TableName}}ByIDReply) {
option (google.api.http) = {
put: "/api/v1/{{.TName}}/{id}"
body: "*"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "update {{.TName}}",
description: "update {{.TName}} by id",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
// get {{.TName}} by id
rpc GetByID(Get{{.TableName}}ByIDRequest) returns (Get{{.TableName}}ByIDReply) {
option (google.api.http) = {
get: "/api/v1/{{.TName}}/{id}"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "get {{.TName}} detail",
description: "get {{.TName}} detail by id",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
// get {{.TName}} by condition
rpc GetByCondition(Get{{.TableName}}ByConditionRequest) returns (Get{{.TableName}}ByConditionReply) {
option (google.api.http) = {
post: "/api/v1/{{.TName}}/condition"
body: "*"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "get {{.TName}} detail by condition",
description: "get {{.TName}} detail by condition",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
// list of {{.TName}} by batch id
rpc ListByIDs(List{{.TableName}}ByIDsRequest) returns (List{{.TableName}}ByIDsReply) {
option (google.api.http) = {
post: "/api/v1/{{.TName}}/list/ids"
body: "*"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "list of {{.TName}}s by batch id",
description: "list of {{.TName}}s by batch id",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
// list {{.TName}} by last id
rpc ListByLastID(List{{.TableName}}ByLastIDRequest) returns (List{{.TableName}}ByLastIDReply) {
option (google.api.http) = {
get: "/api/v1/{{.TName}}/list"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "list of {{.TName}} by last id",
description: "list of {{.TName}} by last id",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
// list of {{.TName}} by query parameters
rpc List(List{{.TableName}}Request) returns (List{{.TableName}}Reply) {
option (google.api.http) = {
post: "/api/v1/{{.TName}}/list"
body: "*"
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "list of {{.TName}}s by parameters",
description: "list of {{.TName}}s by paging and conditions",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
}
// Some notes on defining fields under message:
// (1) Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
// (2) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
// you must add annotations for snake case names, such as string foo_bar = 1 [json_name = "foo_bar"],
// to ensure that the front end and back end JSON naming is consistent.
// (3) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
// message must contain the name of the path parameter and the name should be
// added with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
// (4) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
// a form tag must be added when defining the query parameter in the message,
// such as string name = 1 [(tagger.tags) = "form:\"name\""];
// protoMessageCreateCode
message Create{{.TableName}}Reply {
// createTableReplyFieldCode
}
message Delete{{.TableName}}ByIDRequest {
// deleteTableByIDRequestFieldCode
}
message Delete{{.TableName}}ByIDReply {
}
message Delete{{.TableName}}ByIDsRequest {
// deleteTableByIDsRequestFieldCode
}
message Delete{{.TableName}}ByIDsReply {
}
// protoMessageUpdateCode
message Update{{.TableName}}ByIDReply {
}
// protoMessageDetailCode
message Get{{.TableName}}ByIDRequest {
// getTableByIDRequestFieldCode
}
message Get{{.TableName}}ByIDReply {
{{.TableName}} {{.TName}} = 1;
}
message Get{{.TableName}}ByConditionRequest {
types.Conditions conditions = 1;
}
message Get{{.TableName}}ByConditionReply {
{{.TableName}} {{.TName}} = 1;
}
message List{{.TableName}}ByIDsRequest {
// getTableByIDsRequestFieldCode
}
message List{{.TableName}}ByIDsReply {
repeated {{.TableName}} {{.TName}}s = 1;
}
message List{{.TableName}}ByLastIDRequest {
// listTableByLastIDRequestFieldCode
uint32 limit = 2 [(validate.rules).uint32.gt = 0, (tagger.tags) = "form:\"limit\""]; // limit size per page
string sort = 3 [(tagger.tags) = "form:\"sort\""]; // sort by column name of table, default is -id, the - sign indicates descending order.
}
message List{{.TableName}}ByLastIDReply {
repeated {{.TableName}} {{.TName}}s = 1;
}
message List{{.TableName}}Request {
types.Params params = 1;
}
message List{{.TableName}}Reply {
int64 total =1;
repeated {{.TableName}} {{.TName}}s = 2;
}
`
protoMessageCreateTmpl *template.Template
protoMessageCreateTmplRaw = `message Create{{.TableName}}Request {
{{- range $i, $v := .Fields}}
{{$v.GoType}} {{$v.JSONName}} = {{$v.AddOne $i}}; {{if $v.Comment}} // {{$v.Comment}}{{end}}
{{- end}}
}`
protoMessageUpdateTmpl *template.Template
protoMessageUpdateTmplRaw = `message Update{{.TableName}}ByIDRequest {
{{- range $i, $v := .Fields}}
{{$v.GoType}} {{$v.JSONName}} = {{$v.AddOneWithTag $i}}; {{if $v.Comment}} // {{$v.Comment}}{{end}}
{{- end}}
}`
protoMessageDetailTmpl *template.Template
protoMessageDetailTmplRaw = `message {{.TableName}} {
{{- range $i, $v := .Fields}}
{{$v.GoType}} {{$v.JSONName}} = {{$v.AddOne $i}}; {{if $v.Comment}} // {{$v.Comment}}{{end}}
{{- end}}
}`
serviceStructTmpl *template.Template
serviceStructTmplRaw = `
{
name: "Create",
fn: func() (interface{}, error) {
// todo enter parameters before testing
// serviceCreateStructCode
},
wantErr: false,
},
{
name: "UpdateByID",
fn: func() (interface{}, error) {
// todo enter parameters before testing
// serviceUpdateStructCode
},
wantErr: false,
},
`
serviceCreateStructTmpl *template.Template
serviceCreateStructTmplRaw = ` req := &serverNameExampleV1.Create{{.TableName}}Request{
{{- range .Fields}}
{{.Name}}: {{.GoTypeZero}}, {{if .Comment}} // {{.Comment}}{{end}}
{{- end}}
}
return cli.Create(ctx, req)`
serviceUpdateStructTmpl *template.Template
serviceUpdateStructTmplRaw = ` req := &serverNameExampleV1.Update{{.TableName}}ByIDRequest{
{{- range .Fields}}
{{.Name}}: {{.GoTypeZero}}, {{if .Comment}} // {{.Comment}}{{end}}
{{- end}}
}
return cli.UpdateByID(ctx, req)`
tmplParseOnce sync.Once
)
func initTemplate() {
tmplParseOnce.Do(func() {
var err, errSum error
modelStructTmpl, err = template.New("goStruct").Parse(modelStructTmplRaw)
if err != nil {
errSum = errors.Wrap(err, "modelStructTmplRaw")
}
modelTmpl, err = template.New("goFile").Parse(modelTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "modelTmplRaw:"+err.Error())
}
updateFieldTmpl, err = template.New("goUpdateField").Parse(updateFieldTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "updateFieldTmplRaw:"+err.Error())
}
handlerCreateStructTmpl, err = template.New("goPostStruct").Parse(handlerCreateStructTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "handlerCreateStructTmplRaw:"+err.Error())
}
handlerUpdateStructTmpl, err = template.New("goPutStruct").Parse(handlerUpdateStructTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "handlerUpdateStructTmplRaw:"+err.Error())
}
handlerDetailStructTmpl, err = template.New("goGetStruct").Parse(handlerDetailStructTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "handlerDetailStructTmplRaw:"+err.Error())
}
modelJSONTmpl, err = template.New("modelJSON").Parse(modelJSONTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "modelJSONTmplRaw:"+err.Error())
}
protoFileTmpl, err = template.New("protoFile").Parse(protoFileTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "protoFileTmplRaw:"+err.Error())
}
protoFileForWebTmpl, err = template.New("protoFileForWeb").Parse(protoFileForWebTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "protoFileForWebTmplRaw:"+err.Error())
}
protoMessageCreateTmpl, err = template.New("protoMessageCreate").Parse(protoMessageCreateTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "protoMessageCreateTmplRaw:"+err.Error())
}
protoMessageUpdateTmpl, err = template.New("protoMessageUpdate").Parse(protoMessageUpdateTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "protoMessageUpdateTmplRaw:"+err.Error())
}
protoMessageDetailTmpl, err = template.New("protoMessageDetail").Parse(protoMessageDetailTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "protoMessageDetailTmplRaw:"+err.Error())
}
serviceCreateStructTmpl, err = template.New("serviceCreateStruct").Parse(serviceCreateStructTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "serviceCreateStructTmplRaw:"+err.Error())
}
serviceUpdateStructTmpl, err = template.New("serviceUpdateStruct").Parse(serviceUpdateStructTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "serviceUpdateStructTmplRaw:"+err.Error())
}
serviceStructTmpl, err = template.New("serviceStruct").Parse(serviceStructTmplRaw)
if err != nil {
errSum = errors.Wrap(errSum, "serviceStructTmplRaw:"+err.Error())
}
if errSum != nil {
panic(errSum)
}
})
}