Skip to content

Commit

Permalink
Merge pull request #262 from wubin1989/main
Browse files Browse the repository at this point in the history
...
  • Loading branch information
wubin1989 authored Sep 25, 2024
2 parents 165effd + ca7c513 commit e15f638
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
8 changes: 5 additions & 3 deletions cmd/internal/svc/testdata/outputanonystruct/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ type Page struct {

// 分页筛选条件
type PageQuery struct {
Filter PageFilter
Page Page
Options []struct {
Filter PageFilter
Page Page
Condtions map[string]interface{}
ACondtions []interface{}
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 @@ -2,14 +2,14 @@
* Generated by go-doudou v2.4.2.
* Don't edit!
*
* Version No.: v20240918
* Version No.: v20240926
*/
syntax = "proto3";

package usersvc;
option go_package = "github.com/unionj-cloud/go-doudou/v2/cmd/internal/svc/testdata/outputanonystruct/transport/grpc";

import "google/protobuf/any.proto";
import "google/protobuf/struct.proto";

enum KeyboardLayout {
UNKNOWN = 0;
Expand Down Expand Up @@ -51,11 +51,13 @@ message PageFilter {
message PageQuery {
optional PageFilter filter = 1 [json_name="filter"];
optional Page page = 2 [json_name="page"];
repeated Anonystructkfvagz8uXPWVpn5z9xyjWS options = 3 [json_name="options"];
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"];
}

message PageRet {
optional google.protobuf.Any items = 1 [json_name="items"];
optional google.protobuf.Value items = 1 [json_name="items"];
optional int32 page_no = 2 [json_name="page_no"];
optional int32 page_size = 3 [json_name="page_size"];
optional int32 total = 4 [json_name="total"];
Expand Down
42 changes: 36 additions & 6 deletions toolkit/protobuf/v3/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func (m Message) String() string {
switch {
case reflect.DeepEqual(m, Any):
return "anypb.Any"
case reflect.DeepEqual(m, Struct):
return "structpb.Struct"
case reflect.DeepEqual(m, Value):
return "structpb.Value"
case reflect.DeepEqual(m, ListValue):
return "structpb.ListValue"
case reflect.DeepEqual(m, Empty):
return "emptypb.Empty"
default:
Expand Down Expand Up @@ -188,6 +194,21 @@ var (
IsTopLevel: true,
IsImported: true,
}
Struct = Message{
Name: "google.protobuf.Struct",
IsTopLevel: true,
IsImported: true,
}
Value = Message{
Name: "google.protobuf.Value",
IsTopLevel: true,
IsImported: true,
}
ListValue = Message{
Name: "google.protobuf.ListValue",
IsTopLevel: true,
IsImported: true,
}
Empty = Message{
Name: "google.protobuf.Empty",
IsTopLevel: true,
Expand Down Expand Up @@ -239,18 +260,27 @@ func init() {
}

func (receiver ProtoGenerator) handleDefaultCase(ft string) ProtobufType {
var title string
if ft == "map[string]interface{}" {
ImportStore["google/protobuf/struct.proto"] = struct{}{}
return Struct
}
if ft == "[]interface{}" {
ImportStore["google/protobuf/struct.proto"] = struct{}{}
return ListValue
}
if strings.HasPrefix(ft, "map[") {
elem := ft[strings.Index(ft, "]")+1:]
key := ft[4:strings.Index(ft, "]")]
keyMessage := receiver.MessageOf(key)
if reflect.DeepEqual(keyMessage, Float) || reflect.DeepEqual(keyMessage, Double) || reflect.DeepEqual(keyMessage, Bytes) {
log.Error("floating point types and bytes cannot be key_type of maps, please refer to https://developers.google.com/protocol-buffers/docs/proto3#maps")
return Any
goto ANY
}
elemMessage := receiver.MessageOf(elem)
if strings.HasPrefix(elemMessage.GetName(), "map<") {
log.Error("the value_type cannot be another map, please refer to https://developers.google.com/protocol-buffers/docs/proto3#maps")
return Any
goto ANY
}
return Message{
Name: fmt.Sprintf("map<%s, %s>", keyMessage.GetName(), elemMessage.GetName()),
Expand All @@ -262,7 +292,7 @@ func (receiver ProtoGenerator) handleDefaultCase(ft string) ProtobufType {
elemMessage := receiver.MessageOf(elem)
if strings.HasPrefix(elemMessage.GetName(), "map<") {
log.Error("map fields cannot be repeated, please refer to https://developers.google.com/protocol-buffers/docs/proto3#maps")
return Any
goto ANY
}
messageName := elemMessage.GetName()
if strings.Contains(elemMessage.GetName(), "repeated ") {
Expand Down Expand Up @@ -298,7 +328,6 @@ func (receiver ProtoGenerator) handleDefaultCase(ft string) ProtobufType {
MessageStore[message.Name] = message
return message
}
var title string
if !strings.Contains(ft, ".") {
title = ft
}
Expand All @@ -318,6 +347,7 @@ func (receiver ProtoGenerator) handleDefaultCase(ft string) ProtobufType {
return e
}
}
ImportStore["google/protobuf/any.proto"] = struct{}{}
return Any
ANY:
ImportStore["google/protobuf/struct.proto"] = struct{}{}
return Value
}

0 comments on commit e15f638

Please sign in to comment.