types/dynamic: make Message implement legacy message interface

It's annoying for some users that they can't directly pass
*dynamicpb.Message to APIs that expect the legacy message interfaces.
The proto.MessageV1 wrappers work, but is somewhat suspect since they
were originally designed to wrap legacy generated messages.

Change-Id: I0f9900dcd1c9865c754551f8763680c9bb904813
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/223817
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2020-03-17 14:22:06 -07:00
parent b4c73aa919
commit 29677a9c11

View File

@ -36,6 +36,12 @@ type Message struct {
unknown pref.RawFields
}
var (
_ pref.Message = (*Message)(nil)
_ pref.ProtoMessage = (*Message)(nil)
_ protoiface.MessageV1 = (*Message)(nil)
)
// NewMessage creates a new message with the provided descriptor.
func NewMessage(desc pref.MessageDescriptor) *Message {
return &Message{
@ -45,6 +51,9 @@ func NewMessage(desc pref.MessageDescriptor) *Message {
}
}
// ProtoMessage implements the legacy message interface.
func (m *Message) ProtoMessage() {}
// ProtoReflect implements the protoreflect.ProtoMessage interface.
func (m *Message) ProtoReflect() pref.Message {
return m
@ -55,6 +64,13 @@ func (m *Message) String() string {
return protoimpl.X.MessageStringOf(m)
}
// Reset clears the message to be empty, but preserves the dynamic message type.
func (m *Message) Reset() {
m.known = make(map[pref.FieldNumber]pref.Value)
m.ext = make(map[pref.FieldNumber]pref.FieldDescriptor)
m.unknown = nil
}
// Descriptor returns the message descriptor.
func (m *Message) Descriptor() pref.MessageDescriptor {
return m.typ.desc