internal/impl: preserve nil in ProtoMessageVXOf

ProtoMessageV1(nil) now returns nil.
ProtoMessageV2(nil) now returns nil.

Note that the following continue to panic:
	MessageOf(nil)
	MessageDescriptorOf(nil)
	MessageTypeOf(nil)
It may be reasonable for them to also return nil in the future.

Change-Id: Icc14857252d844eb6d4dbfe0c248cef22023e930
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/193758
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2019-09-06 00:30:59 -07:00
parent 27af11f144
commit 531a03af35

View File

@ -71,6 +71,8 @@ func (m legacyMessageWrapper) ProtoMessage() {}
// ProtoMessageV1Of converts either a v1 or v2 message to a v1 message.
func (Export) ProtoMessageV1Of(m message) piface.MessageV1 {
switch mv := m.(type) {
case nil:
return nil
case piface.MessageV1:
return mv
case unwrapper:
@ -84,6 +86,8 @@ func (Export) ProtoMessageV1Of(m message) piface.MessageV1 {
func (Export) protoMessageV2Of(m message) pref.ProtoMessage {
switch mv := m.(type) {
case nil:
return nil
case pref.ProtoMessage:
return mv
case legacyMessageWrapper:
@ -97,7 +101,7 @@ func (Export) protoMessageV2Of(m message) pref.ProtoMessage {
// ProtoMessageV2Of converts either a v1 or v2 message to a v2 message.
func (Export) ProtoMessageV2Of(m message) pref.ProtoMessage {
if mv := (Export{}).protoMessageV2Of(m); mv != nil {
if mv := (Export{}).protoMessageV2Of(m); mv != nil || m == nil {
return mv
}
return legacyWrapMessage(reflect.ValueOf(m))