Kaynağa Gözat

goprotobuf: Make the default default of an enum field be the value of the first enum value, not zero.

This makes Go consistent with C++/Java/Python.

Fixes #43.

R=r
CC=golang-dev
https://codereview.appspot.com/13501049
David Symonds 12 yıl önce
ebeveyn
işleme
1cb9013ade

+ 3 - 3
proto/testdata/test.pb.go

@@ -244,7 +244,7 @@ func (m *GoEnum) GetFoo() FOO {
 	if m != nil && m.Foo != nil {
 		return *m.Foo
 	}
-	return 0
+	return FOO_FOO1
 }
 
 type GoTestField struct {
@@ -378,7 +378,7 @@ func (m *GoTest) GetKind() GoTest_KIND {
 	if m != nil && m.Kind != nil {
 		return *m.Kind
 	}
-	return 0
+	return GoTest_VOID
 }
 
 func (m *GoTest) GetTable() string {
@@ -1289,7 +1289,7 @@ func (m *MyMessage) GetBikeshed() MyMessage_Color {
 	if m != nil && m.Bikeshed != nil {
 		return *m.Bikeshed
 	}
-	return 0
+	return MyMessage_RED
 }
 
 func (m *MyMessage) GetSomegroup() *MyMessage_SomeGroup {

+ 2 - 2
protoc-gen-go/descriptor/descriptor.pb.go

@@ -487,14 +487,14 @@ func (m *FieldDescriptorProto) GetLabel() FieldDescriptorProto_Label {
 	if m != nil && m.Label != nil {
 		return *m.Label
 	}
-	return 0
+	return FieldDescriptorProto_LABEL_OPTIONAL
 }
 
 func (m *FieldDescriptorProto) GetType() FieldDescriptorProto_Type {
 	if m != nil && m.Type != nil {
 		return *m.Type
 	}
-	return 0
+	return FieldDescriptorProto_TYPE_DOUBLE
 }
 
 func (m *FieldDescriptorProto) GetTypeName() string {

+ 21 - 0
protoc-gen-go/generator/generator.go

@@ -1664,6 +1664,27 @@ func (g *Generator) generateMessage(message *Descriptor) {
 				g.P("return false")
 			case descriptor.FieldDescriptorProto_TYPE_STRING:
 				g.P(`return ""`)
+			case descriptor.FieldDescriptorProto_TYPE_ENUM:
+				// The default default for an enum is the first value in the enum,
+				// not zero.
+				obj := g.ObjectNamed(field.GetTypeName())
+				var enum *EnumDescriptor
+				if id, ok := obj.(*ImportedDescriptor); ok {
+					// The enum type has been publicly imported.
+					enum, _ = id.o.(*EnumDescriptor)
+				} else {
+					enum, _ = obj.(*EnumDescriptor)
+				}
+				if enum == nil {
+					log.Printf("don't know how to generate getter for %s", field.GetName())
+					continue
+				}
+				if len(enum.Value) == 0 {
+					g.P("return 0 // empty enum")
+				} else {
+					first := enum.Value[0].GetName()
+					g.P("return ", g.DefaultPackageName(obj)+enum.prefix()+first)
+				}
 			default:
 				g.P("return 0")
 			}

+ 1 - 1
protoc-gen-go/testdata/my_test/test.pb.go

@@ -199,7 +199,7 @@ func (m *Request) GetHue() Request_Color {
 	if m != nil && m.Hue != nil {
 		return *m.Hue
 	}
-	return 0
+	return Request_RED
 }
 
 func (m *Request) GetHat() HatType {

+ 1 - 1
protoc-gen-go/testdata/my_test/test.pb.go.golden

@@ -199,7 +199,7 @@ func (m *Request) GetHue() Request_Color {
 	if m != nil && m.Hue != nil {
 		return *m.Hue
 	}
-	return 0
+	return Request_RED
 }
 
 func (m *Request) GetHat() HatType {

+ 1 - 1
protoc-gen-go/testdata/my_test/test.proto

@@ -58,7 +58,7 @@ message Request {
   }
   repeated int64 key = 1;
 //  optional imp.ImportedMessage imported_message = 2;
-  optional Color hue = 3;
+  optional Color hue = 3; // no default
   optional HatType hat = 4 [default=FEDORA];
 //  optional imp.ImportedMessage.Owner owner = 6;
   optional float deadline = 7 [default=inf];