internal/impl: simplify getMessageInfo
Our specific protoreflect.Message implementations have a special
ProtoMessageInfo method to obtain the *MessageInfo for v1 compatibility.
Use that instead to implement getMessageInfo.
Change-Id: I6cab9aeaa93714be73bd812c3d9a3be0ec86dd52
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/187777
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/message.go b/internal/impl/message.go
index 5b4beb0..59f04ce 100644
--- a/internal/impl/message.go
+++ b/internal/impl/message.go
@@ -66,26 +66,19 @@
var prefMessageType = reflect.TypeOf((*pref.Message)(nil)).Elem()
-// getMessageInfo returns the MessageInfo (if any) for a type.
-//
-// We find the MessageInfo by calling the ProtoReflect method on the type's
-// zero value and looking at the returned type to see if it is a
-// messageReflectWrapper. Note that the MessageInfo may still be uninitialized
-// at this point.
-func getMessageInfo(mt reflect.Type) (mi *MessageInfo, ok bool) {
- method, ok := mt.MethodByName("ProtoReflect")
+// getMessageInfo returns the MessageInfo for any message type that
+// is generated by our implementation of protoc-gen-go (for v2 and on).
+// If it is unable to obtain a MessageInfo, it returns nil.
+func getMessageInfo(mt reflect.Type) *MessageInfo {
+ m, ok := reflect.Zero(mt).Interface().(pref.ProtoMessage)
if !ok {
- return nil, false
+ return nil
}
- if method.Type.NumIn() != 1 || method.Type.NumOut() != 1 || method.Type.Out(0) != prefMessageType {
- return nil, false
- }
- ret := reflect.Zero(mt).Method(method.Index).Call(nil)
- m, ok := ret[0].Elem().Interface().(*messageReflectWrapper)
+ mr, ok := m.ProtoReflect().(interface{ ProtoMessageInfo() *MessageInfo })
if !ok {
- return nil, ok
+ return nil
}
- return m.mi, true
+ return mr.ProtoMessageInfo()
}
func (mi *MessageInfo) init() {