mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-30 03:32:49 +00:00
9b3d97c473
* Fixes golang/protobuf#842. Unmarshal can now parse singular or repeated message fields without the field separator. * Fixes golang/protobuf#1011. Handles negative 0 properly. * For unknown fields with fixed 32-bit and 64-bit wire types, output is now in hex format with 0x prefix similar to C++ lib output. Previous Go implementation simply outputs these as decimal numbers %d. * All parsing errors, except for unexpected EOF should now contain line and column number info. * Fixed following conformance-related features: * Parse nan,inf,-inf,infinity,-infinity as case-insensitive. * Interpret float32 overflows as inf or -inf. * Parse large int-like number as proto float. * Discard unknown map field if DiscardUnknown=true. * Allow whitespaces/comments in Any type URL and extension field names per spec. * Improves performance and memory usage. It is now as fast and efficient as protojson, if not better on most benchmarks. name old time/op new time/op delta Text/Unmarshal/google_message1_proto2-4 14.1µs ±43% 8.7µs ±12% -38.27% (p=0.000 n=10+10) Text/Unmarshal/google_message1_proto3-4 11.6µs ±18% 7.7µs ± 9% -33.69% (p=0.000 n=10+10) Text/Unmarshal/google_message2-4 6.20ms ±27% 4.10ms ± 5% -33.95% (p=0.000 n=10+10) Text/Marshal/google_message1_proto2-4 12.8µs ± 6% 10.3µs ±23% -19.54% (p=0.000 n=9+10) Text/Marshal/google_message1_proto3-4 11.9µs ±16% 8.6µs ±10% -27.45% (p=0.000 n=10+10) Text/Marshal/google_message2-4 5.59ms ± 5% 5.30ms ±22% ~ (p=0.356 n=9+10) JSON/Unmarshal/google_message1_proto2-4 12.3µs ±61% 13.9µs ±26% ~ (p=0.190 n=10+10) JSON/Unmarshal/google_message1_proto3-4 7.51µs ± 6% 7.86µs ± 1% +4.66% (p=0.010 n=10+9) JSON/Unmarshal/google_message2-4 3.74ms ± 2% 3.94ms ± 2% +5.32% (p=0.000 n=10+10) JSON/Marshal/google_message1_proto2-4 9.90µs ±12% 9.95µs ± 4% ~ (p=0.315 n=9+10) JSON/Marshal/google_message1_proto3-4 7.55µs ± 4% 7.93µs ± 3% +4.98% (p=0.000 n=10+10) JSON/Marshal/google_message2-4 4.29ms ± 5% 4.49ms ± 2% +4.53% (p=0.001 n=10+10) name old alloc/op new alloc/op delta Text/Unmarshal/google_message1_proto2-4 12.5kB ± 0% 2.0kB ± 0% -83.87% (p=0.000 n=10+10) Text/Unmarshal/google_message1_proto3-4 12.2kB ± 0% 1.8kB ± 0% -85.33% (p=0.000 n=10+10) Text/Unmarshal/google_message2-4 5.35MB ± 0% 0.89MB ± 0% -83.28% (p=0.000 n=10+9) Text/Marshal/google_message1_proto2-4 12.0kB ± 0% 1.4kB ± 0% -88.15% (p=0.000 n=10+10) Text/Marshal/google_message1_proto3-4 12.4kB ± 0% 1.9kB ± 0% -84.91% (p=0.000 n=10+10) Text/Marshal/google_message2-4 5.64MB ± 0% 1.02MB ± 0% -81.85% (p=0.000 n=10+9) JSON/Unmarshal/google_message1_proto2-4 2.29kB ± 0% 2.29kB ± 0% ~ (all equal) JSON/Unmarshal/google_message1_proto3-4 2.08kB ± 0% 2.08kB ± 0% ~ (all equal) JSON/Unmarshal/google_message2-4 899kB ± 0% 899kB ± 0% ~ (p=1.000 n=10+10) JSON/Marshal/google_message1_proto2-4 1.46kB ± 0% 1.46kB ± 0% ~ (all equal) JSON/Marshal/google_message1_proto3-4 1.36kB ± 0% 1.36kB ± 0% ~ (all equal) JSON/Marshal/google_message2-4 1.19MB ± 0% 1.19MB ± 0% ~ (p=0.197 n=10+10) name old allocs/op new allocs/op delta Text/Unmarshal/google_message1_proto2-4 133 ± 0% 89 ± 0% -33.08% (p=0.000 n=10+10) Text/Unmarshal/google_message1_proto3-4 108 ± 0% 67 ± 0% -37.96% (p=0.000 n=10+10) Text/Unmarshal/google_message2-4 60.0k ± 0% 38.7k ± 0% -35.52% (p=0.000 n=10+10) Text/Marshal/google_message1_proto2-4 65.0 ± 0% 25.0 ± 0% -61.54% (p=0.000 n=10+10) Text/Marshal/google_message1_proto3-4 59.0 ± 0% 22.0 ± 0% -62.71% (p=0.000 n=10+10) Text/Marshal/google_message2-4 27.4k ± 0% 7.3k ± 0% -73.39% (p=0.000 n=10+10) JSON/Unmarshal/google_message1_proto2-4 95.0 ± 0% 95.0 ± 0% ~ (all equal) JSON/Unmarshal/google_message1_proto3-4 74.0 ± 0% 74.0 ± 0% ~ (all equal) JSON/Unmarshal/google_message2-4 36.3k ± 0% 36.3k ± 0% ~ (all equal) JSON/Marshal/google_message1_proto2-4 27.0 ± 0% 27.0 ± 0% ~ (all equal) JSON/Marshal/google_message1_proto3-4 30.0 ± 0% 30.0 ± 0% ~ (all equal) JSON/Marshal/google_message2-4 11.3k ± 0% 11.3k ± 0% ~ (p=1.000 n=10+10) Change-Id: I377925facde5535f06333b6f25e9c9b358dc062f Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/204602 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
411 lines
11 KiB
Go
411 lines
11 KiB
Go
// 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"
|
|
"strconv"
|
|
"unicode/utf8"
|
|
|
|
"google.golang.org/protobuf/internal/encoding/messageset"
|
|
"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/flags"
|
|
"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"
|
|
)
|
|
|
|
const defaultIndent = " "
|
|
|
|
// Format formats the message as a multiline string.
|
|
// This function is only intended for human consumption and ignores errors.
|
|
// Do not depend on the output being stable. It may change over time across
|
|
// different versions of the program.
|
|
func Format(m proto.Message) string {
|
|
return MarshalOptions{Multiline: true}.Format(m)
|
|
}
|
|
|
|
// Marshal writes the given proto.Message in textproto format using default
|
|
// options. Do not depend on the output being stable. It may change over time
|
|
// across different versions of the program.
|
|
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
|
|
|
|
// EmitUnknown specifies whether to emit unknown fields in the output.
|
|
// If specified, the unmarshaler may be unable to parse the output.
|
|
// The default is to exclude unknown fields.
|
|
EmitUnknown bool
|
|
|
|
// Multiline specifies whether the marshaler should format the output in
|
|
// indented-form with every textual element on a new line.
|
|
// If Indent is an empty string, then an arbitrary indent is chosen.
|
|
Multiline bool
|
|
|
|
// Indent specifies the set of indentation characters to use in a multiline
|
|
// formatted output such that every entry is preceded by Indent and
|
|
// terminated by a newline. If non-empty, then Multiline is treated as true.
|
|
// Indent can only be composed of space or tab characters.
|
|
Indent string
|
|
|
|
// Resolver is used for looking up types when expanding google.protobuf.Any
|
|
// messages. If nil, this defaults to using protoregistry.GlobalTypes.
|
|
Resolver interface {
|
|
protoregistry.ExtensionTypeResolver
|
|
protoregistry.MessageTypeResolver
|
|
}
|
|
}
|
|
|
|
// Format formats the message as a string.
|
|
// This method is only intended for human consumption and ignores errors.
|
|
// Do not depend on the output being stable. It may change over time across
|
|
// different versions of the program.
|
|
func (o MarshalOptions) Format(m proto.Message) string {
|
|
if m == nil || !m.ProtoReflect().IsValid() {
|
|
return "<nil>" // invalid syntax, but okay since this is for debugging
|
|
}
|
|
o.AllowPartial = true
|
|
o.EmitUnknown = true
|
|
b, _ := o.Marshal(m)
|
|
return string(b)
|
|
}
|
|
|
|
// Marshal writes the given proto.Message in textproto format using options in
|
|
// MarshalOptions object. Do not depend on the output being stable. It may
|
|
// change over time across different versions of the program.
|
|
func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
|
|
const outputASCII = false
|
|
var delims = [2]byte{'{', '}'}
|
|
|
|
if o.Multiline && o.Indent == "" {
|
|
o.Indent = defaultIndent
|
|
}
|
|
if o.Resolver == nil {
|
|
o.Resolver = protoregistry.GlobalTypes
|
|
}
|
|
|
|
internalEnc, err := text.NewEncoder(o.Indent, delims, outputASCII)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
enc := encoder{internalEnc, o}
|
|
err = enc.marshalMessage(m.ProtoReflect(), false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := enc.Bytes()
|
|
if len(o.Indent) > 0 && len(out) > 0 {
|
|
out = append(out, '\n')
|
|
}
|
|
if o.AllowPartial {
|
|
return out, nil
|
|
}
|
|
return out, proto.IsInitialized(m)
|
|
}
|
|
|
|
type encoder struct {
|
|
*text.Encoder
|
|
opts MarshalOptions
|
|
}
|
|
|
|
// marshalMessage marshals the given protoreflect.Message.
|
|
func (e encoder) marshalMessage(m pref.Message, inclDelims bool) error {
|
|
messageDesc := m.Descriptor()
|
|
if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
|
|
return errors.New("no support for proto1 MessageSets")
|
|
}
|
|
|
|
if inclDelims {
|
|
e.StartMessage()
|
|
defer e.EndMessage()
|
|
}
|
|
|
|
// Handle Any expansion.
|
|
if messageDesc.FullName() == "google.protobuf.Any" {
|
|
if e.marshalAny(m) {
|
|
return nil
|
|
}
|
|
// If unable to expand, continue on to marshal Any as a regular message.
|
|
}
|
|
|
|
// Marshal known fields.
|
|
fieldDescs := messageDesc.Fields()
|
|
size := fieldDescs.Len()
|
|
for i := 0; i < size; {
|
|
fd := fieldDescs.Get(i)
|
|
if od := fd.ContainingOneof(); od != nil {
|
|
fd = m.WhichOneof(od)
|
|
i += od.Fields().Len()
|
|
} else {
|
|
i++
|
|
}
|
|
|
|
if fd == nil || !m.Has(fd) {
|
|
continue
|
|
}
|
|
|
|
name := fd.Name()
|
|
// Use type name for group field name.
|
|
if fd.Kind() == pref.GroupKind {
|
|
name = fd.Message().Name()
|
|
}
|
|
val := m.Get(fd)
|
|
if err := e.marshalField(string(name), val, fd); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Marshal extensions.
|
|
if err := e.marshalExtensions(m); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Marshal unknown fields.
|
|
if e.opts.EmitUnknown {
|
|
e.marshalUnknown(m.GetUnknown())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// marshalField marshals the given field with protoreflect.Value.
|
|
func (e encoder) marshalField(name string, val pref.Value, fd pref.FieldDescriptor) error {
|
|
switch {
|
|
case fd.IsList():
|
|
return e.marshalList(name, val.List(), fd)
|
|
case fd.IsMap():
|
|
return e.marshalMap(name, val.Map(), fd)
|
|
default:
|
|
e.WriteName(name)
|
|
return e.marshalSingular(val, fd)
|
|
}
|
|
}
|
|
|
|
// marshalSingular marshals the given non-repeated field value. This includes
|
|
// all scalar types, enums, messages, and groups.
|
|
func (e encoder) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error {
|
|
kind := fd.Kind()
|
|
switch kind {
|
|
case pref.BoolKind:
|
|
e.WriteBool(val.Bool())
|
|
|
|
case pref.StringKind:
|
|
s := val.String()
|
|
if !utf8.ValidString(s) {
|
|
return errors.InvalidUTF8(string(fd.FullName()))
|
|
}
|
|
e.WriteString(s)
|
|
|
|
case pref.Int32Kind, pref.Int64Kind,
|
|
pref.Sint32Kind, pref.Sint64Kind,
|
|
pref.Sfixed32Kind, pref.Sfixed64Kind:
|
|
e.WriteInt(val.Int())
|
|
|
|
case pref.Uint32Kind, pref.Uint64Kind,
|
|
pref.Fixed32Kind, pref.Fixed64Kind:
|
|
e.WriteUint(val.Uint())
|
|
|
|
case pref.FloatKind:
|
|
// Encoder.WriteFloat handles the special numbers NaN and infinites.
|
|
e.WriteFloat(val.Float(), 32)
|
|
|
|
case pref.DoubleKind:
|
|
// Encoder.WriteFloat handles the special numbers NaN and infinites.
|
|
e.WriteFloat(val.Float(), 64)
|
|
|
|
case pref.BytesKind:
|
|
e.WriteString(string(val.Bytes()))
|
|
|
|
case pref.EnumKind:
|
|
num := val.Enum()
|
|
if desc := fd.Enum().Values().ByNumber(num); desc != nil {
|
|
e.WriteLiteral(string(desc.Name()))
|
|
} else {
|
|
// Use numeric value if there is no enum description.
|
|
e.WriteInt(int64(num))
|
|
}
|
|
|
|
case pref.MessageKind, pref.GroupKind:
|
|
return e.marshalMessage(val.Message(), true)
|
|
|
|
default:
|
|
panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// marshalList marshals the given protoreflect.List as multiple name-value fields.
|
|
func (e encoder) marshalList(name string, list pref.List, fd pref.FieldDescriptor) error {
|
|
size := list.Len()
|
|
for i := 0; i < size; i++ {
|
|
e.WriteName(name)
|
|
if err := e.marshalSingular(list.Get(i), fd); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// marshalMap marshals the given protoreflect.Map as multiple name-value fields.
|
|
func (e encoder) marshalMap(name string, mmap pref.Map, fd pref.FieldDescriptor) error {
|
|
var err error
|
|
mapsort.Range(mmap, fd.MapKey().Kind(), func(key pref.MapKey, val pref.Value) bool {
|
|
e.WriteName(name)
|
|
e.StartMessage()
|
|
defer e.EndMessage()
|
|
|
|
e.WriteName("key")
|
|
err = e.marshalSingular(key.Value(), fd.MapKey())
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
e.WriteName("value")
|
|
err = e.marshalSingular(val, fd.MapValue())
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
})
|
|
return err
|
|
}
|
|
|
|
// marshalExtensions marshals extension fields.
|
|
func (e encoder) marshalExtensions(m pref.Message) error {
|
|
type entry struct {
|
|
key string
|
|
value pref.Value
|
|
desc pref.FieldDescriptor
|
|
}
|
|
|
|
// Get a sorted list based on field key first.
|
|
var entries []entry
|
|
m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
|
|
if !fd.IsExtension() {
|
|
return true
|
|
}
|
|
// For MessageSet extensions, the name used is the parent message.
|
|
name := fd.FullName()
|
|
if messageset.IsMessageSetExtension(fd) {
|
|
name = name.Parent()
|
|
}
|
|
entries = append(entries, entry{
|
|
key: string(name),
|
|
value: v,
|
|
desc: fd,
|
|
})
|
|
return true
|
|
})
|
|
// Sort extensions lexicographically.
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
return entries[i].key < entries[j].key
|
|
})
|
|
|
|
// Write out sorted list.
|
|
for _, entry := range entries {
|
|
// Extension field name is the proto field name enclosed in [].
|
|
name := "[" + entry.key + "]"
|
|
if err := e.marshalField(name, entry.value, entry.desc); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// marshalUnknown parses the given []byte and marshals fields out.
|
|
// This function assumes proper encoding in the given []byte.
|
|
func (e encoder) marshalUnknown(b []byte) {
|
|
const dec = 10
|
|
const hex = 16
|
|
for len(b) > 0 {
|
|
num, wtype, n := wire.ConsumeTag(b)
|
|
b = b[n:]
|
|
e.WriteName(strconv.FormatInt(int64(num), dec))
|
|
|
|
switch wtype {
|
|
case wire.VarintType:
|
|
var v uint64
|
|
v, n = wire.ConsumeVarint(b)
|
|
e.WriteUint(v)
|
|
case wire.Fixed32Type:
|
|
var v uint32
|
|
v, n = wire.ConsumeFixed32(b)
|
|
e.WriteLiteral("0x" + strconv.FormatUint(uint64(v), hex))
|
|
case wire.Fixed64Type:
|
|
var v uint64
|
|
v, n = wire.ConsumeFixed64(b)
|
|
e.WriteLiteral("0x" + strconv.FormatUint(v, hex))
|
|
case wire.BytesType:
|
|
var v []byte
|
|
v, n = wire.ConsumeBytes(b)
|
|
e.WriteString(string(v))
|
|
case wire.StartGroupType:
|
|
e.StartMessage()
|
|
var v []byte
|
|
v, n = wire.ConsumeGroup(num, b)
|
|
e.marshalUnknown(v)
|
|
e.EndMessage()
|
|
default:
|
|
panic(fmt.Sprintf("prototext: error parsing unknown field wire type: %v", wtype))
|
|
}
|
|
|
|
b = b[n:]
|
|
}
|
|
}
|
|
|
|
// marshalAny marshals the given google.protobuf.Any message in expanded form.
|
|
// It returns true if it was able to marshal, else false.
|
|
func (e encoder) marshalAny(any pref.Message) bool {
|
|
// Construct the embedded message.
|
|
fds := any.Descriptor().Fields()
|
|
fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
|
|
typeURL := any.Get(fdType).String()
|
|
mt, err := e.opts.Resolver.FindMessageByURL(typeURL)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
m := mt.New().Interface()
|
|
|
|
// Unmarshal bytes into embedded message.
|
|
fdValue := fds.ByNumber(fieldnum.Any_Value)
|
|
value := any.Get(fdValue)
|
|
err = proto.UnmarshalOptions{
|
|
AllowPartial: true,
|
|
Resolver: e.opts.Resolver,
|
|
}.Unmarshal(value.Bytes(), m)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
// Get current encoder position. If marshaling fails, reset encoder output
|
|
// back to this position.
|
|
pos := e.Snapshot()
|
|
|
|
// Field name is the proto field name enclosed in [].
|
|
e.WriteName("[" + typeURL + "]")
|
|
err = e.marshalMessage(m.ProtoReflect(), true)
|
|
if err != nil {
|
|
e.Reset(pos)
|
|
return false
|
|
}
|
|
return true
|
|
}
|