From 4035ea6ce43e3ecf214f0de5efd7b6dfd8b42166 Mon Sep 17 00:00:00 2001 From: wubin1989 <328454505@qq.com> Date: Thu, 26 Sep 2024 16:17:48 +0800 Subject: [PATCH] when generate proto file, respect dto json tag to generate message field name --- .../svc/testdata/outputanonystruct/dto/dto.go | 11 ++++++----- .../outputanonystruct/transport/grpc/usersvc.proto | 5 +++-- toolkit/astutils/funcs.go | 3 ++- toolkit/protobuf/v3/message.go | 13 ++++++++++++- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cmd/internal/svc/testdata/outputanonystruct/dto/dto.go b/cmd/internal/svc/testdata/outputanonystruct/dto/dto.go index 354a9e93..1dbf0630 100644 --- a/cmd/internal/svc/testdata/outputanonystruct/dto/dto.go +++ b/cmd/internal/svc/testdata/outputanonystruct/dto/dto.go @@ -24,17 +24,18 @@ type Page struct { // 页码 PageNo int // 每页行数 - Size int + Size int `json:"size,omitempty"` User UserVo } // 分页筛选条件 type PageQuery struct { - Filter PageFilter - Page Page - Condtions map[string]interface{} + Filter PageFilter + Page Page + Condtions map[string]interface{} ACondtions []interface{} - Options []struct { + ID int64 `json:"id,string"` + Options []struct { Label string `json:"label" form:"label"` Value string `json:"value" form:"value"` } `json:"options" form:"options"` diff --git a/cmd/internal/svc/testdata/outputanonystruct/transport/grpc/usersvc.proto b/cmd/internal/svc/testdata/outputanonystruct/transport/grpc/usersvc.proto index 9b023828..dc813997 100644 --- a/cmd/internal/svc/testdata/outputanonystruct/transport/grpc/usersvc.proto +++ b/cmd/internal/svc/testdata/outputanonystruct/transport/grpc/usersvc.proto @@ -35,7 +35,7 @@ message Page { // 页码 optional int32 page_no = 2 [json_name="page_no"]; // 每页行数 - optional int32 size = 3 [json_name="size"]; + optional int32 size = 3 [json_name="size,omitempty"]; optional UserVo user = 4 [json_name="user"]; } @@ -53,7 +53,8 @@ message PageQuery { optional Page page = 2 [json_name="page"]; optional google.protobuf.Struct condtions = 3 [json_name="condtions"]; optional google.protobuf.ListValue a_condtions = 4 [json_name="a_condtions"]; - repeated Anonystructkfvagz8uXPWVpn5z9xyjWS options = 5 [json_name="options"]; + optional int64 id = 5 [json_name="id,string"]; + repeated Anonystructkfvagz8uXPWVpn5z9xyjWS options = 6 [json_name="options"]; } message PageRet { diff --git a/toolkit/astutils/funcs.go b/toolkit/astutils/funcs.go index 1405f9f8..c57b018a 100644 --- a/toolkit/astutils/funcs.go +++ b/toolkit/astutils/funcs.go @@ -207,7 +207,8 @@ func NewStructMeta(structType *ast.StructType, exprString func(ast.Expr) string) if field.Tag != nil { tag = strings.Trim(field.Tag.Value, "`") if re.MatchString(tag) { - docName = strings.TrimSuffix(re.FindStringSubmatch(tag)[1], ",omitempty") + jsonTag := re.FindStringSubmatch(tag)[1] + docName = strings.Split(jsonTag, ",")[0] } } diff --git a/toolkit/protobuf/v3/message.go b/toolkit/protobuf/v3/message.go index c7ab444b..b0ab7520 100644 --- a/toolkit/protobuf/v3/message.go +++ b/toolkit/protobuf/v3/message.go @@ -16,6 +16,8 @@ import ( "github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils" ) +var re = regexp.MustCompile(`json:"(.*?)"`) + var _ ProtobufType = (*Enum)(nil) var _ ProtobufType = (*Message)(nil) @@ -142,7 +144,16 @@ func (receiver ProtoGenerator) newField(field astutils.FieldMeta, index int) Fie message.Name = strcase.ToCamel(field.Name) t = message } - fieldName := receiver.fieldNamingFunc(field.Name) + var fieldName string + if stringutils.IsNotEmpty(field.Tag) && re.MatchString(field.Tag) { + jsonName := re.FindStringSubmatch(field.Tag)[1] + fieldName = strings.Split(jsonName, ",")[0] + if fieldName == "-" { + fieldName = receiver.fieldNamingFunc(field.Name) + } + } else { + fieldName = receiver.fieldNamingFunc(field.Name) + } return Field{ Name: fieldName, Type: t,