reflect/protoreflect: add Enum.Type and Message.Type
CL/174938 removed these methods in favor of a method that returned
only the descriptors. This CL adds back in the Type methods alongside
the Descriptor methods.
In a vast majority of protobuf usages, only the descriptor information
is needed. However, there is a small percentage that legitimately needs
the Go type information. We should provide both, but document that the
descriptor-only information is preferred.
Change-Id: Ia0a098997fb1bd009994940ae8ea5257ccd87cae
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/184578
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/legacy_extension.go b/internal/impl/legacy_extension.go
index ca3475f..c8b13d8 100644
--- a/internal/impl/legacy_extension.go
+++ b/internal/impl/legacy_extension.go
@@ -65,7 +65,7 @@
// Determine the parent type if possible.
var parent piface.MessageV1
- messageName := xt.Descriptor().ContainingMessage().FullName()
+ messageName := xt.ContainingMessage().FullName()
if mt, _ := preg.GlobalTypes.FindMessageByName(messageName); mt != nil {
// Create a new parent message and unwrap it if possible.
mv := mt.New().Interface()
@@ -96,7 +96,7 @@
// Reconstruct the legacy enum full name, which is an odd mixture of the
// proto package name with the Go type name.
var enumName string
- if xt.Descriptor().Kind() == pref.EnumKind {
+ if xt.Kind() == pref.EnumKind {
// Derive Go type name.
t := extType
if t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice {
@@ -107,7 +107,7 @@
// Derive the proto package name.
// For legacy enums, obtain the proto package from the raw descriptor.
var protoPkg string
- if fd := xt.Descriptor().Enum().ParentFile(); fd != nil {
+ if fd := xt.Enum().ParentFile(); fd != nil {
protoPkg = string(fd.Package())
}
if ed, ok := reflect.Zero(t).Interface().(enumV1); ok && protoPkg == "" {
@@ -122,7 +122,7 @@
// Derive the proto file that the extension was declared within.
var filename string
- if fd := xt.Descriptor().ParentFile(); fd != nil {
+ if fd := xt.ParentFile(); fd != nil {
filename = fd.Path()
}
@@ -131,9 +131,9 @@
Type: xt,
ExtendedType: parent,
ExtensionType: reflect.Zero(extType).Interface(),
- Field: int32(xt.Descriptor().Number()),
- Name: string(xt.Descriptor().FullName()),
- Tag: ptag.Marshal(xt.Descriptor(), enumName),
+ Field: int32(xt.Number()),
+ Name: string(xt.FullName()),
+ Tag: ptag.Marshal(xt, enumName),
Filename: filename,
}
if d, ok := legacyExtensionDescCache.LoadOrStore(xt, d); ok {
@@ -287,4 +287,4 @@
func (x *legacyExtensionType) New() pref.Value { return x.new() }
func (x *legacyExtensionType) ValueOf(v interface{}) pref.Value { return x.valueOf(v) }
func (x *legacyExtensionType) InterfaceOf(v pref.Value) interface{} { return x.interfaceOf(v) }
-func (x *legacyExtensionType) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, x.Descriptor()) }
+func (x *legacyExtensionType) Format(s fmt.State, r rune) { descfmt.FormatDesc(s, r, x) }