Update vendor
This commit is contained in:
140
vendor/google.golang.org/protobuf/encoding/prototext/encode.go
generated
vendored
140
vendor/google.golang.org/protobuf/encoding/prototext/encode.go
generated
vendored
@@ -6,7 +6,6 @@ package prototext
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"unicode/utf8"
|
||||
|
||||
@@ -14,13 +13,13 @@ import (
|
||||
"google.golang.org/protobuf/internal/encoding/messageset"
|
||||
"google.golang.org/protobuf/internal/encoding/text"
|
||||
"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/genid"
|
||||
"google.golang.org/protobuf/internal/order"
|
||||
"google.golang.org/protobuf/internal/pragma"
|
||||
"google.golang.org/protobuf/internal/strs"
|
||||
"google.golang.org/protobuf/proto"
|
||||
pref "google.golang.org/protobuf/reflect/protoreflect"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
"google.golang.org/protobuf/reflect/protoregistry"
|
||||
)
|
||||
|
||||
@@ -102,6 +101,13 @@ func (o MarshalOptions) Format(m proto.Message) string {
|
||||
// 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) {
|
||||
return o.marshal(m)
|
||||
}
|
||||
|
||||
// marshal is a centralized function that all marshal operations go through.
|
||||
// For profiling purposes, avoid changing the name of this function or
|
||||
// introducing other code paths for marshal that do not go through this.
|
||||
func (o MarshalOptions) marshal(m proto.Message) ([]byte, error) {
|
||||
var delims = [2]byte{'{', '}'}
|
||||
|
||||
if o.Multiline && o.Indent == "" {
|
||||
@@ -143,7 +149,7 @@ type encoder struct {
|
||||
}
|
||||
|
||||
// marshalMessage marshals the given protoreflect.Message.
|
||||
func (e encoder) marshalMessage(m pref.Message, inclDelims bool) error {
|
||||
func (e encoder) marshalMessage(m protoreflect.Message, inclDelims bool) error {
|
||||
messageDesc := m.Descriptor()
|
||||
if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
|
||||
return errors.New("no support for proto1 MessageSets")
|
||||
@@ -155,42 +161,22 @@ func (e encoder) marshalMessage(m pref.Message, inclDelims bool) error {
|
||||
}
|
||||
|
||||
// Handle Any expansion.
|
||||
if messageDesc.FullName() == "google.protobuf.Any" {
|
||||
if messageDesc.FullName() == genid.Any_message_fullname {
|
||||
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++
|
||||
// Marshal fields.
|
||||
var err error
|
||||
order.RangeFields(m, order.IndexNameFieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
|
||||
if err = e.marshalField(fd.TextName(), v, fd); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
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 true
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -203,7 +189,7 @@ func (e encoder) marshalMessage(m pref.Message, inclDelims bool) error {
|
||||
}
|
||||
|
||||
// marshalField marshals the given field with protoreflect.Value.
|
||||
func (e encoder) marshalField(name string, val pref.Value, fd pref.FieldDescriptor) error {
|
||||
func (e encoder) marshalField(name string, val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
|
||||
switch {
|
||||
case fd.IsList():
|
||||
return e.marshalList(name, val.List(), fd)
|
||||
@@ -217,40 +203,40 @@ func (e encoder) marshalField(name string, val pref.Value, fd pref.FieldDescript
|
||||
|
||||
// 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 {
|
||||
func (e encoder) marshalSingular(val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
|
||||
kind := fd.Kind()
|
||||
switch kind {
|
||||
case pref.BoolKind:
|
||||
case protoreflect.BoolKind:
|
||||
e.WriteBool(val.Bool())
|
||||
|
||||
case pref.StringKind:
|
||||
case protoreflect.StringKind:
|
||||
s := val.String()
|
||||
if !e.opts.allowInvalidUTF8 && strs.EnforceUTF8(fd) && !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:
|
||||
case protoreflect.Int32Kind, protoreflect.Int64Kind,
|
||||
protoreflect.Sint32Kind, protoreflect.Sint64Kind,
|
||||
protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind:
|
||||
e.WriteInt(val.Int())
|
||||
|
||||
case pref.Uint32Kind, pref.Uint64Kind,
|
||||
pref.Fixed32Kind, pref.Fixed64Kind:
|
||||
case protoreflect.Uint32Kind, protoreflect.Uint64Kind,
|
||||
protoreflect.Fixed32Kind, protoreflect.Fixed64Kind:
|
||||
e.WriteUint(val.Uint())
|
||||
|
||||
case pref.FloatKind:
|
||||
case protoreflect.FloatKind:
|
||||
// Encoder.WriteFloat handles the special numbers NaN and infinites.
|
||||
e.WriteFloat(val.Float(), 32)
|
||||
|
||||
case pref.DoubleKind:
|
||||
case protoreflect.DoubleKind:
|
||||
// Encoder.WriteFloat handles the special numbers NaN and infinites.
|
||||
e.WriteFloat(val.Float(), 64)
|
||||
|
||||
case pref.BytesKind:
|
||||
case protoreflect.BytesKind:
|
||||
e.WriteString(string(val.Bytes()))
|
||||
|
||||
case pref.EnumKind:
|
||||
case protoreflect.EnumKind:
|
||||
num := val.Enum()
|
||||
if desc := fd.Enum().Values().ByNumber(num); desc != nil {
|
||||
e.WriteLiteral(string(desc.Name()))
|
||||
@@ -259,7 +245,7 @@ func (e encoder) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error
|
||||
e.WriteInt(int64(num))
|
||||
}
|
||||
|
||||
case pref.MessageKind, pref.GroupKind:
|
||||
case protoreflect.MessageKind, protoreflect.GroupKind:
|
||||
return e.marshalMessage(val.Message(), true)
|
||||
|
||||
default:
|
||||
@@ -269,7 +255,7 @@ func (e encoder) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error
|
||||
}
|
||||
|
||||
// marshalList marshals the given protoreflect.List as multiple name-value fields.
|
||||
func (e encoder) marshalList(name string, list pref.List, fd pref.FieldDescriptor) error {
|
||||
func (e encoder) marshalList(name string, list protoreflect.List, fd protoreflect.FieldDescriptor) error {
|
||||
size := list.Len()
|
||||
for i := 0; i < size; i++ {
|
||||
e.WriteName(name)
|
||||
@@ -281,20 +267,20 @@ func (e encoder) marshalList(name string, list pref.List, fd pref.FieldDescripto
|
||||
}
|
||||
|
||||
// marshalMap marshals the given protoreflect.Map as multiple name-value fields.
|
||||
func (e encoder) marshalMap(name string, mmap pref.Map, fd pref.FieldDescriptor) error {
|
||||
func (e encoder) marshalMap(name string, mmap protoreflect.Map, fd protoreflect.FieldDescriptor) error {
|
||||
var err error
|
||||
mapsort.Range(mmap, fd.MapKey().Kind(), func(key pref.MapKey, val pref.Value) bool {
|
||||
order.RangeEntries(mmap, order.GenericKeyOrder, func(key protoreflect.MapKey, val protoreflect.Value) bool {
|
||||
e.WriteName(name)
|
||||
e.StartMessage()
|
||||
defer e.EndMessage()
|
||||
|
||||
e.WriteName("key")
|
||||
e.WriteName(string(genid.MapEntry_Key_field_name))
|
||||
err = e.marshalSingular(key.Value(), fd.MapKey())
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
e.WriteName("value")
|
||||
e.WriteName(string(genid.MapEntry_Value_field_name))
|
||||
err = e.marshalSingular(val, fd.MapValue())
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -304,48 +290,6 @@ func (e encoder) marshalMap(name string, mmap pref.Map, fd pref.FieldDescriptor)
|
||||
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) {
|
||||
@@ -389,10 +333,10 @@ func (e encoder) marshalUnknown(b []byte) {
|
||||
|
||||
// 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 {
|
||||
func (e encoder) marshalAny(any protoreflect.Message) bool {
|
||||
// Construct the embedded message.
|
||||
fds := any.Descriptor().Fields()
|
||||
fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
|
||||
fdType := fds.ByNumber(genid.Any_TypeUrl_field_number)
|
||||
typeURL := any.Get(fdType).String()
|
||||
mt, err := e.opts.Resolver.FindMessageByURL(typeURL)
|
||||
if err != nil {
|
||||
@@ -401,7 +345,7 @@ func (e encoder) marshalAny(any pref.Message) bool {
|
||||
m := mt.New().Interface()
|
||||
|
||||
// Unmarshal bytes into embedded message.
|
||||
fdValue := fds.ByNumber(fieldnum.Any_Value)
|
||||
fdValue := fds.ByNumber(genid.Any_Value_field_number)
|
||||
value := any.Get(fdValue)
|
||||
err = proto.UnmarshalOptions{
|
||||
AllowPartial: true,
|
||||
|
||||
Reference in New Issue
Block a user