reflect/protoreflect: add FieldDescriptor.DefaultEnumValue method

Default values for enums are specified by name, not number. An enum
may contain multiple values with different names but the same number.
Representing the default as a protoreflect.Value containing an EnumNumber
can discard information.

Add a method returning the EnumValueDescriptor.

Change-Id: If8beee3f81d41c4f9af45423252603b86949c7a5
Reviewed-on: https://go-review.googlesource.com/c/145158
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index 8e57ebd..ed0d07f 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -497,8 +497,9 @@
 		case protoreflect.BytesKind:
 			g.P("var ", defVarName, " []byte = []byte(", strconv.Quote(string(def.Bytes())), ")")
 		case protoreflect.EnumKind:
+			evalueDesc := field.Desc.DefaultEnumValue()
 			enum := field.EnumType
-			evalue := enum.Values[enum.Desc.Values().ByNumber(def.Enum()).Index()]
+			evalue := enum.Values[evalueDesc.Index()]
 			g.P("const ", defVarName, " ", field.EnumType.GoIdent, " = ", evalue.GoIdent)
 		case protoreflect.FloatKind, protoreflect.DoubleKind:
 			// Floating point numbers need extra handling for -Inf/Inf/NaN.
diff --git a/cmd/protoc-gen-go/testdata/proto2/enum.pb.go b/cmd/protoc-gen-go/testdata/proto2/enum.pb.go
index 6e5cf27..5172108 100644
--- a/cmd/protoc-gen-go/testdata/proto2/enum.pb.go
+++ b/cmd/protoc-gen-go/testdata/proto2/enum.pb.go
@@ -256,9 +256,11 @@
 }
 
 type EnumContainerMessage1 struct {
-	XXX_NoUnkeyedLiteral struct{} `json:"-"`
-	XXX_unrecognized     []byte   `json:"-"`
-	XXX_sizecache        int32    `json:"-"`
+	DefaultDuplicate1    *EnumType2 `protobuf:"varint,1,opt,name=default_duplicate1,json=defaultDuplicate1,enum=goproto.protoc.proto2.EnumType2,def=1" json:"default_duplicate1,omitempty"`
+	DefaultDuplicate2    *EnumType2 `protobuf:"varint,2,opt,name=default_duplicate2,json=defaultDuplicate2,enum=goproto.protoc.proto2.EnumType2,def=1" json:"default_duplicate2,omitempty"`
+	XXX_NoUnkeyedLiteral struct{}   `json:"-"`
+	XXX_unrecognized     []byte     `json:"-"`
+	XXX_sizecache        int32      `json:"-"`
 }
 
 func (m *EnumContainerMessage1) Reset()         { *m = EnumContainerMessage1{} }
@@ -286,6 +288,23 @@
 
 var xxx_messageInfo_EnumContainerMessage1 proto.InternalMessageInfo
 
+const Default_EnumContainerMessage1_DefaultDuplicate1 EnumType2 = EnumType2_duplicate1
+const Default_EnumContainerMessage1_DefaultDuplicate2 EnumType2 = EnumType2_duplicate2
+
+func (m *EnumContainerMessage1) GetDefaultDuplicate1() EnumType2 {
+	if m != nil && m.DefaultDuplicate1 != nil {
+		return *m.DefaultDuplicate1
+	}
+	return Default_EnumContainerMessage1_DefaultDuplicate1
+}
+
+func (m *EnumContainerMessage1) GetDefaultDuplicate2() EnumType2 {
+	if m != nil && m.DefaultDuplicate2 != nil {
+		return *m.DefaultDuplicate2
+	}
+	return Default_EnumContainerMessage1_DefaultDuplicate2
+}
+
 type EnumContainerMessage1_EnumContainerMessage2 struct {
 	XXX_NoUnkeyedLiteral struct{} `json:"-"`
 	XXX_unrecognized     []byte   `json:"-"`
@@ -335,21 +354,24 @@
 func init() { proto.RegisterFile("proto2/enum.proto", fileDescriptor_de9f68860d540858) }
 
 var fileDescriptor_de9f68860d540858 = []byte{
-	// 248 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x90, 0x41, 0x4b, 0xc3, 0x30,
-	0x18, 0x86, 0xcd, 0x3c, 0x88, 0x39, 0x68, 0xac, 0xec, 0x32, 0xf0, 0xb2, 0x8b, 0x30, 0xd8, 0x42,
-	0xbe, 0x3f, 0x20, 0xad, 0xe6, 0xa6, 0xdd, 0xc1, 0xaa, 0xe0, 0x65, 0x64, 0xe9,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, 0x82, 0x24, 0x6c, 0xa9, 0x34, 0x64, 0xfa, 0x6f, 0xf8, 0x0e, 0x00, 0x00, 0xff, 0xff,
-	0x5b, 0x88, 0x8a, 0x80, 0x94, 0x01, 0x00, 0x00,
+	// 296 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0xd0, 0xc1, 0x4b, 0xfb, 0x30,
+	0x14, 0x07, 0xf0, 0x5f, 0xb6, 0xc3, 0x0f, 0x73, 0x98, 0x59, 0x64, 0x30, 0x06, 0xc2, 0xd8, 0x45,
+	0x19, 0xac, 0x21, 0xef, 0xe8, 0x45, 0x3a, 0xd7, 0x9b, 0x6e, 0xa0, 0x53, 0x41, 0x0f, 0x25, 0x6b,
+	0xb3, 0x58, 0x68, 0x93, 0xb2, 0x26, 0x82, 0xff, 0x84, 0x7f, 0xb3, 0xac, 0x95, 0xae, 0x62, 0x15,
+	0x3c, 0xe5, 0xbd, 0x97, 0x6f, 0x3e, 0x90, 0x87, 0xfb, 0xf9, 0xce, 0x58, 0x03, 0x4c, 0x6a, 0x97,
+	0x79, 0x65, 0x4d, 0x07, 0xca, 0x94, 0x45, 0xd5, 0x46, 0xd5, 0x01, 0x93, 0xf7, 0x2e, 0x1e, 0x04,
+	0xda, 0x65, 0x57, 0x46, 0x5b, 0x91, 0x68, 0xb9, 0xbb, 0x91, 0x45, 0x21, 0x94, 0xe4, 0xf4, 0x19,
+	0xd3, 0x58, 0x6e, 0x85, 0x4b, 0x6d, 0x18, 0xbb, 0x3c, 0x4d, 0x22, 0x61, 0x25, 0x1f, 0xa2, 0x31,
+	0x3a, 0xef, 0xc1, 0xd8, 0x6b, 0xd5, 0xbc, 0xbd, 0xb4, 0x7e, 0xcb, 0x25, 0x5c, 0xe0, 0xc3, 0x9b,
+	0xdb, 0xfe, 0xa7, 0xb3, 0xa8, 0x47, 0xad, 0x38, 0x0c, 0x3b, 0x7f, 0xc6, 0xe1, 0x3b, 0x0e, 0xa3,
+	0xa4, 0xfd, 0x4b, 0x30, 0x39, 0xc3, 0x64, 0x29, 0x0b, 0x2b, 0xe3, 0x9a, 0xf2, 0xe9, 0x09, 0x3e,
+	0x5e, 0x06, 0x77, 0xeb, 0x60, 0x11, 0x82, 0x1f, 0x3e, 0xf8, 0xd7, 0xf7, 0x01, 0xf9, 0xd7, 0x12,
+	0x9c, 0x37, 0x83, 0xf3, 0x9f, 0x83, 0xbc, 0x29, 0xf2, 0x5f, 0x44, 0xde, 0x14, 0x79, 0x2d, 0x4e,
+	0x4f, 0xf1, 0x51, 0x1d, 0xa1, 0xff, 0x71, 0x77, 0xb5, 0x0c, 0x08, 0xda, 0x17, 0xeb, 0xc7, 0x15,
+	0xe9, 0x4c, 0xd9, 0xe1, 0x1a, 0x68, 0x0f, 0x37, 0xd6, 0x4c, 0xd0, 0x97, 0x1e, 0x08, 0x1a, 0x75,
+	0x08, 0x9a, 0xfb, 0x4f, 0x97, 0x2a, 0xb1, 0x2f, 0x6e, 0xe3, 0x45, 0x26, 0x63, 0xca, 0xa4, 0x42,
+	0x2b, 0x56, 0x6e, 0x74, 0xe3, 0xb6, 0xec, 0x15, 0x58, 0x94, 0xc5, 0x55, 0x1f, 0xcd, 0x94, 0xd4,
+	0x33, 0x65, 0x98, 0x95, 0x85, 0x8d, 0x85, 0x15, 0xd5, 0x18, 0x3e, 0x02, 0x00, 0x00, 0xff, 0xff,
+	0x26, 0xf5, 0x57, 0x52, 0x4e, 0x02, 0x00, 0x00,
 }
diff --git a/cmd/protoc-gen-go/testdata/proto2/enum.proto b/cmd/protoc-gen-go/testdata/proto2/enum.proto
index aa52b9b..849ff6c 100644
--- a/cmd/protoc-gen-go/testdata/proto2/enum.proto
+++ b/cmd/protoc-gen-go/testdata/proto2/enum.proto
@@ -23,6 +23,9 @@
 }
 
 message EnumContainerMessage1 {
+  optional EnumType2 default_duplicate1 = 1 [default=duplicate1];
+  optional EnumType2 default_duplicate2 = 2 [default=duplicate2];
+
   // NestedEnumType1A comment.
   enum NestedEnumType1A {
     // NestedEnumType1A_VALUE comment.