protogen, encoding/jsonpb, encoding/textpb: rename packages

Rename encoding/*pb to follow the convention of prefixing package names
with 'proto':

	google.golang.org/protobuf/encoding/protojson
	google.golang.org/protobuf/encoding/prototext

Move protogen under a compiler/ directory, just in case we ever do add
more compiler-related packages.

	google.golang.org/protobuf/compiler/protogen

Change-Id: I31010cb5cabcea8274fffcac468477b58b56e8eb
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177178
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/encoding/prototext/decode.go b/encoding/prototext/decode.go
new file mode 100644
index 0000000..f9263bd
--- /dev/null
+++ b/encoding/prototext/decode.go
@@ -0,0 +1,512 @@
+// Copyright 2018 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 prototext
+
+import (
+	"fmt"
+	"strings"
+	"unicode/utf8"
+
+	"google.golang.org/protobuf/internal/encoding/text"
+	"google.golang.org/protobuf/internal/errors"
+	"google.golang.org/protobuf/internal/fieldnum"
+	"google.golang.org/protobuf/internal/pragma"
+	"google.golang.org/protobuf/internal/set"
+	"google.golang.org/protobuf/proto"
+	pref "google.golang.org/protobuf/reflect/protoreflect"
+	"google.golang.org/protobuf/reflect/protoregistry"
+)
+
+// Unmarshal reads the given []byte into the given proto.Message.
+func Unmarshal(m proto.Message, b []byte) error {
+	return UnmarshalOptions{}.Unmarshal(m, b)
+}
+
+// UnmarshalOptions is a configurable textproto format unmarshaler.
+type UnmarshalOptions struct {
+	pragma.NoUnkeyedLiterals
+
+	// AllowPartial accepts input for messages that will result in missing
+	// required fields. If AllowPartial is false (the default), Unmarshal will
+	// return error if there are any missing required fields.
+	AllowPartial bool
+
+	// Resolver is the registry used for type lookups when unmarshaling extensions
+	// and processing Any. If Resolver is not set, unmarshaling will default to
+	// using protoregistry.GlobalTypes.
+	Resolver *protoregistry.Types
+}
+
+// Unmarshal reads the given []byte and populates the given proto.Message using options in
+// UnmarshalOptions object.
+func (o UnmarshalOptions) Unmarshal(m proto.Message, b []byte) error {
+	var nerr errors.NonFatal
+
+	mr := m.ProtoReflect()
+	// Clear all fields before populating it.
+	// TODO: Determine if this needs to be consistent with protojson and binary unmarshal where
+	// behavior is to merge values into existing message. If decision is to not clear the fields
+	// ahead, code will need to be updated properly when merging nested messages.
+	resetMessage(mr)
+
+	// Parse into text.Value of message type.
+	val, err := text.Unmarshal(b)
+	if !nerr.Merge(err) {
+		return err
+	}
+
+	if o.Resolver == nil {
+		o.Resolver = protoregistry.GlobalTypes
+	}
+	err = o.unmarshalMessage(val.Message(), mr)
+	if !nerr.Merge(err) {
+		return err
+	}
+
+	if !o.AllowPartial {
+		nerr.Merge(proto.IsInitialized(m))
+	}
+
+	return nerr.E
+}
+
+// resetMessage clears all fields of given protoreflect.Message.
+// TODO: This should go into the proto package.
+func resetMessage(m pref.Message) {
+	knownFields := m.KnownFields()
+	knownFields.Range(func(num pref.FieldNumber, _ pref.Value) bool {
+		knownFields.Clear(num)
+		return true
+	})
+	unknownFields := m.UnknownFields()
+	unknownFields.Range(func(num pref.FieldNumber, _ pref.RawFields) bool {
+		unknownFields.Set(num, nil)
+		return true
+	})
+	extTypes := knownFields.ExtensionTypes()
+	extTypes.Range(func(xt pref.ExtensionType) bool {
+		extTypes.Remove(xt)
+		return true
+	})
+}
+
+// unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
+func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
+	var nerr errors.NonFatal
+
+	messageDesc := m.Descriptor()
+	knownFields := m.KnownFields()
+
+	// Handle expanded Any message.
+	if messageDesc.FullName() == "google.protobuf.Any" && isExpandedAny(tmsg) {
+		return o.unmarshalAny(tmsg[0], knownFields)
+	}
+
+	fieldDescs := messageDesc.Fields()
+	reservedNames := messageDesc.ReservedNames()
+	xtTypes := knownFields.ExtensionTypes()
+	var seenNums set.Ints
+	var seenOneofs set.Ints
+
+	for _, tfield := range tmsg {
+		tkey := tfield[0]
+		tval := tfield[1]
+
+		var fd pref.FieldDescriptor
+		var name pref.Name
+		switch tkey.Type() {
+		case text.Name:
+			name, _ = tkey.Name()
+			fd = fieldDescs.ByName(name)
+			if fd == nil {
+				// Check if this is a group field.
+				fd = fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
+			}
+		case text.String:
+			// Handle extensions only. This code path is not for Any.
+			if messageDesc.FullName() == "google.protobuf.Any" {
+				break
+			}
+			// Extensions have to be registered first in the message's
+			// ExtensionTypes before setting a value to it.
+			xtName := pref.FullName(tkey.String())
+			// Check first if it is already registered. This is the case for
+			// repeated fields.
+			xt := xtTypes.ByName(xtName)
+			if xt == nil {
+				var err error
+				xt, err = o.findExtension(xtName)
+				if err != nil && err != protoregistry.NotFound {
+					return errors.New("unable to resolve [%v]: %v", xtName, err)
+				}
+				if xt != nil {
+					xtTypes.Register(xt)
+				}
+			}
+			if xt != nil {
+				fd = xt.Descriptor()
+			}
+		}
+
+		if fd == nil {
+			// Ignore reserved names.
+			if reservedNames.Has(name) {
+				continue
+			}
+			// TODO: Can provide option to ignore unknown message fields.
+			return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey)
+		}
+
+		switch {
+		case fd.IsList():
+			// If input is not a list, turn it into a list.
+			var items []text.Value
+			if tval.Type() != text.List {
+				items = []text.Value{tval}
+			} else {
+				items = tval.List()
+			}
+
+			list := knownFields.Get(fd.Number()).List()
+			if err := o.unmarshalList(items, fd, list); !nerr.Merge(err) {
+				return err
+			}
+		case fd.IsMap():
+			// If input is not a list, turn it into a list.
+			var items []text.Value
+			if tval.Type() != text.List {
+				items = []text.Value{tval}
+			} else {
+				items = tval.List()
+			}
+
+			mmap := knownFields.Get(fd.Number()).Map()
+			if err := o.unmarshalMap(items, fd, mmap); !nerr.Merge(err) {
+				return err
+			}
+		default:
+			// If field is a oneof, check if it has already been set.
+			if od := fd.ContainingOneof(); od != nil {
+				idx := uint64(od.Index())
+				if seenOneofs.Has(idx) {
+					return errors.New("oneof %v is already set", od.FullName())
+				}
+				seenOneofs.Set(idx)
+			}
+
+			// Required or optional fields.
+			num := uint64(fd.Number())
+			if seenNums.Has(num) {
+				return errors.New("non-repeated field %v is repeated", fd.FullName())
+			}
+			if err := o.unmarshalSingular(tval, fd, knownFields); !nerr.Merge(err) {
+				return err
+			}
+			seenNums.Set(num)
+		}
+	}
+
+	return nerr.E
+}
+
+// findExtension returns protoreflect.ExtensionType from the Resolver if found.
+func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
+	xt, err := o.Resolver.FindExtensionByName(xtName)
+	if err == nil {
+		return xt, nil
+	}
+
+	// Check if this is a MessageSet extension field.
+	xt, err = o.Resolver.FindExtensionByName(xtName + ".message_set_extension")
+	if err == nil && isMessageSetExtension(xt) {
+		return xt, nil
+	}
+	return nil, protoregistry.NotFound
+}
+
+// unmarshalSingular unmarshals given text.Value into the non-repeated field.
+func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, knownFields pref.KnownFields) error {
+	num := fd.Number()
+
+	var nerr errors.NonFatal
+	var val pref.Value
+	switch fd.Kind() {
+	case pref.MessageKind, pref.GroupKind:
+		if input.Type() != text.Message {
+			return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
+		}
+		m := knownFields.NewMessage(num)
+		if err := o.unmarshalMessage(input.Message(), m); !nerr.Merge(err) {
+			return err
+		}
+		val = pref.ValueOf(m)
+	default:
+		var err error
+		val, err = unmarshalScalar(input, fd)
+		if !nerr.Merge(err) {
+			return err
+		}
+	}
+	knownFields.Set(num, val)
+
+	return nerr.E
+}
+
+// unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
+// the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
+func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
+	const b32 = false
+	const b64 = true
+
+	switch kind := fd.Kind(); kind {
+	case pref.BoolKind:
+		if b, ok := input.Bool(); ok {
+			return pref.ValueOf(bool(b)), nil
+		}
+	case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
+		if n, ok := input.Int(b32); ok {
+			return pref.ValueOf(int32(n)), nil
+		}
+	case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
+		if n, ok := input.Int(b64); ok {
+			return pref.ValueOf(int64(n)), nil
+		}
+	case pref.Uint32Kind, pref.Fixed32Kind:
+		if n, ok := input.Uint(b32); ok {
+			return pref.ValueOf(uint32(n)), nil
+		}
+	case pref.Uint64Kind, pref.Fixed64Kind:
+		if n, ok := input.Uint(b64); ok {
+			return pref.ValueOf(uint64(n)), nil
+		}
+	case pref.FloatKind:
+		if n, ok := input.Float(b32); ok {
+			return pref.ValueOf(float32(n)), nil
+		}
+	case pref.DoubleKind:
+		if n, ok := input.Float(b64); ok {
+			return pref.ValueOf(float64(n)), nil
+		}
+	case pref.StringKind:
+		if input.Type() == text.String {
+			s := input.String()
+			if utf8.ValidString(s) {
+				return pref.ValueOf(s), nil
+			}
+			var nerr errors.NonFatal
+			nerr.AppendInvalidUTF8(string(fd.FullName()))
+			return pref.ValueOf(s), nerr.E
+		}
+	case pref.BytesKind:
+		if input.Type() == text.String {
+			return pref.ValueOf([]byte(input.String())), nil
+		}
+	case pref.EnumKind:
+		// If input is int32, use directly.
+		if n, ok := input.Int(b32); ok {
+			return pref.ValueOf(pref.EnumNumber(n)), nil
+		}
+		if name, ok := input.Name(); ok {
+			// Lookup EnumNumber based on name.
+			if enumVal := fd.Enum().Values().ByName(name); enumVal != nil {
+				return pref.ValueOf(enumVal.Number()), nil
+			}
+		}
+	default:
+		panic(fmt.Sprintf("invalid scalar kind %v", kind))
+	}
+
+	return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
+}
+
+// unmarshalList unmarshals given []text.Value into given protoreflect.List.
+func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
+	var nerr errors.NonFatal
+
+	switch fd.Kind() {
+	case pref.MessageKind, pref.GroupKind:
+		for _, input := range inputList {
+			if input.Type() != text.Message {
+				return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
+			}
+			m := list.NewMessage()
+			if err := o.unmarshalMessage(input.Message(), m); !nerr.Merge(err) {
+				return err
+			}
+			list.Append(pref.ValueOf(m))
+		}
+	default:
+		for _, input := range inputList {
+			val, err := unmarshalScalar(input, fd)
+			if !nerr.Merge(err) {
+				return err
+			}
+			list.Append(val)
+		}
+	}
+
+	return nerr.E
+}
+
+// unmarshalMap unmarshals given []text.Value into given protoreflect.Map.
+func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error {
+	var nerr errors.NonFatal
+
+	// Determine ahead whether map entry is a scalar type or a message type in order to call the
+	// appropriate unmarshalMapValue func inside the for loop below.
+	unmarshalMapValue := unmarshalMapScalarValue
+	switch fd.MapValue().Kind() {
+	case pref.MessageKind, pref.GroupKind:
+		unmarshalMapValue = o.unmarshalMapMessageValue
+	}
+
+	for _, entry := range input {
+		if entry.Type() != text.Message {
+			return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
+		}
+		tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
+		if !nerr.Merge(err) {
+			return err
+		}
+		pkey, err := unmarshalMapKey(tkey, fd.MapKey())
+		if !nerr.Merge(err) {
+			return err
+		}
+		err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap)
+		if !nerr.Merge(err) {
+			return err
+		}
+	}
+
+	return nerr.E
+}
+
+// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
+// field values. If there are duplicate field names, the value for the last field is returned. If
+// the field name does not exist, it will return the zero value of text.Value. It will return an
+// error if there are unknown field names.
+func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
+	for _, field := range mapEntry {
+		keyStr, ok := field[0].Name()
+		if ok {
+			switch keyStr {
+			case "key":
+				if key.Type() != 0 {
+					return key, value, errors.New("%v contains duplicate key field", name)
+				}
+				key = field[1]
+			case "value":
+				if value.Type() != 0 {
+					return key, value, errors.New("%v contains duplicate value field", name)
+				}
+				value = field[1]
+			default:
+				ok = false
+			}
+		}
+		if !ok {
+			// TODO: Do not return error if ignore unknown option is added and enabled.
+			return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
+		}
+	}
+	return key, value, nil
+}
+
+// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
+// integral or string type.
+func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
+	// If input is not set, use the zero value.
+	if input.Type() == 0 {
+		return fd.Default().MapKey(), nil
+	}
+
+	var nerr errors.NonFatal
+	val, err := unmarshalScalar(input, fd)
+	if !nerr.Merge(err) {
+		return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
+	}
+	return val.MapKey(), nerr.E
+}
+
+// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
+// the given MapKey.
+func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
+	var nerr errors.NonFatal
+	var value [][2]text.Value
+	if input.Type() != 0 {
+		value = input.Message()
+	}
+	m := mmap.NewMessage()
+	if err := o.unmarshalMessage(value, m); !nerr.Merge(err) {
+		return err
+	}
+	mmap.Set(pkey, pref.ValueOf(m))
+	return nerr.E
+}
+
+// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
+// for the given MapKey.
+func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
+	var nerr errors.NonFatal
+	var val pref.Value
+	if input.Type() == 0 {
+		val = fd.Default()
+	} else {
+		var err error
+		val, err = unmarshalScalar(input, fd)
+		if !nerr.Merge(err) {
+			return err
+		}
+	}
+	mmap.Set(pkey, val)
+	return nerr.E
+}
+
+// isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
+// field with key type of text.String type and value type of text.Message.
+func isExpandedAny(tmsg [][2]text.Value) bool {
+	if len(tmsg) != 1 {
+		return false
+	}
+
+	field := tmsg[0]
+	return field[0].Type() == text.String && field[1].Type() == text.Message
+}
+
+// unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
+// tfield has key type of text.String and value type of text.Message.
+func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, knownFields pref.KnownFields) error {
+	var nerr errors.NonFatal
+
+	typeURL := tfield[0].String()
+	value := tfield[1].Message()
+
+	mt, err := o.Resolver.FindMessageByURL(typeURL)
+	if !nerr.Merge(err) {
+		return errors.New("unable to resolve message [%v]: %v", typeURL, err)
+	}
+	// Create new message for the embedded message type and unmarshal the
+	// value into it.
+	m := mt.New()
+	if err := o.unmarshalMessage(value, m); !nerr.Merge(err) {
+		return err
+	}
+	// Serialize the embedded message and assign the resulting bytes to the value field.
+	// TODO: If binary marshaling returns required not set error, need to
+	// return another required not set error that contains both the path to this
+	// field and the path inside the embedded message.
+	b, err := proto.MarshalOptions{
+		AllowPartial:  o.AllowPartial,
+		Deterministic: true,
+	}.Marshal(m.Interface())
+	if !nerr.Merge(err) {
+		return err
+	}
+
+	knownFields.Set(fieldnum.Any_TypeUrl, pref.ValueOf(typeURL))
+	knownFields.Set(fieldnum.Any_Value, pref.ValueOf(b))
+
+	return nerr.E
+}
diff --git a/encoding/prototext/decode_test.go b/encoding/prototext/decode_test.go
new file mode 100644
index 0000000..4837117
--- /dev/null
+++ b/encoding/prototext/decode_test.go
@@ -0,0 +1,1538 @@
+// Copyright 2018 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 prototext_test
+
+import (
+	"math"
+	"testing"
+
+	protoV1 "github.com/golang/protobuf/proto"
+	"google.golang.org/protobuf/encoding/prototext"
+	"google.golang.org/protobuf/internal/errors"
+	pimpl "google.golang.org/protobuf/internal/impl"
+	"google.golang.org/protobuf/internal/scalar"
+	"google.golang.org/protobuf/proto"
+	preg "google.golang.org/protobuf/reflect/protoregistry"
+
+	"google.golang.org/protobuf/encoding/testprotos/pb2"
+	"google.golang.org/protobuf/encoding/testprotos/pb3"
+	knownpb "google.golang.org/protobuf/types/known"
+)
+
+func TestUnmarshal(t *testing.T) {
+	tests := []struct {
+		desc         string
+		umo          prototext.UnmarshalOptions
+		inputMessage proto.Message
+		inputText    string
+		wantMessage  proto.Message
+		wantErr      bool // TODO: Verify error message content.
+	}{{
+		desc:         "proto2 empty message",
+		inputMessage: &pb2.Scalars{},
+		wantMessage:  &pb2.Scalars{},
+	}, {
+		desc:         "proto2 optional scalars set to zero values",
+		inputMessage: &pb2.Scalars{},
+		inputText: `opt_bool: false
+opt_int32: 0
+opt_int64: 0
+opt_uint32: 0
+opt_uint64: 0
+opt_sint32: 0
+opt_sint64: 0
+opt_fixed32: 0
+opt_fixed64: 0
+opt_sfixed32: 0
+opt_sfixed64: 0
+opt_float: 0
+opt_double: 0
+opt_bytes: ""
+opt_string: ""
+`,
+		wantMessage: &pb2.Scalars{
+			OptBool:     scalar.Bool(false),
+			OptInt32:    scalar.Int32(0),
+			OptInt64:    scalar.Int64(0),
+			OptUint32:   scalar.Uint32(0),
+			OptUint64:   scalar.Uint64(0),
+			OptSint32:   scalar.Int32(0),
+			OptSint64:   scalar.Int64(0),
+			OptFixed32:  scalar.Uint32(0),
+			OptFixed64:  scalar.Uint64(0),
+			OptSfixed32: scalar.Int32(0),
+			OptSfixed64: scalar.Int64(0),
+			OptFloat:    scalar.Float32(0),
+			OptDouble:   scalar.Float64(0),
+			OptBytes:    []byte{},
+			OptString:   scalar.String(""),
+		},
+	}, {
+		desc:         "proto3 scalars set to zero values",
+		inputMessage: &pb3.Scalars{},
+		inputText: `s_bool: false
+s_int32: 0
+s_int64: 0
+s_uint32: 0
+s_uint64: 0
+s_sint32: 0
+s_sint64: 0
+s_fixed32: 0
+s_fixed64: 0
+s_sfixed32: 0
+s_sfixed64: 0
+s_float: 0
+s_double: 0
+s_bytes: ""
+s_string: ""
+`,
+		wantMessage: &pb3.Scalars{},
+	}, {
+		desc:         "proto2 optional scalars",
+		inputMessage: &pb2.Scalars{},
+		inputText: `opt_bool: true
+opt_int32: 255
+opt_int64: 3735928559
+opt_uint32: 0xff
+opt_uint64: 0xdeadbeef
+opt_sint32: -1001
+opt_sint64: -0xffff
+opt_fixed64: 64
+opt_sfixed32: -32
+opt_float: 1.234
+opt_double: 1.23e+100
+opt_bytes: "\xe8\xb0\xb7\xe6\xad\x8c"
+opt_string: "谷歌"
+`,
+		wantMessage: &pb2.Scalars{
+			OptBool:     scalar.Bool(true),
+			OptInt32:    scalar.Int32(0xff),
+			OptInt64:    scalar.Int64(0xdeadbeef),
+			OptUint32:   scalar.Uint32(0xff),
+			OptUint64:   scalar.Uint64(0xdeadbeef),
+			OptSint32:   scalar.Int32(-1001),
+			OptSint64:   scalar.Int64(-0xffff),
+			OptFixed64:  scalar.Uint64(64),
+			OptSfixed32: scalar.Int32(-32),
+			OptFloat:    scalar.Float32(1.234),
+			OptDouble:   scalar.Float64(1.23e100),
+			OptBytes:    []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
+			OptString:   scalar.String("谷歌"),
+		},
+	}, {
+		desc:         "proto3 scalars",
+		inputMessage: &pb3.Scalars{},
+		inputText: `s_bool: true
+s_int32: 255
+s_int64: 3735928559
+s_uint32: 0xff
+s_uint64: 0xdeadbeef
+s_sint32: -1001
+s_sint64: -0xffff
+s_fixed64: 64
+s_sfixed32: -32
+s_float: 1.234
+s_double: 1.23e+100
+s_bytes: "\xe8\xb0\xb7\xe6\xad\x8c"
+s_string: "谷歌"
+`,
+		wantMessage: &pb3.Scalars{
+			SBool:     true,
+			SInt32:    0xff,
+			SInt64:    0xdeadbeef,
+			SUint32:   0xff,
+			SUint64:   0xdeadbeef,
+			SSint32:   -1001,
+			SSint64:   -0xffff,
+			SFixed64:  64,
+			SSfixed32: -32,
+			SFloat:    1.234,
+			SDouble:   1.23e100,
+			SBytes:    []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
+			SString:   "谷歌",
+		},
+	}, {
+		desc:         "string with invalid UTF-8",
+		inputMessage: &pb3.Scalars{},
+		inputText:    `s_string: "abc\xff"`,
+		wantMessage: &pb3.Scalars{
+			SString: "abc\xff",
+		},
+		wantErr: true,
+	}, {
+		desc:         "proto2 message contains unknown field",
+		inputMessage: &pb2.Scalars{},
+		inputText:    "unknown_field: 123",
+		wantErr:      true,
+	}, {
+		desc:         "proto3 message contains unknown field",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "unknown_field: 456",
+		wantErr:      true,
+	}, {
+		desc:         "proto2 numeric key field",
+		inputMessage: &pb2.Scalars{},
+		inputText:    "1: true",
+		wantErr:      true,
+	}, {
+		desc:         "proto3 numeric key field",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "1: true",
+		wantErr:      true,
+	}, {
+		desc:         "invalid bool value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_bool: 123",
+		wantErr:      true,
+	}, {
+		desc:         "invalid int32 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_int32: not_a_num",
+		wantErr:      true,
+	}, {
+		desc:         "invalid int64 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_int64: 'not a num either'",
+		wantErr:      true,
+	}, {
+		desc:         "invalid uint32 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_fixed32: -42",
+		wantErr:      true,
+	}, {
+		desc:         "invalid uint64 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_uint64: -47",
+		wantErr:      true,
+	}, {
+		desc:         "invalid sint32 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_sint32: '42'",
+		wantErr:      true,
+	}, {
+		desc:         "invalid sint64 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_sint64: '-47'",
+		wantErr:      true,
+	}, {
+		desc:         "invalid fixed32 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_fixed32: -42",
+		wantErr:      true,
+	}, {
+		desc:         "invalid fixed64 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_fixed64: -42",
+		wantErr:      true,
+	}, {
+		desc:         "invalid sfixed32 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_sfixed32: 'not valid'",
+		wantErr:      true,
+	}, {
+		desc:         "invalid sfixed64 value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_sfixed64: bad",
+		wantErr:      true,
+	}, {
+		desc:         "float positive infinity",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_float: inf",
+		wantMessage: &pb3.Scalars{
+			SFloat: float32(math.Inf(1)),
+		},
+	}, {
+		desc:         "float negative infinity",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_float: -inf",
+		wantMessage: &pb3.Scalars{
+			SFloat: float32(math.Inf(-1)),
+		},
+	}, {
+		desc:         "double positive infinity",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_double: inf",
+		wantMessage: &pb3.Scalars{
+			SDouble: math.Inf(1),
+		},
+	}, {
+		desc:         "double negative infinity",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_double: -inf",
+		wantMessage: &pb3.Scalars{
+			SDouble: math.Inf(-1),
+		},
+	}, {
+		desc:         "invalid string value",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_string: invalid_string",
+		wantErr:      true,
+	}, {
+		desc:         "proto2 bytes set to empty string",
+		inputMessage: &pb2.Scalars{},
+		inputText:    "opt_bytes: ''",
+		wantMessage: &pb2.Scalars{
+			OptBytes: []byte(""),
+		},
+	}, {
+		desc:         "proto3 bytes set to empty string",
+		inputMessage: &pb3.Scalars{},
+		inputText:    "s_bytes: ''",
+		wantMessage:  &pb3.Scalars{},
+	}, {
+		desc:         "proto2 duplicate singular field",
+		inputMessage: &pb2.Scalars{},
+		inputText: `
+opt_bool: true
+opt_bool: false
+`,
+		wantErr: true,
+	}, {
+		desc:         "proto2 more duplicate singular field",
+		inputMessage: &pb2.Scalars{},
+		inputText: `
+opt_bool: true
+opt_string: "hello"
+opt_bool: false
+`,
+		wantErr: true,
+	}, {
+		desc:         "proto2 invalid singular field",
+		inputMessage: &pb2.Scalars{},
+		inputText: `
+opt_bool: [true, false]
+`,
+		wantErr: true,
+	}, {
+		desc:         "proto3 duplicate singular field",
+		inputMessage: &pb3.Scalars{},
+		inputText: `
+s_bool: false
+s_bool: true
+`,
+		wantErr: true,
+	}, {
+		desc:         "proto3 more duplicate singular field",
+		inputMessage: &pb3.Scalars{},
+		inputText: `
+s_bool: false
+s_string: ""
+s_bool: true
+`,
+		wantErr: true,
+	}, {
+		desc:         "proto2 enum",
+		inputMessage: &pb2.Enums{},
+		inputText: `
+opt_enum: ONE
+opt_nested_enum: UNO
+`,
+		wantMessage: &pb2.Enums{
+			OptEnum:       pb2.Enum_ONE.Enum(),
+			OptNestedEnum: pb2.Enums_UNO.Enum(),
+		},
+	}, {
+		desc:         "proto2 enum set to numeric values",
+		inputMessage: &pb2.Enums{},
+		inputText: `
+opt_enum: 2
+opt_nested_enum: 2
+`,
+		wantMessage: &pb2.Enums{
+			OptEnum:       pb2.Enum_TWO.Enum(),
+			OptNestedEnum: pb2.Enums_DOS.Enum(),
+		},
+	}, {
+		desc:         "proto2 enum set to unnamed numeric values",
+		inputMessage: &pb2.Enums{},
+		inputText: `
+opt_enum: 101
+opt_nested_enum: -101
+`,
+		wantMessage: &pb2.Enums{
+			OptEnum:       pb2Enum(101),
+			OptNestedEnum: pb2Enums_NestedEnum(-101),
+		},
+	}, {
+		desc:         "proto2 enum set to invalid named",
+		inputMessage: &pb2.Enums{},
+		inputText: `
+opt_enum: UNNAMED
+opt_nested_enum: UNNAMED_TOO
+`,
+		wantErr: true,
+	}, {
+		desc:         "proto3 enum name value",
+		inputMessage: &pb3.Enums{},
+		inputText: `
+s_enum: ONE
+s_nested_enum: DIEZ
+`,
+		wantMessage: &pb3.Enums{
+			SEnum:       pb3.Enum_ONE,
+			SNestedEnum: pb3.Enums_DIEZ,
+		},
+	}, {
+		desc:         "proto3 enum numeric value",
+		inputMessage: &pb3.Enums{},
+		inputText: `
+s_enum: 2
+s_nested_enum: 2
+`,
+		wantMessage: &pb3.Enums{
+			SEnum:       pb3.Enum_TWO,
+			SNestedEnum: pb3.Enums_DOS,
+		},
+	}, {
+		desc:         "proto3 enum unnamed numeric value",
+		inputMessage: &pb3.Enums{},
+		inputText: `
+s_enum: 0x7fffffff
+s_nested_enum: -0x80000000
+`,
+		wantMessage: &pb3.Enums{
+			SEnum:       0x7fffffff,
+			SNestedEnum: -0x80000000,
+		},
+	}, {
+		desc:         "proto2 nested empty messages",
+		inputMessage: &pb2.Nests{},
+		inputText: `
+opt_nested: {}
+OptGroup: {}
+`,
+		wantMessage: &pb2.Nests{
+			OptNested: &pb2.Nested{},
+			Optgroup:  &pb2.Nests_OptGroup{},
+		},
+	}, {
+		desc:         "proto2 nested messages",
+		inputMessage: &pb2.Nests{},
+		inputText: `
+opt_nested: {
+  opt_string: "nested message"
+  opt_nested: {
+    opt_string: "another nested message"
+  }
+}
+`,
+		wantMessage: &pb2.Nests{
+			OptNested: &pb2.Nested{
+				OptString: scalar.String("nested message"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("another nested message"),
+				},
+			},
+		},
+	}, {
+		desc:         "proto3 nested empty message",
+		inputMessage: &pb3.Nests{},
+		inputText:    "s_nested: {}",
+		wantMessage: &pb3.Nests{
+			SNested: &pb3.Nested{},
+		},
+	}, {
+		desc:         "proto3 nested message",
+		inputMessage: &pb3.Nests{},
+		inputText: `
+s_nested: {
+  s_string: "nested message"
+  s_nested: {
+    s_string: "another nested message"
+  }
+}
+`,
+		wantMessage: &pb3.Nests{
+			SNested: &pb3.Nested{
+				SString: "nested message",
+				SNested: &pb3.Nested{
+					SString: "another nested message",
+				},
+			},
+		},
+	}, {
+		desc:         "proto3 nested message contains invalid UTF-8",
+		inputMessage: &pb3.Nests{},
+		inputText: `s_nested: {
+  s_string: "abc\xff"
+}
+`,
+		wantMessage: &pb3.Nests{
+			SNested: &pb3.Nested{
+				SString: "abc\xff",
+			},
+		},
+		wantErr: true,
+	}, {
+		desc:         "oneof set to empty string",
+		inputMessage: &pb3.Oneofs{},
+		inputText:    "oneof_string: ''",
+		wantMessage: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofString{},
+		},
+	}, {
+		desc:         "oneof set to string",
+		inputMessage: &pb3.Oneofs{},
+		inputText:    "oneof_string: 'hello'",
+		wantMessage: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofString{
+				OneofString: "hello",
+			},
+		},
+	}, {
+		desc:         "oneof set to enum",
+		inputMessage: &pb3.Oneofs{},
+		inputText:    "oneof_enum: TEN",
+		wantMessage: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofEnum{
+				OneofEnum: pb3.Enum_TEN,
+			},
+		},
+	}, {
+		desc:         "oneof set to empty message",
+		inputMessage: &pb3.Oneofs{},
+		inputText:    "oneof_nested: {}",
+		wantMessage: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofNested{
+				OneofNested: &pb3.Nested{},
+			},
+		},
+	}, {
+		desc:         "oneof set to message",
+		inputMessage: &pb3.Oneofs{},
+		inputText: `
+oneof_nested: {
+  s_string: "nested message"
+}
+`,
+		wantMessage: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofNested{
+				OneofNested: &pb3.Nested{
+					SString: "nested message",
+				},
+			},
+		},
+	}, {
+		desc:         "oneof set to more than one field",
+		inputMessage: &pb3.Oneofs{},
+		inputText: `
+oneof_enum: ZERO
+oneof_string: "hello"
+`,
+		wantErr: true,
+	}, {
+		desc:         "repeated scalar using same field name",
+		inputMessage: &pb2.Repeats{},
+		inputText: `
+rpt_string: "a"
+rpt_string: "b"
+rpt_int32: 0xff
+rpt_float: 1.23
+rpt_bytes: "bytes"
+`,
+		wantMessage: &pb2.Repeats{
+			RptString: []string{"a", "b"},
+			RptInt32:  []int32{0xff},
+			RptFloat:  []float32{1.23},
+			RptBytes:  [][]byte{[]byte("bytes")},
+		},
+	}, {
+		desc:         "repeated using mix of [] and repeated field name",
+		inputMessage: &pb2.Repeats{},
+		inputText: `
+rpt_string: "a"
+rpt_bool: true
+rpt_string: ["x", "y"]
+rpt_bool: [ false, true ]
+rpt_string: "b"
+`,
+		wantMessage: &pb2.Repeats{
+			RptString: []string{"a", "x", "y", "b"},
+			RptBool:   []bool{true, false, true},
+		},
+	}, {
+		desc:         "repeated contains invalid UTF-8",
+		inputMessage: &pb2.Repeats{},
+		inputText:    `rpt_string: "abc\xff"`,
+		wantMessage: &pb2.Repeats{
+			RptString: []string{"abc\xff"},
+		},
+		wantErr: true,
+	}, {
+		desc:         "repeated enums",
+		inputMessage: &pb2.Enums{},
+		inputText: `
+rpt_enum: TEN
+rpt_enum: 1
+rpt_nested_enum: [DOS, 2]
+rpt_enum: 42
+rpt_nested_enum: -47
+`,
+		wantMessage: &pb2.Enums{
+			RptEnum:       []pb2.Enum{pb2.Enum_TEN, pb2.Enum_ONE, 42},
+			RptNestedEnum: []pb2.Enums_NestedEnum{pb2.Enums_DOS, pb2.Enums_DOS, -47},
+		},
+	}, {
+		desc:         "repeated nested messages",
+		inputMessage: &pb2.Nests{},
+		inputText: `
+rpt_nested: {
+  opt_string: "repeat nested one"
+}
+rpt_nested: {
+  opt_string: "repeat nested two"
+  opt_nested: {
+    opt_string: "inside repeat nested two"
+  }
+}
+rpt_nested: {}
+`,
+		wantMessage: &pb2.Nests{
+			RptNested: []*pb2.Nested{
+				{
+					OptString: scalar.String("repeat nested one"),
+				},
+				{
+					OptString: scalar.String("repeat nested two"),
+					OptNested: &pb2.Nested{
+						OptString: scalar.String("inside repeat nested two"),
+					},
+				},
+				{},
+			},
+		},
+	}, {
+		desc:         "repeated group fields",
+		inputMessage: &pb2.Nests{},
+		inputText: `
+RptGroup: {
+  rpt_string: "hello"
+  rpt_string: "world"
+}
+RptGroup: {}
+`,
+		wantMessage: &pb2.Nests{
+			Rptgroup: []*pb2.Nests_RptGroup{
+				{
+					RptString: []string{"hello", "world"},
+				},
+				{},
+			},
+		},
+	}, {
+		desc:         "map fields 1",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: -101
+  value: "-101"
+}
+int32_to_str: {
+  key: 0
+  value: "zero"
+}
+bool_to_uint32: {
+  key: false
+  value: 101
+}
+int32_to_str: {
+  key: 255
+  value: "0xff"
+}
+bool_to_uint32: {
+  key: true
+  value: 42
+}
+`,
+		wantMessage: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				-101: "-101",
+				0xff: "0xff",
+				0:    "zero",
+			},
+			BoolToUint32: map[bool]uint32{
+				true:  42,
+				false: 101,
+			},
+		},
+	}, {
+		desc:         "map fields 2",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+uint64_to_enum: {
+  key: 1
+  value: ONE
+}
+uint64_to_enum: {
+  key: 2
+  value: 2
+}
+uint64_to_enum: {
+  key: 10
+  value: 101
+}
+`,
+		wantMessage: &pb3.Maps{
+			Uint64ToEnum: map[uint64]pb3.Enum{
+				1:  pb3.Enum_ONE,
+				2:  pb3.Enum_TWO,
+				10: 101,
+			},
+		},
+	}, {
+		desc:         "map fields 3",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+str_to_nested: {
+  key: "nested_one"
+  value: {
+    s_string: "nested in a map"
+  }
+}
+`,
+		wantMessage: &pb3.Maps{
+			StrToNested: map[string]*pb3.Nested{
+				"nested_one": &pb3.Nested{
+					SString: "nested in a map",
+				},
+			},
+		},
+	}, {
+		desc:         "map fields 4",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+str_to_oneofs: {
+  key: "nested"
+  value: {
+    oneof_nested: {
+      s_string: "nested oneof in map field value"
+    }
+  }
+}
+str_to_oneofs: {
+  key: "string"
+  value: {
+    oneof_string: "hello"
+  }
+}
+`,
+		wantMessage: &pb3.Maps{
+			StrToOneofs: map[string]*pb3.Oneofs{
+				"string": &pb3.Oneofs{
+					Union: &pb3.Oneofs_OneofString{
+						OneofString: "hello",
+					},
+				},
+				"nested": &pb3.Oneofs{
+					Union: &pb3.Oneofs_OneofNested{
+						OneofNested: &pb3.Nested{
+							SString: "nested oneof in map field value",
+						},
+					},
+				},
+			},
+		},
+	}, {
+		desc:         "map contains duplicate keys",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: 0
+  value: "cero"
+}
+int32_to_str: {
+  key: 0
+  value: "zero"
+}
+`,
+		wantMessage: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				0: "zero",
+			},
+		},
+	}, {
+		desc:         "map contains duplicate key fields",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: 0
+  key: 1
+  value: "cero"
+}
+`,
+		wantErr: true,
+	}, {
+		desc:         "map contains duplicate value fields",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: 1
+  value: "cero"
+  value: "uno"
+}
+`,
+		wantErr: true,
+	}, {
+		desc:         "map contains missing key",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  value: "zero"
+}
+bool_to_uint32: {
+  value: 47
+}
+str_to_nested: {
+  value: {}
+}
+`,
+		wantMessage: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				0: "zero",
+			},
+			BoolToUint32: map[bool]uint32{
+				false: 47,
+			},
+			StrToNested: map[string]*pb3.Nested{
+				"": {},
+			},
+		},
+	}, {
+		desc:         "map contains missing value",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: 100
+}
+bool_to_uint32: {
+  key: true
+}
+uint64_to_enum: {
+  key: 101
+}
+str_to_nested: {
+  key: "hello"
+}
+`,
+		wantMessage: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				100: "",
+			},
+			BoolToUint32: map[bool]uint32{
+				true: 0,
+			},
+			Uint64ToEnum: map[uint64]pb3.Enum{
+				101: pb3.Enum_ZERO,
+			},
+			StrToNested: map[string]*pb3.Nested{
+				"hello": {},
+			},
+		},
+	}, {
+		desc:         "map contains missing key and value",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {}
+bool_to_uint32: {}
+uint64_to_enum: {}
+str_to_nested: {}
+`,
+		wantMessage: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				0: "",
+			},
+			BoolToUint32: map[bool]uint32{
+				false: 0,
+			},
+			Uint64ToEnum: map[uint64]pb3.Enum{
+				0: pb3.Enum_ZERO,
+			},
+			StrToNested: map[string]*pb3.Nested{
+				"": {},
+			},
+		},
+	}, {
+		desc:         "map contains overriding entries",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: 0
+}
+int32_to_str: {
+  value: "empty"
+}
+int32_to_str: {}
+`,
+		wantMessage: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				0: "",
+			},
+		},
+	}, {
+		desc:         "map field value contains invalid UTF-8",
+		inputMessage: &pb3.Maps{},
+		inputText: `int32_to_str: {
+  key: 101
+  value: "abc\xff"
+}
+`,
+		wantMessage: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				101: "abc\xff",
+			},
+		},
+		wantErr: true,
+	}, {
+		desc:         "map field key contains invalid UTF-8",
+		inputMessage: &pb3.Maps{},
+		inputText: `str_to_nested: {
+  key: "abc\xff"
+  value: {}
+}
+`,
+		wantMessage: &pb3.Maps{
+			StrToNested: map[string]*pb3.Nested{
+				"abc\xff": {},
+			},
+		},
+		wantErr: true,
+	}, {
+		desc:         "map contains unknown field",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: 0
+  value: "cero"
+  unknown: "bad"
+}
+`,
+		wantErr: true,
+	}, {
+		desc:         "map contains extension-like key field",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  [key]: 10
+  value: "ten"
+}
+`,
+		wantErr: true,
+	}, {
+		desc:         "map contains invalid key",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: "invalid"
+  value: "cero"
+}
+`,
+		wantErr: true,
+	}, {
+		desc:         "map contains invalid value",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: 100
+  value: 101
+}
+`,
+		wantErr: true,
+	}, {
+		desc:         "map using mix of [] and repeated",
+		inputMessage: &pb3.Maps{},
+		inputText: `
+int32_to_str: {
+  key: 1
+  value: "one"
+}
+int32_to_str: [
+  {
+    key: 2
+    value: "not this"
+  },
+  {
+  },
+  {
+    key: 3
+    value: "three"
+  }
+]
+int32_to_str: {
+  key: 2
+  value: "two"
+}
+`,
+		wantMessage: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				0: "",
+				1: "one",
+				2: "two",
+				3: "three",
+			},
+		},
+	}, {
+		desc:         "required fields not set",
+		inputMessage: &pb2.Requireds{},
+		wantErr:      true,
+	}, {
+		desc:         "required field set",
+		inputMessage: &pb2.PartialRequired{},
+		inputText:    "req_string: 'this is required'",
+		wantMessage: &pb2.PartialRequired{
+			ReqString: scalar.String("this is required"),
+		},
+	}, {
+		desc:         "required fields partially set",
+		inputMessage: &pb2.Requireds{},
+		inputText: `
+req_bool: false
+req_sfixed64: 3203386110
+req_string: "hello"
+req_enum: ONE
+`,
+		wantMessage: &pb2.Requireds{
+			ReqBool:     scalar.Bool(false),
+			ReqSfixed64: scalar.Int64(0xbeefcafe),
+			ReqString:   scalar.String("hello"),
+			ReqEnum:     pb2.Enum_ONE.Enum(),
+		},
+		wantErr: true,
+	}, {
+		desc:         "required fields partially set with AllowPartial",
+		umo:          prototext.UnmarshalOptions{AllowPartial: true},
+		inputMessage: &pb2.Requireds{},
+		inputText: `
+req_bool: false
+req_sfixed64: 3203386110
+req_string: "hello"
+req_enum: ONE
+`,
+		wantMessage: &pb2.Requireds{
+			ReqBool:     scalar.Bool(false),
+			ReqSfixed64: scalar.Int64(0xbeefcafe),
+			ReqString:   scalar.String("hello"),
+			ReqEnum:     pb2.Enum_ONE.Enum(),
+		},
+	}, {
+		desc:         "required fields all set",
+		inputMessage: &pb2.Requireds{},
+		inputText: `
+req_bool: false
+req_sfixed64: 0
+req_double: 0
+req_string: ""
+req_enum: ONE
+req_nested: {}
+`,
+		wantMessage: &pb2.Requireds{
+			ReqBool:     scalar.Bool(false),
+			ReqSfixed64: scalar.Int64(0),
+			ReqDouble:   scalar.Float64(0),
+			ReqString:   scalar.String(""),
+			ReqEnum:     pb2.Enum_ONE.Enum(),
+			ReqNested:   &pb2.Nested{},
+		},
+	}, {
+		desc:         "indirect required field",
+		inputMessage: &pb2.IndirectRequired{},
+		inputText:    "opt_nested: {}",
+		wantMessage: &pb2.IndirectRequired{
+			OptNested: &pb2.NestedWithRequired{},
+		},
+		wantErr: true,
+	}, {
+		desc:         "indirect required field with AllowPartial",
+		umo:          prototext.UnmarshalOptions{AllowPartial: true},
+		inputMessage: &pb2.IndirectRequired{},
+		inputText:    "opt_nested: {}",
+		wantMessage: &pb2.IndirectRequired{
+			OptNested: &pb2.NestedWithRequired{},
+		},
+	}, {
+		desc:         "indirect required field in repeated",
+		inputMessage: &pb2.IndirectRequired{},
+		inputText: `
+rpt_nested: {
+  req_string: "one"
+}
+rpt_nested: {}
+`,
+		wantMessage: &pb2.IndirectRequired{
+			RptNested: []*pb2.NestedWithRequired{
+				{
+					ReqString: scalar.String("one"),
+				},
+				{},
+			},
+		},
+		wantErr: true,
+	}, {
+		desc:         "indirect required field in repeated with AllowPartial",
+		umo:          prototext.UnmarshalOptions{AllowPartial: true},
+		inputMessage: &pb2.IndirectRequired{},
+		inputText: `
+rpt_nested: {
+  req_string: "one"
+}
+rpt_nested: {}
+`,
+		wantMessage: &pb2.IndirectRequired{
+			RptNested: []*pb2.NestedWithRequired{
+				{
+					ReqString: scalar.String("one"),
+				},
+				{},
+			},
+		},
+	}, {
+		desc:         "indirect required field in map",
+		inputMessage: &pb2.IndirectRequired{},
+		inputText: `
+str_to_nested: {
+  key: "missing"
+}
+str_to_nested: {
+  key: "contains"
+  value: {
+    req_string: "here"
+  }
+}
+`,
+		wantMessage: &pb2.IndirectRequired{
+			StrToNested: map[string]*pb2.NestedWithRequired{
+				"missing": &pb2.NestedWithRequired{},
+				"contains": &pb2.NestedWithRequired{
+					ReqString: scalar.String("here"),
+				},
+			},
+		},
+		wantErr: true,
+	}, {
+		desc:         "indirect required field in map with AllowPartial",
+		umo:          prototext.UnmarshalOptions{AllowPartial: true},
+		inputMessage: &pb2.IndirectRequired{},
+		inputText: `
+str_to_nested: {
+  key: "missing"
+}
+str_to_nested: {
+  key: "contains"
+  value: {
+    req_string: "here"
+  }
+}
+`,
+		wantMessage: &pb2.IndirectRequired{
+			StrToNested: map[string]*pb2.NestedWithRequired{
+				"missing": &pb2.NestedWithRequired{},
+				"contains": &pb2.NestedWithRequired{
+					ReqString: scalar.String("here"),
+				},
+			},
+		},
+	}, {
+		desc:         "indirect required field in oneof",
+		inputMessage: &pb2.IndirectRequired{},
+		inputText: `oneof_nested: {}
+`,
+		wantMessage: &pb2.IndirectRequired{
+			Union: &pb2.IndirectRequired_OneofNested{
+				OneofNested: &pb2.NestedWithRequired{},
+			},
+		},
+		wantErr: true,
+	}, {
+		desc:         "indirect required field in oneof with AllowPartial",
+		umo:          prototext.UnmarshalOptions{AllowPartial: true},
+		inputMessage: &pb2.IndirectRequired{},
+		inputText: `oneof_nested: {}
+`,
+		wantMessage: &pb2.IndirectRequired{
+			Union: &pb2.IndirectRequired_OneofNested{
+				OneofNested: &pb2.NestedWithRequired{},
+			},
+		},
+	}, {
+		desc:         "ignore reserved field",
+		inputMessage: &pb2.Nests{},
+		inputText:    "reserved_field: 'ignore this'",
+		wantMessage:  &pb2.Nests{},
+	}, {
+		desc:         "extensions of non-repeated fields",
+		inputMessage: &pb2.Extensions{},
+		inputText: `opt_string: "non-extension field"
+[pb2.opt_ext_bool]: true
+opt_bool: true
+[pb2.opt_ext_nested]: {
+  opt_string: "nested in an extension"
+  opt_nested: {
+    opt_string: "another nested in an extension"
+  }
+}
+[pb2.opt_ext_string]: "extension field"
+opt_int32: 42
+[pb2.opt_ext_enum]: TEN
+`,
+		wantMessage: func() proto.Message {
+			m := &pb2.Extensions{
+				OptString: scalar.String("non-extension field"),
+				OptBool:   scalar.Bool(true),
+				OptInt32:  scalar.Int32(42),
+			}
+			setExtension(m, pb2.E_OptExtBool, true)
+			setExtension(m, pb2.E_OptExtString, "extension field")
+			setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
+			setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
+				OptString: scalar.String("nested in an extension"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("another nested in an extension"),
+				},
+			})
+			return m
+		}(),
+	}, {
+		desc:         "extension field contains invalid UTF-8",
+		inputMessage: &pb2.Extensions{},
+		inputText:    `[pb2.opt_ext_string]: "abc\xff"`,
+		wantMessage: func() proto.Message {
+			m := &pb2.Extensions{}
+			setExtension(m, pb2.E_OptExtString, "abc\xff")
+			return m
+		}(),
+		wantErr: true,
+	}, {
+		desc:         "extensions of repeated fields",
+		inputMessage: &pb2.Extensions{},
+		inputText: `[pb2.rpt_ext_enum]: TEN
+[pb2.rpt_ext_enum]: 101
+[pb2.rpt_ext_fixed32]: 42
+[pb2.rpt_ext_enum]: ONE
+[pb2.rpt_ext_nested]: {
+  opt_string: "one"
+}
+[pb2.rpt_ext_nested]: {
+  opt_string: "two"
+}
+[pb2.rpt_ext_fixed32]: 47
+[pb2.rpt_ext_nested]: {
+  opt_string: "three"
+}
+`,
+		wantMessage: func() proto.Message {
+			m := &pb2.Extensions{}
+			setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
+			setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
+			setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
+				&pb2.Nested{OptString: scalar.String("one")},
+				&pb2.Nested{OptString: scalar.String("two")},
+				&pb2.Nested{OptString: scalar.String("three")},
+			})
+			return m
+		}(),
+	}, {
+		desc:         "extensions of non-repeated fields in another message",
+		inputMessage: &pb2.Extensions{},
+		inputText: `[pb2.ExtensionsContainer.opt_ext_bool]: true
+[pb2.ExtensionsContainer.opt_ext_enum]: TEN
+[pb2.ExtensionsContainer.opt_ext_nested]: {
+  opt_string: "nested in an extension"
+  opt_nested: {
+    opt_string: "another nested in an extension"
+  }
+}
+[pb2.ExtensionsContainer.opt_ext_string]: "extension field"
+`,
+		wantMessage: func() proto.Message {
+			m := &pb2.Extensions{}
+			setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
+			setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
+			setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
+			setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
+				OptString: scalar.String("nested in an extension"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("another nested in an extension"),
+				},
+			})
+			return m
+		}(),
+	}, {
+		desc:         "extensions of repeated fields in another message",
+		inputMessage: &pb2.Extensions{},
+		inputText: `opt_string: "non-extension field"
+opt_bool: true
+opt_int32: 42
+[pb2.ExtensionsContainer.rpt_ext_nested]: {
+  opt_string: "one"
+}
+[pb2.ExtensionsContainer.rpt_ext_enum]: TEN
+[pb2.ExtensionsContainer.rpt_ext_nested]: {
+  opt_string: "two"
+}
+[pb2.ExtensionsContainer.rpt_ext_enum]: 101
+[pb2.ExtensionsContainer.rpt_ext_string]: "hello"
+[pb2.ExtensionsContainer.rpt_ext_enum]: ONE
+[pb2.ExtensionsContainer.rpt_ext_nested]: {
+  opt_string: "three"
+}
+[pb2.ExtensionsContainer.rpt_ext_string]: "world"
+`,
+		wantMessage: func() proto.Message {
+			m := &pb2.Extensions{
+				OptString: scalar.String("non-extension field"),
+				OptBool:   scalar.Bool(true),
+				OptInt32:  scalar.Int32(42),
+			}
+			setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
+			setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
+			setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
+				&pb2.Nested{OptString: scalar.String("one")},
+				&pb2.Nested{OptString: scalar.String("two")},
+				&pb2.Nested{OptString: scalar.String("three")},
+			})
+			return m
+		}(),
+	}, {
+		desc:         "invalid extension field name",
+		inputMessage: &pb2.Extensions{},
+		inputText:    "[pb2.invalid_message_field]: true",
+		wantErr:      true,
+	}, {
+		desc:         "MessageSet",
+		inputMessage: &pb2.MessageSet{},
+		inputText: `
+[pb2.MessageSetExtension]: {
+  opt_string: "a messageset extension"
+}
+[pb2.MessageSetExtension.ext_nested]: {
+  opt_string: "just a regular extension"
+}
+[pb2.MessageSetExtension.not_message_set_extension]: {
+  opt_string: "not a messageset extension"
+}
+`,
+		wantMessage: func() proto.Message {
+			m := &pb2.MessageSet{}
+			setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
+				OptString: scalar.String("a messageset extension"),
+			})
+			setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
+				OptString: scalar.String("not a messageset extension"),
+			})
+			setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
+				OptString: scalar.String("just a regular extension"),
+			})
+			return m
+		}(),
+	}, {
+		desc:         "not real MessageSet 1",
+		inputMessage: &pb2.FakeMessageSet{},
+		inputText: `
+[pb2.FakeMessageSetExtension.message_set_extension]: {
+  opt_string: "not a messageset extension"
+}
+`,
+		wantMessage: func() proto.Message {
+			m := &pb2.FakeMessageSet{}
+			setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
+				OptString: scalar.String("not a messageset extension"),
+			})
+			return m
+		}(),
+	}, {
+		desc:         "not real MessageSet 2",
+		inputMessage: &pb2.FakeMessageSet{},
+		inputText: `
+[pb2.FakeMessageSetExtension]: {
+  opt_string: "not a messageset extension"
+}
+`,
+		wantErr: true,
+	}, {
+		desc:         "not real MessageSet 3",
+		inputMessage: &pb2.MessageSet{},
+		inputText: `
+[pb2.message_set_extension]: {
+  opt_string: "another not a messageset extension"
+}`,
+		wantMessage: func() proto.Message {
+			m := &pb2.MessageSet{}
+			setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
+				OptString: scalar.String("another not a messageset extension"),
+			})
+			return m
+		}(),
+	}, {
+		desc:         "Any not expanded",
+		inputMessage: &knownpb.Any{},
+		inputText: `
+type_url: "pb2.Nested"
+value: "some bytes"
+`,
+		wantMessage: &knownpb.Any{
+			TypeUrl: "pb2.Nested",
+			Value:   []byte("some bytes"),
+		},
+	}, {
+		desc:         "Any not expanded missing value",
+		inputMessage: &knownpb.Any{},
+		inputText:    `type_url: "pb2.Nested"`,
+		wantMessage: &knownpb.Any{
+			TypeUrl: "pb2.Nested",
+		},
+	}, {
+		desc:         "Any not expanded missing type_url",
+		inputMessage: &knownpb.Any{},
+		inputText:    `value: "some bytes"`,
+		wantMessage: &knownpb.Any{
+			Value: []byte("some bytes"),
+		},
+	}, {
+		desc: "Any expanded",
+		umo: prototext.UnmarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
+		},
+		inputMessage: &knownpb.Any{},
+		inputText: `
+[foobar/pb2.Nested]: {
+  opt_string: "embedded inside Any"
+  opt_nested: {
+    opt_string: "inception"
+  }
+}
+`,
+		wantMessage: func() proto.Message {
+			m := &pb2.Nested{
+				OptString: scalar.String("embedded inside Any"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("inception"),
+				},
+			}
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &knownpb.Any{
+				TypeUrl: "foobar/pb2.Nested",
+				Value:   b,
+			}
+		}(),
+	}, {
+		desc: "Any expanded with empty value",
+		umo: prototext.UnmarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
+		},
+		inputMessage: &knownpb.Any{},
+		inputText:    `[foo.com/pb2.Nested]: {}`,
+		wantMessage: &knownpb.Any{
+			TypeUrl: "foo.com/pb2.Nested",
+		},
+	}, {
+		desc: "Any expanded with missing required error",
+		umo: prototext.UnmarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
+		},
+		inputMessage: &knownpb.Any{},
+		inputText: `
+[pb2.PartialRequired]: {
+  opt_string: "embedded inside Any"
+}
+`,
+		wantMessage: func() proto.Message {
+			m := &pb2.PartialRequired{
+				OptString: scalar.String("embedded inside Any"),
+			}
+			b, err := proto.MarshalOptions{
+				AllowPartial:  true,
+				Deterministic: true,
+			}.Marshal(m)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &knownpb.Any{
+				TypeUrl: "pb2.PartialRequired",
+				Value:   b,
+			}
+		}(),
+		wantErr: true,
+	}, {
+		desc: "Any with invalid UTF-8",
+		umo: prototext.UnmarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb3.Nested{})),
+		},
+		inputMessage: &knownpb.Any{},
+		inputText: `
+[pb3.Nested]: {
+  s_string: "abc\xff"
+}
+`,
+		wantMessage: func() proto.Message {
+			m := &pb3.Nested{
+				SString: "abc\xff",
+			}
+			var nerr errors.NonFatal
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
+			if !nerr.Merge(err) {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &knownpb.Any{
+				TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
+				Value:   b,
+			}
+		}(),
+		wantErr: true,
+	}, {
+		desc:         "Any expanded with unregistered type",
+		umo:          prototext.UnmarshalOptions{Resolver: preg.NewTypes()},
+		inputMessage: &knownpb.Any{},
+		inputText:    `[SomeMessage]: {}`,
+		wantErr:      true,
+	}, {
+		desc: "Any expanded with invalid value",
+		umo: prototext.UnmarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
+		},
+		inputMessage: &knownpb.Any{},
+		inputText:    `[pb2.Nested]: 123`,
+		wantErr:      true,
+	}, {
+		desc: "Any expanded with unknown fields",
+		umo: prototext.UnmarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
+		},
+		inputMessage: &knownpb.Any{},
+		inputText: `
+[pb2.Nested]: {}
+unknown: ""
+`,
+		wantErr: true,
+	}, {
+		desc: "Any contains expanded and unexpanded fields",
+		umo: prototext.UnmarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
+		},
+		inputMessage: &knownpb.Any{},
+		inputText: `
+[pb2.Nested]: {}
+type_url: "pb2.Nested"
+`,
+		wantErr: true,
+	}}
+
+	for _, tt := range tests {
+		tt := tt
+		t.Run(tt.desc, func(t *testing.T) {
+			err := tt.umo.Unmarshal(tt.inputMessage, []byte(tt.inputText))
+			if err != nil && !tt.wantErr {
+				t.Errorf("Unmarshal() returned error: %v\n\n", err)
+			}
+			if err == nil && tt.wantErr {
+				t.Error("Unmarshal() got nil error, want error\n\n")
+			}
+			if tt.wantMessage != nil && !protoV1.Equal(tt.inputMessage.(protoV1.Message), tt.wantMessage.(protoV1.Message)) {
+				t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", tt.inputMessage, tt.wantMessage)
+			}
+		})
+	}
+}
diff --git a/encoding/prototext/doc.go b/encoding/prototext/doc.go
new file mode 100644
index 0000000..162b4f9
--- /dev/null
+++ b/encoding/prototext/doc.go
@@ -0,0 +1,7 @@
+// 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 prototext marshals and unmarshals protocol buffer messages as the
+// textproto format.
+package prototext
diff --git a/encoding/prototext/encode.go b/encoding/prototext/encode.go
new file mode 100644
index 0000000..8f06370
--- /dev/null
+++ b/encoding/prototext/encode.go
@@ -0,0 +1,384 @@
+// Copyright 2018 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 prototext
+
+import (
+	"fmt"
+	"sort"
+	"unicode/utf8"
+
+	"google.golang.org/protobuf/internal/encoding/text"
+	"google.golang.org/protobuf/internal/encoding/wire"
+	"google.golang.org/protobuf/internal/errors"
+	"google.golang.org/protobuf/internal/fieldnum"
+	"google.golang.org/protobuf/internal/mapsort"
+	"google.golang.org/protobuf/internal/pragma"
+	"google.golang.org/protobuf/proto"
+	pref "google.golang.org/protobuf/reflect/protoreflect"
+	"google.golang.org/protobuf/reflect/protoregistry"
+)
+
+// Marshal writes the given proto.Message in textproto format using default options.
+func Marshal(m proto.Message) ([]byte, error) {
+	return MarshalOptions{}.Marshal(m)
+}
+
+// MarshalOptions is a configurable text format marshaler.
+type MarshalOptions struct {
+	pragma.NoUnkeyedLiterals
+
+	// AllowPartial allows messages that have missing required fields to marshal
+	// without returning an error. If AllowPartial is false (the default),
+	// Marshal will return error if there are any missing required fields.
+	AllowPartial bool
+
+	// If Indent is a non-empty string, it causes entries for a Message to be
+	// preceded by the indent and trailed by a newline. Indent can only be
+	// composed of space or tab characters.
+	Indent string
+
+	// Resolver is the registry used for type lookups when marshaling out
+	// google.protobuf.Any messages in expanded form. If Resolver is not set,
+	// marshaling will default to using protoregistry.GlobalTypes.  If a type is
+	// not found, an Any message will be marshaled as a regular message.
+	Resolver *protoregistry.Types
+}
+
+// Marshal writes the given proto.Message in textproto format using options in MarshalOptions object.
+func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
+	if o.Resolver == nil {
+		o.Resolver = protoregistry.GlobalTypes
+	}
+
+	var nerr errors.NonFatal
+	v, err := o.marshalMessage(m.ProtoReflect())
+	if !nerr.Merge(err) {
+		return nil, err
+	}
+
+	delims := [2]byte{'{', '}'}
+	const outputASCII = false
+	b, err := text.Marshal(v, o.Indent, delims, outputASCII)
+	if !nerr.Merge(err) {
+		return nil, err
+	}
+	if !o.AllowPartial {
+		nerr.Merge(proto.IsInitialized(m))
+	}
+	return b, nerr.E
+}
+
+// marshalMessage converts a protoreflect.Message to a text.Value.
+func (o MarshalOptions) marshalMessage(m pref.Message) (text.Value, error) {
+	var nerr errors.NonFatal
+	var msgFields [][2]text.Value
+	messageDesc := m.Descriptor()
+
+	// Handle Any expansion.
+	if messageDesc.FullName() == "google.protobuf.Any" {
+		msg, err := o.marshalAny(m)
+		if err == nil || nerr.Merge(err) {
+			// Return as is for nil or non-fatal error.
+			return msg, nerr.E
+		}
+		// For other errors, continue on to marshal Any as a regular message.
+	}
+
+	// Handle known fields.
+	fieldDescs := messageDesc.Fields()
+	knownFields := m.KnownFields()
+	size := fieldDescs.Len()
+	for i := 0; i < size; i++ {
+		fd := fieldDescs.Get(i)
+		num := fd.Number()
+
+		if !knownFields.Has(num) {
+			continue
+		}
+
+		name := text.ValueOf(fd.Name())
+		// Use type name for group field name.
+		if fd.Kind() == pref.GroupKind {
+			name = text.ValueOf(fd.Message().Name())
+		}
+		pval := knownFields.Get(num)
+		var err error
+		msgFields, err = o.appendField(msgFields, name, pval, fd)
+		if !nerr.Merge(err) {
+			return text.Value{}, err
+		}
+	}
+
+	// Handle extensions.
+	var err error
+	msgFields, err = o.appendExtensions(msgFields, knownFields)
+	if !nerr.Merge(err) {
+		return text.Value{}, err
+	}
+
+	// Handle unknown fields.
+	// TODO: Provide option to exclude or include unknown fields.
+	m.UnknownFields().Range(func(_ pref.FieldNumber, raw pref.RawFields) bool {
+		msgFields = appendUnknown(msgFields, raw)
+		return true
+	})
+
+	return text.ValueOf(msgFields), nerr.E
+}
+
+// appendField marshals a protoreflect.Value and appends it to the given [][2]text.Value.
+func (o MarshalOptions) appendField(msgFields [][2]text.Value, name text.Value, pval pref.Value, fd pref.FieldDescriptor) ([][2]text.Value, error) {
+	var nerr errors.NonFatal
+
+	switch {
+	case fd.IsList():
+		items, err := o.marshalList(pval.List(), fd)
+		if !nerr.Merge(err) {
+			return msgFields, err
+		}
+
+		for _, item := range items {
+			msgFields = append(msgFields, [2]text.Value{name, item})
+		}
+	case fd.IsMap():
+		items, err := o.marshalMap(pval.Map(), fd)
+		if !nerr.Merge(err) {
+			return msgFields, err
+		}
+
+		for _, item := range items {
+			msgFields = append(msgFields, [2]text.Value{name, item})
+		}
+	default:
+		tval, err := o.marshalSingular(pval, fd)
+		if !nerr.Merge(err) {
+			return msgFields, err
+		}
+		msgFields = append(msgFields, [2]text.Value{name, tval})
+	}
+
+	return msgFields, nerr.E
+}
+
+// marshalSingular converts a non-repeated field value to text.Value.
+// This includes all scalar types, enums, messages, and groups.
+func (o MarshalOptions) marshalSingular(val pref.Value, fd pref.FieldDescriptor) (text.Value, error) {
+	kind := fd.Kind()
+	switch kind {
+	case pref.BoolKind,
+		pref.Int32Kind, pref.Sint32Kind, pref.Uint32Kind,
+		pref.Int64Kind, pref.Sint64Kind, pref.Uint64Kind,
+		pref.Sfixed32Kind, pref.Fixed32Kind,
+		pref.Sfixed64Kind, pref.Fixed64Kind,
+		pref.FloatKind, pref.DoubleKind,
+		pref.BytesKind:
+		return text.ValueOf(val.Interface()), nil
+
+	case pref.StringKind:
+		s := val.String()
+		if utf8.ValidString(s) {
+			return text.ValueOf(s), nil
+		}
+		var nerr errors.NonFatal
+		nerr.AppendInvalidUTF8(string(fd.FullName()))
+		return text.ValueOf(s), nerr.E
+
+	case pref.EnumKind:
+		num := val.Enum()
+		if desc := fd.Enum().Values().ByNumber(num); desc != nil {
+			return text.ValueOf(desc.Name()), nil
+		}
+		// Use numeric value if there is no enum description.
+		return text.ValueOf(int32(num)), nil
+
+	case pref.MessageKind, pref.GroupKind:
+		return o.marshalMessage(val.Message())
+	}
+
+	panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
+}
+
+// marshalList converts a protoreflect.List to []text.Value.
+func (o MarshalOptions) marshalList(list pref.List, fd pref.FieldDescriptor) ([]text.Value, error) {
+	var nerr errors.NonFatal
+	size := list.Len()
+	values := make([]text.Value, 0, size)
+
+	for i := 0; i < size; i++ {
+		item := list.Get(i)
+		val, err := o.marshalSingular(item, fd)
+		if !nerr.Merge(err) {
+			// Return already marshaled values.
+			return values, err
+		}
+		values = append(values, val)
+	}
+
+	return values, nerr.E
+}
+
+var (
+	mapKeyName   = text.ValueOf(pref.Name("key"))
+	mapValueName = text.ValueOf(pref.Name("value"))
+)
+
+// marshalMap converts a protoreflect.Map to []text.Value.
+func (o MarshalOptions) marshalMap(mmap pref.Map, fd pref.FieldDescriptor) ([]text.Value, error) {
+	var nerr errors.NonFatal
+	// values is a list of messages.
+	values := make([]text.Value, 0, mmap.Len())
+
+	var err error
+	mapsort.Range(mmap, fd.MapKey().Kind(), func(key pref.MapKey, val pref.Value) bool {
+		var keyTxtVal text.Value
+		keyTxtVal, err = o.marshalSingular(key.Value(), fd.MapKey())
+		if !nerr.Merge(err) {
+			return false
+		}
+		var valTxtVal text.Value
+		valTxtVal, err = o.marshalSingular(val, fd.MapValue())
+		if !nerr.Merge(err) {
+			return false
+		}
+		// Map entry (message) contains 2 fields, first field for key and second field for value.
+		msg := text.ValueOf([][2]text.Value{
+			{mapKeyName, keyTxtVal},
+			{mapValueName, valTxtVal},
+		})
+		values = append(values, msg)
+		err = nil
+		return true
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	return values, nerr.E
+}
+
+// appendExtensions marshals extension fields and appends them to the given [][2]text.Value.
+func (o MarshalOptions) appendExtensions(msgFields [][2]text.Value, knownFields pref.KnownFields) ([][2]text.Value, error) {
+	xtTypes := knownFields.ExtensionTypes()
+	xtFields := make([][2]text.Value, 0, xtTypes.Len())
+
+	var nerr errors.NonFatal
+	var err error
+	xtTypes.Range(func(xt pref.ExtensionType) bool {
+		name := xt.Descriptor().FullName()
+		// If extended type is a MessageSet, set field name to be the message type name.
+		if isMessageSetExtension(xt) {
+			name = xt.Descriptor().Message().FullName()
+		}
+
+		num := xt.Descriptor().Number()
+		if knownFields.Has(num) {
+			// Use string type to produce [name] format.
+			tname := text.ValueOf(string(name))
+			pval := knownFields.Get(num)
+			xtFields, err = o.appendField(xtFields, tname, pval, xt.Descriptor())
+			if !nerr.Merge(err) {
+				return false
+			}
+			err = nil
+		}
+		return true
+	})
+	if err != nil {
+		return msgFields, err
+	}
+
+	// Sort extensions lexicographically and append to output.
+	sort.SliceStable(xtFields, func(i, j int) bool {
+		return xtFields[i][0].String() < xtFields[j][0].String()
+	})
+	return append(msgFields, xtFields...), nerr.E
+}
+
+// isMessageSetExtension reports whether extension extends a message set.
+func isMessageSetExtension(xt pref.ExtensionType) bool {
+	xd := xt.Descriptor()
+	if xd.Name() != "message_set_extension" {
+		return false
+	}
+	md := xd.Message()
+	if md == nil {
+		return false
+	}
+	if xd.FullName().Parent() != md.FullName() {
+		return false
+	}
+	xmd, ok := xd.ContainingMessage().(interface{ IsMessageSet() bool })
+	return ok && xmd.IsMessageSet()
+}
+
+// appendUnknown parses the given []byte and appends field(s) into the given fields slice.
+// This function assumes proper encoding in the given []byte.
+func appendUnknown(fields [][2]text.Value, b []byte) [][2]text.Value {
+	for len(b) > 0 {
+		var value interface{}
+		num, wtype, n := wire.ConsumeTag(b)
+		b = b[n:]
+
+		switch wtype {
+		case wire.VarintType:
+			value, n = wire.ConsumeVarint(b)
+		case wire.Fixed32Type:
+			value, n = wire.ConsumeFixed32(b)
+		case wire.Fixed64Type:
+			value, n = wire.ConsumeFixed64(b)
+		case wire.BytesType:
+			value, n = wire.ConsumeBytes(b)
+		case wire.StartGroupType:
+			var v []byte
+			v, n = wire.ConsumeGroup(num, b)
+			var msg [][2]text.Value
+			value = appendUnknown(msg, v)
+		default:
+			panic(fmt.Sprintf("error parsing unknown field wire type: %v", wtype))
+		}
+
+		fields = append(fields, [2]text.Value{text.ValueOf(uint32(num)), text.ValueOf(value)})
+		b = b[n:]
+	}
+	return fields
+}
+
+// marshalAny converts a google.protobuf.Any protoreflect.Message to a text.Value.
+func (o MarshalOptions) marshalAny(m pref.Message) (text.Value, error) {
+	var nerr errors.NonFatal
+	knownFields := m.KnownFields()
+	typeURL := knownFields.Get(fieldnum.Any_TypeUrl).String()
+	value := knownFields.Get(fieldnum.Any_Value)
+
+	emt, err := o.Resolver.FindMessageByURL(typeURL)
+	if !nerr.Merge(err) {
+		return text.Value{}, err
+	}
+	em := emt.New().Interface()
+	// TODO: Need to set types registry in binary unmarshaling.
+	// TODO: If binary unmarshaling returns required not set error, need to
+	// return another required not set error that contains both the path to this
+	// field and the path inside the embedded message.
+	err = proto.UnmarshalOptions{
+		AllowPartial: o.AllowPartial,
+	}.Unmarshal(value.Bytes(), em)
+	if !nerr.Merge(err) {
+		return text.Value{}, err
+	}
+
+	msg, err := o.marshalMessage(em.ProtoReflect())
+	if !nerr.Merge(err) {
+		return text.Value{}, err
+	}
+	// Expanded Any field value contains only a single field with the type_url field value as the
+	// field name in [] and a text marshaled field value of the embedded message.
+	msgFields := [][2]text.Value{
+		{
+			text.ValueOf(typeURL),
+			msg,
+		},
+	}
+	return text.ValueOf(msgFields), nerr.E
+}
diff --git a/encoding/prototext/encode_test.go b/encoding/prototext/encode_test.go
new file mode 100644
index 0000000..b2fc57b
--- /dev/null
+++ b/encoding/prototext/encode_test.go
@@ -0,0 +1,1304 @@
+// Copyright 2018 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 prototext_test
+
+import (
+	"bytes"
+	"encoding/hex"
+	"math"
+	"strings"
+	"testing"
+
+	"github.com/google/go-cmp/cmp"
+	"github.com/google/go-cmp/cmp/cmpopts"
+	"google.golang.org/protobuf/encoding/prototext"
+	"google.golang.org/protobuf/internal/detrand"
+	"google.golang.org/protobuf/internal/encoding/pack"
+	"google.golang.org/protobuf/internal/encoding/wire"
+	pimpl "google.golang.org/protobuf/internal/impl"
+	"google.golang.org/protobuf/internal/scalar"
+	"google.golang.org/protobuf/proto"
+	preg "google.golang.org/protobuf/reflect/protoregistry"
+	"google.golang.org/protobuf/runtime/protoiface"
+
+	"google.golang.org/protobuf/encoding/testprotos/pb2"
+	"google.golang.org/protobuf/encoding/testprotos/pb3"
+	knownpb "google.golang.org/protobuf/types/known"
+)
+
+func init() {
+	// Disable detrand to enable direct comparisons on outputs.
+	detrand.Disable()
+}
+
+// splitLines is a cmpopts.Option for comparing strings with line breaks.
+var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
+	return strings.Split(s, "\n")
+})
+
+func pb2Enum(i int32) *pb2.Enum {
+	p := new(pb2.Enum)
+	*p = pb2.Enum(i)
+	return p
+}
+
+func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum {
+	p := new(pb2.Enums_NestedEnum)
+	*p = pb2.Enums_NestedEnum(i)
+	return p
+}
+
+func setExtension(m proto.Message, xd *protoiface.ExtensionDescV1, val interface{}) {
+	knownFields := m.ProtoReflect().KnownFields()
+	extTypes := knownFields.ExtensionTypes()
+	extTypes.Register(xd.Type)
+	if val == nil {
+		return
+	}
+	pval := xd.Type.ValueOf(val)
+	knownFields.Set(wire.Number(xd.Field), pval)
+}
+
+// dhex decodes a hex-string and returns the bytes and panics if s is invalid.
+func dhex(s string) []byte {
+	b, err := hex.DecodeString(s)
+	if err != nil {
+		panic(err)
+	}
+	return b
+}
+
+func TestMarshal(t *testing.T) {
+	tests := []struct {
+		desc    string
+		mo      prototext.MarshalOptions
+		input   proto.Message
+		want    string
+		wantErr bool // TODO: Verify error message content.
+	}{{
+		desc:  "proto2 optional scalars not set",
+		input: &pb2.Scalars{},
+		want:  "\n",
+	}, {
+		desc:  "proto3 scalars not set",
+		input: &pb3.Scalars{},
+		want:  "\n",
+	}, {
+		desc: "proto2 optional scalars set to zero values",
+		input: &pb2.Scalars{
+			OptBool:     scalar.Bool(false),
+			OptInt32:    scalar.Int32(0),
+			OptInt64:    scalar.Int64(0),
+			OptUint32:   scalar.Uint32(0),
+			OptUint64:   scalar.Uint64(0),
+			OptSint32:   scalar.Int32(0),
+			OptSint64:   scalar.Int64(0),
+			OptFixed32:  scalar.Uint32(0),
+			OptFixed64:  scalar.Uint64(0),
+			OptSfixed32: scalar.Int32(0),
+			OptSfixed64: scalar.Int64(0),
+			OptFloat:    scalar.Float32(0),
+			OptDouble:   scalar.Float64(0),
+			OptBytes:    []byte{},
+			OptString:   scalar.String(""),
+		},
+		want: `opt_bool: false
+opt_int32: 0
+opt_int64: 0
+opt_uint32: 0
+opt_uint64: 0
+opt_sint32: 0
+opt_sint64: 0
+opt_fixed32: 0
+opt_fixed64: 0
+opt_sfixed32: 0
+opt_sfixed64: 0
+opt_float: 0
+opt_double: 0
+opt_bytes: ""
+opt_string: ""
+`,
+	}, {
+		desc: "proto3 scalars set to zero values",
+		input: &pb3.Scalars{
+			SBool:     false,
+			SInt32:    0,
+			SInt64:    0,
+			SUint32:   0,
+			SUint64:   0,
+			SSint32:   0,
+			SSint64:   0,
+			SFixed32:  0,
+			SFixed64:  0,
+			SSfixed32: 0,
+			SSfixed64: 0,
+			SFloat:    0,
+			SDouble:   0,
+			SBytes:    []byte{},
+			SString:   "",
+		},
+		want: "\n",
+	}, {
+		desc: "proto2 optional scalars set to some values",
+		input: &pb2.Scalars{
+			OptBool:     scalar.Bool(true),
+			OptInt32:    scalar.Int32(0xff),
+			OptInt64:    scalar.Int64(0xdeadbeef),
+			OptUint32:   scalar.Uint32(47),
+			OptUint64:   scalar.Uint64(0xdeadbeef),
+			OptSint32:   scalar.Int32(-1001),
+			OptSint64:   scalar.Int64(-0xffff),
+			OptFixed64:  scalar.Uint64(64),
+			OptSfixed32: scalar.Int32(-32),
+			OptFloat:    scalar.Float32(1.02),
+			OptDouble:   scalar.Float64(1.0199999809265137),
+			OptBytes:    []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
+			OptString:   scalar.String("谷歌"),
+		},
+		want: `opt_bool: true
+opt_int32: 255
+opt_int64: 3735928559
+opt_uint32: 47
+opt_uint64: 3735928559
+opt_sint32: -1001
+opt_sint64: -65535
+opt_fixed64: 64
+opt_sfixed32: -32
+opt_float: 1.02
+opt_double: 1.0199999809265137
+opt_bytes: "谷歌"
+opt_string: "谷歌"
+`,
+	}, {
+		desc: "string with invalid UTF-8",
+		input: &pb3.Scalars{
+			SString: "abc\xff",
+		},
+		want: `s_string: "abc\xff"
+`,
+		wantErr: true,
+	}, {
+		desc: "float nan",
+		input: &pb3.Scalars{
+			SFloat: float32(math.NaN()),
+		},
+		want: "s_float: nan\n",
+	}, {
+		desc: "float positive infinity",
+		input: &pb3.Scalars{
+			SFloat: float32(math.Inf(1)),
+		},
+		want: "s_float: inf\n",
+	}, {
+		desc: "float negative infinity",
+		input: &pb3.Scalars{
+			SFloat: float32(math.Inf(-1)),
+		},
+		want: "s_float: -inf\n",
+	}, {
+		desc: "double nan",
+		input: &pb3.Scalars{
+			SDouble: math.NaN(),
+		},
+		want: "s_double: nan\n",
+	}, {
+		desc: "double positive infinity",
+		input: &pb3.Scalars{
+			SDouble: math.Inf(1),
+		},
+		want: "s_double: inf\n",
+	}, {
+		desc: "double negative infinity",
+		input: &pb3.Scalars{
+			SDouble: math.Inf(-1),
+		},
+		want: "s_double: -inf\n",
+	}, {
+		desc:  "proto2 enum not set",
+		input: &pb2.Enums{},
+		want:  "\n",
+	}, {
+		desc: "proto2 enum set to zero value",
+		input: &pb2.Enums{
+			OptEnum:       pb2Enum(0),
+			OptNestedEnum: pb2Enums_NestedEnum(0),
+		},
+		want: `opt_enum: 0
+opt_nested_enum: 0
+`,
+	}, {
+		desc: "proto2 enum",
+		input: &pb2.Enums{
+			OptEnum:       pb2.Enum_ONE.Enum(),
+			OptNestedEnum: pb2.Enums_UNO.Enum(),
+		},
+		want: `opt_enum: ONE
+opt_nested_enum: UNO
+`,
+	}, {
+		desc: "proto2 enum set to numeric values",
+		input: &pb2.Enums{
+			OptEnum:       pb2Enum(2),
+			OptNestedEnum: pb2Enums_NestedEnum(2),
+		},
+		want: `opt_enum: TWO
+opt_nested_enum: DOS
+`,
+	}, {
+		desc: "proto2 enum set to unnamed numeric values",
+		input: &pb2.Enums{
+			OptEnum:       pb2Enum(101),
+			OptNestedEnum: pb2Enums_NestedEnum(-101),
+		},
+		want: `opt_enum: 101
+opt_nested_enum: -101
+`,
+	}, {
+		desc:  "proto3 enum not set",
+		input: &pb3.Enums{},
+		want:  "\n",
+	}, {
+		desc: "proto3 enum set to zero value",
+		input: &pb3.Enums{
+			SEnum:       pb3.Enum_ZERO,
+			SNestedEnum: pb3.Enums_CERO,
+		},
+		want: "\n",
+	}, {
+		desc: "proto3 enum",
+		input: &pb3.Enums{
+			SEnum:       pb3.Enum_ONE,
+			SNestedEnum: pb3.Enums_UNO,
+		},
+		want: `s_enum: ONE
+s_nested_enum: UNO
+`,
+	}, {
+		desc: "proto3 enum set to numeric values",
+		input: &pb3.Enums{
+			SEnum:       2,
+			SNestedEnum: 2,
+		},
+		want: `s_enum: TWO
+s_nested_enum: DOS
+`,
+	}, {
+		desc: "proto3 enum set to unnamed numeric values",
+		input: &pb3.Enums{
+			SEnum:       -47,
+			SNestedEnum: 47,
+		},
+		want: `s_enum: -47
+s_nested_enum: 47
+`,
+	}, {
+		desc:  "proto2 nested message not set",
+		input: &pb2.Nests{},
+		want:  "\n",
+	}, {
+		desc: "proto2 nested message set to empty",
+		input: &pb2.Nests{
+			OptNested: &pb2.Nested{},
+			Optgroup:  &pb2.Nests_OptGroup{},
+		},
+		want: `opt_nested: {}
+OptGroup: {}
+`,
+	}, {
+		desc: "proto2 nested messages",
+		input: &pb2.Nests{
+			OptNested: &pb2.Nested{
+				OptString: scalar.String("nested message"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("another nested message"),
+				},
+			},
+		},
+		want: `opt_nested: {
+  opt_string: "nested message"
+  opt_nested: {
+    opt_string: "another nested message"
+  }
+}
+`,
+	}, {
+		desc: "proto2 groups",
+		input: &pb2.Nests{
+			Optgroup: &pb2.Nests_OptGroup{
+				OptString: scalar.String("inside a group"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("nested message inside a group"),
+				},
+				Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
+					OptFixed32: scalar.Uint32(47),
+				},
+			},
+		},
+		want: `OptGroup: {
+  opt_string: "inside a group"
+  opt_nested: {
+    opt_string: "nested message inside a group"
+  }
+  OptNestedGroup: {
+    opt_fixed32: 47
+  }
+}
+`,
+	}, {
+		desc:  "proto3 nested message not set",
+		input: &pb3.Nests{},
+		want:  "\n",
+	}, {
+		desc: "proto3 nested message set to empty",
+		input: &pb3.Nests{
+			SNested: &pb3.Nested{},
+		},
+		want: "s_nested: {}\n",
+	}, {
+		desc: "proto3 nested message",
+		input: &pb3.Nests{
+			SNested: &pb3.Nested{
+				SString: "nested message",
+				SNested: &pb3.Nested{
+					SString: "another nested message",
+				},
+			},
+		},
+		want: `s_nested: {
+  s_string: "nested message"
+  s_nested: {
+    s_string: "another nested message"
+  }
+}
+`,
+	}, {
+		desc: "proto3 nested message contains invalid UTF-8",
+		input: &pb3.Nests{
+			SNested: &pb3.Nested{
+				SString: "abc\xff",
+			},
+		},
+		want: `s_nested: {
+  s_string: "abc\xff"
+}
+`,
+		wantErr: true,
+	}, {
+		desc:  "oneof not set",
+		input: &pb3.Oneofs{},
+		want:  "\n",
+	}, {
+		desc: "oneof set to empty string",
+		input: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofString{},
+		},
+		want: `oneof_string: ""
+`,
+	}, {
+		desc: "oneof set to string",
+		input: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofString{
+				OneofString: "hello",
+			},
+		},
+		want: `oneof_string: "hello"
+`,
+	}, {
+		desc: "oneof set to enum",
+		input: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofEnum{
+				OneofEnum: pb3.Enum_ZERO,
+			},
+		},
+		want: `oneof_enum: ZERO
+`,
+	}, {
+		desc: "oneof set to empty message",
+		input: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofNested{
+				OneofNested: &pb3.Nested{},
+			},
+		},
+		want: "oneof_nested: {}\n",
+	}, {
+		desc: "oneof set to message",
+		input: &pb3.Oneofs{
+			Union: &pb3.Oneofs_OneofNested{
+				OneofNested: &pb3.Nested{
+					SString: "nested message",
+				},
+			},
+		},
+		want: `oneof_nested: {
+  s_string: "nested message"
+}
+`,
+	}, {
+		desc:  "repeated fields not set",
+		input: &pb2.Repeats{},
+		want:  "\n",
+	}, {
+		desc: "repeated fields set to empty slices",
+		input: &pb2.Repeats{
+			RptBool:   []bool{},
+			RptInt32:  []int32{},
+			RptInt64:  []int64{},
+			RptUint32: []uint32{},
+			RptUint64: []uint64{},
+			RptFloat:  []float32{},
+			RptDouble: []float64{},
+			RptBytes:  [][]byte{},
+		},
+		want: "\n",
+	}, {
+		desc: "repeated fields set to some values",
+		input: &pb2.Repeats{
+			RptBool:   []bool{true, false, true, true},
+			RptInt32:  []int32{1, 6, 0, 0},
+			RptInt64:  []int64{-64, 47},
+			RptUint32: []uint32{0xff, 0xffff},
+			RptUint64: []uint64{0xdeadbeef},
+			RptFloat:  []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034},
+			RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
+			RptString: []string{"hello", "世界"},
+			RptBytes: [][]byte{
+				[]byte("hello"),
+				[]byte("\xe4\xb8\x96\xe7\x95\x8c"),
+			},
+		},
+		want: `rpt_bool: true
+rpt_bool: false
+rpt_bool: true
+rpt_bool: true
+rpt_int32: 1
+rpt_int32: 6
+rpt_int32: 0
+rpt_int32: 0
+rpt_int64: -64
+rpt_int64: 47
+rpt_uint32: 255
+rpt_uint32: 65535
+rpt_uint64: 3735928559
+rpt_float: nan
+rpt_float: inf
+rpt_float: -inf
+rpt_float: 1.034
+rpt_double: nan
+rpt_double: inf
+rpt_double: -inf
+rpt_double: 1.23e-308
+rpt_string: "hello"
+rpt_string: "世界"
+rpt_bytes: "hello"
+rpt_bytes: "世界"
+`,
+	}, {
+		desc: "repeated contains invalid UTF-8",
+		input: &pb2.Repeats{
+			RptString: []string{"abc\xff"},
+		},
+		want: `rpt_string: "abc\xff"
+`,
+		wantErr: true,
+	}, {
+		desc: "repeated enums",
+		input: &pb2.Enums{
+			RptEnum:       []pb2.Enum{pb2.Enum_ONE, 2, pb2.Enum_TEN, 42},
+			RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
+		},
+		want: `rpt_enum: ONE
+rpt_enum: TWO
+rpt_enum: TEN
+rpt_enum: 42
+rpt_nested_enum: DOS
+rpt_nested_enum: 47
+rpt_nested_enum: DIEZ
+`,
+	}, {
+		desc: "repeated messages set to empty",
+		input: &pb2.Nests{
+			RptNested: []*pb2.Nested{},
+			Rptgroup:  []*pb2.Nests_RptGroup{},
+		},
+		want: "\n",
+	}, {
+		desc: "repeated messages",
+		input: &pb2.Nests{
+			RptNested: []*pb2.Nested{
+				{
+					OptString: scalar.String("repeat nested one"),
+				},
+				{
+					OptString: scalar.String("repeat nested two"),
+					OptNested: &pb2.Nested{
+						OptString: scalar.String("inside repeat nested two"),
+					},
+				},
+				{},
+			},
+		},
+		want: `rpt_nested: {
+  opt_string: "repeat nested one"
+}
+rpt_nested: {
+  opt_string: "repeat nested two"
+  opt_nested: {
+    opt_string: "inside repeat nested two"
+  }
+}
+rpt_nested: {}
+`,
+	}, {
+		desc: "repeated messages contains nil value",
+		input: &pb2.Nests{
+			RptNested: []*pb2.Nested{nil, {}},
+		},
+		want: `rpt_nested: {}
+rpt_nested: {}
+`,
+	}, {
+		desc: "repeated groups",
+		input: &pb2.Nests{
+			Rptgroup: []*pb2.Nests_RptGroup{
+				{
+					RptString: []string{"hello", "world"},
+				},
+				{},
+				nil,
+			},
+		},
+		want: `RptGroup: {
+  rpt_string: "hello"
+  rpt_string: "world"
+}
+RptGroup: {}
+RptGroup: {}
+`,
+	}, {
+		desc:  "map fields not set",
+		input: &pb3.Maps{},
+		want:  "\n",
+	}, {
+		desc: "map fields set to empty",
+		input: &pb3.Maps{
+			Int32ToStr:   map[int32]string{},
+			BoolToUint32: map[bool]uint32{},
+			Uint64ToEnum: map[uint64]pb3.Enum{},
+			StrToNested:  map[string]*pb3.Nested{},
+			StrToOneofs:  map[string]*pb3.Oneofs{},
+		},
+		want: "\n",
+	}, {
+		desc: "map fields 1",
+		input: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				-101: "-101",
+				0xff: "0xff",
+				0:    "zero",
+			},
+			BoolToUint32: map[bool]uint32{
+				true:  42,
+				false: 101,
+			},
+		},
+		want: `int32_to_str: {
+  key: -101
+  value: "-101"
+}
+int32_to_str: {
+  key: 0
+  value: "zero"
+}
+int32_to_str: {
+  key: 255
+  value: "0xff"
+}
+bool_to_uint32: {
+  key: false
+  value: 101
+}
+bool_to_uint32: {
+  key: true
+  value: 42
+}
+`,
+	}, {
+		desc: "map fields 2",
+		input: &pb3.Maps{
+			Uint64ToEnum: map[uint64]pb3.Enum{
+				1:  pb3.Enum_ONE,
+				2:  pb3.Enum_TWO,
+				10: pb3.Enum_TEN,
+				47: 47,
+			},
+		},
+		want: `uint64_to_enum: {
+  key: 1
+  value: ONE
+}
+uint64_to_enum: {
+  key: 2
+  value: TWO
+}
+uint64_to_enum: {
+  key: 10
+  value: TEN
+}
+uint64_to_enum: {
+  key: 47
+  value: 47
+}
+`,
+	}, {
+		desc: "map fields 3",
+		input: &pb3.Maps{
+			StrToNested: map[string]*pb3.Nested{
+				"nested": &pb3.Nested{
+					SString: "nested in a map",
+				},
+			},
+		},
+		want: `str_to_nested: {
+  key: "nested"
+  value: {
+    s_string: "nested in a map"
+  }
+}
+`,
+	}, {
+		desc: "map fields 4",
+		input: &pb3.Maps{
+			StrToOneofs: map[string]*pb3.Oneofs{
+				"string": &pb3.Oneofs{
+					Union: &pb3.Oneofs_OneofString{
+						OneofString: "hello",
+					},
+				},
+				"nested": &pb3.Oneofs{
+					Union: &pb3.Oneofs_OneofNested{
+						OneofNested: &pb3.Nested{
+							SString: "nested oneof in map field value",
+						},
+					},
+				},
+			},
+		},
+		want: `str_to_oneofs: {
+  key: "nested"
+  value: {
+    oneof_nested: {
+      s_string: "nested oneof in map field value"
+    }
+  }
+}
+str_to_oneofs: {
+  key: "string"
+  value: {
+    oneof_string: "hello"
+  }
+}
+`,
+	}, {
+		desc: "map field value contains invalid UTF-8",
+		input: &pb3.Maps{
+			Int32ToStr: map[int32]string{
+				101: "abc\xff",
+			},
+		},
+		want: `int32_to_str: {
+  key: 101
+  value: "abc\xff"
+}
+`,
+		wantErr: true,
+	}, {
+		desc: "map field key contains invalid UTF-8",
+		input: &pb3.Maps{
+			StrToNested: map[string]*pb3.Nested{
+				"abc\xff": {},
+			},
+		},
+		want: `str_to_nested: {
+  key: "abc\xff"
+  value: {}
+}
+`,
+		wantErr: true,
+	}, {
+		desc: "map field contains nil value",
+		input: &pb3.Maps{
+			StrToNested: map[string]*pb3.Nested{
+				"nil": nil,
+			},
+		},
+		want: `str_to_nested: {
+  key: "nil"
+  value: {}
+}
+`,
+	}, {
+		desc:    "required fields not set",
+		input:   &pb2.Requireds{},
+		want:    "\n",
+		wantErr: true,
+	}, {
+		desc: "required fields partially set",
+		input: &pb2.Requireds{
+			ReqBool:     scalar.Bool(false),
+			ReqSfixed64: scalar.Int64(0xbeefcafe),
+			ReqDouble:   scalar.Float64(math.NaN()),
+			ReqString:   scalar.String("hello"),
+			ReqEnum:     pb2.Enum_ONE.Enum(),
+		},
+		want: `req_bool: false
+req_sfixed64: 3203386110
+req_double: nan
+req_string: "hello"
+req_enum: ONE
+`,
+		wantErr: true,
+	}, {
+		desc: "required fields not set with AllowPartial",
+		mo:   prototext.MarshalOptions{AllowPartial: true},
+		input: &pb2.Requireds{
+			ReqBool:     scalar.Bool(false),
+			ReqSfixed64: scalar.Int64(0xbeefcafe),
+			ReqDouble:   scalar.Float64(math.NaN()),
+			ReqString:   scalar.String("hello"),
+			ReqEnum:     pb2.Enum_ONE.Enum(),
+		},
+		want: `req_bool: false
+req_sfixed64: 3203386110
+req_double: nan
+req_string: "hello"
+req_enum: ONE
+`,
+	}, {
+		desc: "required fields all set",
+		input: &pb2.Requireds{
+			ReqBool:     scalar.Bool(false),
+			ReqSfixed64: scalar.Int64(0),
+			ReqDouble:   scalar.Float64(1.23),
+			ReqString:   scalar.String(""),
+			ReqEnum:     pb2.Enum_ONE.Enum(),
+			ReqNested:   &pb2.Nested{},
+		},
+		want: `req_bool: false
+req_sfixed64: 0
+req_double: 1.23
+req_string: ""
+req_enum: ONE
+req_nested: {}
+`,
+	}, {
+		desc: "indirect required field",
+		input: &pb2.IndirectRequired{
+			OptNested: &pb2.NestedWithRequired{},
+		},
+		want:    "opt_nested: {}\n",
+		wantErr: true,
+	}, {
+		desc: "indirect required field with AllowPartial",
+		mo:   prototext.MarshalOptions{AllowPartial: true},
+		input: &pb2.IndirectRequired{
+			OptNested: &pb2.NestedWithRequired{},
+		},
+		want: "opt_nested: {}\n",
+	}, {
+		desc: "indirect required field in empty repeated",
+		input: &pb2.IndirectRequired{
+			RptNested: []*pb2.NestedWithRequired{},
+		},
+		want: "\n",
+	}, {
+		desc: "indirect required field in repeated",
+		input: &pb2.IndirectRequired{
+			RptNested: []*pb2.NestedWithRequired{
+				&pb2.NestedWithRequired{},
+			},
+		},
+		want:    "rpt_nested: {}\n",
+		wantErr: true,
+	}, {
+		desc: "indirect required field in repeated with AllowPartial",
+		mo:   prototext.MarshalOptions{AllowPartial: true},
+		input: &pb2.IndirectRequired{
+			RptNested: []*pb2.NestedWithRequired{
+				&pb2.NestedWithRequired{},
+			},
+		},
+		want: "rpt_nested: {}\n",
+	}, {
+		desc: "indirect required field in empty map",
+		input: &pb2.IndirectRequired{
+			StrToNested: map[string]*pb2.NestedWithRequired{},
+		},
+		want: "\n",
+	}, {
+		desc: "indirect required field in map",
+		input: &pb2.IndirectRequired{
+			StrToNested: map[string]*pb2.NestedWithRequired{
+				"fail": &pb2.NestedWithRequired{},
+			},
+		},
+		want: `str_to_nested: {
+  key: "fail"
+  value: {}
+}
+`,
+		wantErr: true,
+	}, {
+		desc: "indirect required field in map with AllowPartial",
+		mo:   prototext.MarshalOptions{AllowPartial: true},
+		input: &pb2.IndirectRequired{
+			StrToNested: map[string]*pb2.NestedWithRequired{
+				"fail": &pb2.NestedWithRequired{},
+			},
+		},
+		want: `str_to_nested: {
+  key: "fail"
+  value: {}
+}
+`,
+	}, {
+		desc: "indirect required field in oneof",
+		input: &pb2.IndirectRequired{
+			Union: &pb2.IndirectRequired_OneofNested{
+				OneofNested: &pb2.NestedWithRequired{},
+			},
+		},
+		want:    "oneof_nested: {}\n",
+		wantErr: true,
+	}, {
+		desc: "indirect required field in oneof with AllowPartial",
+		mo:   prototext.MarshalOptions{AllowPartial: true},
+		input: &pb2.IndirectRequired{
+			Union: &pb2.IndirectRequired_OneofNested{
+				OneofNested: &pb2.NestedWithRequired{},
+			},
+		},
+		want: "oneof_nested: {}\n",
+	}, {
+		desc: "unknown varint and fixed types",
+		input: &pb2.Scalars{
+			OptString: scalar.String("this message contains unknown fields"),
+			XXX_unrecognized: pack.Message{
+				pack.Tag{101, pack.VarintType}, pack.Bool(true),
+				pack.Tag{102, pack.VarintType}, pack.Varint(0xff),
+				pack.Tag{103, pack.Fixed32Type}, pack.Uint32(47),
+				pack.Tag{104, pack.Fixed64Type}, pack.Int64(0xdeadbeef),
+			}.Marshal(),
+		},
+		want: `opt_string: "this message contains unknown fields"
+101: 1
+102: 255
+103: 47
+104: 3735928559
+`,
+	}, {
+		desc: "unknown length-delimited",
+		input: &pb2.Scalars{
+			XXX_unrecognized: pack.Message{
+				pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false)},
+				pack.Tag{102, pack.BytesType}, pack.String("hello world"),
+				pack.Tag{103, pack.BytesType}, pack.Bytes("\xe4\xb8\x96\xe7\x95\x8c"),
+			}.Marshal(),
+		},
+		want: `101: "\x01\x00"
+102: "hello world"
+103: "世界"
+`,
+	}, {
+		desc: "unknown group type",
+		input: &pb2.Scalars{
+			XXX_unrecognized: pack.Message{
+				pack.Tag{101, pack.StartGroupType}, pack.Tag{101, pack.EndGroupType},
+				pack.Tag{102, pack.StartGroupType},
+				pack.Tag{101, pack.VarintType}, pack.Bool(false),
+				pack.Tag{102, pack.BytesType}, pack.String("inside a group"),
+				pack.Tag{102, pack.EndGroupType},
+			}.Marshal(),
+		},
+		want: `101: {}
+102: {
+  101: 0
+  102: "inside a group"
+}
+`,
+	}, {
+		desc: "unknown unpack repeated field",
+		input: &pb2.Scalars{
+			XXX_unrecognized: pack.Message{
+				pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Bool(true)},
+				pack.Tag{102, pack.BytesType}, pack.String("hello"),
+				pack.Tag{101, pack.VarintType}, pack.Bool(true),
+				pack.Tag{102, pack.BytesType}, pack.String("世界"),
+			}.Marshal(),
+		},
+		want: `101: "\x01\x00\x01"
+101: 1
+102: "hello"
+102: "世界"
+`,
+	}, {
+		desc: "extensions of non-repeated fields",
+		input: func() proto.Message {
+			m := &pb2.Extensions{
+				OptString: scalar.String("non-extension field"),
+				OptBool:   scalar.Bool(true),
+				OptInt32:  scalar.Int32(42),
+			}
+			setExtension(m, pb2.E_OptExtBool, true)
+			setExtension(m, pb2.E_OptExtString, "extension field")
+			setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
+			setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
+				OptString: scalar.String("nested in an extension"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("another nested in an extension"),
+				},
+			})
+			return m
+		}(),
+		want: `opt_string: "non-extension field"
+opt_bool: true
+opt_int32: 42
+[pb2.opt_ext_bool]: true
+[pb2.opt_ext_enum]: TEN
+[pb2.opt_ext_nested]: {
+  opt_string: "nested in an extension"
+  opt_nested: {
+    opt_string: "another nested in an extension"
+  }
+}
+[pb2.opt_ext_string]: "extension field"
+`,
+	}, {
+		desc: "extension field contains invalid UTF-8",
+		input: func() proto.Message {
+			m := &pb2.Extensions{}
+			setExtension(m, pb2.E_OptExtString, "abc\xff")
+			return m
+		}(),
+		want: `[pb2.opt_ext_string]: "abc\xff"
+`,
+		wantErr: true,
+	}, {
+		desc: "extension partial returns error",
+		input: func() proto.Message {
+			m := &pb2.Extensions{}
+			setExtension(m, pb2.E_OptExtPartial, &pb2.PartialRequired{
+				OptString: scalar.String("partial1"),
+			})
+			setExtension(m, pb2.E_ExtensionsContainer_OptExtPartial, &pb2.PartialRequired{
+				OptString: scalar.String("partial2"),
+			})
+			return m
+		}(),
+		want: `[pb2.ExtensionsContainer.opt_ext_partial]: {
+  opt_string: "partial2"
+}
+[pb2.opt_ext_partial]: {
+  opt_string: "partial1"
+}
+`,
+		wantErr: true,
+	}, {
+		desc: "extension partial with AllowPartial",
+		mo:   prototext.MarshalOptions{AllowPartial: true},
+		input: func() proto.Message {
+			m := &pb2.Extensions{}
+			setExtension(m, pb2.E_OptExtPartial, &pb2.PartialRequired{
+				OptString: scalar.String("partial1"),
+			})
+			return m
+		}(),
+		want: `[pb2.opt_ext_partial]: {
+  opt_string: "partial1"
+}
+`,
+	}, {
+		desc: "extension message field set to nil",
+		input: func() proto.Message {
+			m := &pb2.Extensions{}
+			setExtension(m, pb2.E_OptExtNested, nil)
+			return m
+		}(),
+		want: "\n",
+	}, {
+		desc: "extensions of repeated fields",
+		input: func() proto.Message {
+			m := &pb2.Extensions{}
+			setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
+			setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
+			setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
+				&pb2.Nested{OptString: scalar.String("one")},
+				&pb2.Nested{OptString: scalar.String("two")},
+				&pb2.Nested{OptString: scalar.String("three")},
+			})
+			return m
+		}(),
+		want: `[pb2.rpt_ext_enum]: TEN
+[pb2.rpt_ext_enum]: 101
+[pb2.rpt_ext_enum]: ONE
+[pb2.rpt_ext_fixed32]: 42
+[pb2.rpt_ext_fixed32]: 47
+[pb2.rpt_ext_nested]: {
+  opt_string: "one"
+}
+[pb2.rpt_ext_nested]: {
+  opt_string: "two"
+}
+[pb2.rpt_ext_nested]: {
+  opt_string: "three"
+}
+`,
+	}, {
+		desc: "extensions of non-repeated fields in another message",
+		input: func() proto.Message {
+			m := &pb2.Extensions{}
+			setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
+			setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
+			setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
+			setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
+				OptString: scalar.String("nested in an extension"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("another nested in an extension"),
+				},
+			})
+			return m
+		}(),
+		want: `[pb2.ExtensionsContainer.opt_ext_bool]: true
+[pb2.ExtensionsContainer.opt_ext_enum]: TEN
+[pb2.ExtensionsContainer.opt_ext_nested]: {
+  opt_string: "nested in an extension"
+  opt_nested: {
+    opt_string: "another nested in an extension"
+  }
+}
+[pb2.ExtensionsContainer.opt_ext_string]: "extension field"
+`,
+	}, {
+		desc: "extensions of repeated fields in another message",
+		input: func() proto.Message {
+			m := &pb2.Extensions{
+				OptString: scalar.String("non-extension field"),
+				OptBool:   scalar.Bool(true),
+				OptInt32:  scalar.Int32(42),
+			}
+			setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
+			setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
+			setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
+				&pb2.Nested{OptString: scalar.String("one")},
+				&pb2.Nested{OptString: scalar.String("two")},
+				&pb2.Nested{OptString: scalar.String("three")},
+			})
+			return m
+		}(),
+		want: `opt_string: "non-extension field"
+opt_bool: true
+opt_int32: 42
+[pb2.ExtensionsContainer.rpt_ext_enum]: TEN
+[pb2.ExtensionsContainer.rpt_ext_enum]: 101
+[pb2.ExtensionsContainer.rpt_ext_enum]: ONE
+[pb2.ExtensionsContainer.rpt_ext_nested]: {
+  opt_string: "one"
+}
+[pb2.ExtensionsContainer.rpt_ext_nested]: {
+  opt_string: "two"
+}
+[pb2.ExtensionsContainer.rpt_ext_nested]: {
+  opt_string: "three"
+}
+[pb2.ExtensionsContainer.rpt_ext_string]: "hello"
+[pb2.ExtensionsContainer.rpt_ext_string]: "world"
+`,
+	}, {
+		desc: "MessageSet",
+		input: func() proto.Message {
+			m := &pb2.MessageSet{}
+			setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
+				OptString: scalar.String("a messageset extension"),
+			})
+			setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
+				OptString: scalar.String("not a messageset extension"),
+			})
+			setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
+				OptString: scalar.String("just a regular extension"),
+			})
+			return m
+		}(),
+		want: `[pb2.MessageSetExtension]: {
+  opt_string: "a messageset extension"
+}
+[pb2.MessageSetExtension.ext_nested]: {
+  opt_string: "just a regular extension"
+}
+[pb2.MessageSetExtension.not_message_set_extension]: {
+  opt_string: "not a messageset extension"
+}
+`,
+	}, {
+		desc: "not real MessageSet 1",
+		input: func() proto.Message {
+			m := &pb2.FakeMessageSet{}
+			setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
+				OptString: scalar.String("not a messageset extension"),
+			})
+			return m
+		}(),
+		want: `[pb2.FakeMessageSetExtension.message_set_extension]: {
+  opt_string: "not a messageset extension"
+}
+`,
+	}, {
+		desc: "not real MessageSet 2",
+		input: func() proto.Message {
+			m := &pb2.MessageSet{}
+			setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
+				OptString: scalar.String("another not a messageset extension"),
+			})
+			return m
+		}(),
+		want: `[pb2.message_set_extension]: {
+  opt_string: "another not a messageset extension"
+}
+`,
+	}, {
+		desc: "Any not expanded",
+		mo: prototext.MarshalOptions{
+			Resolver: preg.NewTypes(),
+		},
+		input: func() proto.Message {
+			m := &pb2.Nested{
+				OptString: scalar.String("embedded inside Any"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("inception"),
+				},
+			}
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &knownpb.Any{
+				TypeUrl: "pb2.Nested",
+				Value:   b,
+			}
+		}(),
+		want: `type_url: "pb2.Nested"
+value: "\n\x13embedded inside Any\x12\x0b\n\tinception"
+`,
+	}, {
+		desc: "Any expanded",
+		mo: prototext.MarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
+		},
+		input: func() proto.Message {
+			m := &pb2.Nested{
+				OptString: scalar.String("embedded inside Any"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("inception"),
+				},
+			}
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &knownpb.Any{
+				TypeUrl: "foo/pb2.Nested",
+				Value:   b,
+			}
+		}(),
+		want: `[foo/pb2.Nested]: {
+  opt_string: "embedded inside Any"
+  opt_nested: {
+    opt_string: "inception"
+  }
+}
+`,
+	}, {
+		desc: "Any expanded with missing required error",
+		mo: prototext.MarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
+		},
+		input: func() proto.Message {
+			m := &pb2.PartialRequired{
+				OptString: scalar.String("embedded inside Any"),
+			}
+			b, err := proto.MarshalOptions{
+				AllowPartial:  true,
+				Deterministic: true,
+			}.Marshal(m)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &knownpb.Any{
+				TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
+				Value:   b,
+			}
+		}(),
+		want: `[pb2.PartialRequired]: {
+  opt_string: "embedded inside Any"
+}
+`,
+		wantErr: true,
+	}, {
+		desc: "Any with invalid UTF-8",
+		mo: prototext.MarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb3.Nested{})),
+		},
+		input: func() proto.Message {
+			m := &pb3.Nested{
+				SString: "abcd",
+			}
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &knownpb.Any{
+				TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
+				Value:   bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
+			}
+		}(),
+		want: `[pb3.Nested]: {
+  s_string: "abc\xff"
+}
+`,
+		wantErr: true,
+	}, {
+		desc: "Any with invalid value",
+		mo: prototext.MarshalOptions{
+			Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
+		},
+		input: &knownpb.Any{
+			TypeUrl: "foo/pb2.Nested",
+			Value:   dhex("80"),
+		},
+		want: `type_url: "foo/pb2.Nested"
+value: "\x80"
+`,
+	}}
+
+	for _, tt := range tests {
+		tt := tt
+		t.Run(tt.desc, func(t *testing.T) {
+			// Use 2-space indentation on all MarshalOptions.
+			tt.mo.Indent = "  "
+			b, err := tt.mo.Marshal(tt.input)
+			if err != nil && !tt.wantErr {
+				t.Errorf("Marshal() returned error: %v\n", err)
+			}
+			if err == nil && tt.wantErr {
+				t.Error("Marshal() got nil error, want error\n")
+			}
+			got := string(b)
+			if tt.want != "" && got != tt.want {
+				t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
+				if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
+					t.Errorf("Marshal() diff -want +got\n%v\n", diff)
+				}
+			}
+		})
+	}
+}
diff --git a/encoding/prototext/other_test.go b/encoding/prototext/other_test.go
new file mode 100644
index 0000000..4819f7d
--- /dev/null
+++ b/encoding/prototext/other_test.go
@@ -0,0 +1,239 @@
+package prototext_test
+
+import (
+	"testing"
+
+	protoV1 "github.com/golang/protobuf/proto"
+	"google.golang.org/protobuf/encoding/prototext"
+	"google.golang.org/protobuf/internal/impl"
+	pimpl "google.golang.org/protobuf/internal/impl"
+	"google.golang.org/protobuf/internal/scalar"
+	"google.golang.org/protobuf/proto"
+	preg "google.golang.org/protobuf/reflect/protoregistry"
+
+	"google.golang.org/protobuf/encoding/testprotos/pb2"
+	knownpb "google.golang.org/protobuf/types/known"
+)
+
+func TestRoundTrip(t *testing.T) {
+	tests := []struct {
+		desc     string
+		resolver *preg.Types
+		message  proto.Message
+	}{{
+		desc: "well-known type fields set to empty messages",
+		message: &pb2.KnownTypes{
+			OptBool:      &knownpb.BoolValue{},
+			OptInt32:     &knownpb.Int32Value{},
+			OptInt64:     &knownpb.Int64Value{},
+			OptUint32:    &knownpb.UInt32Value{},
+			OptUint64:    &knownpb.UInt64Value{},
+			OptFloat:     &knownpb.FloatValue{},
+			OptDouble:    &knownpb.DoubleValue{},
+			OptString:    &knownpb.StringValue{},
+			OptBytes:     &knownpb.BytesValue{},
+			OptDuration:  &knownpb.Duration{},
+			OptTimestamp: &knownpb.Timestamp{},
+			OptStruct:    &knownpb.Struct{},
+			OptList:      &knownpb.ListValue{},
+			OptValue:     &knownpb.Value{},
+			OptEmpty:     &knownpb.Empty{},
+			OptAny:       &knownpb.Any{},
+		},
+	}, {
+		desc: "well-known type scalar fields",
+		message: &pb2.KnownTypes{
+			OptBool: &knownpb.BoolValue{
+				Value: true,
+			},
+			OptInt32: &knownpb.Int32Value{
+				Value: -42,
+			},
+			OptInt64: &knownpb.Int64Value{
+				Value: -42,
+			},
+			OptUint32: &knownpb.UInt32Value{
+				Value: 0xff,
+			},
+			OptUint64: &knownpb.UInt64Value{
+				Value: 0xffff,
+			},
+			OptFloat: &knownpb.FloatValue{
+				Value: 1.234,
+			},
+			OptDouble: &knownpb.DoubleValue{
+				Value: 1.23e308,
+			},
+			OptString: &knownpb.StringValue{
+				Value: "谷歌",
+			},
+			OptBytes: &knownpb.BytesValue{
+				Value: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
+			},
+		},
+	}, {
+		desc: "well-known type time-related fields",
+		message: &pb2.KnownTypes{
+			OptDuration: &knownpb.Duration{
+				Seconds: -3600,
+				Nanos:   -123,
+			},
+			OptTimestamp: &knownpb.Timestamp{
+				Seconds: 1257894000,
+				Nanos:   123,
+			},
+		},
+	}, {
+		desc: "Struct field and different Value types",
+		message: &pb2.KnownTypes{
+			OptStruct: &knownpb.Struct{
+				Fields: map[string]*knownpb.Value{
+					"bool": &knownpb.Value{
+						Kind: &knownpb.Value_BoolValue{
+							BoolValue: true,
+						},
+					},
+					"double": &knownpb.Value{
+						Kind: &knownpb.Value_NumberValue{
+							NumberValue: 3.1415,
+						},
+					},
+					"null": &knownpb.Value{
+						Kind: &knownpb.Value_NullValue{
+							NullValue: knownpb.NullValue_NULL_VALUE,
+						},
+					},
+					"string": &knownpb.Value{
+						Kind: &knownpb.Value_StringValue{
+							StringValue: "string",
+						},
+					},
+					"struct": &knownpb.Value{
+						Kind: &knownpb.Value_StructValue{
+							StructValue: &knownpb.Struct{
+								Fields: map[string]*knownpb.Value{
+									"bool": &knownpb.Value{
+										Kind: &knownpb.Value_BoolValue{
+											BoolValue: false,
+										},
+									},
+								},
+							},
+						},
+					},
+					"list": &knownpb.Value{
+						Kind: &knownpb.Value_ListValue{
+							ListValue: &knownpb.ListValue{
+								Values: []*knownpb.Value{
+									{
+										Kind: &knownpb.Value_BoolValue{
+											BoolValue: false,
+										},
+									},
+									{
+										Kind: &knownpb.Value_StringValue{
+											StringValue: "hello",
+										},
+									},
+								},
+							},
+						},
+					},
+				},
+			},
+		},
+	}, {
+		desc:     "Any field without registered type",
+		resolver: preg.NewTypes(),
+		message: func() proto.Message {
+			m := &pb2.Nested{
+				OptString: scalar.String("embedded inside Any"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("inception"),
+				},
+			}
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &pb2.KnownTypes{
+				OptAny: &knownpb.Any{
+					TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
+					Value:   b,
+				},
+			}
+		}(),
+	}, {
+		desc:     "Any field with registered type",
+		resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
+		message: func() *pb2.KnownTypes {
+			m := &pb2.Nested{
+				OptString: scalar.String("embedded inside Any"),
+				OptNested: &pb2.Nested{
+					OptString: scalar.String("inception"),
+				},
+			}
+			b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &pb2.KnownTypes{
+				OptAny: &knownpb.Any{
+					TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
+					Value:   b,
+				},
+			}
+		}(),
+	}, {
+		desc: "Any field containing Any message",
+		resolver: func() *preg.Types {
+			mt1 := impl.Export{}.MessageTypeOf(&pb2.Nested{})
+			mt2 := impl.Export{}.MessageTypeOf(&knownpb.Any{})
+			return preg.NewTypes(mt1, mt2)
+		}(),
+		message: func() *pb2.KnownTypes {
+			m1 := &pb2.Nested{
+				OptString: scalar.String("message inside Any of another Any field"),
+			}
+			b1, err := proto.MarshalOptions{Deterministic: true}.Marshal(m1)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			m2 := &knownpb.Any{
+				TypeUrl: "pb2.Nested",
+				Value:   b1,
+			}
+			b2, err := proto.MarshalOptions{Deterministic: true}.Marshal(m2)
+			if err != nil {
+				t.Fatalf("error in binary marshaling message for Any.value: %v", err)
+			}
+			return &pb2.KnownTypes{
+				OptAny: &knownpb.Any{
+					TypeUrl: "google.protobuf.Any",
+					Value:   b2,
+				},
+			}
+		}(),
+	}}
+
+	for _, tt := range tests {
+		tt := tt
+		t.Run(tt.desc, func(t *testing.T) {
+			t.Parallel()
+			b, err := prototext.MarshalOptions{Resolver: tt.resolver}.Marshal(tt.message)
+			if err != nil {
+				t.Errorf("Marshal() returned error: %v\n\n", err)
+			}
+
+			gotMessage := new(pb2.KnownTypes)
+			err = prototext.UnmarshalOptions{Resolver: tt.resolver}.Unmarshal(gotMessage, b)
+			if err != nil {
+				t.Errorf("Unmarshal() returned error: %v\n\n", err)
+			}
+
+			if !protoV1.Equal(gotMessage, tt.message.(protoV1.Message)) {
+				t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", gotMessage, tt.message)
+			}
+		})
+	}
+}