Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when generate proto file, respect dto json tag to generate message fi… #263

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions cmd/internal/svc/testdata/outputanonystruct/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
}

Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion toolkit/astutils/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand Down
13 changes: 12 additions & 1 deletion toolkit/protobuf/v3/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
Loading