2019-06-20 10:12:23 -07:00
|
|
|
// 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 impl
|
|
|
|
|
|
|
|
import (
|
2019-06-19 15:22:13 -07:00
|
|
|
"fmt"
|
2019-06-20 10:12:23 -07:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2019-08-13 15:36:08 -07:00
|
|
|
"sync"
|
2019-06-20 10:12:23 -07:00
|
|
|
|
2019-06-19 15:22:13 -07:00
|
|
|
"google.golang.org/protobuf/internal/encoding/messageset"
|
2019-06-20 10:12:23 -07:00
|
|
|
"google.golang.org/protobuf/internal/encoding/wire"
|
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
piface "google.golang.org/protobuf/runtime/protoiface"
|
|
|
|
)
|
|
|
|
|
|
|
|
// coderMessageInfo contains per-message information used by the fast-path functions.
|
|
|
|
// This is a different type from MessageInfo to keep MessageInfo as general-purpose as
|
|
|
|
// possible.
|
|
|
|
type coderMessageInfo struct {
|
2019-08-13 15:36:08 -07:00
|
|
|
methods piface.Methods
|
|
|
|
|
2019-06-20 10:12:23 -07:00
|
|
|
orderedCoderFields []*coderFieldInfo
|
2019-06-27 10:54:42 -07:00
|
|
|
denseCoderFields []*coderFieldInfo
|
|
|
|
coderFields map[wire.Number]*coderFieldInfo
|
2019-06-20 10:12:23 -07:00
|
|
|
sizecacheOffset offset
|
|
|
|
unknownOffset offset
|
2019-07-06 13:05:11 -07:00
|
|
|
extensionOffset offset
|
2019-06-20 10:12:23 -07:00
|
|
|
needsInitCheck bool
|
2019-08-13 15:36:08 -07:00
|
|
|
|
|
|
|
extensionFieldInfosMu sync.RWMutex
|
|
|
|
extensionFieldInfos map[pref.ExtensionType]*extensionFieldInfo
|
2019-06-20 10:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type coderFieldInfo struct {
|
|
|
|
funcs pointerCoderFuncs // fast-path per-field functions
|
|
|
|
num pref.FieldNumber // field number
|
|
|
|
offset offset // struct field offset
|
|
|
|
wiretag uint64 // field tag (number + wire type)
|
|
|
|
tagsize int // size of the varint-encoded tag
|
|
|
|
isPointer bool // true if IsNil may be called on the struct field
|
|
|
|
isRequired bool // true if field is required
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:36:08 -07:00
|
|
|
func (mi *MessageInfo) makeCoderMethods(t reflect.Type, si structInfo) {
|
2019-07-06 13:02:14 -07:00
|
|
|
mi.sizecacheOffset = si.sizecacheOffset
|
|
|
|
mi.unknownOffset = si.unknownOffset
|
|
|
|
mi.extensionOffset = si.extensionOffset
|
2019-06-20 10:12:23 -07:00
|
|
|
|
2019-06-27 10:54:42 -07:00
|
|
|
mi.coderFields = make(map[wire.Number]*coderFieldInfo)
|
2019-08-06 15:43:25 -07:00
|
|
|
fields := mi.Desc.Fields()
|
2019-08-02 16:58:08 -07:00
|
|
|
for i := 0; i < fields.Len(); i++ {
|
|
|
|
fd := fields.Get(i)
|
2019-06-20 10:12:23 -07:00
|
|
|
|
|
|
|
fs := si.fieldsByNumber[fd.Number()]
|
2019-06-27 10:54:42 -07:00
|
|
|
if fd.ContainingOneof() != nil {
|
|
|
|
fs = si.oneofsByName[fd.ContainingOneof().Name()]
|
|
|
|
}
|
2019-06-20 10:12:23 -07:00
|
|
|
ft := fs.Type
|
|
|
|
var wiretag uint64
|
|
|
|
if !fd.IsPacked() {
|
|
|
|
wiretag = wire.EncodeTag(fd.Number(), wireTypes[fd.Kind()])
|
|
|
|
} else {
|
|
|
|
wiretag = wire.EncodeTag(fd.Number(), wire.BytesType)
|
|
|
|
}
|
2019-08-10 13:56:36 -07:00
|
|
|
var fieldOffset offset
|
2019-06-27 10:54:42 -07:00
|
|
|
var funcs pointerCoderFuncs
|
2019-08-10 13:56:36 -07:00
|
|
|
switch {
|
|
|
|
case fd.ContainingOneof() != nil:
|
|
|
|
fieldOffset = offsetOf(fs, mi.Exporter)
|
|
|
|
funcs = makeOneofFieldCoder(fd, si)
|
|
|
|
case fd.IsWeak():
|
|
|
|
fieldOffset = si.weakOffset
|
|
|
|
funcs = makeWeakMessageFieldCoder(fd)
|
|
|
|
default:
|
|
|
|
fieldOffset = offsetOf(fs, mi.Exporter)
|
2019-06-27 10:54:42 -07:00
|
|
|
funcs = fieldCoder(fd, ft)
|
|
|
|
}
|
|
|
|
cf := &coderFieldInfo{
|
2019-06-20 10:12:23 -07:00
|
|
|
num: fd.Number(),
|
2019-08-10 13:56:36 -07:00
|
|
|
offset: fieldOffset,
|
2019-06-20 10:12:23 -07:00
|
|
|
wiretag: wiretag,
|
|
|
|
tagsize: wire.SizeVarint(wiretag),
|
2019-06-27 10:54:42 -07:00
|
|
|
funcs: funcs,
|
2019-06-20 10:12:23 -07:00
|
|
|
isPointer: (fd.Cardinality() == pref.Repeated ||
|
|
|
|
fd.Kind() == pref.MessageKind ||
|
|
|
|
fd.Kind() == pref.GroupKind ||
|
|
|
|
fd.Syntax() != pref.Proto3),
|
|
|
|
isRequired: fd.Cardinality() == pref.Required,
|
2019-06-27 10:54:42 -07:00
|
|
|
}
|
|
|
|
mi.orderedCoderFields = append(mi.orderedCoderFields, cf)
|
|
|
|
mi.coderFields[cf.num] = cf
|
2019-06-20 10:12:23 -07:00
|
|
|
}
|
2019-08-06 15:43:25 -07:00
|
|
|
if messageset.IsMessageSet(mi.Desc) {
|
2019-06-19 15:22:13 -07:00
|
|
|
if !mi.extensionOffset.IsValid() {
|
2019-08-06 15:43:25 -07:00
|
|
|
panic(fmt.Sprintf("%v: MessageSet with no extensions field", mi.Desc.FullName()))
|
2019-06-19 15:22:13 -07:00
|
|
|
}
|
|
|
|
cf := &coderFieldInfo{
|
|
|
|
num: messageset.FieldItem,
|
|
|
|
offset: si.extensionOffset,
|
|
|
|
isPointer: true,
|
|
|
|
funcs: makeMessageSetFieldCoder(mi),
|
|
|
|
}
|
|
|
|
mi.orderedCoderFields = append(mi.orderedCoderFields, cf)
|
|
|
|
mi.coderFields[cf.num] = cf
|
|
|
|
// Invalidate the extension offset, since the field codec handles extensions.
|
|
|
|
mi.extensionOffset = invalidOffset
|
|
|
|
}
|
2019-06-20 10:12:23 -07:00
|
|
|
sort.Slice(mi.orderedCoderFields, func(i, j int) bool {
|
|
|
|
return mi.orderedCoderFields[i].num < mi.orderedCoderFields[j].num
|
|
|
|
})
|
|
|
|
|
2019-06-27 10:54:42 -07:00
|
|
|
var maxDense pref.FieldNumber
|
|
|
|
for _, cf := range mi.orderedCoderFields {
|
|
|
|
if cf.num >= 16 && cf.num >= 2*maxDense {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
maxDense = cf.num
|
|
|
|
}
|
|
|
|
mi.denseCoderFields = make([]*coderFieldInfo, maxDense+1)
|
|
|
|
for _, cf := range mi.orderedCoderFields {
|
|
|
|
if int(cf.num) > len(mi.denseCoderFields) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
mi.denseCoderFields[cf.num] = cf
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:43:25 -07:00
|
|
|
mi.needsInitCheck = needsInitCheck(mi.Desc)
|
2019-09-25 16:51:15 -07:00
|
|
|
if mi.methods.MarshalAppend == nil && mi.methods.Size == nil {
|
|
|
|
mi.methods.Flags |= piface.SupportMarshalDeterministic
|
|
|
|
mi.methods.MarshalAppend = mi.marshalAppend
|
|
|
|
mi.methods.Size = mi.size
|
|
|
|
}
|
|
|
|
if mi.methods.Unmarshal == nil {
|
|
|
|
mi.methods.Flags |= piface.SupportUnmarshalDiscardUnknown
|
|
|
|
mi.methods.Unmarshal = mi.unmarshal
|
|
|
|
}
|
|
|
|
if mi.methods.IsInitialized == nil {
|
|
|
|
mi.methods.IsInitialized = mi.isInitialized
|
2019-06-20 10:12:23 -07:00
|
|
|
}
|
|
|
|
}
|