Skip to content

Commit

Permalink
Merge pull request unionj-cloud#263 from wubin1989/main
Browse files Browse the repository at this point in the history
when generate proto file, respect dto json tag to generate message fi…
  • Loading branch information
wubin1989 authored Sep 26, 2024
2 parents e15f638 + 4035ea6 commit 0f58ae7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
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

0 comments on commit 0f58ae7

Please sign in to comment.