2018-12-06 15:28:53 -08:00
|
|
|
// 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.
|
|
|
|
|
2019-05-14 12:44:37 -07:00
|
|
|
package prototext
|
2018-12-06 15:28:53 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-01-14 15:32:26 -08:00
|
|
|
"strings"
|
2019-04-08 17:32:44 -07:00
|
|
|
"unicode/utf8"
|
2018-12-06 15:28:53 -08:00
|
|
|
|
2019-07-11 18:23:08 -07:00
|
|
|
"google.golang.org/protobuf/internal/encoding/messageset"
|
2019-05-13 23:55:40 -07:00
|
|
|
"google.golang.org/protobuf/internal/encoding/text"
|
|
|
|
"google.golang.org/protobuf/internal/errors"
|
|
|
|
"google.golang.org/protobuf/internal/fieldnum"
|
2019-07-11 18:23:08 -07:00
|
|
|
"google.golang.org/protobuf/internal/flags"
|
2019-05-13 23:55:40 -07:00
|
|
|
"google.golang.org/protobuf/internal/pragma"
|
|
|
|
"google.golang.org/protobuf/internal/set"
|
encoding/prototext: adjust handling of invalid UTF-8
The following changes are made:
* Permit invalid UTF-8 in proto2. This goes against specified behavior,
but matches functional behavior in wire marshaling (not just for Go,
but also in the other major language implementations as well).
* The Format function is specified as ignoring errors since its intended
purpose is to surface information to the human user even if it's not
exactly parsible back into a message. As such, add an unexported
allowInvalidUTF8 option that is specially used by Format.
* Add an EmitASCII option that forces the formatting of
strings and bytes to always be encoded as ASCII.
This ensures that the entire output is always ASCII as well.
Note that we do not replicate this behavior for protojson since:
* The JSON format fundamentally has a stricter and well-specified
grammar for exactly what is valid/invalid, while the text format
has not had a well-specified grammar for the longest time,
leading to all sorts of weird usages due to Hyrum's law.
* This is to ease migration from the legacy implementation,
which did permit invalid UTF-8 in proto2.
* The EmitASCII option relies on the ability to always escape
Unicode characters using ASCII escape sequences, but this is not
possible in JSON since the grammar only has an escape sequence defined
for Unicode characters \u0000 to \uffff, inclusive.
However, Unicode v12.0.0 defines characters up to \U0010FFFF,
which is beyond what the JSON grammar provides escape sequences for.
Change-Id: I2b524a904e9ec59f9ed5500e299613bc27c31a14
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/233077
Reviewed-by: Herbie Ong <herbie@google.com>
2020-05-07 15:10:54 -07:00
|
|
|
"google.golang.org/protobuf/internal/strs"
|
2019-05-13 23:55:40 -07:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
"google.golang.org/protobuf/reflect/protoregistry"
|
2018-12-06 15:28:53 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Unmarshal reads the given []byte into the given proto.Message.
|
2019-05-14 16:05:06 -07:00
|
|
|
func Unmarshal(b []byte, m proto.Message) error {
|
|
|
|
return UnmarshalOptions{}.Unmarshal(b, m)
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-03-26 16:26:22 -07:00
|
|
|
// UnmarshalOptions is a configurable textproto format unmarshaler.
|
2018-12-06 15:28:53 -08:00
|
|
|
type UnmarshalOptions struct {
|
|
|
|
pragma.NoUnkeyedLiterals
|
2018-12-18 18:04:31 -08:00
|
|
|
|
2019-03-26 16:26:22 -07:00
|
|
|
// 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
|
|
|
|
|
2019-09-15 00:17:39 -07:00
|
|
|
// DiscardUnknown specifies whether to ignore unknown fields when parsing.
|
|
|
|
// An unknown field is any field whose field name or field number does not
|
|
|
|
// resolve to any known or extension field in the message.
|
|
|
|
// By default, unmarshal rejects unknown fields as an error.
|
|
|
|
DiscardUnknown bool
|
|
|
|
|
2019-05-14 14:28:19 -07:00
|
|
|
// Resolver is used for looking up types when unmarshaling
|
|
|
|
// google.protobuf.Any messages or extension fields.
|
|
|
|
// If nil, this defaults to using protoregistry.GlobalTypes.
|
|
|
|
Resolver interface {
|
|
|
|
protoregistry.MessageTypeResolver
|
|
|
|
protoregistry.ExtensionTypeResolver
|
|
|
|
}
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal reads the given []byte and populates the given proto.Message using options in
|
|
|
|
// UnmarshalOptions object.
|
2019-05-14 16:05:06 -07:00
|
|
|
func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
|
2019-04-25 23:48:08 -07:00
|
|
|
proto.Reset(m)
|
2018-12-06 15:28:53 -08:00
|
|
|
|
2018-12-18 18:04:31 -08:00
|
|
|
if o.Resolver == nil {
|
|
|
|
o.Resolver = protoregistry.GlobalTypes
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
|
|
|
dec := decoder{text.NewDecoder(b), o}
|
|
|
|
if err := dec.unmarshalMessage(m.ProtoReflect(), false); err != nil {
|
2018-12-06 15:28:53 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-06-19 09:28:29 -07:00
|
|
|
if o.AllowPartial {
|
|
|
|
return nil
|
2019-04-05 13:31:40 -07:00
|
|
|
}
|
2020-02-20 10:05:37 -08:00
|
|
|
return proto.CheckInitialized(m)
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
type decoder struct {
|
|
|
|
*text.Decoder
|
|
|
|
opts UnmarshalOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// newError returns an error object with position info.
|
|
|
|
func (d decoder) newError(pos int, f string, x ...interface{}) error {
|
|
|
|
line, column := d.Position(pos)
|
|
|
|
head := fmt.Sprintf("(line %d:%d): ", line, column)
|
|
|
|
return errors.New(head+f, x...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unexpectedTokenError returns a syntax error for the given unexpected token.
|
|
|
|
func (d decoder) unexpectedTokenError(tok text.Token) error {
|
|
|
|
return d.syntaxError(tok.Pos(), "unexpected token: %s", tok.RawString())
|
|
|
|
}
|
|
|
|
|
|
|
|
// syntaxError returns a syntax error for given position.
|
|
|
|
func (d decoder) syntaxError(pos int, f string, x ...interface{}) error {
|
|
|
|
line, column := d.Position(pos)
|
|
|
|
head := fmt.Sprintf("syntax error (line %d:%d): ", line, column)
|
|
|
|
return errors.New(head+f, x...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshalMessage unmarshals into the given protoreflect.Message.
|
|
|
|
func (d decoder) unmarshalMessage(m pref.Message, checkDelims bool) error {
|
2019-05-01 12:29:25 -07:00
|
|
|
messageDesc := m.Descriptor()
|
2019-08-08 13:31:59 -07:00
|
|
|
if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
|
2019-07-11 18:23:08 -07:00
|
|
|
return errors.New("no support for proto1 MessageSets")
|
|
|
|
}
|
2019-01-04 14:08:41 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
if messageDesc.FullName() == "google.protobuf.Any" {
|
|
|
|
return d.unmarshalAny(m, checkDelims)
|
|
|
|
}
|
|
|
|
|
|
|
|
if checkDelims {
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tok.Kind() != text.MessageOpen {
|
|
|
|
return d.unexpectedTokenError(tok)
|
|
|
|
}
|
2019-01-04 14:08:41 -08:00
|
|
|
}
|
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
var seenNums set.Ints
|
2019-04-02 20:19:36 -07:00
|
|
|
var seenOneofs set.Ints
|
2019-07-11 18:23:08 -07:00
|
|
|
fieldDescs := messageDesc.Fields()
|
2019-10-31 17:10:15 -07:00
|
|
|
|
|
|
|
for {
|
|
|
|
// Read field name.
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch typ := tok.Kind(); typ {
|
|
|
|
case text.Name:
|
|
|
|
// Continue below.
|
|
|
|
case text.EOF:
|
|
|
|
if checkDelims {
|
2020-02-06 18:43:12 -08:00
|
|
|
return text.ErrUnexpectedEOF
|
2019-10-31 17:10:15 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
if checkDelims && typ == text.MessageClose {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return d.unexpectedTokenError(tok)
|
|
|
|
}
|
2018-12-06 15:28:53 -08:00
|
|
|
|
2019-09-15 00:17:39 -07:00
|
|
|
// Resolve the field descriptor.
|
2018-12-18 18:04:31 -08:00
|
|
|
var name pref.Name
|
2019-09-15 00:17:39 -07:00
|
|
|
var fd pref.FieldDescriptor
|
|
|
|
var xt pref.ExtensionType
|
|
|
|
var xtErr error
|
2019-10-31 17:10:15 -07:00
|
|
|
var isFieldNumberName bool
|
|
|
|
|
|
|
|
switch tok.NameKind() {
|
|
|
|
case text.IdentName:
|
|
|
|
name = pref.Name(tok.IdentName())
|
2018-12-06 15:28:53 -08:00
|
|
|
fd = fieldDescs.ByName(name)
|
2019-09-15 00:17:39 -07:00
|
|
|
if fd == nil {
|
2019-07-09 22:38:15 -07:00
|
|
|
// The proto name of a group field is in all lowercase,
|
|
|
|
// while the textproto field name is the group message name.
|
2019-06-24 19:21:46 -07:00
|
|
|
gd := fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
|
|
|
|
if gd != nil && gd.Kind() == pref.GroupKind && gd.Message().Name() == name {
|
|
|
|
fd = gd
|
|
|
|
}
|
2019-09-15 00:17:39 -07:00
|
|
|
} else if fd.Kind() == pref.GroupKind && fd.Message().Name() != name {
|
2019-07-09 22:38:15 -07:00
|
|
|
fd = nil // reset since field name is actually the message name
|
2019-01-14 15:32:26 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
|
|
|
case text.TypeName:
|
2019-01-04 14:08:41 -08:00
|
|
|
// Handle extensions only. This code path is not for Any.
|
2019-10-31 17:10:15 -07:00
|
|
|
xt, xtErr = d.findExtension(pref.FullName(tok.TypeName()))
|
|
|
|
|
|
|
|
case text.FieldNumber:
|
|
|
|
isFieldNumberName = true
|
|
|
|
num := pref.FieldNumber(tok.FieldNumber())
|
2019-09-15 00:17:39 -07:00
|
|
|
if !num.IsValid() {
|
2019-10-31 17:10:15 -07:00
|
|
|
return d.newError(tok.Pos(), "invalid field number: %d", num)
|
2019-05-01 12:29:25 -07:00
|
|
|
}
|
2019-09-15 00:17:39 -07:00
|
|
|
fd = fieldDescs.ByNumber(num)
|
|
|
|
if fd == nil {
|
2019-10-31 17:10:15 -07:00
|
|
|
xt, xtErr = d.opts.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
|
2019-08-02 16:58:08 -07:00
|
|
|
}
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2019-09-15 00:17:39 -07:00
|
|
|
if xt != nil {
|
|
|
|
fd = xt.TypeDescriptor()
|
|
|
|
if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() {
|
2019-10-31 17:10:15 -07:00
|
|
|
return d.newError(tok.Pos(), "message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName())
|
2019-09-15 00:17:39 -07:00
|
|
|
}
|
|
|
|
} else if xtErr != nil && xtErr != protoregistry.NotFound {
|
2019-10-31 17:10:15 -07:00
|
|
|
return d.newError(tok.Pos(), "unable to resolve [%s]: %v", tok.RawString(), xtErr)
|
2019-09-15 00:17:39 -07:00
|
|
|
}
|
2019-08-10 13:56:36 -07:00
|
|
|
if flags.ProtoLegacy {
|
|
|
|
if fd != nil && fd.IsWeak() && fd.Message().IsPlaceholder() {
|
|
|
|
fd = nil // reset since the weak reference is not linked in
|
|
|
|
}
|
2019-09-15 00:17:39 -07:00
|
|
|
}
|
2018-12-18 18:04:31 -08:00
|
|
|
|
2019-09-15 00:17:39 -07:00
|
|
|
// Handle unknown fields.
|
2018-12-06 15:28:53 -08:00
|
|
|
if fd == nil {
|
2019-10-31 17:10:15 -07:00
|
|
|
if d.opts.DiscardUnknown || messageDesc.ReservedNames().Has(name) {
|
|
|
|
d.skipValue()
|
2018-12-13 14:41:22 -08:00
|
|
|
continue
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
return d.newError(tok.Pos(), "unknown field: %v", tok.RawString())
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-09-15 00:17:39 -07:00
|
|
|
// Handle fields identified by field number.
|
2019-10-31 17:10:15 -07:00
|
|
|
if isFieldNumberName {
|
2019-09-15 00:17:39 -07:00
|
|
|
// TODO: Add an option to permit parsing field numbers.
|
|
|
|
//
|
|
|
|
// This requires careful thought as the MarshalOptions.EmitUnknown
|
2019-10-31 17:10:15 -07:00
|
|
|
// option allows formatting unknown fields as the field number and the
|
|
|
|
// best-effort textual representation of the field value. In that case,
|
|
|
|
// it may not be possible to unmarshal the value from a parser that does
|
|
|
|
// have information about the unknown field.
|
|
|
|
return d.newError(tok.Pos(), "cannot specify field by number: %v", tok.RawString())
|
2019-09-15 00:17:39 -07:00
|
|
|
}
|
|
|
|
|
2019-05-13 14:32:56 -07:00
|
|
|
switch {
|
|
|
|
case fd.IsList():
|
2019-10-31 17:10:15 -07:00
|
|
|
kind := fd.Kind()
|
|
|
|
if kind != pref.MessageKind && kind != pref.GroupKind && !tok.HasSeparator() {
|
|
|
|
return d.syntaxError(tok.Pos(), "missing field separator :")
|
2019-05-13 14:32:56 -07:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
list := m.Mutable(fd).List()
|
2019-10-31 17:10:15 -07:00
|
|
|
if err := d.unmarshalList(fd, list); err != nil {
|
2019-05-13 14:32:56 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
case fd.IsMap():
|
2019-04-25 23:48:08 -07:00
|
|
|
mmap := m.Mutable(fd).Map()
|
2019-10-31 17:10:15 -07:00
|
|
|
if err := d.unmarshalMap(fd, mmap); err != nil {
|
2018-12-06 15:28:53 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2019-05-13 14:32:56 -07:00
|
|
|
default:
|
2019-10-31 17:10:15 -07:00
|
|
|
kind := fd.Kind()
|
|
|
|
if kind != pref.MessageKind && kind != pref.GroupKind && !tok.HasSeparator() {
|
|
|
|
return d.syntaxError(tok.Pos(), "missing field separator :")
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:19:36 -07:00
|
|
|
// If field is a oneof, check if it has already been set.
|
2019-05-13 14:32:56 -07:00
|
|
|
if od := fd.ContainingOneof(); od != nil {
|
2019-04-02 20:19:36 -07:00
|
|
|
idx := uint64(od.Index())
|
|
|
|
if seenOneofs.Has(idx) {
|
2019-10-31 17:10:15 -07:00
|
|
|
return d.newError(tok.Pos(), "error parsing %q, oneof %v is already set", tok.RawString(), od.FullName())
|
2019-04-02 20:19:36 -07:00
|
|
|
}
|
|
|
|
seenOneofs.Set(idx)
|
|
|
|
}
|
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
num := uint64(fd.Number())
|
|
|
|
if seenNums.Has(num) {
|
2019-10-31 17:10:15 -07:00
|
|
|
return d.newError(tok.Pos(), "non-repeated field %q is repeated", tok.RawString())
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
|
|
|
if err := d.unmarshalSingular(fd, m); err != nil {
|
2018-12-06 15:28:53 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
seenNums.Set(num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 09:28:29 -07:00
|
|
|
return nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:56:57 -08:00
|
|
|
// findExtension returns protoreflect.ExtensionType from the Resolver if found.
|
2019-10-31 17:10:15 -07:00
|
|
|
func (d decoder) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
|
|
|
|
xt, err := d.opts.Resolver.FindExtensionByName(xtName)
|
2019-01-07 18:56:57 -08:00
|
|
|
if err == nil {
|
|
|
|
return xt, nil
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
return messageset.FindMessageSetExtension(d.opts.Resolver, xtName)
|
2019-01-07 18:56:57 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// unmarshalSingular unmarshals a non-repeated field value specified by the
|
|
|
|
// given FieldDescriptor.
|
|
|
|
func (d decoder) unmarshalSingular(fd pref.FieldDescriptor, m pref.Message) error {
|
2018-12-06 15:28:53 -08:00
|
|
|
var val pref.Value
|
2019-10-31 17:10:15 -07:00
|
|
|
var err error
|
2018-12-06 15:28:53 -08:00
|
|
|
switch fd.Kind() {
|
|
|
|
case pref.MessageKind, pref.GroupKind:
|
2019-09-04 10:46:00 -07:00
|
|
|
val = m.NewField(fd)
|
2019-10-31 17:10:15 -07:00
|
|
|
err = d.unmarshalMessage(val.Message(), true)
|
2018-12-06 15:28:53 -08:00
|
|
|
default:
|
2019-10-31 17:10:15 -07:00
|
|
|
val, err = d.unmarshalScalar(fd)
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
if err == nil {
|
|
|
|
m.Set(fd, val)
|
|
|
|
}
|
|
|
|
return err
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// unmarshalScalar unmarshals a scalar/enum protoreflect.Value specified by the
|
|
|
|
// given FieldDescriptor.
|
|
|
|
func (d decoder) unmarshalScalar(fd pref.FieldDescriptor) (pref.Value, error) {
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return pref.Value{}, err
|
|
|
|
}
|
2018-12-06 15:28:53 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
if tok.Kind() != text.Scalar {
|
|
|
|
return pref.Value{}, d.unexpectedTokenError(tok)
|
|
|
|
}
|
|
|
|
|
|
|
|
kind := fd.Kind()
|
|
|
|
switch kind {
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.BoolKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if b, ok := tok.Bool(); ok {
|
|
|
|
return pref.ValueOfBool(b), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if n, ok := tok.Int32(); ok {
|
|
|
|
return pref.ValueOfInt32(n), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if n, ok := tok.Int64(); ok {
|
|
|
|
return pref.ValueOfInt64(n), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.Uint32Kind, pref.Fixed32Kind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if n, ok := tok.Uint32(); ok {
|
|
|
|
return pref.ValueOfUint32(n), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.Uint64Kind, pref.Fixed64Kind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if n, ok := tok.Uint64(); ok {
|
|
|
|
return pref.ValueOfUint64(n), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.FloatKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if n, ok := tok.Float32(); ok {
|
|
|
|
return pref.ValueOfFloat32(n), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.DoubleKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if n, ok := tok.Float64(); ok {
|
|
|
|
return pref.ValueOfFloat64(n), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.StringKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if s, ok := tok.String(); ok {
|
encoding/prototext: adjust handling of invalid UTF-8
The following changes are made:
* Permit invalid UTF-8 in proto2. This goes against specified behavior,
but matches functional behavior in wire marshaling (not just for Go,
but also in the other major language implementations as well).
* The Format function is specified as ignoring errors since its intended
purpose is to surface information to the human user even if it's not
exactly parsible back into a message. As such, add an unexported
allowInvalidUTF8 option that is specially used by Format.
* Add an EmitASCII option that forces the formatting of
strings and bytes to always be encoded as ASCII.
This ensures that the entire output is always ASCII as well.
Note that we do not replicate this behavior for protojson since:
* The JSON format fundamentally has a stricter and well-specified
grammar for exactly what is valid/invalid, while the text format
has not had a well-specified grammar for the longest time,
leading to all sorts of weird usages due to Hyrum's law.
* This is to ease migration from the legacy implementation,
which did permit invalid UTF-8 in proto2.
* The EmitASCII option relies on the ability to always escape
Unicode characters using ASCII escape sequences, but this is not
possible in JSON since the grammar only has an escape sequence defined
for Unicode characters \u0000 to \uffff, inclusive.
However, Unicode v12.0.0 defines characters up to \U0010FFFF,
which is beyond what the JSON grammar provides escape sequences for.
Change-Id: I2b524a904e9ec59f9ed5500e299613bc27c31a14
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/233077
Reviewed-by: Herbie Ong <herbie@google.com>
2020-05-07 15:10:54 -07:00
|
|
|
if strs.EnforceUTF8(fd) && !utf8.ValidString(s) {
|
|
|
|
return pref.Value{}, d.newError(tok.Pos(), "contains invalid UTF-8")
|
2019-04-08 17:32:44 -07:00
|
|
|
}
|
encoding/prototext: adjust handling of invalid UTF-8
The following changes are made:
* Permit invalid UTF-8 in proto2. This goes against specified behavior,
but matches functional behavior in wire marshaling (not just for Go,
but also in the other major language implementations as well).
* The Format function is specified as ignoring errors since its intended
purpose is to surface information to the human user even if it's not
exactly parsible back into a message. As such, add an unexported
allowInvalidUTF8 option that is specially used by Format.
* Add an EmitASCII option that forces the formatting of
strings and bytes to always be encoded as ASCII.
This ensures that the entire output is always ASCII as well.
Note that we do not replicate this behavior for protojson since:
* The JSON format fundamentally has a stricter and well-specified
grammar for exactly what is valid/invalid, while the text format
has not had a well-specified grammar for the longest time,
leading to all sorts of weird usages due to Hyrum's law.
* This is to ease migration from the legacy implementation,
which did permit invalid UTF-8 in proto2.
* The EmitASCII option relies on the ability to always escape
Unicode characters using ASCII escape sequences, but this is not
possible in JSON since the grammar only has an escape sequence defined
for Unicode characters \u0000 to \uffff, inclusive.
However, Unicode v12.0.0 defines characters up to \U0010FFFF,
which is beyond what the JSON grammar provides escape sequences for.
Change-Id: I2b524a904e9ec59f9ed5500e299613bc27c31a14
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/233077
Reviewed-by: Herbie Ong <herbie@google.com>
2020-05-07 15:10:54 -07:00
|
|
|
return pref.ValueOfString(s), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.BytesKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if b, ok := tok.String(); ok {
|
|
|
|
return pref.ValueOfBytes([]byte(b)), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.EnumKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
if lit, ok := tok.Enum(); ok {
|
2019-01-04 14:08:41 -08:00
|
|
|
// Lookup EnumNumber based on name.
|
2019-10-31 17:10:15 -07:00
|
|
|
if enumVal := fd.Enum().Values().ByName(pref.Name(lit)); enumVal != nil {
|
2019-09-17 13:38:48 -07:00
|
|
|
return pref.ValueOfEnum(enumVal.Number()), nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
if num, ok := tok.Int32(); ok {
|
|
|
|
return pref.ValueOfEnum(pref.EnumNumber(num)), nil
|
|
|
|
}
|
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid scalar kind %v", kind))
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
return pref.Value{}, d.newError(tok.Pos(), "invalid value for %v type: %v", kind, tok.RawString())
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// unmarshalList unmarshals into given protoreflect.List. A list value can
|
|
|
|
// either be in [] syntax or simply just a single scalar/message value.
|
|
|
|
func (d decoder) unmarshalList(fd pref.FieldDescriptor, list pref.List) error {
|
|
|
|
tok, err := d.Peek()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
switch fd.Kind() {
|
|
|
|
case pref.MessageKind, pref.GroupKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
switch tok.Kind() {
|
|
|
|
case text.ListOpen:
|
|
|
|
d.Read()
|
|
|
|
for {
|
|
|
|
tok, err := d.Peek()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tok.Kind() {
|
|
|
|
case text.ListClose:
|
|
|
|
d.Read()
|
|
|
|
return nil
|
|
|
|
case text.MessageOpen:
|
|
|
|
pval := list.NewElement()
|
|
|
|
if err := d.unmarshalMessage(pval.Message(), true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
list.Append(pval)
|
|
|
|
default:
|
|
|
|
return d.unexpectedTokenError(tok)
|
|
|
|
}
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
|
|
|
case text.MessageOpen:
|
|
|
|
pval := list.NewElement()
|
|
|
|
if err := d.unmarshalMessage(pval.Message(), true); err != nil {
|
2018-12-06 15:28:53 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
list.Append(pval)
|
|
|
|
return nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2018-12-06 15:28:53 -08:00
|
|
|
default:
|
2019-10-31 17:10:15 -07:00
|
|
|
switch tok.Kind() {
|
|
|
|
case text.ListOpen:
|
|
|
|
d.Read()
|
|
|
|
for {
|
|
|
|
tok, err := d.Peek()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tok.Kind() {
|
|
|
|
case text.ListClose:
|
|
|
|
d.Read()
|
|
|
|
return nil
|
|
|
|
case text.Scalar:
|
|
|
|
pval, err := d.unmarshalScalar(fd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
list.Append(pval)
|
|
|
|
default:
|
|
|
|
return d.unexpectedTokenError(tok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case text.Scalar:
|
|
|
|
pval, err := d.unmarshalScalar(fd)
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2018-12-06 15:28:53 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
list.Append(pval)
|
|
|
|
return nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
return d.unexpectedTokenError(tok)
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// unmarshalMap unmarshals into given protoreflect.Map. A map value is a
|
|
|
|
// textproto message containing {key: <kvalue>, value: <mvalue>}.
|
|
|
|
func (d decoder) unmarshalMap(fd pref.FieldDescriptor, mmap pref.Map) error {
|
|
|
|
// Determine ahead whether map entry is a scalar type or a message type in
|
|
|
|
// order to call the appropriate unmarshalMapValue func inside
|
|
|
|
// unmarshalMapEntry.
|
|
|
|
var unmarshalMapValue func() (pref.Value, error)
|
2019-05-13 14:32:56 -07:00
|
|
|
switch fd.MapValue().Kind() {
|
2018-12-06 15:28:53 -08:00
|
|
|
case pref.MessageKind, pref.GroupKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
unmarshalMapValue = func() (pref.Value, error) {
|
|
|
|
pval := mmap.NewValue()
|
|
|
|
if err := d.unmarshalMessage(pval.Message(), true); err != nil {
|
|
|
|
return pref.Value{}, err
|
|
|
|
}
|
|
|
|
return pval, nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
default:
|
|
|
|
unmarshalMapValue = func() (pref.Value, error) {
|
|
|
|
return d.unmarshalScalar(fd.MapValue())
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch tok.Kind() {
|
|
|
|
case text.MessageOpen:
|
|
|
|
return d.unmarshalMapEntry(fd, mmap, unmarshalMapValue)
|
2018-12-06 15:28:53 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
case text.ListOpen:
|
|
|
|
for {
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch tok.Kind() {
|
|
|
|
case text.ListClose:
|
|
|
|
return nil
|
|
|
|
case text.MessageOpen:
|
|
|
|
if err := d.unmarshalMapEntry(fd, mmap, unmarshalMapValue); err != nil {
|
|
|
|
return err
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
default:
|
2019-10-31 17:10:15 -07:00
|
|
|
return d.unexpectedTokenError(tok)
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
return d.unexpectedTokenError(tok)
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// unmarshalMap unmarshals into given protoreflect.Map. A map value is a
|
|
|
|
// textproto message containing {key: <kvalue>, value: <mvalue>}.
|
|
|
|
func (d decoder) unmarshalMapEntry(fd pref.FieldDescriptor, mmap pref.Map, unmarshalMapValue func() (pref.Value, error)) error {
|
|
|
|
var key pref.MapKey
|
|
|
|
var pval pref.Value
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
// Read field name.
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch tok.Kind() {
|
|
|
|
case text.Name:
|
|
|
|
if tok.NameKind() != text.IdentName {
|
|
|
|
if !d.opts.DiscardUnknown {
|
|
|
|
return d.newError(tok.Pos(), "unknown map entry field %q", tok.RawString())
|
|
|
|
}
|
|
|
|
d.skipValue()
|
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
// Continue below.
|
|
|
|
case text.MessageClose:
|
|
|
|
break Loop
|
|
|
|
default:
|
|
|
|
return d.unexpectedTokenError(tok)
|
|
|
|
}
|
2018-12-06 15:28:53 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
name := tok.IdentName()
|
|
|
|
switch name {
|
|
|
|
case "key":
|
|
|
|
if !tok.HasSeparator() {
|
|
|
|
return d.syntaxError(tok.Pos(), "missing field separator :")
|
|
|
|
}
|
|
|
|
if key.IsValid() {
|
|
|
|
return d.newError(tok.Pos(), `map entry "key" cannot be repeated`)
|
|
|
|
}
|
|
|
|
val, err := d.unmarshalScalar(fd.MapKey())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key = val.MapKey()
|
|
|
|
|
|
|
|
case "value":
|
|
|
|
if kind := fd.MapValue().Kind(); (kind != pref.MessageKind) && (kind != pref.GroupKind) {
|
|
|
|
if !tok.HasSeparator() {
|
|
|
|
return d.syntaxError(tok.Pos(), "missing field separator :")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pval.IsValid() {
|
|
|
|
return d.newError(tok.Pos(), `map entry "value" cannot be repeated`)
|
|
|
|
}
|
|
|
|
pval, err = unmarshalMapValue()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
if !d.opts.DiscardUnknown {
|
|
|
|
return d.newError(tok.Pos(), "unknown map entry field %q", name)
|
|
|
|
}
|
|
|
|
d.skipValue()
|
|
|
|
}
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
if !key.IsValid() {
|
|
|
|
key = fd.MapKey().Default().MapKey()
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
if !pval.IsValid() {
|
|
|
|
switch fd.MapValue().Kind() {
|
|
|
|
case pref.MessageKind, pref.GroupKind:
|
|
|
|
// If value field is not set for message/group types, construct an
|
|
|
|
// empty one as default.
|
|
|
|
pval = mmap.NewValue()
|
|
|
|
default:
|
|
|
|
pval = fd.MapValue().Default()
|
|
|
|
}
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
mmap.Set(key, pval)
|
2019-06-19 09:28:29 -07:00
|
|
|
return nil
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// unmarshalAny unmarshals an Any textproto. It can either be in expanded form
|
|
|
|
// or non-expanded form.
|
|
|
|
func (d decoder) unmarshalAny(m pref.Message, checkDelims bool) error {
|
|
|
|
var typeURL string
|
|
|
|
var bValue []byte
|
|
|
|
|
|
|
|
// hasFields tracks which valid fields have been seen in the loop below in
|
|
|
|
// order to flag an error if there are duplicates or conflicts. It may
|
|
|
|
// contain the strings "type_url", "value" and "expanded". The literal
|
|
|
|
// "expanded" is used to indicate that the expanded form has been
|
|
|
|
// encountered already.
|
|
|
|
hasFields := map[string]bool{}
|
|
|
|
|
|
|
|
if checkDelims {
|
|
|
|
tok, err := d.Read()
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2018-12-06 15:28:53 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
|
|
|
if tok.Kind() != text.MessageOpen {
|
|
|
|
return d.unexpectedTokenError(tok)
|
|
|
|
}
|
2018-12-06 15:28:53 -08:00
|
|
|
}
|
2019-01-04 14:08:41 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
// Read field name. Can only have 3 possible field names, i.e. type_url,
|
|
|
|
// value and type URL name inside [].
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if typ := tok.Kind(); typ != text.Name {
|
|
|
|
if checkDelims {
|
|
|
|
if typ == text.MessageClose {
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
} else if typ == text.EOF {
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
return d.unexpectedTokenError(tok)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tok.NameKind() {
|
|
|
|
case text.IdentName:
|
|
|
|
// Both type_url and value fields require field separator :.
|
|
|
|
if !tok.HasSeparator() {
|
|
|
|
return d.syntaxError(tok.Pos(), "missing field separator :")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tok.IdentName() {
|
|
|
|
case "type_url":
|
|
|
|
if hasFields["type_url"] {
|
|
|
|
return d.newError(tok.Pos(), "duplicate Any type_url field")
|
|
|
|
}
|
|
|
|
if hasFields["expanded"] {
|
|
|
|
return d.newError(tok.Pos(), "conflict with [%s] field", typeURL)
|
|
|
|
}
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
typeURL, ok = tok.String()
|
|
|
|
if !ok {
|
|
|
|
return d.newError(tok.Pos(), "invalid Any type_url: %v", tok.RawString())
|
|
|
|
}
|
|
|
|
hasFields["type_url"] = true
|
|
|
|
|
|
|
|
case "value":
|
|
|
|
if hasFields["value"] {
|
|
|
|
return d.newError(tok.Pos(), "duplicate Any value field")
|
|
|
|
}
|
|
|
|
if hasFields["expanded"] {
|
|
|
|
return d.newError(tok.Pos(), "conflict with [%s] field", typeURL)
|
|
|
|
}
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s, ok := tok.String()
|
|
|
|
if !ok {
|
|
|
|
return d.newError(tok.Pos(), "invalid Any value: %v", tok.RawString())
|
|
|
|
}
|
|
|
|
bValue = []byte(s)
|
|
|
|
hasFields["value"] = true
|
|
|
|
|
|
|
|
default:
|
|
|
|
if !d.opts.DiscardUnknown {
|
|
|
|
return d.newError(tok.Pos(), "invalid field name %q in google.protobuf.Any message", tok.RawString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case text.TypeName:
|
|
|
|
if hasFields["expanded"] {
|
|
|
|
return d.newError(tok.Pos(), "cannot have more than one type")
|
|
|
|
}
|
|
|
|
if hasFields["type_url"] {
|
|
|
|
return d.newError(tok.Pos(), "conflict with type_url field")
|
|
|
|
}
|
|
|
|
typeURL = tok.TypeName()
|
|
|
|
var err error
|
|
|
|
bValue, err = d.unmarshalExpandedAny(typeURL, tok.Pos())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hasFields["expanded"] = true
|
|
|
|
|
|
|
|
default:
|
|
|
|
if !d.opts.DiscardUnknown {
|
|
|
|
return d.newError(tok.Pos(), "invalid field name %q in google.protobuf.Any message", tok.RawString())
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 14:08:41 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
fds := m.Descriptor().Fields()
|
|
|
|
if len(typeURL) > 0 {
|
|
|
|
m.Set(fds.ByNumber(fieldnum.Any_TypeUrl), pref.ValueOfString(typeURL))
|
|
|
|
}
|
|
|
|
if len(bValue) > 0 {
|
|
|
|
m.Set(fds.ByNumber(fieldnum.Any_Value), pref.ValueOfBytes(bValue))
|
|
|
|
}
|
|
|
|
return nil
|
2019-01-04 14:08:41 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
func (d decoder) unmarshalExpandedAny(typeURL string, pos int) ([]byte, error) {
|
|
|
|
mt, err := d.opts.Resolver.FindMessageByURL(typeURL)
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2019-10-31 17:10:15 -07:00
|
|
|
return nil, d.newError(pos, "unable to resolve message [%v]: %v", typeURL, err)
|
2019-01-04 14:08:41 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
// Create new message for the embedded message type and unmarshal the value
|
|
|
|
// field into it.
|
|
|
|
m := mt.New()
|
|
|
|
if err := d.unmarshalMessage(m, true); err != nil {
|
|
|
|
return nil, err
|
2019-01-04 14:08:41 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
// Serialize the embedded message and return the resulting bytes.
|
2019-04-03 12:17:24 -07:00
|
|
|
b, err := proto.MarshalOptions{
|
2019-10-31 17:10:15 -07:00
|
|
|
AllowPartial: true, // Never check required fields inside an Any.
|
2019-04-03 12:17:24 -07:00
|
|
|
Deterministic: true,
|
2019-10-31 17:10:15 -07:00
|
|
|
}.Marshal(m.Interface())
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2019-10-31 17:10:15 -07:00
|
|
|
return nil, d.newError(pos, "error in marshaling message into Any.value: %v", err)
|
2019-01-04 14:08:41 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
return b, nil
|
|
|
|
}
|
2019-01-04 14:08:41 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// skipValue makes the decoder parse a field value in order to advance the read
|
|
|
|
// to the next field. It relies on Read returning an error if the types are not
|
|
|
|
// in valid sequence.
|
|
|
|
func (d decoder) skipValue() error {
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Only need to continue reading for messages and lists.
|
|
|
|
switch tok.Kind() {
|
|
|
|
case text.MessageOpen:
|
|
|
|
return d.skipMessageValue()
|
|
|
|
|
|
|
|
case text.ListOpen:
|
|
|
|
for {
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch tok.Kind() {
|
|
|
|
case text.ListClose:
|
|
|
|
return nil
|
|
|
|
case text.MessageOpen:
|
|
|
|
return d.skipMessageValue()
|
|
|
|
default:
|
|
|
|
// Skip items. This will not validate whether skipped values are
|
|
|
|
// of the same type or not, same behavior as C++
|
|
|
|
// TextFormat::Parser::AllowUnknownField(true) version 3.8.0.
|
|
|
|
if err := d.skipValue(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 09:28:29 -07:00
|
|
|
return nil
|
2019-01-04 14:08:41 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
|
|
|
// skipMessageValue makes the decoder parse and skip over all fields in a
|
|
|
|
// message. It assumes that the previous read type is MessageOpen.
|
|
|
|
func (d decoder) skipMessageValue() error {
|
|
|
|
for {
|
|
|
|
tok, err := d.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch tok.Kind() {
|
|
|
|
case text.MessageClose:
|
|
|
|
return nil
|
|
|
|
case text.Name:
|
|
|
|
if err := d.skipValue(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|