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/encoding/prototext/encode.go b/encoding/prototext/encode.go
index 4d73041..7d244a9 100644
--- a/encoding/prototext/encode.go
+++ b/encoding/prototext/encode.go
@@ -252,17 +252,16 @@
if !fd.IsExtension() {
return true
}
- xt := fd.(pref.ExtensionType)
// If extended type is a MessageSet, set field name to be the message type name.
- name := xt.Descriptor().FullName()
- if isMessageSetExtension(xt) {
- name = xt.Descriptor().Message().FullName()
+ name := fd.FullName()
+ if isMessageSetExtension(fd) {
+ name = fd.Message().FullName()
}
// Use string type to produce [name] format.
tname := text.ValueOf(string(name))
- entries, err = o.appendField(entries, tname, v, xt)
+ entries, err = o.appendField(entries, tname, v, fd)
if err != nil {
return false
}
@@ -281,19 +280,18 @@
}
// isMessageSetExtension reports whether extension extends a message set.
-func isMessageSetExtension(xt pref.ExtensionType) bool {
- xd := xt.Descriptor()
- if xd.Name() != "message_set_extension" {
+func isMessageSetExtension(fd pref.FieldDescriptor) bool {
+ if fd.Name() != "message_set_extension" {
return false
}
- md := xd.Message()
+ md := fd.Message()
if md == nil {
return false
}
- if xd.FullName().Parent() != md.FullName() {
+ if fd.FullName().Parent() != md.FullName() {
return false
}
- xmd, ok := xd.ContainingMessage().(interface{ IsMessageSet() bool })
+ xmd, ok := fd.ContainingMessage().(interface{ IsMessageSet() bool })
return ok && xmd.IsMessageSet()
}