mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-02-21 09:40:03 +00:00
Added API: Message.Len Message.Range Message.Has Message.Clear Message.Get Message.Set Message.Mutable Message.NewMessage Message.WhichOneof Message.GetUnknown Message.SetUnknown Deprecated API (to be removed in subsequent CL): Message.KnownFields Message.UnknownFields The primary difference with the new API is that the top-level Message methods are keyed by FieldDescriptor rather than FieldNumber with the following semantics: * For known fields, the FieldDescriptor must exactly match the field descriptor known by the message. * For extension fields, the FieldDescriptor must implement ExtensionType, where ContainingMessage.FullName matches the message name, and the field number is within the message's extension range. When setting an extension field, it automatically stores the extension type information. * Extension fields are always considered nullable, implying that repeated extension fields are nullable. That is, you can distinguish between a unpopulated list and an empty list. * Message.Get always returns a valid Value even if unpopulated. The behavior is already well-defined for scalars, but for unpopulated composite types, it now returns an empty read-only version of it. Change-Id: Ia120630b4db221aeaaf743d0f64160e1a61a0f61 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/175458 Reviewed-by: Damien Neil <dneil@google.com>
381 lines
11 KiB
Go
381 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"
|
|
"unicode/utf8"
|
|
|
|
"google.golang.org/protobuf/internal/encoding/text"
|
|
"google.golang.org/protobuf/internal/encoding/wire"
|
|
"google.golang.org/protobuf/internal/errors"
|
|
"google.golang.org/protobuf/internal/fieldnum"
|
|
"google.golang.org/protobuf/internal/mapsort"
|
|
"google.golang.org/protobuf/internal/pragma"
|
|
"google.golang.org/protobuf/proto"
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
"google.golang.org/protobuf/reflect/protoregistry"
|
|
)
|
|
|
|
// Marshal writes the given proto.Message in textproto format using default options.
|
|
func Marshal(m proto.Message) ([]byte, error) {
|
|
return MarshalOptions{}.Marshal(m)
|
|
}
|
|
|
|
// MarshalOptions is a configurable text format marshaler.
|
|
type MarshalOptions struct {
|
|
pragma.NoUnkeyedLiterals
|
|
|
|
// AllowPartial allows messages that have missing required fields to marshal
|
|
// without returning an error. If AllowPartial is false (the default),
|
|
// Marshal will return error if there are any missing required fields.
|
|
AllowPartial bool
|
|
|
|
// If Indent is a non-empty string, it causes entries for a Message to be
|
|
// preceded by the indent and trailed by a newline. Indent can only be
|
|
// composed of space or tab characters.
|
|
Indent string
|
|
|
|
// Resolver is used for looking up types when expanding google.protobuf.Any
|
|
// messages. If nil, this defaults to using protoregistry.GlobalTypes.
|
|
Resolver interface {
|
|
protoregistry.MessageTypeResolver
|
|
}
|
|
}
|
|
|
|
// Marshal writes the given proto.Message in textproto format using options in MarshalOptions object.
|
|
func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
|
|
if o.Resolver == nil {
|
|
o.Resolver = protoregistry.GlobalTypes
|
|
}
|
|
|
|
var nerr errors.NonFatal
|
|
v, err := o.marshalMessage(m.ProtoReflect())
|
|
if !nerr.Merge(err) {
|
|
return nil, err
|
|
}
|
|
|
|
delims := [2]byte{'{', '}'}
|
|
const outputASCII = false
|
|
b, err := text.Marshal(v, o.Indent, delims, outputASCII)
|
|
if !nerr.Merge(err) {
|
|
return nil, err
|
|
}
|
|
if !o.AllowPartial {
|
|
nerr.Merge(proto.IsInitialized(m))
|
|
}
|
|
return b, nerr.E
|
|
}
|
|
|
|
// marshalMessage converts a protoreflect.Message to a text.Value.
|
|
func (o MarshalOptions) marshalMessage(m pref.Message) (text.Value, error) {
|
|
var nerr errors.NonFatal
|
|
var msgFields [][2]text.Value
|
|
messageDesc := m.Descriptor()
|
|
|
|
// Handle Any expansion.
|
|
if messageDesc.FullName() == "google.protobuf.Any" {
|
|
msg, err := o.marshalAny(m)
|
|
if err == nil || nerr.Merge(err) {
|
|
// Return as is for nil or non-fatal error.
|
|
return msg, nerr.E
|
|
}
|
|
// For other errors, continue on to marshal Any as a regular message.
|
|
}
|
|
|
|
// Handle known fields.
|
|
fieldDescs := messageDesc.Fields()
|
|
size := fieldDescs.Len()
|
|
for i := 0; i < size; i++ {
|
|
fd := fieldDescs.Get(i)
|
|
if !m.Has(fd) {
|
|
continue
|
|
}
|
|
|
|
name := text.ValueOf(fd.Name())
|
|
// Use type name for group field name.
|
|
if fd.Kind() == pref.GroupKind {
|
|
name = text.ValueOf(fd.Message().Name())
|
|
}
|
|
pval := m.Get(fd)
|
|
var err error
|
|
msgFields, err = o.appendField(msgFields, name, pval, fd)
|
|
if !nerr.Merge(err) {
|
|
return text.Value{}, err
|
|
}
|
|
}
|
|
|
|
// Handle extensions.
|
|
var err error
|
|
msgFields, err = o.appendExtensions(msgFields, m)
|
|
if !nerr.Merge(err) {
|
|
return text.Value{}, err
|
|
}
|
|
|
|
// Handle unknown fields.
|
|
// TODO: Provide option to exclude or include unknown fields.
|
|
msgFields = appendUnknown(msgFields, m.GetUnknown())
|
|
|
|
return text.ValueOf(msgFields), nerr.E
|
|
}
|
|
|
|
// appendField marshals a protoreflect.Value and appends it to the given [][2]text.Value.
|
|
func (o MarshalOptions) appendField(msgFields [][2]text.Value, name text.Value, pval pref.Value, fd pref.FieldDescriptor) ([][2]text.Value, error) {
|
|
var nerr errors.NonFatal
|
|
|
|
switch {
|
|
case fd.IsList():
|
|
items, err := o.marshalList(pval.List(), fd)
|
|
if !nerr.Merge(err) {
|
|
return msgFields, err
|
|
}
|
|
|
|
for _, item := range items {
|
|
msgFields = append(msgFields, [2]text.Value{name, item})
|
|
}
|
|
case fd.IsMap():
|
|
items, err := o.marshalMap(pval.Map(), fd)
|
|
if !nerr.Merge(err) {
|
|
return msgFields, err
|
|
}
|
|
|
|
for _, item := range items {
|
|
msgFields = append(msgFields, [2]text.Value{name, item})
|
|
}
|
|
default:
|
|
tval, err := o.marshalSingular(pval, fd)
|
|
if !nerr.Merge(err) {
|
|
return msgFields, err
|
|
}
|
|
msgFields = append(msgFields, [2]text.Value{name, tval})
|
|
}
|
|
|
|
return msgFields, nerr.E
|
|
}
|
|
|
|
// marshalSingular converts a non-repeated field value to text.Value.
|
|
// This includes all scalar types, enums, messages, and groups.
|
|
func (o MarshalOptions) marshalSingular(val pref.Value, fd pref.FieldDescriptor) (text.Value, error) {
|
|
kind := fd.Kind()
|
|
switch kind {
|
|
case pref.BoolKind,
|
|
pref.Int32Kind, pref.Sint32Kind, pref.Uint32Kind,
|
|
pref.Int64Kind, pref.Sint64Kind, pref.Uint64Kind,
|
|
pref.Sfixed32Kind, pref.Fixed32Kind,
|
|
pref.Sfixed64Kind, pref.Fixed64Kind,
|
|
pref.FloatKind, pref.DoubleKind,
|
|
pref.BytesKind:
|
|
return text.ValueOf(val.Interface()), nil
|
|
|
|
case pref.StringKind:
|
|
s := val.String()
|
|
if utf8.ValidString(s) {
|
|
return text.ValueOf(s), nil
|
|
}
|
|
var nerr errors.NonFatal
|
|
nerr.AppendInvalidUTF8(string(fd.FullName()))
|
|
return text.ValueOf(s), nerr.E
|
|
|
|
case pref.EnumKind:
|
|
num := val.Enum()
|
|
if desc := fd.Enum().Values().ByNumber(num); desc != nil {
|
|
return text.ValueOf(desc.Name()), nil
|
|
}
|
|
// Use numeric value if there is no enum description.
|
|
return text.ValueOf(int32(num)), nil
|
|
|
|
case pref.MessageKind, pref.GroupKind:
|
|
return o.marshalMessage(val.Message())
|
|
}
|
|
|
|
panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
|
|
}
|
|
|
|
// marshalList converts a protoreflect.List to []text.Value.
|
|
func (o MarshalOptions) marshalList(list pref.List, fd pref.FieldDescriptor) ([]text.Value, error) {
|
|
var nerr errors.NonFatal
|
|
size := list.Len()
|
|
values := make([]text.Value, 0, size)
|
|
|
|
for i := 0; i < size; i++ {
|
|
item := list.Get(i)
|
|
val, err := o.marshalSingular(item, fd)
|
|
if !nerr.Merge(err) {
|
|
// Return already marshaled values.
|
|
return values, err
|
|
}
|
|
values = append(values, val)
|
|
}
|
|
|
|
return values, nerr.E
|
|
}
|
|
|
|
var (
|
|
mapKeyName = text.ValueOf(pref.Name("key"))
|
|
mapValueName = text.ValueOf(pref.Name("value"))
|
|
)
|
|
|
|
// marshalMap converts a protoreflect.Map to []text.Value.
|
|
func (o MarshalOptions) marshalMap(mmap pref.Map, fd pref.FieldDescriptor) ([]text.Value, error) {
|
|
var nerr errors.NonFatal
|
|
// values is a list of messages.
|
|
values := make([]text.Value, 0, mmap.Len())
|
|
|
|
var err error
|
|
mapsort.Range(mmap, fd.MapKey().Kind(), func(key pref.MapKey, val pref.Value) bool {
|
|
var keyTxtVal text.Value
|
|
keyTxtVal, err = o.marshalSingular(key.Value(), fd.MapKey())
|
|
if !nerr.Merge(err) {
|
|
return false
|
|
}
|
|
var valTxtVal text.Value
|
|
valTxtVal, err = o.marshalSingular(val, fd.MapValue())
|
|
if !nerr.Merge(err) {
|
|
return false
|
|
}
|
|
// Map entry (message) contains 2 fields, first field for key and second field for value.
|
|
msg := text.ValueOf([][2]text.Value{
|
|
{mapKeyName, keyTxtVal},
|
|
{mapValueName, valTxtVal},
|
|
})
|
|
values = append(values, msg)
|
|
err = nil
|
|
return true
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return values, nerr.E
|
|
}
|
|
|
|
// appendExtensions marshals extension fields and appends them to the given [][2]text.Value.
|
|
func (o MarshalOptions) appendExtensions(msgFields [][2]text.Value, m pref.Message) ([][2]text.Value, error) {
|
|
var nerr errors.NonFatal
|
|
var err error
|
|
var entries [][2]text.Value
|
|
m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
|
|
if !fd.IsExtension() {
|
|
return true
|
|
}
|
|
xt := fd.(pref.ExtensionType)
|
|
|
|
// If extended type is a MessageSet, set field name to be the message type name.
|
|
name := xt.Descriptor().FullName()
|
|
if isMessageSetExtension(xt) {
|
|
name = xt.Descriptor().Message().FullName()
|
|
}
|
|
|
|
// Use string type to produce [name] format.
|
|
tname := text.ValueOf(string(name))
|
|
entries, err = o.appendField(entries, tname, v, xt)
|
|
if !nerr.Merge(err) {
|
|
return false
|
|
}
|
|
err = nil
|
|
return true
|
|
})
|
|
if err != nil {
|
|
return msgFields, err
|
|
}
|
|
|
|
// Sort extensions lexicographically and append to output.
|
|
sort.SliceStable(entries, func(i, j int) bool {
|
|
return entries[i][0].String() < entries[j][0].String()
|
|
})
|
|
return append(msgFields, entries...), nerr.E
|
|
}
|
|
|
|
// isMessageSetExtension reports whether extension extends a message set.
|
|
func isMessageSetExtension(xt pref.ExtensionType) bool {
|
|
xd := xt.Descriptor()
|
|
if xd.Name() != "message_set_extension" {
|
|
return false
|
|
}
|
|
md := xd.Message()
|
|
if md == nil {
|
|
return false
|
|
}
|
|
if xd.FullName().Parent() != md.FullName() {
|
|
return false
|
|
}
|
|
xmd, ok := xd.ContainingMessage().(interface{ IsMessageSet() bool })
|
|
return ok && xmd.IsMessageSet()
|
|
}
|
|
|
|
// appendUnknown parses the given []byte and appends field(s) into the given fields slice.
|
|
// This function assumes proper encoding in the given []byte.
|
|
func appendUnknown(fields [][2]text.Value, b []byte) [][2]text.Value {
|
|
for len(b) > 0 {
|
|
var value interface{}
|
|
num, wtype, n := wire.ConsumeTag(b)
|
|
b = b[n:]
|
|
|
|
switch wtype {
|
|
case wire.VarintType:
|
|
value, n = wire.ConsumeVarint(b)
|
|
case wire.Fixed32Type:
|
|
value, n = wire.ConsumeFixed32(b)
|
|
case wire.Fixed64Type:
|
|
value, n = wire.ConsumeFixed64(b)
|
|
case wire.BytesType:
|
|
value, n = wire.ConsumeBytes(b)
|
|
case wire.StartGroupType:
|
|
var v []byte
|
|
v, n = wire.ConsumeGroup(num, b)
|
|
var msg [][2]text.Value
|
|
value = appendUnknown(msg, v)
|
|
default:
|
|
panic(fmt.Sprintf("error parsing unknown field wire type: %v", wtype))
|
|
}
|
|
|
|
fields = append(fields, [2]text.Value{text.ValueOf(uint32(num)), text.ValueOf(value)})
|
|
b = b[n:]
|
|
}
|
|
return fields
|
|
}
|
|
|
|
// marshalAny converts a google.protobuf.Any protoreflect.Message to a text.Value.
|
|
func (o MarshalOptions) marshalAny(m pref.Message) (text.Value, error) {
|
|
fds := m.Descriptor().Fields()
|
|
fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
|
|
fdValue := fds.ByNumber(fieldnum.Any_Value)
|
|
|
|
typeURL := m.Get(fdType).String()
|
|
value := m.Get(fdValue)
|
|
|
|
var nerr errors.NonFatal
|
|
emt, err := o.Resolver.FindMessageByURL(typeURL)
|
|
if !nerr.Merge(err) {
|
|
return text.Value{}, err
|
|
}
|
|
em := emt.New().Interface()
|
|
// TODO: Need to set types registry in binary unmarshaling.
|
|
// TODO: If binary unmarshaling returns required not set error, need to
|
|
// return another required not set error that contains both the path to this
|
|
// field and the path inside the embedded message.
|
|
err = proto.UnmarshalOptions{
|
|
AllowPartial: o.AllowPartial,
|
|
}.Unmarshal(value.Bytes(), em)
|
|
if !nerr.Merge(err) {
|
|
return text.Value{}, err
|
|
}
|
|
|
|
msg, err := o.marshalMessage(em.ProtoReflect())
|
|
if !nerr.Merge(err) {
|
|
return text.Value{}, err
|
|
}
|
|
// Expanded Any field value contains only a single field with the type_url field value as the
|
|
// field name in [] and a text marshaled field value of the embedded message.
|
|
msgFields := [][2]text.Value{
|
|
{
|
|
text.ValueOf(typeURL),
|
|
msg,
|
|
},
|
|
}
|
|
return text.ValueOf(msgFields), nerr.E
|
|
}
|