mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-01 03:14:16 +00:00
ce3384cd34
In the v1 implementation, unknown MessageSet items are stored in a message's unknown fields section in non-MessageSet format. For example, consider a MessageSet containing an item with type_id T and value V. If the type_id is not resolvable, the item will be placed in the unknown fields as a bytes-valued field with number T and contents V. This conversion is then reversed when marshaling a MessageSet containing unknown fields. Preserve this behavior in v2. One consequence of this change is that actual unknown fields in a MessageSet (any field other than 1) are now discarded. This matches the previous behavior. Change-Id: I3d913613f84e0ae82481078dbc91cb25628651cc Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/205697 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
89 lines
2.7 KiB
Go
89 lines
2.7 KiB
Go
// Copyright 2019 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 proto
|
|
|
|
import (
|
|
"google.golang.org/protobuf/internal/encoding/messageset"
|
|
"google.golang.org/protobuf/internal/encoding/wire"
|
|
"google.golang.org/protobuf/internal/errors"
|
|
"google.golang.org/protobuf/internal/flags"
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
"google.golang.org/protobuf/reflect/protoregistry"
|
|
)
|
|
|
|
func sizeMessageSet(m protoreflect.Message) (size int) {
|
|
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
|
|
size += messageset.SizeField(fd.Number())
|
|
size += wire.SizeTag(messageset.FieldMessage)
|
|
size += wire.SizeBytes(sizeMessage(v.Message()))
|
|
return true
|
|
})
|
|
size += messageset.SizeUnknown(m.GetUnknown())
|
|
return size
|
|
}
|
|
|
|
func marshalMessageSet(b []byte, m protoreflect.Message, o MarshalOptions) ([]byte, error) {
|
|
if !flags.ProtoLegacy {
|
|
return b, errors.New("no support for message_set_wire_format")
|
|
}
|
|
var err error
|
|
o.rangeFields(m, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
|
|
b, err = marshalMessageSetField(b, fd, v, o)
|
|
return err == nil
|
|
})
|
|
if err != nil {
|
|
return b, err
|
|
}
|
|
return messageset.AppendUnknown(b, m.GetUnknown())
|
|
}
|
|
|
|
func marshalMessageSetField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value, o MarshalOptions) ([]byte, error) {
|
|
b = messageset.AppendFieldStart(b, fd.Number())
|
|
b = wire.AppendTag(b, messageset.FieldMessage, wire.BytesType)
|
|
b = wire.AppendVarint(b, uint64(o.Size(value.Message().Interface())))
|
|
b, err := o.marshalMessage(b, value.Message())
|
|
if err != nil {
|
|
return b, err
|
|
}
|
|
b = messageset.AppendFieldEnd(b)
|
|
return b, nil
|
|
}
|
|
|
|
func unmarshalMessageSet(b []byte, m protoreflect.Message, o UnmarshalOptions) error {
|
|
if !flags.ProtoLegacy {
|
|
return errors.New("no support for message_set_wire_format")
|
|
}
|
|
return messageset.Unmarshal(b, false, func(num wire.Number, v []byte) error {
|
|
err := unmarshalMessageSetField(m, num, v, o)
|
|
if err == errUnknown {
|
|
unknown := m.GetUnknown()
|
|
unknown = wire.AppendTag(unknown, num, wire.BytesType)
|
|
unknown = wire.AppendBytes(unknown, v)
|
|
m.SetUnknown(unknown)
|
|
return nil
|
|
}
|
|
return err
|
|
})
|
|
}
|
|
|
|
func unmarshalMessageSetField(m protoreflect.Message, num wire.Number, v []byte, o UnmarshalOptions) error {
|
|
md := m.Descriptor()
|
|
if !md.ExtensionRanges().Has(num) {
|
|
return errUnknown
|
|
}
|
|
xt, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
|
|
if err == protoregistry.NotFound {
|
|
return errUnknown
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
xd := xt.TypeDescriptor()
|
|
if err := o.unmarshalMessage(v, m.Mutable(xd).Message()); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|