internal/impl: print <nil> for typed nil messages
The Message.String method is intended for debugging,
so outputting <nil> for typed nil messages actually has use.
This matches the current behavior of Message.String
for messages generated by the v1 protoc-gen-go.
Change-Id: I6e31183961c83d7bce6338eb99aa7758cfda1ff4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/199397
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/api_export.go b/internal/impl/api_export.go
index 6dcf85a..d7b844d 100644
--- a/internal/impl/api_export.go
+++ b/internal/impl/api_export.go
@@ -134,6 +134,10 @@
// MessageStringOf returns the message value as a string,
// which is the message serialized in the protobuf text format.
func (Export) MessageStringOf(m pref.ProtoMessage) string {
+ v := reflect.ValueOf(m)
+ if m == nil || (v.Kind() == reflect.Ptr && v.IsNil()) {
+ return "<nil>"
+ }
b, _ := prototext.MarshalOptions{
AllowPartial: true,
EmitUnknown: true,