Kaynağa Gözat

protoc-gen-go: additional tests for public imports

Adds cases for importing nested messages, nested enums, and oneofs.
Damien Neil 7 yıl önce
ebeveyn
işleme
e6eece43ca

+ 23 - 0
protoc-gen-go/testdata/import_public/a.pb.go

@@ -23,6 +23,13 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
 
 
 // M from public import import_public/sub/a.proto
 // M from public import import_public/sub/a.proto
 type M = sub.M
 type M = sub.M
+type M_OneofInt32 = sub.M_OneofInt32
+type M_OneofInt64 = sub.M_OneofInt64
+
+// Submessage from public import import_public/sub/a.proto
+type M_Submessage = sub.M_Submessage
+type M_Submessage_SubmessageOneofInt32 = sub.M_Submessage_SubmessageOneofInt32
+type M_Submessage_SubmessageOneofInt64 = sub.M_Submessage_SubmessageOneofInt64
 
 
 // E from public import import_public/sub/a.proto
 // E from public import import_public/sub/a.proto
 type E = sub.E
 type E = sub.E
@@ -32,6 +39,22 @@ var E_value = sub.E_value
 
 
 const E_ZERO = E(sub.E_ZERO)
 const E_ZERO = E(sub.E_ZERO)
 
 
+// Subenum from public import import_public/sub/a.proto
+type M_Subenum = sub.M_Subenum
+
+var M_Subenum_name = sub.M_Subenum_name
+var M_Subenum_value = sub.M_Subenum_value
+
+const M_M_ZERO = M_Subenum(sub.M_M_ZERO)
+
+// Submessage_Subenum from public import import_public/sub/a.proto
+type M_Submessage_Submessage_Subenum = sub.M_Submessage_Submessage_Subenum
+
+var M_Submessage_Submessage_Subenum_name = sub.M_Submessage_Submessage_Subenum_name
+var M_Submessage_Submessage_Subenum_value = sub.M_Submessage_Submessage_Subenum_value
+
+const M_Submessage_M_SUBMESSAGE_ZERO = M_Submessage_Submessage_Subenum(sub.M_Submessage_M_SUBMESSAGE_ZERO)
+
 // Ignoring public import of Local from import_public/b.proto
 // Ignoring public import of Local from import_public/b.proto
 
 
 type Public struct {
 type Public struct {

+ 313 - 16
protoc-gen-go/testdata/import_public/sub/a.pb.go

@@ -42,12 +42,60 @@ func (E) EnumDescriptor() ([]byte, []int) {
 	return fileDescriptor_382f7805394b5c4e, []int{0}
 	return fileDescriptor_382f7805394b5c4e, []int{0}
 }
 }
 
 
+type M_Subenum int32
+
+const (
+	M_M_ZERO M_Subenum = 0
+)
+
+var M_Subenum_name = map[int32]string{
+	0: "M_ZERO",
+}
+
+var M_Subenum_value = map[string]int32{
+	"M_ZERO": 0,
+}
+
+func (x M_Subenum) String() string {
+	return proto.EnumName(M_Subenum_name, int32(x))
+}
+
+func (M_Subenum) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_382f7805394b5c4e, []int{0, 0}
+}
+
+type M_Submessage_Submessage_Subenum int32
+
+const (
+	M_Submessage_M_SUBMESSAGE_ZERO M_Submessage_Submessage_Subenum = 0
+)
+
+var M_Submessage_Submessage_Subenum_name = map[int32]string{
+	0: "M_SUBMESSAGE_ZERO",
+}
+
+var M_Submessage_Submessage_Subenum_value = map[string]int32{
+	"M_SUBMESSAGE_ZERO": 0,
+}
+
+func (x M_Submessage_Submessage_Subenum) String() string {
+	return proto.EnumName(M_Submessage_Submessage_Subenum_name, int32(x))
+}
+
+func (M_Submessage_Submessage_Subenum) EnumDescriptor() ([]byte, []int) {
+	return fileDescriptor_382f7805394b5c4e, []int{0, 0, 0}
+}
+
 type M struct {
 type M struct {
 	// Field using a type in the same Go package, but a different source file.
 	// Field using a type in the same Go package, but a different source file.
-	M2                   *M2      `protobuf:"bytes,1,opt,name=m2,proto3" json:"m2,omitempty"`
-	XXX_NoUnkeyedLiteral struct{} `json:"-"`
-	XXX_unrecognized     []byte   `json:"-"`
-	XXX_sizecache        int32    `json:"-"`
+	M2 *M2 `protobuf:"bytes,1,opt,name=m2,proto3" json:"m2,omitempty"`
+	// Types that are valid to be assigned to OneofField:
+	//	*M_OneofInt32
+	//	*M_OneofInt64
+	OneofField           isM_OneofField `protobuf_oneof:"oneof_field"`
+	XXX_NoUnkeyedLiteral struct{}       `json:"-"`
+	XXX_unrecognized     []byte         `json:"-"`
+	XXX_sizecache        int32          `json:"-"`
 }
 }
 
 
 func (m *M) Reset()         { *m = M{} }
 func (m *M) Reset()         { *m = M{} }
@@ -82,24 +130,273 @@ func (m *M) GetM2() *M2 {
 	return nil
 	return nil
 }
 }
 
 
+type isM_OneofField interface {
+	isM_OneofField()
+}
+
+type M_OneofInt32 struct {
+	OneofInt32 int32 `protobuf:"varint,2,opt,name=oneof_int32,json=oneofInt32,proto3,oneof"`
+}
+
+type M_OneofInt64 struct {
+	OneofInt64 int64 `protobuf:"varint,3,opt,name=oneof_int64,json=oneofInt64,proto3,oneof"`
+}
+
+func (*M_OneofInt32) isM_OneofField() {}
+
+func (*M_OneofInt64) isM_OneofField() {}
+
+func (m *M) GetOneofField() isM_OneofField {
+	if m != nil {
+		return m.OneofField
+	}
+	return nil
+}
+
+func (m *M) GetOneofInt32() int32 {
+	if x, ok := m.GetOneofField().(*M_OneofInt32); ok {
+		return x.OneofInt32
+	}
+	return 0
+}
+
+func (m *M) GetOneofInt64() int64 {
+	if x, ok := m.GetOneofField().(*M_OneofInt64); ok {
+		return x.OneofInt64
+	}
+	return 0
+}
+
+// XXX_OneofFuncs is for the internal use of the proto package.
+func (*M) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) {
+	return _M_OneofMarshaler, _M_OneofUnmarshaler, _M_OneofSizer, []interface{}{
+		(*M_OneofInt32)(nil),
+		(*M_OneofInt64)(nil),
+	}
+}
+
+func _M_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
+	m := msg.(*M)
+	// oneof_field
+	switch x := m.OneofField.(type) {
+	case *M_OneofInt32:
+		b.EncodeVarint(2<<3 | proto.WireVarint)
+		b.EncodeVarint(uint64(x.OneofInt32))
+	case *M_OneofInt64:
+		b.EncodeVarint(3<<3 | proto.WireVarint)
+		b.EncodeVarint(uint64(x.OneofInt64))
+	case nil:
+	default:
+		return fmt.Errorf("M.OneofField has unexpected type %T", x)
+	}
+	return nil
+}
+
+func _M_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
+	m := msg.(*M)
+	switch tag {
+	case 2: // oneof_field.oneof_int32
+		if wire != proto.WireVarint {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeVarint()
+		m.OneofField = &M_OneofInt32{int32(x)}
+		return true, err
+	case 3: // oneof_field.oneof_int64
+		if wire != proto.WireVarint {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeVarint()
+		m.OneofField = &M_OneofInt64{int64(x)}
+		return true, err
+	default:
+		return false, nil
+	}
+}
+
+func _M_OneofSizer(msg proto.Message) (n int) {
+	m := msg.(*M)
+	// oneof_field
+	switch x := m.OneofField.(type) {
+	case *M_OneofInt32:
+		n += 1 // tag and wire
+		n += proto.SizeVarint(uint64(x.OneofInt32))
+	case *M_OneofInt64:
+		n += 1 // tag and wire
+		n += proto.SizeVarint(uint64(x.OneofInt64))
+	case nil:
+	default:
+		panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
+	}
+	return n
+}
+
+type M_Submessage struct {
+	// Types that are valid to be assigned to SubmessageOneofField:
+	//	*M_Submessage_SubmessageOneofInt32
+	//	*M_Submessage_SubmessageOneofInt64
+	SubmessageOneofField isM_Submessage_SubmessageOneofField `protobuf_oneof:"submessage_oneof_field"`
+	XXX_NoUnkeyedLiteral struct{}                            `json:"-"`
+	XXX_unrecognized     []byte                              `json:"-"`
+	XXX_sizecache        int32                               `json:"-"`
+}
+
+func (m *M_Submessage) Reset()         { *m = M_Submessage{} }
+func (m *M_Submessage) String() string { return proto.CompactTextString(m) }
+func (*M_Submessage) ProtoMessage()    {}
+func (*M_Submessage) Descriptor() ([]byte, []int) {
+	return fileDescriptor_382f7805394b5c4e, []int{0, 0}
+}
+
+func (m *M_Submessage) XXX_Unmarshal(b []byte) error {
+	return xxx_messageInfo_M_Submessage.Unmarshal(m, b)
+}
+func (m *M_Submessage) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
+	return xxx_messageInfo_M_Submessage.Marshal(b, m, deterministic)
+}
+func (m *M_Submessage) XXX_Merge(src proto.Message) {
+	xxx_messageInfo_M_Submessage.Merge(m, src)
+}
+func (m *M_Submessage) XXX_Size() int {
+	return xxx_messageInfo_M_Submessage.Size(m)
+}
+func (m *M_Submessage) XXX_DiscardUnknown() {
+	xxx_messageInfo_M_Submessage.DiscardUnknown(m)
+}
+
+var xxx_messageInfo_M_Submessage proto.InternalMessageInfo
+
+type isM_Submessage_SubmessageOneofField interface {
+	isM_Submessage_SubmessageOneofField()
+}
+
+type M_Submessage_SubmessageOneofInt32 struct {
+	SubmessageOneofInt32 int32 `protobuf:"varint,1,opt,name=submessage_oneof_int32,json=submessageOneofInt32,proto3,oneof"`
+}
+
+type M_Submessage_SubmessageOneofInt64 struct {
+	SubmessageOneofInt64 int64 `protobuf:"varint,2,opt,name=submessage_oneof_int64,json=submessageOneofInt64,proto3,oneof"`
+}
+
+func (*M_Submessage_SubmessageOneofInt32) isM_Submessage_SubmessageOneofField() {}
+
+func (*M_Submessage_SubmessageOneofInt64) isM_Submessage_SubmessageOneofField() {}
+
+func (m *M_Submessage) GetSubmessageOneofField() isM_Submessage_SubmessageOneofField {
+	if m != nil {
+		return m.SubmessageOneofField
+	}
+	return nil
+}
+
+func (m *M_Submessage) GetSubmessageOneofInt32() int32 {
+	if x, ok := m.GetSubmessageOneofField().(*M_Submessage_SubmessageOneofInt32); ok {
+		return x.SubmessageOneofInt32
+	}
+	return 0
+}
+
+func (m *M_Submessage) GetSubmessageOneofInt64() int64 {
+	if x, ok := m.GetSubmessageOneofField().(*M_Submessage_SubmessageOneofInt64); ok {
+		return x.SubmessageOneofInt64
+	}
+	return 0
+}
+
+// XXX_OneofFuncs is for the internal use of the proto package.
+func (*M_Submessage) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) {
+	return _M_Submessage_OneofMarshaler, _M_Submessage_OneofUnmarshaler, _M_Submessage_OneofSizer, []interface{}{
+		(*M_Submessage_SubmessageOneofInt32)(nil),
+		(*M_Submessage_SubmessageOneofInt64)(nil),
+	}
+}
+
+func _M_Submessage_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
+	m := msg.(*M_Submessage)
+	// submessage_oneof_field
+	switch x := m.SubmessageOneofField.(type) {
+	case *M_Submessage_SubmessageOneofInt32:
+		b.EncodeVarint(1<<3 | proto.WireVarint)
+		b.EncodeVarint(uint64(x.SubmessageOneofInt32))
+	case *M_Submessage_SubmessageOneofInt64:
+		b.EncodeVarint(2<<3 | proto.WireVarint)
+		b.EncodeVarint(uint64(x.SubmessageOneofInt64))
+	case nil:
+	default:
+		return fmt.Errorf("M_Submessage.SubmessageOneofField has unexpected type %T", x)
+	}
+	return nil
+}
+
+func _M_Submessage_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
+	m := msg.(*M_Submessage)
+	switch tag {
+	case 1: // submessage_oneof_field.submessage_oneof_int32
+		if wire != proto.WireVarint {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeVarint()
+		m.SubmessageOneofField = &M_Submessage_SubmessageOneofInt32{int32(x)}
+		return true, err
+	case 2: // submessage_oneof_field.submessage_oneof_int64
+		if wire != proto.WireVarint {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeVarint()
+		m.SubmessageOneofField = &M_Submessage_SubmessageOneofInt64{int64(x)}
+		return true, err
+	default:
+		return false, nil
+	}
+}
+
+func _M_Submessage_OneofSizer(msg proto.Message) (n int) {
+	m := msg.(*M_Submessage)
+	// submessage_oneof_field
+	switch x := m.SubmessageOneofField.(type) {
+	case *M_Submessage_SubmessageOneofInt32:
+		n += 1 // tag and wire
+		n += proto.SizeVarint(uint64(x.SubmessageOneofInt32))
+	case *M_Submessage_SubmessageOneofInt64:
+		n += 1 // tag and wire
+		n += proto.SizeVarint(uint64(x.SubmessageOneofInt64))
+	case nil:
+	default:
+		panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
+	}
+	return n
+}
+
 func init() {
 func init() {
 	proto.RegisterType((*M)(nil), "goproto.test.import_public.sub.M")
 	proto.RegisterType((*M)(nil), "goproto.test.import_public.sub.M")
+	proto.RegisterType((*M_Submessage)(nil), "goproto.test.import_public.sub.M.Submessage")
 	proto.RegisterEnum("goproto.test.import_public.sub.E", E_name, E_value)
 	proto.RegisterEnum("goproto.test.import_public.sub.E", E_name, E_value)
+	proto.RegisterEnum("goproto.test.import_public.sub.M_Subenum", M_Subenum_name, M_Subenum_value)
+	proto.RegisterEnum("goproto.test.import_public.sub.M_Submessage_Submessage_Subenum", M_Submessage_Submessage_Subenum_name, M_Submessage_Submessage_Subenum_value)
 }
 }
 
 
 func init() { proto.RegisterFile("import_public/sub/a.proto", fileDescriptor_382f7805394b5c4e) }
 func init() { proto.RegisterFile("import_public/sub/a.proto", fileDescriptor_382f7805394b5c4e) }
 
 
 var fileDescriptor_382f7805394b5c4e = []byte{
 var fileDescriptor_382f7805394b5c4e = []byte{
-	// 172 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xcc, 0xcc, 0x2d, 0xc8,
-	0x2f, 0x2a, 0x89, 0x2f, 0x28, 0x4d, 0xca, 0xc9, 0x4c, 0xd6, 0x2f, 0x2e, 0x4d, 0xd2, 0x4f, 0xd4,
-	0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x4b, 0xcf, 0x07, 0x33, 0xf4, 0x4a, 0x52, 0x8b, 0x4b,
-	0xf4, 0x50, 0xd4, 0xe9, 0x15, 0x97, 0x26, 0x49, 0x61, 0xd1, 0x9a, 0x04, 0xd1, 0xaa, 0x64, 0xce,
-	0xc5, 0xe8, 0x2b, 0x64, 0xc4, 0xc5, 0x94, 0x6b, 0x24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2,
+	0x73, 0xf8, 0xb7, 0x9d, 0xa6, 0x93, 0x27, 0x51, 0x9a, 0x77, 0xcb, 0xc8, 0x54, 0xc9, 0x50, 0xa8,
+	0x45, 0x51, 0x89, 0xd0, 0xfd, 0x3c, 0xb3, 0xf3, 0x4d, 0x31, 0xed, 0x0b, 0x5e, 0xf5, 0x85, 0x0a,
+	0xd7, 0xab, 0x98, 0x15, 0xa6, 0x08, 0x7f, 0x6c, 0x92, 0x35, 0x9c, 0x72, 0xf0, 0x15, 0x00, 0x00,
+	0xff, 0xff, 0xe0, 0xdc, 0x9c, 0xbe, 0x20, 0x02, 0x00, 0x00,
 }
 }

+ 20 - 0
protoc-gen-go/testdata/import_public/sub/a.proto

@@ -40,6 +40,26 @@ import "import_public/sub/b.proto";
 message M {
 message M {
   // Field using a type in the same Go package, but a different source file.
   // Field using a type in the same Go package, but a different source file.
   M2 m2 = 1;
   M2 m2 = 1;
+
+  oneof oneof_field {
+    int32 oneof_int32 = 2;
+    int64 oneof_int64 = 3;
+  }
+
+  message Submessage {
+    enum Submessage_Subenum {
+      M_SUBMESSAGE_ZERO = 0;
+    }
+
+    oneof submessage_oneof_field {
+      int32 submessage_oneof_int32 = 1;
+      int64 submessage_oneof_int64 = 2;
+    }
+  }
+
+  enum Subenum {
+    M_ZERO = 0;
+  }
 }
 }
 
 
 enum E {
 enum E {