all: remove {Enum,Message,Extension}Type.GoType methods

These methods are difficult or impossible to use correctly; types
created by the dynamicpb package, for example, all have the same GoType.

Fixes golang/protobuf#938

Change-Id: I33d4ef381579ff18569b11df501d0ba7f38a6b5c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/199060
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
This commit is contained in:
Damien Neil 2019-10-03 16:38:33 -07:00
parent 73618879f4
commit 3b65992c65
12 changed files with 35 additions and 91 deletions

View File

@ -18,5 +18,4 @@ type EnumInfo struct {
func (t *EnumInfo) New(n pref.EnumNumber) pref.Enum {
return reflect.ValueOf(n).Convert(t.GoReflectType).Interface().(pref.Enum)
}
func (t *EnumInfo) GoType() reflect.Type { return t.GoReflectType }
func (t *EnumInfo) Descriptor() pref.EnumDescriptor { return t.Desc }

View File

@ -5,7 +5,6 @@
package impl_test
import (
"reflect"
"testing"
pref "google.golang.org/protobuf/reflect/protoreflect"
@ -15,9 +14,6 @@ import (
func TestEnum(t *testing.T) {
et := testpb.ForeignEnum_FOREIGN_FOO.Type()
if got, want := et.GoType(), reflect.TypeOf(testpb.ForeignEnum_FOREIGN_FOO); got != want {
t.Errorf("testpb.ForeignEnum_FOREIGN_FOO.Type().GoType() = %v, want %v", got, want)
}
if got, want := et.New(pref.EnumNumber(testpb.ForeignEnum_FOREIGN_FOO)), pref.Enum(testpb.ForeignEnum_FOREIGN_FOO); got != want {
t.Errorf("testpb.ForeignEnum_FOREIGN_FOO.Type().New() = %[1]T(%[1]v), want %[2]T(%[2]v)", got, want)
}

View File

@ -46,16 +46,12 @@ type ExtensionInfo struct {
// Deprecated: Use the ExtendedType method instead.
ExtendedType piface.MessageV1
// ExtensionType is zero value of the extension type.
// ExtensionType is the zero value of the extension type.
//
// For historical reasons, reflect.TypeOf(ExtensionType) and Type.GoType
// may not be identical:
// * for scalars (except []byte), where ExtensionType uses *T,
// while Type.GoType uses T.
// * for repeated fields, where ExtensionType uses []T,
// while Type.GoType uses *[]T.
// For historical reasons, reflect.TypeOf(ExtensionType) and the
// type returned by InterfaceOf may not be identical.
//
// Deprecated: Use the GoType method instead.
// Deprecated: Use InterfaceOf(xt.Zero()) instead.
ExtensionType interface{}
// Field is the field number of the extension.
@ -110,10 +106,6 @@ func (xi *ExtensionInfo) IsValidValue(v pref.Value) bool {
func (xi *ExtensionInfo) IsValidInterface(v interface{}) bool {
return xi.lazyInit().IsValidGo(reflect.ValueOf(v))
}
func (xi *ExtensionInfo) GoType() reflect.Type {
xi.lazyInit()
return xi.goType
}
func (xi *ExtensionInfo) TypeDescriptor() pref.ExtensionTypeDescriptor {
if atomic.LoadUint32(&xi.init) < extensionInfoDescInit {
xi.lazyInitSlow()

View File

@ -76,9 +76,6 @@ func (t *legacyEnumType) New(n pref.EnumNumber) pref.Enum {
t.m.Store(n, e)
return e
}
func (t *legacyEnumType) GoType() reflect.Type {
return t.goType
}
func (t *legacyEnumType) Descriptor() pref.EnumDescriptor {
return t.desc
}

View File

@ -473,7 +473,6 @@ func TestLegacyExtensionConvert(t *testing.T) {
xt := xt
go func() { xt.New() }()
go func() { xt.Zero() }()
go func() { xt.GoType() }()
go func() { xt.TypeDescriptor() }()
}
@ -527,7 +526,6 @@ func TestLegacyExtensionConvert(t *testing.T) {
cmp.Transformer("", func(xt pref.ExtensionType) map[string]interface{} {
return map[string]interface{}{
"Descriptor": xt.TypeDescriptor(),
"GoType": xt.GoType(),
}
}),
cmp.Transformer("", func(v pref.Value) interface{} {

View File

@ -198,9 +198,6 @@ fieldLoop:
return si
}
func (mi *MessageInfo) GoType() reflect.Type {
return mi.GoReflectType
}
func (mi *MessageInfo) New() protoreflect.Message {
return mi.MessageOf(reflect.New(mi.GoReflectType.Elem()).Interface())
}

View File

@ -940,7 +940,6 @@ func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Desc }
func (e EnumProto2) Type() pref.EnumType { return e }
func (e EnumProto2) Enum() *EnumProto2 { return &e }
func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
func (t EnumProto2) GoType() reflect.Type { return reflect.TypeOf(t) }
func (t EnumProto2) New(n pref.EnumNumber) pref.Enum { return EnumProto2(n) }
type EnumProto3 int32
@ -954,7 +953,6 @@ func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Desc }
func (e EnumProto3) Type() pref.EnumType { return e }
func (e EnumProto3) Enum() *EnumProto3 { return &e }
func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
func (t EnumProto3) GoType() reflect.Type { return reflect.TypeOf(t) }
func (t EnumProto3) New(n pref.EnumNumber) pref.Enum { return EnumProto3(n) }
type EnumMessages struct {

View File

@ -5,8 +5,6 @@
package irregular
import (
"reflect"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/reflect/protodesc"
pref "google.golang.org/protobuf/reflect/protoreflect"
@ -27,7 +25,6 @@ func (m *message) Descriptor() pref.MessageDescriptor { return fileDesc.Messages
func (m *message) Type() pref.MessageType { return m }
func (m *message) New() pref.Message { return &message{} }
func (m *message) Zero() pref.Message { return (*message)(nil) }
func (m *message) GoType() reflect.Type { return reflect.TypeOf(&message{}) }
func (m *message) Interface() pref.ProtoMessage { return (*IrregularMessage)(m) }
var fieldDescS = fileDesc.Messages().Get(0).Fields().Get(0)

View File

@ -4,8 +4,6 @@
package protoreflect
import "reflect"
// Descriptor provides a set of accessors that are common to every descriptor.
// Each descriptor type wraps the equivalent google.protobuf.XXXDescriptorProto,
// but provides efficient lookup and immutability.
@ -235,11 +233,6 @@ type MessageType interface {
// Zero returns an immutable empty message.
Zero() Message
// GoType returns the Go type of the allocated message.
//
// Invariant: t.GoType() == reflect.TypeOf(t.New().Interface())
GoType() reflect.Type
// Descriptor returns the message descriptor.
//
// Invariant: t.Descriptor() == t.New().Descriptor()
@ -455,12 +448,6 @@ type ExtensionType interface {
// For composite types, this returns an empty, immutable message, list, or map.
Zero() Value
// GoType returns the Go type of the field value.
//
// Invariants:
// t.GoType() == reflect.TypeOf(t.InterfaceOf(t.New()))
GoType() reflect.Type
// TypeDescriptor returns the extension type descriptor.
TypeDescriptor() ExtensionTypeDescriptor
@ -517,11 +504,6 @@ type EnumType interface {
// New returns an instance of this enum type with its value set to n.
New(n EnumNumber) Enum
// GoType returns the Go type of the enum value.
//
// Invariants: t.GoType() == reflect.TypeOf(t.New(0))
GoType() reflect.Type
// Descriptor returns the enum descriptor.
//
// Invariant: t.Descriptor() == t.New(0).Descriptor()

View File

@ -18,7 +18,6 @@ package protoregistry
import (
"fmt"
"log"
"reflect"
"strings"
"sync"
@ -361,17 +360,8 @@ func rangeTopLevelDescriptors(fd protoreflect.FileDescriptor, f func(protoreflec
}
}
// Type is an interface satisfied by protoreflect.EnumType,
// protoreflect.MessageType, or protoreflect.ExtensionType.
type Type interface {
GoType() reflect.Type
}
var (
_ Type = protoreflect.EnumType(nil)
_ Type = protoreflect.MessageType(nil)
_ Type = protoreflect.ExtensionType(nil)
)
// A Type is a protoreflect.EnumType, protoreflect.MessageType, or protoreflect.ExtensionType.
type Type interface{}
// MessageTypeResolver is an interface for looking up messages.
//

View File

@ -539,28 +539,32 @@ func TestTypes(t *testing.T) {
}
})
fullName := func(t preg.Type) pref.FullName {
switch t := t.(type) {
case pref.EnumType:
return t.Descriptor().FullName()
case pref.MessageType:
return t.Descriptor().FullName()
case pref.ExtensionType:
return t.TypeDescriptor().FullName()
default:
panic("invalid type")
}
sortTypes := cmp.Options{
cmpopts.SortSlices(func(x, y pref.EnumType) bool {
return x.Descriptor().FullName() < y.Descriptor().FullName()
}),
cmpopts.SortSlices(func(x, y pref.MessageType) bool {
return x.Descriptor().FullName() < y.Descriptor().FullName()
}),
cmpopts.SortSlices(func(x, y pref.ExtensionType) bool {
return x.TypeDescriptor().FullName() < y.TypeDescriptor().FullName()
}),
}
compare := cmp.Options{
cmp.Comparer(func(x, y pref.EnumType) bool {
return x == y
}),
cmp.Comparer(func(x, y pref.ExtensionType) bool {
return x == y
}),
cmp.Comparer(func(x, y pref.MessageType) bool {
return x == y
}),
}
sortTypes := cmpopts.SortSlices(func(x, y preg.Type) bool {
return fullName(x) < fullName(y)
})
compare := cmp.Comparer(func(x, y preg.Type) bool {
return x == y
})
t.Run("RangeEnums", func(t *testing.T) {
want := []preg.Type{et1}
var got []preg.Type
want := []pref.EnumType{et1}
var got []pref.EnumType
var gotCnt int
wantCnt := registry.NumEnums()
registry.RangeEnums(func(et pref.EnumType) bool {
@ -578,8 +582,8 @@ func TestTypes(t *testing.T) {
})
t.Run("RangeMessages", func(t *testing.T) {
want := []preg.Type{mt1}
var got []preg.Type
want := []pref.MessageType{mt1}
var got []pref.MessageType
var gotCnt int
wantCnt := registry.NumMessages()
registry.RangeMessages(func(mt pref.MessageType) bool {
@ -597,8 +601,8 @@ func TestTypes(t *testing.T) {
})
t.Run("RangeExtensions", func(t *testing.T) {
want := []preg.Type{xt1, xt2}
var got []preg.Type
want := []pref.ExtensionType{xt1, xt2}
var got []pref.ExtensionType
var gotCnt int
wantCnt := registry.NumExtensions()
registry.RangeExtensions(func(xt pref.ExtensionType) bool {
@ -616,8 +620,8 @@ func TestTypes(t *testing.T) {
})
t.Run("RangeExtensionsByMessage", func(t *testing.T) {
want := []preg.Type{xt1, xt2}
var got []preg.Type
want := []pref.ExtensionType{xt1, xt2}
var got []pref.ExtensionType
var gotCnt int
wantCnt := registry.NumExtensionsByMessage("testprotos.Message1")
registry.RangeExtensionsByMessage("testprotos.Message1", func(xt pref.ExtensionType) bool {

View File

@ -7,7 +7,6 @@ package dynamicpb
import (
"math"
"reflect"
"google.golang.org/protobuf/internal/errors"
pref "google.golang.org/protobuf/reflect/protoreflect"
@ -285,7 +284,6 @@ func NewMessageType(desc pref.MessageDescriptor) pref.MessageType {
func (mt messageType) New() pref.Message { return NewMessage(mt.desc) }
func (mt messageType) Zero() pref.Message { return NewMessage(mt.desc) }
func (mt messageType) GoType() reflect.Type { return reflect.TypeOf((*Message)(nil)) }
func (mt messageType) Descriptor() pref.MessageDescriptor { return mt.desc }
type emptyList struct {
@ -559,10 +557,6 @@ func (xt extensionType) Zero() pref.Value {
}
}
func (xt extensionType) GoType() reflect.Type {
return reflect.TypeOf(xt.InterfaceOf(xt.New()))
}
func (xt extensionType) TypeDescriptor() pref.ExtensionTypeDescriptor {
return xt.desc
}