From 21f62f4d539bf2ef68401631ca4c7e1f9cb2caa4 Mon Sep 17 00:00:00 2001 From: Damien Neil Date: Mon, 8 Oct 2018 14:08:27 -0700 Subject: [PATCH] reflect/prototype: fix name resolution when file has no package name A .proto source file with no 'package' statement may still contain references to descriptors within the file. Change-Id: I86e942c9c06e5a2915e9722162e0455ffa9ba2ab Reviewed-on: https://go-review.googlesource.com/c/140899 Reviewed-by: Joe Tsai --- .../testdata/nopackage/nopackage.pb.go | 19 +++++++++++-------- .../testdata/nopackage/nopackage.proto | 2 +- reflect/prototype/protofile_type.go | 2 ++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cmd/protoc-gen-go/testdata/nopackage/nopackage.pb.go b/cmd/protoc-gen-go/testdata/nopackage/nopackage.pb.go index 7754ac0e..fbb7da39 100644 --- a/cmd/protoc-gen-go/testdata/nopackage/nopackage.pb.go +++ b/cmd/protoc-gen-go/testdata/nopackage/nopackage.pb.go @@ -59,7 +59,7 @@ func (Enum) EnumDescriptor() ([]byte, []int) { type Message struct { StringField *string `protobuf:"bytes,1,opt,name=string_field,json=stringField" json:"string_field,omitempty"` - EnumField *Enum `protobuf:"varint,2,opt,name=enum_field,json=enumField,enum=Enum" json:"enum_field,omitempty"` + EnumField *Enum `protobuf:"varint,2,opt,name=enum_field,json=enumField,enum=Enum,def=0" json:"enum_field,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -90,6 +90,8 @@ func (m *Message) XXX_DiscardUnknown() { var xxx_messageInfo_Message proto.InternalMessageInfo +const Default_Message_EnumField Enum = Enum_ZERO + func (m *Message) GetStringField() string { if m != nil && m.StringField != nil { return *m.StringField @@ -101,7 +103,7 @@ func (m *Message) GetEnumField() Enum { if m != nil && m.EnumField != nil { return *m.EnumField } - return Enum_ZERO + return Default_Message_EnumField } func init() { @@ -112,13 +114,14 @@ func init() { func init() { proto.RegisterFile("nopackage/nopackage.proto", fileDescriptor_f33a1d5d178c43c9) } var fileDescriptor_f33a1d5d178c43c9 = []byte{ - // 127 bytes of a gzipped FileDescriptorProto + // 130 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xcc, 0xcb, 0x2f, 0x48, - 0x4c, 0xce, 0x4e, 0x4c, 0x4f, 0xd5, 0x87, 0xb3, 0xf4, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x95, 0x82, + 0x4c, 0xce, 0x4e, 0x4c, 0x4f, 0xd5, 0x87, 0xb3, 0xf4, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x95, 0x22, 0xb8, 0xd8, 0x7d, 0x53, 0x8b, 0x8b, 0x13, 0xd3, 0x53, 0x85, 0x14, 0xb9, 0x78, 0x8a, 0x4b, 0x8a, 0x32, 0xf3, 0xd2, 0xe3, 0xd3, 0x32, 0x53, 0x73, 0x52, 0x24, 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, - 0xb8, 0x21, 0x62, 0x6e, 0x20, 0x21, 0x21, 0x15, 0x2e, 0xae, 0xd4, 0xbc, 0xd2, 0x5c, 0xa8, 0x02, - 0x26, 0x05, 0x46, 0x0d, 0x3e, 0x23, 0x56, 0x3d, 0xd7, 0xbc, 0xd2, 0xdc, 0x20, 0x4e, 0x90, 0x04, - 0x58, 0x95, 0x96, 0x00, 0x17, 0x0b, 0x48, 0x48, 0x88, 0x83, 0x8b, 0x25, 0xca, 0x35, 0xc8, 0x5f, - 0x80, 0x01, 0x10, 0x00, 0x00, 0xff, 0xff, 0xb2, 0xbc, 0x78, 0x1f, 0x81, 0x00, 0x00, 0x00, + 0xb8, 0x21, 0x62, 0x6e, 0x20, 0x21, 0x21, 0x2d, 0x2e, 0xae, 0xd4, 0xbc, 0xd2, 0x5c, 0xa8, 0x02, + 0x26, 0x05, 0x46, 0x0d, 0x3e, 0x23, 0x56, 0x3d, 0xd7, 0xbc, 0xd2, 0x5c, 0x2b, 0x96, 0x28, 0xd7, + 0x20, 0xff, 0x20, 0x4e, 0x90, 0x34, 0x58, 0xad, 0x96, 0x00, 0x17, 0x0b, 0x48, 0x42, 0x88, 0x83, + 0x0b, 0x2c, 0x25, 0xc0, 0x00, 0x08, 0x00, 0x00, 0xff, 0xff, 0x31, 0x29, 0xe4, 0xb2, 0x87, 0x00, + 0x00, 0x00, } diff --git a/cmd/protoc-gen-go/testdata/nopackage/nopackage.proto b/cmd/protoc-gen-go/testdata/nopackage/nopackage.proto index e2f2f969..892bb066 100644 --- a/cmd/protoc-gen-go/testdata/nopackage/nopackage.proto +++ b/cmd/protoc-gen-go/testdata/nopackage/nopackage.proto @@ -12,5 +12,5 @@ enum Enum { message Message { optional string string_field = 1; - optional Enum enum_field = 2; + optional Enum enum_field = 2 [default=ZERO]; } diff --git a/reflect/prototype/protofile_type.go b/reflect/prototype/protofile_type.go index 99f3be91..66e8a622 100644 --- a/reflect/prototype/protofile_type.go +++ b/reflect/prototype/protofile_type.go @@ -544,6 +544,8 @@ func resolveReference(parent pref.Descriptor, refName pref.FullName) pref.Descri } else if refName[len(curName)] == '.' { refName = refName[len(curName)+len("."):] break // e.g., refName: foo.firetruck.driver, curName: foo.firetruck + } else if len(curName) == 0 { + break // FileDescriptor has no package name } // No match. (e.g., refName: foo.firetruck, curName: foo.fire) }