encoding: add Format helper function and method

The Format function and MarshalOptions.Format method are helper
functions for directly obtaining the formatted string for a message
without having to deal with errors or convert a []byte to string.
It is only intended for human consumption (e.g., debugging or logging).

We also add a MarshalOptions.Multiline option to specify that the output
should use some default indentation in a multiline output.

This assists in the v1 to v2 migration where:
	protoV1.CompactTextString(m) => prototext.MarshalOptions{}.Format(m)
	protoV1.MarshalTextString(m) => prototext.Format(m)

At Google, there are approximately 10x more usages of MarshalTextString than
CompactTextString, so it makes sense that the top-level Format function
does multiline expansion by default.

Fixes #850

Change-Id: I149c9e190a6d99b985d3884df675499a3313e9b3
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/213460
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/proto/encode_test.go b/proto/encode_test.go
index 339249c..c2fefc3 100644
--- a/proto/encode_test.go
+++ b/proto/encode_test.go
@@ -11,6 +11,7 @@
 	"testing"
 
 	"github.com/google/go-cmp/cmp"
+	"google.golang.org/protobuf/encoding/prototext"
 	"google.golang.org/protobuf/internal/encoding/wire"
 	"google.golang.org/protobuf/proto"
 	pref "google.golang.org/protobuf/reflect/protoreflect"
@@ -29,12 +30,12 @@
 				}
 				wire, err := opts.Marshal(want)
 				if err != nil {
-					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
+					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want))
 				}
 
 				size := proto.Size(want)
 				if size != len(wire) {
-					t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), marshalText(want))
+					t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), prototext.Format(want))
 				}
 
 				got := want.ProtoReflect().New().Interface()
@@ -42,11 +43,11 @@
 					AllowPartial: test.partial,
 				}
 				if err := uopts.Unmarshal(wire, got); err != nil {
-					t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
+					t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want))
 					return
 				}
 				if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() {
-					t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
+					t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want))
 				}
 			})
 		}
@@ -63,11 +64,11 @@
 				}
 				wire, err := opts.Marshal(want)
 				if err != nil {
-					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
+					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want))
 				}
 				wire2, err := opts.Marshal(want)
 				if err != nil {
-					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
+					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want))
 				}
 				if !bytes.Equal(wire, wire2) {
 					t.Fatalf("deterministic marshal returned varying results:\n%v", cmp.Diff(wire, wire2))
@@ -78,11 +79,11 @@
 					AllowPartial: test.partial,
 				}
 				if err := uopts.Unmarshal(wire, got); err != nil {
-					t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
+					t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want))
 					return
 				}
 				if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() {
-					t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
+					t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want))
 				}
 			})
 		}
@@ -98,7 +99,7 @@
 			t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
 				_, err := proto.Marshal(m)
 				if err == nil {
-					t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", marshalText(m))
+					t.Fatalf("Marshal succeeded (want error)\nMessage:\n%v", prototext.Format(m))
 				}
 			})
 		}
@@ -131,7 +132,7 @@
 				}
 				got, err := opts.Marshal(m)
 				if err == nil {
-					t.Fatalf("Marshal unexpectedly succeeded\noutput bytes: [%x]\nMessage:\n%v", got, marshalText(m))
+					t.Fatalf("Marshal unexpectedly succeeded\noutput bytes: [%x]\nMessage:\n%v", got, prototext.Format(m))
 				}
 			})
 		}