Skip to content

Commit

Permalink
match name xxxID in proto file
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufuyi committed Jul 27, 2024
1 parent 5f30dd9 commit 9b65e1a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion internal/service/userExample.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
if err != nil {
return nil, err
}
// Note: if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
value.Id = record.ID
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
// todo generate the conversion createdAt and updatedAt code here
// delete the templates code start
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/userExample.go.exp
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
if err != nil {
return nil, err
}
// Note: if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
value.Id = record.ID
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
// todo generate the conversion createdAt and updatedAt code here
// delete the templates code start
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/userExample.go.mgo
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
if err != nil {
return nil, err
}
// Note: if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
value.Id = record.ID.Hex()
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
// todo generate the conversion createdAt and updatedAt code here
// delete the templates code start
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/userExample.go.mgo.exp
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ func convertUserExample(record *model.UserExample) (*serverNameExampleV1.UserExa
if err != nil {
return nil, err
}
// Note: if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
value.Id = record.ID.Hex()
// todo if copier.Copy cannot assign a value to a field, add it here, e.g. CreatedAt, UpdatedAt
// todo generate the conversion createdAt and updatedAt code here
// delete the templates code start
value.CreatedAt = record.CreatedAt.Format(time.RFC3339)
Expand Down
14 changes: 10 additions & 4 deletions pkg/sql2code/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ func goTypeToProto(fields []tmplField) []tmplField {
field.GoType = "uint64"
}
}

field.JSONName = customToCamel(field.ColName)
newFields = append(newFields, field)
}
return newFields
Expand Down Expand Up @@ -1080,9 +1080,8 @@ func getDefaultValue(expr ast.ExprNode) (value string) {
}

var acronym = map[string]struct{}{
"ID": {},
"IP": {},
"RPC": {},
"ID": {},
"IP": {},
}

func toCamel(s string) string {
Expand Down Expand Up @@ -1139,3 +1138,10 @@ func firstLetterToLow(str string) string {

return str
}

func customToCamel(str string) string {
if strings.ToLower(str) == "id" {
return str
}
return firstLetterToLow(toCamel(str))
}
11 changes: 9 additions & 2 deletions pkg/sql2code/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,15 @@ func TestParseSQLs(t *testing.T) {
}

func Test_toCamel(t *testing.T) {
str := "user_example"
t.Log(toCamel(str))
names := []string{"id", "user_id", "productId", "orderId", "user_name", "host_ip", "teacherId"}
var convertNames []string
var convertNames2 []string
for _, name := range names {
convertNames = append(convertNames, toCamel(name))
convertNames2 = append(convertNames2, customToCamel(name))
}
t.Log(convertNames)
t.Log(convertNames2)
}

func Test_parseOption(t *testing.T) {
Expand Down
46 changes: 32 additions & 14 deletions pkg/sql2code/parser/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ message List{{.TableName}}Request {
}
message List{{.TableName}}Reply {
int64 total =1;
int64 total = 1;
repeated {{.TableName}} {{.TName}}s = 2;
}
Expand Down Expand Up @@ -265,7 +265,7 @@ message List{{.TableName}}Request {
}
message List{{.TableName}}Reply {
int64 total =1;
int64 total = 1;
repeated {{.TableName}} {{.TName}}s = 2;
}
`
Expand Down Expand Up @@ -320,6 +320,12 @@ service {{.TName}} {
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "create {{.TName}}",
description: "submit information to create {{.TName}}",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
Expand Down Expand Up @@ -465,17 +471,20 @@ service {{.TName}} {
}
}
// 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"],
// (2) Suggest using camel hump naming for message field names, and for names ending in 'id',
// use xxxID naming format, such as userID, orderID, etc.
// (3) 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 fieldName = 1 [json_name = "field_name"],
// 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
// (4) 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,
// (5) 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\""];
// such as string name = 1 [(tagger.tags) = "form:\"name\""].
// protoMessageCreateCode
Expand Down Expand Up @@ -513,7 +522,7 @@ message List{{.TableName}}Request {
}
message List{{.TableName}}Reply {
int64 total =1;
int64 total = 1;
repeated {{.TableName}} {{.TName}}s = 2;
}
Expand Down Expand Up @@ -602,6 +611,12 @@ service {{.TName}} {
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
summary: "create {{.TName}}",
description: "submit information to create {{.TName}}",
//security: {
// security_requirement: {
// key: "BearerAuth";
// value: {}
// }
//}
};
}
Expand Down Expand Up @@ -676,17 +691,20 @@ service {{.TName}} {
}
}
// 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"],
// (2) Suggest using camel hump naming for message field names, and for names ending in 'id',
// use xxxID naming format, such as userID, orderID, etc.
// (3) 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 fieldName = 1 [json_name = "field_name"],
// 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
// (4) 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,
// (5) 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\""];
// such as string name = 1 [(tagger.tags) = "form:\"name\""].
// protoMessageCreateCode
Expand Down Expand Up @@ -724,7 +742,7 @@ message List{{.TableName}}Request {
}
message List{{.TableName}}Reply {
int64 total =1;
int64 total = 1;
repeated {{.TableName}} {{.TName}}s = 2;
}
`
Expand Down

0 comments on commit 9b65e1a

Please sign in to comment.