proto: allow nil with Clone

The v1 behavior of Clone returned nil as is.
Replicate this behavior in v2 for easier migration.

Change-Id: I79d93f15dd9913604027e31e5d9ff309e0c2da61
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/220437
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2020-02-20 12:28:37 -08:00 committed by Joe Tsai
parent ec552f691d
commit 49f8611134

View File

@ -39,6 +39,9 @@ func Clone(m Message) Message {
// Embedding protobuf messages breaks this since the parent type will have
// a forwarded ProtoReflect method, but the Interface method will return
// the underlying embedded message type.
if m == nil {
return nil
}
src := m.ProtoReflect()
if !src.IsValid() {
return src.Type().Zero().Interface()