diff options
author | David Symonds <dsymonds@golang.org> | 2013-09-18 11:42:34 +1000 |
---|---|---|
committer | David Symonds <dsymonds@golang.org> | 2013-09-18 11:42:34 +1000 |
commit | 1cb9013ade0675cdb8d6739dade6cb6717f43b9c (patch) | |
tree | cb4599708ea35e0436f4705ccbad5c97b408fe3a | |
parent | a7f3a0f1487abe20837086c9fd975acf54929068 (diff) | |
download | protobuf-1cb9013ade0675cdb8d6739dade6cb6717f43b9c.tar.gz |
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
-rw-r--r-- | proto/testdata/test.pb.go | 6 | ||||
-rw-r--r-- | protoc-gen-go/descriptor/descriptor.pb.go | 4 | ||||
-rw-r--r-- | protoc-gen-go/generator/generator.go | 21 | ||||
-rw-r--r-- | protoc-gen-go/testdata/my_test/test.pb.go | 2 | ||||
-rw-r--r-- | protoc-gen-go/testdata/my_test/test.pb.go.golden | 2 | ||||
-rw-r--r-- | protoc-gen-go/testdata/my_test/test.proto | 2 |
6 files changed, 29 insertions, 8 deletions
diff --git a/proto/testdata/test.pb.go b/proto/testdata/test.pb.go index d5e2d5e..b8e40cf 100644 --- a/proto/testdata/test.pb.go +++ b/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 { diff --git a/protoc-gen-go/descriptor/descriptor.pb.go b/protoc-gen-go/descriptor/descriptor.pb.go index 3acbe29..0b34acb 100644 --- a/protoc-gen-go/descriptor/descriptor.pb.go +++ b/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 { diff --git a/protoc-gen-go/generator/generator.go b/protoc-gen-go/generator/generator.go index 413f361..0b769d4 100644 --- a/protoc-gen-go/generator/generator.go +++ b/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") } diff --git a/protoc-gen-go/testdata/my_test/test.pb.go b/protoc-gen-go/testdata/my_test/test.pb.go index a9d538d..cfe9777 100644 --- a/protoc-gen-go/testdata/my_test/test.pb.go +++ b/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 { diff --git a/protoc-gen-go/testdata/my_test/test.pb.go.golden b/protoc-gen-go/testdata/my_test/test.pb.go.golden index a9d538d..cfe9777 100644 --- a/protoc-gen-go/testdata/my_test/test.pb.go.golden +++ b/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 { diff --git a/protoc-gen-go/testdata/my_test/test.proto b/protoc-gen-go/testdata/my_test/test.proto index 478e697..551585d 100644 --- a/protoc-gen-go/testdata/my_test/test.proto +++ b/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]; |