From 1c3e198c9c0bcc7f9022c139f67227c9f52642b4 Mon Sep 17 00:00:00 2001 From: wubin1989 <328454505@qq.com> Date: Thu, 26 Sep 2024 00:06:06 +0800 Subject: [PATCH] add google/protobuf/struct.proto --- .../svc/testdata/outputanonystruct/dto/dto.go | 8 ++-- .../transport/grpc/usersvc.proto | 10 +++-- toolkit/protobuf/v3/message.go | 42 ++++++++++++++++--- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/cmd/internal/svc/testdata/outputanonystruct/dto/dto.go b/cmd/internal/svc/testdata/outputanonystruct/dto/dto.go index d38045e5..354a9e93 100644 --- a/cmd/internal/svc/testdata/outputanonystruct/dto/dto.go +++ b/cmd/internal/svc/testdata/outputanonystruct/dto/dto.go @@ -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"` diff --git a/cmd/internal/svc/testdata/outputanonystruct/transport/grpc/usersvc.proto b/cmd/internal/svc/testdata/outputanonystruct/transport/grpc/usersvc.proto index 3c3d11f2..9b023828 100644 --- a/cmd/internal/svc/testdata/outputanonystruct/transport/grpc/usersvc.proto +++ b/cmd/internal/svc/testdata/outputanonystruct/transport/grpc/usersvc.proto @@ -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; @@ -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"]; diff --git a/toolkit/protobuf/v3/message.go b/toolkit/protobuf/v3/message.go index b5e5f910..c7ab444b 100644 --- a/toolkit/protobuf/v3/message.go +++ b/toolkit/protobuf/v3/message.go @@ -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: @@ -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, @@ -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()), @@ -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 ") { @@ -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 } @@ -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 }