proto, internal/impl: store unknown MessageSet items in non-mset format
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>
diff --git a/internal/impl/codec_message.go b/internal/impl/codec_message.go
index 4694718..ab1d4ec 100644
--- a/internal/impl/codec_message.go
+++ b/internal/impl/codec_message.go
@@ -29,6 +29,7 @@
unknownOffset offset
extensionOffset offset
needsInitCheck bool
+ isMessageSet bool
extensionFieldInfosMu sync.RWMutex
extensionFieldInfos map[pref.ExtensionType]*extensionFieldInfo
@@ -97,16 +98,10 @@
if !mi.extensionOffset.IsValid() {
panic(fmt.Sprintf("%v: MessageSet with no extensions field", mi.Desc.FullName()))
}
- cf := &coderFieldInfo{
- num: messageset.FieldItem,
- offset: si.extensionOffset,
- isPointer: true,
- funcs: makeMessageSetFieldCoder(mi),
+ if !mi.unknownOffset.IsValid() {
+ panic(fmt.Sprintf("%v: MessageSet with no unknown field", mi.Desc.FullName()))
}
- mi.orderedCoderFields = append(mi.orderedCoderFields, cf)
- mi.coderFields[cf.num] = cf
- // Invalidate the extension offset, since the field codec handles extensions.
- mi.extensionOffset = invalidOffset
+ mi.isMessageSet = true
}
sort.Slice(mi.orderedCoderFields, func(i, j int) bool {
return mi.orderedCoderFields[i].num < mi.orderedCoderFields[j].num