diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go index b3058193..53d64580 100644 --- a/cmd/protoc-gen-go/internal_gengo/main.go +++ b/cmd/protoc-gen-go/internal_gengo/main.go @@ -172,8 +172,14 @@ func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp GoName: "Default_" + message.GoIdent.GoName + "_" + field.GoName, } decl := "const" - if field.Desc.Kind() == protoreflect.BytesKind { + switch field.Desc.Kind() { + case protoreflect.BytesKind: decl = "var" + case protoreflect.FloatKind, protoreflect.DoubleKind: + f := field.Desc.Default().Float() + if math.IsInf(f, -1) || math.IsInf(f, 1) || math.IsNaN(f) { + decl = "var" + } } g.P(decl, " ", defVar.GoName, " = ", defVar) } diff --git a/cmd/protoc-gen-go/testdata/import_public/a.pb.go b/cmd/protoc-gen-go/testdata/import_public/a.pb.go index 3870ac0c..62a43410 100644 --- a/cmd/protoc-gen-go/testdata/import_public/a.pb.go +++ b/cmd/protoc-gen-go/testdata/import_public/a.pb.go @@ -23,6 +23,9 @@ const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package const Default_M_S = sub.Default_M_S +var Default_M_B = sub.Default_M_B +var Default_M_F = sub.Default_M_F + // M from public import import_public/sub/a.proto type M = sub.M type M_OneofInt32 = sub.M_OneofInt32 diff --git a/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go b/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go index 35b37f22..9394cb1a 100644 --- a/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go +++ b/cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go @@ -133,8 +133,10 @@ func (M_Submessage_Submessage_Subenum) EnumDescriptor() ([]byte, []int) { type M struct { // Field using a type in the same Go package, but a different source file. - M2 *M2 `protobuf:"bytes,1,opt,name=m2" json:"m2,omitempty"` - S *string `protobuf:"bytes,4,opt,name=s,def=default" json:"s,omitempty"` + M2 *M2 `protobuf:"bytes,1,opt,name=m2" json:"m2,omitempty"` + S *string `protobuf:"bytes,4,opt,name=s,def=default" json:"s,omitempty"` + B []byte `protobuf:"bytes,5,opt,name=b,def=default" json:"b,omitempty"` + F *float64 `protobuf:"fixed64,6,opt,name=f,def=nan" json:"f,omitempty"` // Types that are valid to be assigned to OneofField: // *M_OneofInt32 // *M_OneofInt64 @@ -180,6 +182,9 @@ var xxx_messageInfo_M proto.InternalMessageInfo const Default_M_S string = "default" +var Default_M_B []byte = []byte("default") +var Default_M_F float64 = math.NaN() + func (m *M) GetM2() *M2 { if m != nil { return m.M2 @@ -194,6 +199,20 @@ func (m *M) GetS() string { return Default_M_S } +func (m *M) GetB() []byte { + if m != nil && m.B != nil { + return m.B + } + return append([]byte(nil), Default_M_B...) +} + +func (m *M) GetF() float64 { + if m != nil && m.F != nil { + return *m.F + } + return Default_M_F +} + type isM_OneofField interface { isM_OneofField() } @@ -340,29 +359,31 @@ func init() { func init() { proto.RegisterFile("import_public/sub/a.proto", fileDescriptor_382f7805394b5c4e) } var fileDescriptor_382f7805394b5c4e = []byte{ - // 377 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xcf, 0x8f, 0x93, 0x40, - 0x14, 0xc7, 0x77, 0x60, 0x75, 0xeb, 0xdb, 0xac, 0xae, 0x13, 0x6b, 0x70, 0x4f, 0x58, 0x3d, 0x90, - 0x35, 0x65, 0x12, 0x24, 0x1c, 0x7a, 0x93, 0x04, 0x7f, 0xa5, 0xa4, 0x09, 0xc4, 0x4b, 0x2f, 0x84, - 0x81, 0x01, 0x49, 0x80, 0x69, 0x3a, 0x33, 0xc6, 0x23, 0x7f, 0x80, 0xff, 0x95, 0xff, 0x98, 0x29, - 0xf4, 0x67, 0xac, 0x71, 0x6f, 0xbc, 0xc7, 0xe7, 0xf3, 0x7d, 0x93, 0x79, 0x03, 0xaf, 0xaa, 0x66, - 0xc5, 0xd7, 0x32,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1, 0x6a, 0xe7, 0x02, 0x3c, 0x0e, 0xb7, 0xc2, - 0xfd, 0x68, 0x94, 0xdf, 0x76, 0x5d, 0xd7, 0x69, 0xfe, 0xcd, 0xee, 0x26, 0x7a, 0xfe, 0xfe, 0x06, - 0x50, 0x80, 0x47, 0x70, 0x39, 0x70, 0xb3, 0x39, 0x3c, 0x63, 0x3f, 0x25, 0x6b, 0x45, 0xc5, 0xdb, - 0x81, 0xc0, 0x6f, 0x1e, 0xb0, 0x1e, 0x23, 0xdf, 0x2c, 0x25, 0x7a, 0xba, 0x77, 0x3f, 0x6e, 0x54, - 0x7f, 0xbe, 0xfc, 0x5a, 0x56, 0xf2, 0xbb, 0xa2, 0x76, 0xc6, 0x1b, 0x52, 0xf2, 0x3a, 0x6d, 0x4b, - 0xd2, 0xa7, 0x50, 0x55, 0x90, 0x1f, 0x0e, 0xc9, 0x9a, 0x7c, 0xa8, 0xb3, 0x69, 0xc9, 0xda, 0x69, - 0xc9, 0x89, 0x64, 0x42, 0xe6, 0xa9, 0x4c, 0xc9, 0x5f, 0x2f, 0xe8, 0x4f, 0x00, 0x00, 0x00, 0xff, - 0xff, 0xcd, 0x86, 0xfe, 0xf7, 0x92, 0x02, 0x00, 0x00, + // 402 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xcb, 0x6e, 0xd3, 0x40, + 0x14, 0x86, 0x7b, 0xe2, 0x5e, 0xc2, 0x29, 0x85, 0x76, 0x44, 0xd0, 0xd0, 0x95, 0x09, 0x2c, 0xac, + 0xa2, 0x7a, 0x24, 0x63, 0x79, 0x91, 0x1d, 0x91, 0xc2, 0x4d, 0xb5, 0x2a, 0xd9, 0x62, 0xd3, 0x8d, + 0xe5, 0xb1, 0xc7, 0xc6, 0x52, 0x3c, 0x13, 0x75, 0x66, 0x10, 0xcb, 0x3c, 0x15, 0x2f, 0xc0, 0x8b, + 0xa1, 0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a2, 0x70, 0xfc, 0x0e, 0xc9, 0x7a, 0x7b, 0x96, 0x5a, + 0x2e, 0xa4, 0x6d, 0xc9, 0x08, 0xcf, 0xe2, 0x2c, 0xfd, 0x36, 0x8d, 0x67, 0x69, 0xfa, 0xe1, 0xd3, + 0x2c, 0xbb, 0x99, 0x25, 0xd7, 0xa7, 0x7b, 0x53, 0xba, 0x63, 0x49, 0xd5, 0x88, 0x79, 0x39, 0x1e, + 0xe1, 0xd1, 0xbd, 0x8b, 0x78, 0x18, 0xdf, 0x09, 0x17, 0xc3, 0x61, 0x79, 0xba, 0x5c, 0x2e, 0x97, + 0x83, 0xe9, 0xc9, 0xfd, 0x9f, 0xe8, 0xf8, 0x8b, 0x13, 0x84, 0x19, 0x19, 0xe2, 0x7e, 0xcf, 0x4d, + 0xae, 0xf0, 0xb9, 0xf8, 0x69, 0x84, 0xd4, 0x8d, 0x92, 0x3d, 0x41, 0xde, 0x3c, 0xe2, 0x20, 0x69, + 0xb9, 0x3a, 0xbe, 0xe4, 0xd9, 0x83, 0xfb, 0x71, 0xa5, 0x4e, 0xaf, 0x6e, 0xbe, 0xd6, 0x8d, 0xf9, + 0x6e, 0xb9, 0x5f, 0xa8, 0x96, 0xd5, 0x6a, 0x9e, 0xcb, 0x9a, 0x75, 0x2d, 0xdc, 0x56, 0xec, 0x47, + 0xc0, 0x8a, 0xb6, 0xec, 0xe7, 0xe2, 0xb2, 0x16, 0xf2, 0xb2, 0x56, 0xcc, 0x08, 0x6d, 0xca, 0xdc, + 0xe4, 0xec, 0xaf, 0xbb, 0xf6, 0x27, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xa9, 0x17, 0xb5, 0xbc, 0x02, + 0x00, 0x00, } diff --git a/cmd/protoc-gen-go/testdata/import_public/sub/a.proto b/cmd/protoc-gen-go/testdata/import_public/sub/a.proto index d3c77bd5..a833b110 100644 --- a/cmd/protoc-gen-go/testdata/import_public/sub/a.proto +++ b/cmd/protoc-gen-go/testdata/import_public/sub/a.proto @@ -14,6 +14,8 @@ message M { // Field using a type in the same Go package, but a different source file. optional M2 m2 = 1; optional string s = 4 [default="default"]; + optional bytes b = 5 [default="default"]; + optional double f = 6 [default=nan]; oneof oneof_field { int32 oneof_int32 = 2;