Skip to content

Commit 9b65e1a

Browse files
committed
match name xxxID in proto file
1 parent 5f30dd9 commit 9b65e1a

File tree

7 files changed

+55
-24
lines changed

7 files changed

+55
-24
lines changed

internal/service/userExample.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
191191
if err != nil {
192192
return nil, err
193193
}
194+
// Note: if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
194195
value.Id = record.ID
195-
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
196196
// todo generate the conversion createdAt and updatedAt code here
197197
// delete the templates code start
198198
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)

internal/service/userExample.go.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
316316
if err != nil {
317317
return nil, err
318318
}
319+
// Note: if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
319320
value.Id = record.ID
320-
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
321321
// todo generate the conversion createdAt and updatedAt code here
322322
// delete the templates code start
323323
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)

internal/service/userExample.go.mgo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
191191
if err != nil {
192192
return nil, err
193193
}
194+
// Note: if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
194195
value.Id = record.ID.Hex()
195-
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
196196
// todo generate the conversion createdAt and updatedAt code here
197197
// delete the templates code start
198198
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)

internal/service/userExample.go.mgo.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
316316
if err != nil {
317317
return nil, err
318318
}
319+
// Note: if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
319320
value.Id = record.ID.Hex()
320-
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
321321
// todo generate the conversion createdAt and updatedAt code here
322322
// delete the templates code start
323323
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)

pkg/sql2code/parser/parser.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ func goTypeToProto(fields []tmplField) []tmplField {
10461046
field.GoType = "uint64"
10471047
}
10481048
}
1049-
1049+
field.JSONName = customToCamel(field.ColName)
10501050
newFields = append(newFields, field)
10511051
}
10521052
return newFields
@@ -1080,9 +1080,8 @@ func getDefaultValue(expr ast.ExprNode) (value string) {
10801080
}
10811081

10821082
var acronym = map[string]struct{}{
1083-
"ID": {},
1084-
"IP": {},
1085-
"RPC": {},
1083+
"ID": {},
1084+
"IP": {},
10861085
}
10871086

10881087
func toCamel(s string) string {
@@ -1139,3 +1138,10 @@ func firstLetterToLow(str string) string {
11391138

11401139
return str
11411140
}
1141+
1142+
func customToCamel(str string) string {
1143+
if strings.ToLower(str) == "id" {
1144+
return str
1145+
}
1146+
return firstLetterToLow(toCamel(str))
1147+
}

pkg/sql2code/parser/parser_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,15 @@ func TestParseSQLs(t *testing.T) {
116116
}
117117

118118
func Test_toCamel(t *testing.T) {
119-
str := "user_example"
120-
t.Log(toCamel(str))
119+
names := []string{"id", "user_id", "productId", "orderId", "user_name", "host_ip", "teacherId"}
120+
var convertNames []string
121+
var convertNames2 []string
122+
for _, name := range names {
123+
convertNames = append(convertNames, toCamel(name))
124+
convertNames2 = append(convertNames2, customToCamel(name))
125+
}
126+
t.Log(convertNames)
127+
t.Log(convertNames2)
121128
}
122129

123130
func Test_parseOption(t *testing.T) {

pkg/sql2code/parser/template.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ message List{{.TableName}}Request {
161161
}
162162
163163
message List{{.TableName}}Reply {
164-
int64 total =1;
164+
int64 total = 1;
165165
repeated {{.TableName}} {{.TName}}s = 2;
166166
}
167167
@@ -265,7 +265,7 @@ message List{{.TableName}}Request {
265265
}
266266
267267
message List{{.TableName}}Reply {
268-
int64 total =1;
268+
int64 total = 1;
269269
repeated {{.TableName}} {{.TName}}s = 2;
270270
}
271271
`
@@ -320,6 +320,12 @@ service {{.TName}} {
320320
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
321321
summary: "create {{.TName}}",
322322
description: "submit information to create {{.TName}}",
323+
//security: {
324+
// security_requirement: {
325+
// key: "BearerAuth";
326+
// value: {}
327+
// }
328+
//}
323329
};
324330
}
325331
@@ -465,17 +471,20 @@ service {{.TName}} {
465471
}
466472
}
467473
474+
468475
// Some notes on defining fields under message:
469476
// (1) Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
470-
// (2) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
471-
// you must add annotations for snake case names, such as string foo_bar = 1 [json_name = "foo_bar"],
477+
// (2) Suggest using camel hump naming for message field names, and for names ending in 'id',
478+
// use xxxID naming format, such as userID, orderID, etc.
479+
// (3) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
480+
// you must add annotations for snake case names, such as string fieldName = 1 [json_name = "field_name"],
472481
// to ensure that the front end and back end JSON naming is consistent.
473-
// (3) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
482+
// (4) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
474483
// message must contain the name of the path parameter and the name should be
475484
// added with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
476-
// (4) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
485+
// (5) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
477486
// a form tag must be added when defining the query parameter in the message,
478-
// such as string name = 1 [(tagger.tags) = "form:\"name\""];
487+
// such as string name = 1 [(tagger.tags) = "form:\"name\""].
479488
480489
481490
// protoMessageCreateCode
@@ -513,7 +522,7 @@ message List{{.TableName}}Request {
513522
}
514523
515524
message List{{.TableName}}Reply {
516-
int64 total =1;
525+
int64 total = 1;
517526
repeated {{.TableName}} {{.TName}}s = 2;
518527
}
519528
@@ -602,6 +611,12 @@ service {{.TName}} {
602611
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
603612
summary: "create {{.TName}}",
604613
description: "submit information to create {{.TName}}",
614+
//security: {
615+
// security_requirement: {
616+
// key: "BearerAuth";
617+
// value: {}
618+
// }
619+
//}
605620
};
606621
}
607622
@@ -676,17 +691,20 @@ service {{.TName}} {
676691
}
677692
}
678693
694+
679695
// Some notes on defining fields under message:
680696
// (1) Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
681-
// (2) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
682-
// you must add annotations for snake case names, such as string foo_bar = 1 [json_name = "foo_bar"],
697+
// (2) Suggest using camel hump naming for message field names, and for names ending in 'id',
698+
// use xxxID naming format, such as userID, orderID, etc.
699+
// (3) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
700+
// you must add annotations for snake case names, such as string fieldName = 1 [json_name = "field_name"],
683701
// to ensure that the front end and back end JSON naming is consistent.
684-
// (3) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
702+
// (4) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
685703
// message must contain the name of the path parameter and the name should be
686704
// added with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
687-
// (4) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
705+
// (5) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
688706
// a form tag must be added when defining the query parameter in the message,
689-
// such as string name = 1 [(tagger.tags) = "form:\"name\""];
707+
// such as string name = 1 [(tagger.tags) = "form:\"name\""].
690708
691709
692710
// protoMessageCreateCode
@@ -724,7 +742,7 @@ message List{{.TableName}}Request {
724742
}
725743
726744
message List{{.TableName}}Reply {
727-
int64 total =1;
745+
int64 total = 1;
728746
repeated {{.TableName}} {{.TName}}s = 2;
729747
}
730748
`

0 commit comments

Comments
 (0)