proto: add IsInitialized

Move all checks for required fields into a proto.IsInitialized function.

Initial testing makes me confident that we can provide a fast-path
implementation of IsInitialized which will perform more than
acceptably.  (In the degenerate-but-common case where a message
transitively contains no required fields, this check can be nearly
zero cost.)

Unifying checks into a single function provides consistent behavior
between the wire, text, and json codecs.

Performing the check after decoding eliminates the wire decoder bug
where a split message is incorrectly seen as missing required fields.

Performing the check after decoding also provides consistent and
arguably more correct behavior when the target message was partially
prepopulated.

Change-Id: I9478b7bebb263af00c0d9f66a1f26e31ff553522
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/170787
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/proto/isinit_test.go b/proto/isinit_test.go
new file mode 100644
index 0000000..951b95f
--- /dev/null
+++ b/proto/isinit_test.go
@@ -0,0 +1,60 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style.
+// license that can be found in the LICENSE file.
+
+package proto_test
+
+import (
+	"fmt"
+	"testing"
+
+	"github.com/golang/protobuf/v2/internal/scalar"
+	"github.com/golang/protobuf/v2/proto"
+
+	testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
+)
+
+func TestIsInitializedErrors(t *testing.T) {
+	for _, test := range []struct {
+		m    proto.Message
+		want string
+	}{
+		{
+			&testpb.TestRequired{},
+			`proto: required field required_field not set`,
+		},
+		{
+			&testpb.TestRequiredForeign{
+				OptionalMessage: &testpb.TestRequired{},
+			},
+			`proto: required field optional_message.required_field not set`,
+		},
+		{
+			&testpb.TestRequiredForeign{
+				RepeatedMessage: []*testpb.TestRequired{
+					{RequiredField: scalar.Int32(1)},
+					{},
+				},
+			},
+			`proto: required field repeated_message[1].required_field not set`,
+		},
+		{
+			&testpb.TestRequiredForeign{
+				MapMessage: map[int32]*testpb.TestRequired{
+					1: {},
+				},
+			},
+			`proto: required field map_message[1].required_field not set`,
+		},
+	} {
+		err := proto.IsInitialized(test.m)
+		got := "<nil>"
+		if err != nil {
+			got = fmt.Sprintf("%q", err)
+		}
+		want := fmt.Sprintf("%q", test.want)
+		if got != want {
+			t.Errorf("IsInitialized(m):\n got: %v\nwant: %v\nMessage:\n%v", got, want, marshalText(test.m))
+		}
+	}
+}