From d4803f5a4f6887bf1227c03893095b296704f4fc Mon Sep 17 00:00:00 2001 From: Damien Neil Date: Wed, 19 Sep 2018 11:43:35 -0700 Subject: [PATCH] cmd/protoc-gen-go: add "packed" encoding tag to fields Change-Id: I423039d6c9edb50a07781a6e76336ed364f21866 Reviewed-on: https://go-review.googlesource.com/136359 Reviewed-by: Joe Tsai --- cmd/protoc-gen-go/main.go | 3 + .../testdata/proto3/fields.pb.go | 228 +++++++++++++++--- .../testdata/proto3/fields.proto | 18 ++ reflect/prototype/protofile_desc.go | 39 ++- 4 files changed, 245 insertions(+), 43 deletions(-) diff --git a/cmd/protoc-gen-go/main.go b/cmd/protoc-gen-go/main.go index d5bef266..e7119f12 100644 --- a/cmd/protoc-gen-go/main.go +++ b/cmd/protoc-gen-go/main.go @@ -631,6 +631,9 @@ func fieldProtobufTag(field *protogen.Field) string { case protoreflect.Repeated: tag = append(tag, "rep") } + if field.Desc.IsPacked() { + tag = append(tag, "packed") + } // TODO: packed // name name := string(field.Desc.Name()) diff --git a/cmd/protoc-gen-go/testdata/proto3/fields.pb.go b/cmd/protoc-gen-go/testdata/proto3/fields.pb.go index 92d9e235..b09aafb1 100644 --- a/cmd/protoc-gen-go/testdata/proto3/fields.pb.go +++ b/cmd/protoc-gen-go/testdata/proto3/fields.pb.go @@ -60,6 +60,23 @@ type FieldTestMessage struct { OptionalString string `protobuf:"bytes,15,opt,name=optional_string,json=optionalString,proto3" json:"optional_string,omitempty"` OptionalBytes []byte `protobuf:"bytes,16,opt,name=optional_bytes,json=optionalBytes,proto3" json:"optional_bytes,omitempty"` Optional_Message *FieldTestMessage_Message `protobuf:"bytes,17,opt,name=optional_Message,json=optionalMessage,proto3" json:"optional_Message,omitempty"` + RepeatedBool []bool `protobuf:"varint,201,rep,packed,name=repeated_bool,json=repeatedBool,proto3" json:"repeated_bool,omitempty"` + RepeatedEnum []FieldTestMessage_Enum `protobuf:"varint,202,rep,packed,name=repeated_enum,json=repeatedEnum,proto3,enum=goproto.protoc.proto3.FieldTestMessage_Enum" json:"repeated_enum,omitempty"` + RepeatedInt32 []int32 `protobuf:"varint,203,rep,packed,name=repeated_int32,json=repeatedInt32,proto3" json:"repeated_int32,omitempty"` + RepeatedSint32 []int32 `protobuf:"zigzag32,204,rep,packed,name=repeated_sint32,json=repeatedSint32,proto3" json:"repeated_sint32,omitempty"` + RepeatedUint32 []uint32 `protobuf:"varint,205,rep,packed,name=repeated_uint32,json=repeatedUint32,proto3" json:"repeated_uint32,omitempty"` + RepeatedInt64 []int64 `protobuf:"varint,206,rep,packed,name=repeated_int64,json=repeatedInt64,proto3" json:"repeated_int64,omitempty"` + RepeatedSint64 []int64 `protobuf:"zigzag64,207,rep,packed,name=repeated_sint64,json=repeatedSint64,proto3" json:"repeated_sint64,omitempty"` + RepeatedUint64 []uint64 `protobuf:"varint,208,rep,packed,name=repeated_uint64,json=repeatedUint64,proto3" json:"repeated_uint64,omitempty"` + RepeatedSfixed32 []int32 `protobuf:"fixed32,209,rep,packed,name=repeated_sfixed32,json=repeatedSfixed32,proto3" json:"repeated_sfixed32,omitempty"` + RepeatedFixed32 []uint32 `protobuf:"fixed32,210,rep,packed,name=repeated_fixed32,json=repeatedFixed32,proto3" json:"repeated_fixed32,omitempty"` + RepeatedFloat []float32 `protobuf:"fixed32,211,rep,packed,name=repeated_float,json=repeatedFloat,proto3" json:"repeated_float,omitempty"` + RepeatedSfixed64 []int64 `protobuf:"fixed64,212,rep,packed,name=repeated_sfixed64,json=repeatedSfixed64,proto3" json:"repeated_sfixed64,omitempty"` + RepeatedFixed64 []uint64 `protobuf:"fixed64,213,rep,packed,name=repeated_fixed64,json=repeatedFixed64,proto3" json:"repeated_fixed64,omitempty"` + RepeatedDouble []float64 `protobuf:"fixed64,214,rep,packed,name=repeated_double,json=repeatedDouble,proto3" json:"repeated_double,omitempty"` + RepeatedString []string `protobuf:"bytes,215,rep,name=repeated_string,json=repeatedString,proto3" json:"repeated_string,omitempty"` + RepeatedBytes [][]byte `protobuf:"bytes,216,rep,name=repeated_bytes,json=repeatedBytes,proto3" json:"repeated_bytes,omitempty"` + Repeated_Message []*FieldTestMessage_Message `protobuf:"bytes,217,rep,name=repeated_Message,json=repeatedMessage,proto3" json:"repeated_Message,omitempty"` MapInt32Int64 map[int32]int64 `protobuf:"bytes,500,rep,name=map_int32_int64,json=mapInt32Int64,proto3" json:"map_int32_int64,omitempty" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3"` MapStringMessage map[string]*FieldTestMessage_Message `protobuf:"bytes,501,rep,name=map_string_message,json=mapStringMessage,proto3" json:"map_string_message,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` MapFixed64Enum map[uint64]FieldTestMessage_Enum `protobuf:"bytes,502,rep,name=map_fixed64_enum,json=mapFixed64Enum,proto3" json:"map_fixed64_enum,omitempty" protobuf_key:"fixed64,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3,enum=goproto.protoc.proto3.FieldTestMessage_Enum"` @@ -212,6 +229,125 @@ func (m *FieldTestMessage) GetOptional_Message() *FieldTestMessage_Message { return nil } +func (m *FieldTestMessage) GetRepeatedBool() []bool { + if m != nil { + return m.RepeatedBool + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedEnum() []FieldTestMessage_Enum { + if m != nil { + return m.RepeatedEnum + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedInt32() []int32 { + if m != nil { + return m.RepeatedInt32 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedSint32() []int32 { + if m != nil { + return m.RepeatedSint32 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedUint32() []uint32 { + if m != nil { + return m.RepeatedUint32 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedInt64() []int64 { + if m != nil { + return m.RepeatedInt64 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedSint64() []int64 { + if m != nil { + return m.RepeatedSint64 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedUint64() []uint64 { + if m != nil { + return m.RepeatedUint64 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedSfixed32() []int32 { + if m != nil { + return m.RepeatedSfixed32 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedFixed32() []uint32 { + if m != nil { + return m.RepeatedFixed32 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedFloat() []float32 { + if m != nil { + return m.RepeatedFloat + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedSfixed64() []int64 { + if m != nil { + return m.RepeatedSfixed64 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedFixed64() []uint64 { + if m != nil { + return m.RepeatedFixed64 + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedDouble() []float64 { + if m != nil { + return m.RepeatedDouble + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedString() []string { + if m != nil { + return m.RepeatedString + } + return nil +} + +func (m *FieldTestMessage) GetRepeatedBytes() [][]byte { + if m != nil { + return m.RepeatedBytes + } + return nil +} + +func (m *FieldTestMessage) GetRepeated_Message() []*FieldTestMessage_Message { + if m != nil { + return m.Repeated_Message + } + return nil +} + func (m *FieldTestMessage) GetMapInt32Int64() map[int32]int64 { if m != nil { return m.MapInt32Int64 @@ -276,43 +412,57 @@ func init() { func init() { proto.RegisterFile("proto3/fields.proto", fileDescriptor_f1e3ea068187307c) } var fileDescriptor_f1e3ea068187307c = []byte{ - // 596 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x94, 0x4b, 0x8f, 0xd3, 0x3c, - 0x18, 0x85, 0x3f, 0x4f, 0x7a, 0x99, 0xba, 0xb7, 0xd4, 0xf3, 0x8d, 0x64, 0xcd, 0xca, 0x1a, 0x84, - 0x30, 0x82, 0x49, 0xa5, 0x36, 0x8a, 0x60, 0x46, 0x48, 0xa8, 0xa2, 0x95, 0x58, 0x20, 0x44, 0x80, - 0x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x00, + // 826 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x95, 0xdb, 0x6f, 0xda, 0x48, + 0x14, 0xc6, 0x77, 0xb8, 0x86, 0x09, 0x17, 0x33, 0xd9, 0x48, 0xa3, 0x3c, 0x8d, 0xb2, 0xb7, 0xd9, + 0xdd, 0x04, 0x24, 0xb0, 0xac, 0xdd, 0x44, 0x2b, 0xad, 0xd0, 0x12, 0x69, 0x1f, 0xaa, 0xaa, 0x6e, + 0xfb, 0x92, 0x3e, 0x44, 0x06, 0x06, 0x0b, 0xd5, 0xf6, 0x20, 0x30, 0x55, 0xf3, 0x27, 0xf6, 0x9e, + 0xde, 0xdb, 0xff, 0xa3, 0xed, 0x73, 0x35, 0x9e, 0xb1, 0x3d, 0x36, 0x3c, 0x84, 0x3c, 0xd9, 0x3e, + 0x7c, 0x7c, 0xbf, 0x39, 0xc7, 0xe7, 0x03, 0xb8, 0x37, 0x5f, 0xf0, 0x90, 0xf7, 0xbb, 0xd3, 0x19, + 0xf3, 0x26, 0xcb, 0x4e, 0xf4, 0x84, 0xf6, 0x5d, 0x1e, 0xdd, 0xc8, 0xc7, 0xb1, 0xbc, 0xf4, 0x0f, + 0xaf, 0xda, 0xd0, 0x38, 0x13, 0xba, 0x7b, 0x6c, 0x19, 0xde, 0x62, 0xcb, 0xa5, 0xe3, 0x32, 0xf4, + 0x13, 0x6c, 0xf0, 0x79, 0x38, 0xe3, 0x81, 0xe3, 0x5d, 0x8c, 0x38, 0xf7, 0x30, 0x20, 0x80, 0xd6, + 0xec, 0x7a, 0x5c, 0x1c, 0x70, 0xee, 0xa1, 0x3b, 0x9a, 0x88, 0x05, 0x2b, 0x1f, 0x17, 0x08, 0xa0, + 0xcd, 0xde, 0x51, 0x67, 0x23, 0xa8, 0x93, 0x87, 0x74, 0x86, 0xc1, 0xca, 0x4f, 0x2d, 0xc5, 0x13, + 0xfa, 0x05, 0x36, 0x13, 0xcb, 0x59, 0x10, 0xf6, 0x7b, 0xb8, 0x48, 0x00, 0x2d, 0xdb, 0x09, 0xe8, + 0x7f, 0x51, 0x44, 0xbf, 0xc1, 0x56, 0x22, 0x5b, 0x4a, 0x5d, 0x89, 0x00, 0xda, 0xb6, 0x93, 0x6f, + 0xdf, 0x9d, 0xad, 0x09, 0x57, 0x52, 0x58, 0x26, 0x80, 0x36, 0x52, 0xe1, 0x7d, 0x29, 0xcc, 0x81, + 0x2d, 0x13, 0x57, 0x08, 0xa0, 0xc5, 0x0c, 0xd8, 0x32, 0xd7, 0xc0, 0x96, 0x89, 0xab, 0x04, 0x50, + 0x94, 0x05, 0xe7, 0x84, 0x2b, 0x29, 0xdc, 0x21, 0x80, 0x96, 0xb2, 0x60, 0xcb, 0x44, 0x7f, 0xc2, + 0x76, 0xea, 0x38, 0x9d, 0x3d, 0x66, 0x93, 0x7e, 0x0f, 0xd7, 0x08, 0xa0, 0x2d, 0xdb, 0x48, 0x3c, + 0x55, 0x1d, 0xfd, 0x0e, 0x93, 0xda, 0x45, 0xac, 0x85, 0x04, 0xd0, 0xaa, 0x9d, 0xd0, 0xce, 0x94, + 0x54, 0x6f, 0x68, 0xea, 0x71, 0x27, 0xc4, 0xbb, 0x04, 0xd0, 0x42, 0xda, 0xd0, 0x99, 0x28, 0x6e, + 0xc0, 0x5b, 0x26, 0xae, 0x13, 0x40, 0x8d, 0x3c, 0xde, 0x32, 0xd7, 0xf1, 0x96, 0x89, 0x1b, 0x04, + 0xd0, 0x4a, 0x0e, 0x9f, 0xeb, 0x7f, 0xc2, 0x57, 0x23, 0x8f, 0xe1, 0x26, 0x01, 0x14, 0xa4, 0xfd, + 0xff, 0x17, 0x55, 0xb3, 0x13, 0x0d, 0x17, 0xb3, 0xc0, 0xc5, 0xad, 0x68, 0xd7, 0xd2, 0x89, 0x46, + 0xd5, 0x4c, 0x43, 0xa3, 0xcb, 0x90, 0x2d, 0xb1, 0x41, 0x00, 0xad,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xa1, 0x6e, 0x72, 0x83, 0x17, 0xaf, 0x51, 0x39, 0xdc, + 0xdb, 0xd0, 0x9e, 0xce, 0xac, 0x48, 0xe6, 0x40, 0x67, 0x6e, 0xfb, 0x9b, 0xa8, 0x01, 0x6b, 0xb0, + 0xaa, 0x3e, 0x3a, 0x34, 0x60, 0x29, 0xfa, 0x8d, 0xdc, 0x81, 0xa5, 0xf3, 0xa1, 0x7d, 0xdb, 0xf8, + 0x61, 0x70, 0x7a, 0xfe, 0xb7, 0xcb, 0xb9, 0xeb, 0xb1, 0x8e, 0xcb, 0x3d, 0x27, 0x70, 0x3b, 0x7c, + 0xe1, 0x76, 0x23, 0xe7, 0xee, 0xd8, 0x9f, 0xc8, 0xbb, 0xf1, 0xb1, 0xcb, 0x82, 0x63, 0x97, 0x77, + 0x43, 0xb6, 0x0c, 0x27, 0x4e, 0xe8, 0xc8, 0x72, 0x7f, 0x54, 0x91, 0xd7, 0xef, 0x01, 0x00, 0x00, + 0xff, 0xff, 0xc1, 0x70, 0x3f, 0x3d, 0x44, 0x09, 0x00, 0x00, } diff --git a/cmd/protoc-gen-go/testdata/proto3/fields.proto b/cmd/protoc-gen-go/testdata/proto3/fields.proto index f16bf819..5ae7344e 100644 --- a/cmd/protoc-gen-go/testdata/proto3/fields.proto +++ b/cmd/protoc-gen-go/testdata/proto3/fields.proto @@ -27,6 +27,24 @@ message FieldTestMessage { bytes optional_bytes = 16; Message optional_Message = 17; + repeated bool repeated_bool = 201; + repeated Enum repeated_enum = 202; + repeated int32 repeated_int32 = 203; + repeated sint32 repeated_sint32 = 204; + repeated uint32 repeated_uint32 = 205; + repeated int64 repeated_int64 = 206; + repeated sint64 repeated_sint64 = 207; + repeated uint64 repeated_uint64 = 208; + repeated sfixed32 repeated_sfixed32 = 209; + repeated fixed32 repeated_fixed32 = 210; + repeated float repeated_float = 211; + repeated sfixed64 repeated_sfixed64 = 212; + repeated fixed64 repeated_fixed64 = 213; + repeated double repeated_double = 214; + repeated string repeated_string = 215; + repeated bytes repeated_bytes = 216; + repeated Message repeated_Message = 217; + map map_int32_int64 = 500; map map_string_message = 501; map map_fixed64_enum = 502; diff --git a/reflect/prototype/protofile_desc.go b/reflect/prototype/protofile_desc.go index 8ab32794..161980e9 100644 --- a/reflect/prototype/protofile_desc.go +++ b/reflect/prototype/protofile_desc.go @@ -95,7 +95,7 @@ func NewFileFromDescriptorProto(fd *descriptorV1.FileDescriptorProto, r *protore } var err error - f.Messages, err = messagesFromDescriptorProto(fd.GetMessageType(), r) + f.Messages, err = messagesFromDescriptorProto(fd.GetMessageType(), f.Syntax, r) if err != nil { return nil, err } @@ -115,7 +115,7 @@ func NewFileFromDescriptorProto(fd *descriptorV1.FileDescriptorProto, r *protore return NewFile(&f) } -func messagesFromDescriptorProto(mds []*descriptorV1.DescriptorProto, r *protoregistry.Files) (ms []Message, err error) { +func messagesFromDescriptorProto(mds []*descriptorV1.DescriptorProto, syntax protoreflect.Syntax, r *protoregistry.Files) (ms []Message, err error) { for _, md := range mds { var m Message m.Name = protoreflect.Name(md.GetName()) @@ -127,7 +127,16 @@ func messagesFromDescriptorProto(mds []*descriptorV1.DescriptorProto, r *protore f.Cardinality = protoreflect.Cardinality(fd.GetLabel()) f.Kind = protoreflect.Kind(fd.GetType()) f.JSONName = fd.GetJsonName() - f.IsPacked = fd.GetOptions().GetPacked() + if opts := fd.GetOptions(); opts != nil && opts.Packed != nil { + f.IsPacked = *opts.Packed + } else { + // https://developers.google.com/protocol-buffers/docs/proto3: + // "In proto3, repeated fields of scalar numeric types use packed + // encoding by default." + f.IsPacked = (syntax == protoreflect.Proto3 && + f.Cardinality == protoreflect.Repeated && + isScalarNumeric[f.Kind]) + } f.IsWeak = fd.GetOptions().GetWeak() if fd.DefaultValue != nil { f.Default, err = parseDefault(fd.GetDefaultValue(), f.Kind) @@ -172,7 +181,7 @@ func messagesFromDescriptorProto(mds []*descriptorV1.DescriptorProto, r *protore }) } - m.Messages, err = messagesFromDescriptorProto(md.GetNestedType(), r) + m.Messages, err = messagesFromDescriptorProto(md.GetNestedType(), syntax, r) if err != nil { return nil, err } @@ -190,6 +199,23 @@ func messagesFromDescriptorProto(mds []*descriptorV1.DescriptorProto, r *protore return ms, nil } +var isScalarNumeric = map[protoreflect.Kind]bool{ + protoreflect.BoolKind: true, + protoreflect.EnumKind: true, + protoreflect.Int32Kind: true, + protoreflect.Sint32Kind: true, + protoreflect.Uint32Kind: true, + protoreflect.Int64Kind: true, + protoreflect.Sint64Kind: true, + protoreflect.Uint64Kind: true, + protoreflect.Sfixed32Kind: true, + protoreflect.Fixed32Kind: true, + protoreflect.FloatKind: true, + protoreflect.Sfixed64Kind: true, + protoreflect.Fixed64Kind: true, + protoreflect.DoubleKind: true, +} + func enumsFromDescriptorProto(eds []*descriptorV1.EnumDescriptorProto, r *protoregistry.Files) (es []Enum, err error) { for _, ed := range eds { var e Enum @@ -212,6 +238,11 @@ func extensionsFromDescriptorProto(xds []*descriptorV1.FieldDescriptorProto, r * x.Number = protoreflect.FieldNumber(xd.GetNumber()) x.Cardinality = protoreflect.Cardinality(xd.GetLabel()) x.Kind = protoreflect.Kind(xd.GetType()) + // TODO: When a proto3 file extends a proto2 message (permitted only for + // extending descriptor options), does the extension have proto2 or proto3 + // semantics? If the latter, repeated, scalar, numeric, proto3 extension + // fields should default to packed. If the former, perhaps the extension syntax + // should be protoreflect.Proto2. x.IsPacked = xd.GetOptions().GetPacked() if xd.DefaultValue != nil { x.Default, err = parseDefault(xd.GetDefaultValue(), x.Kind)