2018-11-28 18:25:20 -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-11-28 18:25:20 -08:00
|
|
|
|
|
|
|
import (
|
2018-12-11 21:08:58 -08:00
|
|
|
"fmt"
|
2019-10-31 17:10:15 -07:00
|
|
|
"strconv"
|
2019-04-08 17:32:44 -07:00
|
|
|
"unicode/utf8"
|
2018-11-28 18:25:20 -08:00
|
|
|
|
2020-02-14 18:08:02 -08:00
|
|
|
"google.golang.org/protobuf/encoding/protowire"
|
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"
|
2019-07-11 18:23:08 -07:00
|
|
|
"google.golang.org/protobuf/internal/flags"
|
2020-05-26 11:21:59 -07:00
|
|
|
"google.golang.org/protobuf/internal/genid"
|
2020-06-24 14:28:07 -07:00
|
|
|
"google.golang.org/protobuf/internal/order"
|
2019-05-13 23:55:40 -07:00
|
|
|
"google.golang.org/protobuf/internal/pragma"
|
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"
|
2020-06-24 14:28:07 -07:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2019-05-13 23:55:40 -07:00
|
|
|
"google.golang.org/protobuf/reflect/protoregistry"
|
2018-11-28 18:25:20 -08:00
|
|
|
)
|
|
|
|
|
2020-01-06 15:44:09 -08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-05 11:40:38 +08:00
|
|
|
// 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.
|
2018-11-28 18:25:20 -08:00
|
|
|
func Marshal(m proto.Message) ([]byte, error) {
|
|
|
|
return MarshalOptions{}.Marshal(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalOptions is a configurable text format marshaler.
|
|
|
|
type MarshalOptions struct {
|
|
|
|
pragma.NoUnkeyedLiterals
|
|
|
|
|
2020-01-06 15:44:09 -08:00
|
|
|
// 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.
|
2019-03-20 14:04:24 -07:00
|
|
|
Indent string
|
2019-01-02 15:46:07 -08: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
|
|
|
// EmitASCII specifies whether to format strings and bytes as ASCII only
|
|
|
|
// as opposed to using UTF-8 encoding when possible.
|
|
|
|
EmitASCII bool
|
|
|
|
|
|
|
|
// allowInvalidUTF8 specifies whether to permit the encoding of strings
|
|
|
|
// with invalid UTF-8. This is unexported as it is intended to only
|
|
|
|
// be specified by the Format method.
|
|
|
|
allowInvalidUTF8 bool
|
|
|
|
|
2020-02-14 10:51:41 -08:00
|
|
|
// 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
|
|
|
|
|
2019-05-14 14:28:19 -07:00
|
|
|
// Resolver is used for looking up types when expanding google.protobuf.Any
|
|
|
|
// messages. If nil, this defaults to using protoregistry.GlobalTypes.
|
|
|
|
Resolver interface {
|
2019-06-19 10:03:37 -07:00
|
|
|
protoregistry.ExtensionTypeResolver
|
2019-05-14 14:28:19 -07:00
|
|
|
protoregistry.MessageTypeResolver
|
|
|
|
}
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
|
|
|
|
2020-01-06 15:44:09 -08:00
|
|
|
// 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
|
|
|
|
}
|
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
|
|
|
o.allowInvalidUTF8 = true
|
2020-01-06 15:44:09 -08:00
|
|
|
o.AllowPartial = true
|
|
|
|
o.EmitUnknown = true
|
|
|
|
b, _ := o.Marshal(m)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2019-08-05 11:40:38 +08:00
|
|
|
// 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.
|
2018-11-28 18:25:20 -08:00
|
|
|
func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
|
2020-05-14 15:29:58 -07:00
|
|
|
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) {
|
2019-10-31 17:10:15 -07:00
|
|
|
var delims = [2]byte{'{', '}'}
|
|
|
|
|
2020-01-06 15:44:09 -08:00
|
|
|
if o.Multiline && o.Indent == "" {
|
|
|
|
o.Indent = defaultIndent
|
|
|
|
}
|
2019-01-02 15:46:07 -08:00
|
|
|
if o.Resolver == nil {
|
|
|
|
o.Resolver = protoregistry.GlobalTypes
|
|
|
|
}
|
|
|
|
|
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
|
|
|
internalEnc, err := text.NewEncoder(o.Indent, delims, o.EmitASCII)
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2018-12-06 15:28:53 -08:00
|
|
|
return nil, err
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
|
|
|
|
2020-04-17 10:55:19 -07:00
|
|
|
// Treat nil message interface as an empty message,
|
|
|
|
// in which case there is nothing to output.
|
|
|
|
if m == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
enc := encoder{internalEnc, o}
|
|
|
|
err = enc.marshalMessage(m.ProtoReflect(), false)
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2018-11-28 18:25:20 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
out := enc.Bytes()
|
|
|
|
if len(o.Indent) > 0 && len(out) > 0 {
|
|
|
|
out = append(out, '\n')
|
|
|
|
}
|
2019-06-19 09:28:29 -07:00
|
|
|
if o.AllowPartial {
|
2019-10-31 17:10:15 -07:00
|
|
|
return out, nil
|
2019-04-05 13:31:40 -07:00
|
|
|
}
|
2020-02-20 10:05:37 -08:00
|
|
|
return out, proto.CheckInitialized(m)
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
type encoder struct {
|
|
|
|
*text.Encoder
|
|
|
|
opts MarshalOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshalMessage marshals the given protoreflect.Message.
|
2022-05-11 22:50:14 +09:00
|
|
|
func (e encoder) marshalMessage(m protoreflect.Message, inclDelims 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-10-31 17:10:15 -07:00
|
|
|
return errors.New("no support for proto1 MessageSets")
|
|
|
|
}
|
|
|
|
|
|
|
|
if inclDelims {
|
|
|
|
e.StartMessage()
|
|
|
|
defer e.EndMessage()
|
2019-07-11 18:23:08 -07:00
|
|
|
}
|
2019-01-02 15:46:07 -08:00
|
|
|
|
|
|
|
// Handle Any expansion.
|
2020-05-26 11:21:59 -07:00
|
|
|
if messageDesc.FullName() == genid.Any_message_fullname {
|
2019-10-31 17:10:15 -07:00
|
|
|
if e.marshalAny(m) {
|
|
|
|
return nil
|
2019-01-02 15:46:07 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
// If unable to expand, continue on to marshal Any as a regular message.
|
2019-01-02 15:46:07 -08:00
|
|
|
}
|
2018-11-28 18:25:20 -08:00
|
|
|
|
2020-06-24 14:28:07 -07:00
|
|
|
// Marshal fields.
|
|
|
|
var err error
|
|
|
|
order.RangeFields(m, order.IndexNameFieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
|
2020-06-24 16:47:32 -07:00
|
|
|
if err = e.marshalField(fd.TextName(), v, fd); err != nil {
|
2020-06-24 14:28:07 -07:00
|
|
|
return false
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
2020-06-24 14:28:07 -07:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-10-31 17:10:15 -07:00
|
|
|
return err
|
2018-12-17 17:13:07 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// Marshal unknown fields.
|
|
|
|
if e.opts.EmitUnknown {
|
|
|
|
e.marshalUnknown(m.GetUnknown())
|
2019-09-14 19:14:24 -07:00
|
|
|
}
|
2018-12-11 21:08:58 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
return nil
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// marshalField marshals the given field with protoreflect.Value.
|
2022-05-11 22:50:14 +09:00
|
|
|
func (e encoder) marshalField(name string, val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
|
2019-05-13 14:32:56 -07:00
|
|
|
switch {
|
|
|
|
case fd.IsList():
|
2019-10-31 17:10:15 -07:00
|
|
|
return e.marshalList(name, val.List(), fd)
|
2019-05-13 14:32:56 -07:00
|
|
|
case fd.IsMap():
|
2019-10-31 17:10:15 -07:00
|
|
|
return e.marshalMap(name, val.Map(), fd)
|
2019-05-13 14:32:56 -07:00
|
|
|
default:
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteName(name)
|
|
|
|
return e.marshalSingular(val, fd)
|
2018-12-17 17:13:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// marshalSingular marshals the given non-repeated field value. This includes
|
|
|
|
// all scalar types, enums, messages, and groups.
|
2022-05-11 22:50:14 +09:00
|
|
|
func (e encoder) marshalSingular(val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
|
2018-11-28 18:25:20 -08:00
|
|
|
kind := fd.Kind()
|
|
|
|
switch kind {
|
2022-05-11 22:50:14 +09:00
|
|
|
case protoreflect.BoolKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteBool(val.Bool())
|
2018-11-28 18:25:20 -08:00
|
|
|
|
2022-05-11 22:50:14 +09:00
|
|
|
case protoreflect.StringKind:
|
2019-04-08 17:32:44 -07:00
|
|
|
s := val.String()
|
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 !e.opts.allowInvalidUTF8 && strs.EnforceUTF8(fd) && !utf8.ValidString(s) {
|
2019-10-31 17:10:15 -07:00
|
|
|
return errors.InvalidUTF8(string(fd.FullName()))
|
2019-04-08 17:32:44 -07:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteString(s)
|
|
|
|
|
2022-05-11 22:50:14 +09:00
|
|
|
case protoreflect.Int32Kind, protoreflect.Int64Kind,
|
|
|
|
protoreflect.Sint32Kind, protoreflect.Sint64Kind,
|
|
|
|
protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind:
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteInt(val.Int())
|
|
|
|
|
2022-05-11 22:50:14 +09:00
|
|
|
case protoreflect.Uint32Kind, protoreflect.Uint64Kind,
|
|
|
|
protoreflect.Fixed32Kind, protoreflect.Fixed64Kind:
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteUint(val.Uint())
|
|
|
|
|
2022-05-11 22:50:14 +09:00
|
|
|
case protoreflect.FloatKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
// Encoder.WriteFloat handles the special numbers NaN and infinites.
|
|
|
|
e.WriteFloat(val.Float(), 32)
|
|
|
|
|
2022-05-11 22:50:14 +09:00
|
|
|
case protoreflect.DoubleKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
// Encoder.WriteFloat handles the special numbers NaN and infinites.
|
|
|
|
e.WriteFloat(val.Float(), 64)
|
|
|
|
|
2022-05-11 22:50:14 +09:00
|
|
|
case protoreflect.BytesKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteString(string(val.Bytes()))
|
2019-04-08 17:32:44 -07:00
|
|
|
|
2022-05-11 22:50:14 +09:00
|
|
|
case protoreflect.EnumKind:
|
2018-11-28 18:25:20 -08:00
|
|
|
num := val.Enum()
|
2019-04-15 23:39:09 -07:00
|
|
|
if desc := fd.Enum().Values().ByNumber(num); desc != nil {
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteLiteral(string(desc.Name()))
|
|
|
|
} else {
|
|
|
|
// Use numeric value if there is no enum description.
|
|
|
|
e.WriteInt(int64(num))
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
|
|
|
|
2022-05-11 22:50:14 +09:00
|
|
|
case protoreflect.MessageKind, protoreflect.GroupKind:
|
2019-10-31 17:10:15 -07:00
|
|
|
return e.marshalMessage(val.Message(), true)
|
2018-11-28 18:25:20 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
|
|
|
|
}
|
|
|
|
return nil
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// marshalList marshals the given protoreflect.List as multiple name-value fields.
|
2022-05-11 22:50:14 +09:00
|
|
|
func (e encoder) marshalList(name string, list protoreflect.List, fd protoreflect.FieldDescriptor) error {
|
2018-11-28 18:25:20 -08:00
|
|
|
size := list.Len()
|
|
|
|
for i := 0; i < size; i++ {
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteName(name)
|
|
|
|
if err := e.marshalSingular(list.Get(i), fd); err != nil {
|
|
|
|
return err
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
return nil
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// marshalMap marshals the given protoreflect.Map as multiple name-value fields.
|
2022-05-11 22:50:14 +09:00
|
|
|
func (e encoder) marshalMap(name string, mmap protoreflect.Map, fd protoreflect.FieldDescriptor) error {
|
2019-04-03 15:42:41 -07:00
|
|
|
var err error
|
2022-05-11 22:50:14 +09:00
|
|
|
order.RangeEntries(mmap, order.GenericKeyOrder, func(key protoreflect.MapKey, val protoreflect.Value) bool {
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteName(name)
|
|
|
|
e.StartMessage()
|
|
|
|
defer e.EndMessage()
|
|
|
|
|
2020-05-26 11:21:59 -07:00
|
|
|
e.WriteName(string(genid.MapEntry_Key_field_name))
|
2019-10-31 17:10:15 -07:00
|
|
|
err = e.marshalSingular(key.Value(), fd.MapKey())
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2018-11-28 18:25:20 -08:00
|
|
|
return false
|
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
|
2020-05-26 11:21:59 -07:00
|
|
|
e.WriteName(string(genid.MapEntry_Value_field_name))
|
2019-10-31 17:10:15 -07:00
|
|
|
err = e.marshalSingular(val, fd.MapValue())
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2018-11-28 18:25:20 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2019-10-31 17:10:15 -07:00
|
|
|
return err
|
2018-11-28 18:25:20 -08:00
|
|
|
}
|
2018-12-11 21:08:58 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// marshalUnknown parses the given []byte and marshals fields out.
|
2018-12-11 21:08:58 -08:00
|
|
|
// This function assumes proper encoding in the given []byte.
|
2019-10-31 17:10:15 -07:00
|
|
|
func (e encoder) marshalUnknown(b []byte) {
|
|
|
|
const dec = 10
|
|
|
|
const hex = 16
|
2018-12-11 21:08:58 -08:00
|
|
|
for len(b) > 0 {
|
2020-02-14 18:08:02 -08:00
|
|
|
num, wtype, n := protowire.ConsumeTag(b)
|
2018-12-11 21:08:58 -08:00
|
|
|
b = b[n:]
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteName(strconv.FormatInt(int64(num), dec))
|
2018-12-11 21:08:58 -08:00
|
|
|
|
|
|
|
switch wtype {
|
2020-02-14 18:08:02 -08:00
|
|
|
case protowire.VarintType:
|
2019-10-31 17:10:15 -07:00
|
|
|
var v uint64
|
2020-02-14 18:08:02 -08:00
|
|
|
v, n = protowire.ConsumeVarint(b)
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteUint(v)
|
2020-02-14 18:08:02 -08:00
|
|
|
case protowire.Fixed32Type:
|
2019-10-31 17:10:15 -07:00
|
|
|
var v uint32
|
2020-02-14 18:08:02 -08:00
|
|
|
v, n = protowire.ConsumeFixed32(b)
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteLiteral("0x" + strconv.FormatUint(uint64(v), hex))
|
2020-02-14 18:08:02 -08:00
|
|
|
case protowire.Fixed64Type:
|
2019-10-31 17:10:15 -07:00
|
|
|
var v uint64
|
2020-02-14 18:08:02 -08:00
|
|
|
v, n = protowire.ConsumeFixed64(b)
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteLiteral("0x" + strconv.FormatUint(v, hex))
|
2020-02-14 18:08:02 -08:00
|
|
|
case protowire.BytesType:
|
2019-10-31 17:10:15 -07:00
|
|
|
var v []byte
|
2020-02-14 18:08:02 -08:00
|
|
|
v, n = protowire.ConsumeBytes(b)
|
2019-10-31 17:10:15 -07:00
|
|
|
e.WriteString(string(v))
|
2020-02-14 18:08:02 -08:00
|
|
|
case protowire.StartGroupType:
|
2019-10-31 17:10:15 -07:00
|
|
|
e.StartMessage()
|
2018-12-11 21:08:58 -08:00
|
|
|
var v []byte
|
2020-02-14 18:08:02 -08:00
|
|
|
v, n = protowire.ConsumeGroup(num, b)
|
2019-10-31 17:10:15 -07:00
|
|
|
e.marshalUnknown(v)
|
|
|
|
e.EndMessage()
|
2018-12-11 21:08:58 -08:00
|
|
|
default:
|
2019-10-31 17:10:15 -07:00
|
|
|
panic(fmt.Sprintf("prototext: error parsing unknown field wire type: %v", wtype))
|
2018-12-11 21:08:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
b = b[n:]
|
|
|
|
}
|
|
|
|
}
|
2019-01-02 15:46:07 -08:00
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// marshalAny marshals the given google.protobuf.Any message in expanded form.
|
|
|
|
// It returns true if it was able to marshal, else false.
|
2022-05-11 22:50:14 +09:00
|
|
|
func (e encoder) marshalAny(any protoreflect.Message) bool {
|
2019-10-31 17:10:15 -07:00
|
|
|
// Construct the embedded message.
|
|
|
|
fds := any.Descriptor().Fields()
|
2020-05-26 11:21:59 -07:00
|
|
|
fdType := fds.ByNumber(genid.Any_TypeUrl_field_number)
|
2019-10-31 17:10:15 -07:00
|
|
|
typeURL := any.Get(fdType).String()
|
|
|
|
mt, err := e.opts.Resolver.FindMessageByURL(typeURL)
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2019-10-31 17:10:15 -07:00
|
|
|
return false
|
2019-01-02 15:46:07 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
m := mt.New().Interface()
|
|
|
|
|
|
|
|
// Unmarshal bytes into embedded message.
|
2020-05-26 11:21:59 -07:00
|
|
|
fdValue := fds.ByNumber(genid.Any_Value_field_number)
|
2019-10-31 17:10:15 -07:00
|
|
|
value := any.Get(fdValue)
|
2019-04-03 12:17:24 -07:00
|
|
|
err = proto.UnmarshalOptions{
|
2019-06-19 10:41:09 -07:00
|
|
|
AllowPartial: true,
|
2019-10-31 17:10:15 -07:00
|
|
|
Resolver: e.opts.Resolver,
|
|
|
|
}.Unmarshal(value.Bytes(), m)
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2019-10-31 17:10:15 -07:00
|
|
|
return false
|
2019-01-02 15:46:07 -08:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:10:15 -07:00
|
|
|
// 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)
|
2019-06-19 09:28:29 -07:00
|
|
|
if err != nil {
|
2019-10-31 17:10:15 -07:00
|
|
|
e.Reset(pos)
|
|
|
|
return false
|
2019-01-02 15:46:07 -08:00
|
|
|
}
|
2019-10-31 17:10:15 -07:00
|
|
|
return true
|
2019-01-02 15:46:07 -08:00
|
|
|
}
|