From 29677a9c119abeb49579e132b0482ced314018e9 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 17 Mar 2020 14:22:06 -0700 Subject: [PATCH] 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 --- types/dynamicpb/dynamic.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/types/dynamicpb/dynamic.go b/types/dynamicpb/dynamic.go index f206c18e..86371cfb 100644 --- a/types/dynamicpb/dynamic.go +++ b/types/dynamicpb/dynamic.go @@ -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