From 39de359b43ec8c80c216f8b9c43a85b024eaeff6 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Wed, 11 Dec 2024 12:53:19 +1100 Subject: [PATCH] fix: go2proto slices of int weren't working (#3713) --- cmd/go2proto/main.go | 31 +++-- cmd/go2proto/pbtmpl.go | 2 +- cmd/go2proto/testdata/go2proto.to.go | 2 + cmd/go2proto/testdata/model.go | 20 +-- cmd/go2proto/testdata/model_test.go | 2 + cmd/go2proto/testdata/testdatapb/model.pb.go | 124 +++++++++++-------- cmd/go2proto/testdata/testdatapb/model.proto | 2 + cmd/go2proto/toprototmpl.go | 26 ++-- 8 files changed, 122 insertions(+), 87 deletions(-) diff --git a/cmd/go2proto/main.go b/cmd/go2proto/main.go index 2b485fd4ff..87597841fc 100644 --- a/cmd/go2proto/main.go +++ b/cmd/go2proto/main.go @@ -144,10 +144,11 @@ func (m Message) DeclName() string { return m.Name } type Field struct { ID int Name string - Type string + OriginType string // The original type of the field, eg. int, string, float32, etc. + ProtoType string // The type of the field in the generated .proto file. + ProtoGoType string // The type of the field in the generated Go protobuf code. eg. int -> int64. Optional bool Repeated bool - ProtoGoType string Pointer bool } @@ -531,6 +532,7 @@ func parsePBTag(tag string) (pbTag, error) { } func (s *State) applyFieldType(t types.Type, field *Field) error { + field.OriginType = t.String() switch t := t.(type) { case *types.Named: if err := s.extractDecl(t.Obj(), t); err != nil { @@ -538,14 +540,18 @@ func (s *State) applyFieldType(t types.Type, field *Field) error { } ref := t.Obj().Pkg().Path() + "." + t.Obj().Name() if bt, ok := stdTypes[ref]; ok { - field.Type = bt.ref + field.ProtoType = bt.ref + field.ProtoGoType = protoName(bt.ref) + field.OriginType = t.Obj().Name() } else { - field.Type = t.Obj().Name() + field.ProtoType = t.Obj().Name() + field.ProtoGoType = protoName(t.Obj().Name()) + field.OriginType = t.Obj().Name() } case *types.Slice: if t.Elem().String() == "byte" { - field.Type = "bytes" + field.ProtoType = "bytes" } else { field.Repeated = true return s.applyFieldType(t.Elem(), field) @@ -554,31 +560,30 @@ func (s *State) applyFieldType(t types.Type, field *Field) error { case *types.Pointer: field.Pointer = true if _, ok := t.Elem().(*types.Slice); ok { - return fmt.Errorf("pointer to named type is not supported") + return fmt.Errorf("pointer to slice is not supported") } return s.applyFieldType(t.Elem(), field) default: + field.OriginType = t.String() + field.ProtoType = t.String() field.ProtoGoType = t.String() switch t.String() { case "int": - field.Type = "int64" + field.ProtoType = "int64" field.ProtoGoType = "int64" case "uint": - field.Type = "uint64" + field.ProtoType = "uint64" field.ProtoGoType = "uint64" case "float64": - field.Type = "double" - field.ProtoGoType = "float64" + field.ProtoType = "double" case "float32": - field.Type = "float" - field.ProtoGoType = "float32" + field.ProtoType = "float" case "string", "bool", "uint64", "int64", "uint32", "int32": - field.Type = t.String() default: return fmt.Errorf("unsupported type %s", t.String()) diff --git a/cmd/go2proto/pbtmpl.go b/cmd/go2proto/pbtmpl.go index fb4b5357d1..9c93d95175 100644 --- a/cmd/go2proto/pbtmpl.go +++ b/cmd/go2proto/pbtmpl.go @@ -34,7 +34,7 @@ option {{ $name }} = {{ $value }}; {{- if eq (typeof $decl) "Message" }} message {{ .Name }} { {{- range $name, $field := .Fields }} - {{ if .Repeated }}repeated {{else if .Optional}}optional {{ end }}{{ .Type }} {{ .Name | toLowerSnake }} = {{ .ID }}; + {{ if .Repeated }}repeated {{else if .Optional}}optional {{ end }}{{ .ProtoType }} {{ .Name | toLowerSnake }} = {{ .ID }}; {{- end }} } {{- else if eq (typeof $decl) "Enum" }} diff --git a/cmd/go2proto/testdata/go2proto.to.go b/cmd/go2proto/testdata/go2proto.to.go index 0d2e927df4..637db38e98 100644 --- a/cmd/go2proto/testdata/go2proto.to.go +++ b/cmd/go2proto/testdata/go2proto.to.go @@ -58,6 +58,8 @@ func (x *Root) ToProto() *destpb.Root { OptionalInt: proto.Int64(int64(x.OptionalInt)), OptionalIntPtr: proto.Int64(int64(*x.OptionalIntPtr)), OptionalMsg: x.OptionalMsg.ToProto(), + RepeatedInt: protoSlicef(x.RepeatedInt, func(v int) int64 { return int64(v) }), + RepeatedMsg: protoSlice[*destpb.Message](x.RepeatedMsg), } } diff --git a/cmd/go2proto/testdata/model.go b/cmd/go2proto/testdata/model.go index 7973ddd105..ce350397b8 100644 --- a/cmd/go2proto/testdata/model.go +++ b/cmd/go2proto/testdata/model.go @@ -5,16 +5,16 @@ import ( ) type Root struct { - Int int `protobuf:"1"` - String string `protobuf:"2"` - MessagePtr *Message `protobuf:"4"` - Enum Enum `protobuf:"5"` - SumType SumType `protobuf:"6"` - OptionalInt int `protobuf:"7,optional"` - OptionalIntPtr *int `protobuf:"8,optional"` - OptionalMsg *Message `protobuf:"9,optional"` - // RepeatedInt []int `protobuf:"9"` - // RepeatedMsg []Message `protobuf:"10"` + Int int `protobuf:"1"` + String string `protobuf:"2"` + MessagePtr *Message `protobuf:"4"` + Enum Enum `protobuf:"5"` + SumType SumType `protobuf:"6"` + OptionalInt int `protobuf:"7,optional"` + OptionalIntPtr *int `protobuf:"8,optional"` + OptionalMsg *Message `protobuf:"9,optional"` + RepeatedInt []int `protobuf:"10"` + RepeatedMsg []*Message `protobuf:"11"` } type Message struct { diff --git a/cmd/go2proto/testdata/model_test.go b/cmd/go2proto/testdata/model_test.go index a437e5b321..5400dda6cc 100644 --- a/cmd/go2proto/testdata/model_test.go +++ b/cmd/go2proto/testdata/model_test.go @@ -21,6 +21,8 @@ func TestModel(t *testing.T) { OptionalInt: 2, OptionalIntPtr: &intv, OptionalMsg: &Message{Time: time.Now()}, + RepeatedInt: []int{1, 2, 3}, + RepeatedMsg: []*Message{&Message{Time: time.Now()}, &Message{Time: time.Now()}}, } pb := model.ToProto() data, err := proto.Marshal(pb) diff --git a/cmd/go2proto/testdata/testdatapb/model.pb.go b/cmd/go2proto/testdata/testdatapb/model.pb.go index 3003ab0825..67bb31343d 100644 --- a/cmd/go2proto/testdata/testdatapb/model.pb.go +++ b/cmd/go2proto/testdata/testdatapb/model.pb.go @@ -128,14 +128,16 @@ type Root struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Int int64 `protobuf:"varint,1,opt,name=int,proto3" json:"int,omitempty"` - String_ string `protobuf:"bytes,2,opt,name=string,proto3" json:"string,omitempty"` - MessagePtr *Message `protobuf:"bytes,4,opt,name=message_ptr,json=messagePtr,proto3" json:"message_ptr,omitempty"` - Enum Enum `protobuf:"varint,5,opt,name=enum,proto3,enum=xyz.block.ftl.go2proto.test.Enum" json:"enum,omitempty"` - SumType *SumType `protobuf:"bytes,6,opt,name=sum_type,json=sumType,proto3" json:"sum_type,omitempty"` - OptionalInt *int64 `protobuf:"varint,7,opt,name=optional_int,json=optionalInt,proto3,oneof" json:"optional_int,omitempty"` - OptionalIntPtr *int64 `protobuf:"varint,8,opt,name=optional_int_ptr,json=optionalIntPtr,proto3,oneof" json:"optional_int_ptr,omitempty"` - OptionalMsg *Message `protobuf:"bytes,9,opt,name=optional_msg,json=optionalMsg,proto3,oneof" json:"optional_msg,omitempty"` + Int int64 `protobuf:"varint,1,opt,name=int,proto3" json:"int,omitempty"` + String_ string `protobuf:"bytes,2,opt,name=string,proto3" json:"string,omitempty"` + MessagePtr *Message `protobuf:"bytes,4,opt,name=message_ptr,json=messagePtr,proto3" json:"message_ptr,omitempty"` + Enum Enum `protobuf:"varint,5,opt,name=enum,proto3,enum=xyz.block.ftl.go2proto.test.Enum" json:"enum,omitempty"` + SumType *SumType `protobuf:"bytes,6,opt,name=sum_type,json=sumType,proto3" json:"sum_type,omitempty"` + OptionalInt *int64 `protobuf:"varint,7,opt,name=optional_int,json=optionalInt,proto3,oneof" json:"optional_int,omitempty"` + OptionalIntPtr *int64 `protobuf:"varint,8,opt,name=optional_int_ptr,json=optionalIntPtr,proto3,oneof" json:"optional_int_ptr,omitempty"` + OptionalMsg *Message `protobuf:"bytes,9,opt,name=optional_msg,json=optionalMsg,proto3,oneof" json:"optional_msg,omitempty"` + RepeatedInt []int64 `protobuf:"varint,10,rep,packed,name=repeated_int,json=repeatedInt,proto3" json:"repeated_int,omitempty"` + RepeatedMsg []*Message `protobuf:"bytes,11,rep,name=repeated_msg,json=repeatedMsg,proto3" json:"repeated_msg,omitempty"` } func (x *Root) Reset() { @@ -224,6 +226,20 @@ func (x *Root) GetOptionalMsg() *Message { return nil } +func (x *Root) GetRepeatedInt() []int64 { + if x != nil { + return x.RepeatedInt + } + return nil +} + +func (x *Root) GetRepeatedMsg() []*Message { + if x != nil { + return x.RepeatedMsg + } + return nil +} + type SumType struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -468,7 +484,7 @@ var file_model_proto_rawDesc = []byte{ 0x52, 0x04, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x35, 0x0a, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x44, 0x75, 0x72, 0x61, 0x74, - 0x69, 0x6f, 0x6e, 0x52, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0xcb, 0x03, + 0x69, 0x6f, 0x6e, 0x52, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0xb7, 0x04, 0x0a, 0x04, 0x52, 0x6f, 0x6f, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x69, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x03, 0x69, 0x6e, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, @@ -494,33 +510,40 @@ var file_model_proto_rawDesc = []byte{ 0x79, 0x7a, 0x2e, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x2e, 0x66, 0x74, 0x6c, 0x2e, 0x67, 0x6f, 0x32, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x48, 0x02, 0x52, 0x0b, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x4d, 0x73, - 0x67, 0x88, 0x01, 0x01, 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, - 0x6c, 0x5f, 0x69, 0x6e, 0x74, 0x42, 0x13, 0x0a, 0x11, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, - 0x61, 0x6c, 0x5f, 0x69, 0x6e, 0x74, 0x5f, 0x70, 0x74, 0x72, 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x6f, - 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x5f, 0x6d, 0x73, 0x67, 0x22, 0xb7, 0x01, 0x0a, 0x07, - 0x53, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x12, 0x35, 0x0a, 0x01, 0x61, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x0b, 0x32, 0x25, 0x2e, 0x78, 0x79, 0x7a, 0x2e, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x2e, 0x66, - 0x74, 0x6c, 0x2e, 0x67, 0x6f, 0x32, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, - 0x2e, 0x53, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x41, 0x48, 0x00, 0x52, 0x01, 0x61, 0x12, 0x35, - 0x0a, 0x01, 0x62, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x25, 0x2e, 0x78, 0x79, 0x7a, 0x2e, - 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x2e, 0x66, 0x74, 0x6c, 0x2e, 0x67, 0x6f, 0x32, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x53, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x42, - 0x48, 0x00, 0x52, 0x01, 0x62, 0x12, 0x35, 0x0a, 0x01, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, - 0x32, 0x25, 0x2e, 0x78, 0x79, 0x7a, 0x2e, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x2e, 0x66, 0x74, 0x6c, - 0x2e, 0x67, 0x6f, 0x32, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x53, - 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x43, 0x48, 0x00, 0x52, 0x01, 0x63, 0x42, 0x07, 0x0a, 0x05, - 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x18, 0x0a, 0x08, 0x53, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, - 0x41, 0x12, 0x0c, 0x0a, 0x01, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x01, 0x61, 0x22, - 0x18, 0x0a, 0x08, 0x53, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x42, 0x12, 0x0c, 0x0a, 0x01, 0x62, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x01, 0x62, 0x22, 0x18, 0x0a, 0x08, 0x53, 0x75, 0x6d, - 0x54, 0x79, 0x70, 0x65, 0x43, 0x12, 0x0c, 0x0a, 0x01, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x01, - 0x52, 0x01, 0x63, 0x2a, 0x1e, 0x0a, 0x04, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x0a, 0x0a, 0x06, 0x45, - 0x4e, 0x55, 0x4d, 0x5f, 0x41, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x45, 0x4e, 0x55, 0x4d, 0x5f, - 0x42, 0x10, 0x01, 0x42, 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, - 0x6d, 0x2f, 0x54, 0x42, 0x44, 0x35, 0x34, 0x35, 0x36, 0x36, 0x39, 0x37, 0x35, 0x2f, 0x66, 0x74, - 0x6c, 0x2f, 0x63, 0x6d, 0x64, 0x2f, 0x67, 0x6f, 0x32, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74, - 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, - 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x67, 0x88, 0x01, 0x01, 0x12, 0x21, 0x0a, 0x0c, 0x72, 0x65, 0x70, 0x65, 0x61, 0x74, 0x65, 0x64, + 0x5f, 0x69, 0x6e, 0x74, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x03, 0x52, 0x0b, 0x72, 0x65, 0x70, 0x65, + 0x61, 0x74, 0x65, 0x64, 0x49, 0x6e, 0x74, 0x12, 0x47, 0x0a, 0x0c, 0x72, 0x65, 0x70, 0x65, 0x61, + 0x74, 0x65, 0x64, 0x5f, 0x6d, 0x73, 0x67, 0x18, 0x0b, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, + 0x78, 0x79, 0x7a, 0x2e, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x2e, 0x66, 0x74, 0x6c, 0x2e, 0x67, 0x6f, + 0x32, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x4d, 0x65, 0x73, 0x73, + 0x61, 0x67, 0x65, 0x52, 0x0b, 0x72, 0x65, 0x70, 0x65, 0x61, 0x74, 0x65, 0x64, 0x4d, 0x73, 0x67, + 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x5f, 0x69, 0x6e, + 0x74, 0x42, 0x13, 0x0a, 0x11, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x5f, 0x69, + 0x6e, 0x74, 0x5f, 0x70, 0x74, 0x72, 0x42, 0x0f, 0x0a, 0x0d, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, + 0x6e, 0x61, 0x6c, 0x5f, 0x6d, 0x73, 0x67, 0x22, 0xb7, 0x01, 0x0a, 0x07, 0x53, 0x75, 0x6d, 0x54, + 0x79, 0x70, 0x65, 0x12, 0x35, 0x0a, 0x01, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x25, + 0x2e, 0x78, 0x79, 0x7a, 0x2e, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x2e, 0x66, 0x74, 0x6c, 0x2e, 0x67, + 0x6f, 0x32, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x53, 0x75, 0x6d, + 0x54, 0x79, 0x70, 0x65, 0x41, 0x48, 0x00, 0x52, 0x01, 0x61, 0x12, 0x35, 0x0a, 0x01, 0x62, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x25, 0x2e, 0x78, 0x79, 0x7a, 0x2e, 0x62, 0x6c, 0x6f, 0x63, + 0x6b, 0x2e, 0x66, 0x74, 0x6c, 0x2e, 0x67, 0x6f, 0x32, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x74, + 0x65, 0x73, 0x74, 0x2e, 0x53, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x42, 0x48, 0x00, 0x52, 0x01, + 0x62, 0x12, 0x35, 0x0a, 0x01, 0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x25, 0x2e, 0x78, + 0x79, 0x7a, 0x2e, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x2e, 0x66, 0x74, 0x6c, 0x2e, 0x67, 0x6f, 0x32, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x53, 0x75, 0x6d, 0x54, 0x79, + 0x70, 0x65, 0x43, 0x48, 0x00, 0x52, 0x01, 0x63, 0x42, 0x07, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, + 0x65, 0x22, 0x18, 0x0a, 0x08, 0x53, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x41, 0x12, 0x0c, 0x0a, + 0x01, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x01, 0x61, 0x22, 0x18, 0x0a, 0x08, 0x53, + 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x42, 0x12, 0x0c, 0x0a, 0x01, 0x62, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x03, 0x52, 0x01, 0x62, 0x22, 0x18, 0x0a, 0x08, 0x53, 0x75, 0x6d, 0x54, 0x79, 0x70, 0x65, + 0x43, 0x12, 0x0c, 0x0a, 0x01, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x01, 0x52, 0x01, 0x63, 0x2a, + 0x1e, 0x0a, 0x04, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x0a, 0x0a, 0x06, 0x45, 0x4e, 0x55, 0x4d, 0x5f, + 0x41, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x45, 0x4e, 0x55, 0x4d, 0x5f, 0x42, 0x10, 0x01, 0x42, + 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x54, 0x42, + 0x44, 0x35, 0x34, 0x35, 0x36, 0x36, 0x39, 0x37, 0x35, 0x2f, 0x66, 0x74, 0x6c, 0x2f, 0x63, 0x6d, + 0x64, 0x2f, 0x67, 0x6f, 0x32, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, + 0x61, 0x74, 0x61, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x70, 0x62, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -549,20 +572,21 @@ var file_model_proto_goTypes = []any{ (*durationpb.Duration)(nil), // 8: google.protobuf.Duration } var file_model_proto_depIdxs = []int32{ - 7, // 0: xyz.block.ftl.go2proto.test.Message.time:type_name -> google.protobuf.Timestamp - 8, // 1: xyz.block.ftl.go2proto.test.Message.duration:type_name -> google.protobuf.Duration - 1, // 2: xyz.block.ftl.go2proto.test.Root.message_ptr:type_name -> xyz.block.ftl.go2proto.test.Message - 0, // 3: xyz.block.ftl.go2proto.test.Root.enum:type_name -> xyz.block.ftl.go2proto.test.Enum - 3, // 4: xyz.block.ftl.go2proto.test.Root.sum_type:type_name -> xyz.block.ftl.go2proto.test.SumType - 1, // 5: xyz.block.ftl.go2proto.test.Root.optional_msg:type_name -> xyz.block.ftl.go2proto.test.Message - 4, // 6: xyz.block.ftl.go2proto.test.SumType.a:type_name -> xyz.block.ftl.go2proto.test.SumTypeA - 5, // 7: xyz.block.ftl.go2proto.test.SumType.b:type_name -> xyz.block.ftl.go2proto.test.SumTypeB - 6, // 8: xyz.block.ftl.go2proto.test.SumType.c:type_name -> xyz.block.ftl.go2proto.test.SumTypeC - 9, // [9:9] is the sub-list for method output_type - 9, // [9:9] is the sub-list for method input_type - 9, // [9:9] is the sub-list for extension type_name - 9, // [9:9] is the sub-list for extension extendee - 0, // [0:9] is the sub-list for field type_name + 7, // 0: xyz.block.ftl.go2proto.test.Message.time:type_name -> google.protobuf.Timestamp + 8, // 1: xyz.block.ftl.go2proto.test.Message.duration:type_name -> google.protobuf.Duration + 1, // 2: xyz.block.ftl.go2proto.test.Root.message_ptr:type_name -> xyz.block.ftl.go2proto.test.Message + 0, // 3: xyz.block.ftl.go2proto.test.Root.enum:type_name -> xyz.block.ftl.go2proto.test.Enum + 3, // 4: xyz.block.ftl.go2proto.test.Root.sum_type:type_name -> xyz.block.ftl.go2proto.test.SumType + 1, // 5: xyz.block.ftl.go2proto.test.Root.optional_msg:type_name -> xyz.block.ftl.go2proto.test.Message + 1, // 6: xyz.block.ftl.go2proto.test.Root.repeated_msg:type_name -> xyz.block.ftl.go2proto.test.Message + 4, // 7: xyz.block.ftl.go2proto.test.SumType.a:type_name -> xyz.block.ftl.go2proto.test.SumTypeA + 5, // 8: xyz.block.ftl.go2proto.test.SumType.b:type_name -> xyz.block.ftl.go2proto.test.SumTypeB + 6, // 9: xyz.block.ftl.go2proto.test.SumType.c:type_name -> xyz.block.ftl.go2proto.test.SumTypeC + 10, // [10:10] is the sub-list for method output_type + 10, // [10:10] is the sub-list for method input_type + 10, // [10:10] is the sub-list for extension type_name + 10, // [10:10] is the sub-list for extension extendee + 0, // [0:10] is the sub-list for field type_name } func init() { file_model_proto_init() } diff --git a/cmd/go2proto/testdata/testdatapb/model.proto b/cmd/go2proto/testdata/testdatapb/model.proto index 2f5beeac06..fa6640a418 100644 --- a/cmd/go2proto/testdata/testdatapb/model.proto +++ b/cmd/go2proto/testdata/testdatapb/model.proto @@ -28,6 +28,8 @@ message Root { optional int64 optional_int = 7; optional int64 optional_int_ptr = 8; optional Message optional_msg = 9; + repeated int64 repeated_int = 10; + repeated Message repeated_msg = 11; } diff --git a/cmd/go2proto/toprototmpl.go b/cmd/go2proto/toprototmpl.go index 79ae46122d..0cfe86efb9 100644 --- a/cmd/go2proto/toprototmpl.go +++ b/cmd/go2proto/toprototmpl.go @@ -13,13 +13,13 @@ import ( var _ fmt.Stringer -var go2protoTmpl = template.Must(template.New("go2proto.mapper.go"). +var go2protoTmpl = template.Must(template.New("go2proto.to.go.tmpl"). Funcs(template.FuncMap{ "typeof": func(t any) string { return reflect.Indirect(reflect.ValueOf(t)).Type().Name() }, // Return true if the type is a builtin proto type. "isBuiltin": func(t Field) bool { - switch t.Type { - case "int32", "int64", "uint32", "uint64", "float", "double", "bool", "string": + switch t.OriginType { + case "int", "uint", "int32", "int64", "uint32", "uint64", "float32", "float64", "bool", "string": return true } return false @@ -88,34 +88,34 @@ func (x *{{ .Name }}) ToProto() *destpb.{{ .Name }} { {{- if $field.Optional}} {{ $field.EscapedName }}: proto.{{ $field.ProtoGoType | toUpperCamel }}({{ $field.ProtoGoType }}({{if $field.Pointer}}*{{end}}x.{{ $field.Name }})), {{- else if .Repeated}} - {{ $field.EscapedName }}: protoSlicef(x.{{ $field.Name }}, func(v {{ $field.Type }}) {{ $field.ProtoGoType }} { return {{ $field.ProtoGoType }}(v) }), + {{ $field.EscapedName }}: protoSlicef(x.{{ $field.Name }}, func(v {{ $field.OriginType }}) {{ $field.ProtoGoType }} { return {{ $field.ProtoGoType }}(v) }), {{- else }} {{ $field.EscapedName }}: {{ $field.ProtoGoType }}(x.{{ $field.Name }}), {{- end}} -{{- else if eq $field.Type "google.protobuf.Timestamp" }} +{{- else if eq $field.ProtoType "google.protobuf.Timestamp" }} {{ $field.EscapedName }}: timestamppb.New(x.{{ $field.Name }}), -{{- else if eq $field.Type "google.protobuf.Duration" }} +{{- else if eq $field.ProtoType "google.protobuf.Duration" }} {{ $field.EscapedName }}: durationpb.New(x.{{ $field.Name }}), -{{- else if eq (.Type | $.TypeOf) "Message" }} +{{- else if eq (.OriginType | $.TypeOf) "Message" }} {{- if .Repeated }} - {{ $field.EscapedName }}: protoSlice[*destpb.{{ .Type }}](x.{{ $field.Name }}), + {{ $field.EscapedName }}: protoSlice[*destpb.{{ .ProtoGoType }}](x.{{ $field.Name }}), {{- else}} {{ $field.EscapedName }}: x.{{ $field.Name }}.ToProto(), {{- end}} -{{- else if eq (.Type | $.TypeOf) "Enum" }} +{{- else if eq (.OriginType | $.TypeOf) "Enum" }} {{- if .Repeated }} {{ $field.EscapedName }}: protoSlice[destpb.{{ .Type }}](x.{{ $field.Name }}), {{- else}} {{ $field.EscapedName }}: x.{{ $field.Name }}.ToProto(), {{- end}} -{{- else if eq (.Type | $.TypeOf) "SumType" }} +{{- else if eq (.OriginType | $.TypeOf) "SumType" }} {{- if .Repeated }} - {{ $field.EscapedName }}: protoSlicef(x.{{ $field.Name }}, {{$field.Type}}ToProto), + {{ $field.EscapedName }}: protoSlicef(x.{{ $field.Name }}, {{$field.OriginType}}ToProto), {{- else}} - {{ $field.EscapedName }}: {{ $field.Type }}ToProto(x.{{ $field.Name }}), + {{ $field.EscapedName }}: {{ $field.OriginType }}ToProto(x.{{ $field.Name }}), {{- end}} {{- else }} - {{ $field.EscapedName }}: ??, // x.{{ $field.Name }}.ToProto() // Unknown type: {{ $field.Type }} + {{ $field.EscapedName }}: ??, // x.{{ $field.Name }}.ToProto() // Unknown type: {{ $field.OriginType }} {{- end}} {{- end}} }