2018-09-12 16:20:37 -07:00
|
|
|
// Copyright 2018 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 (
|
2018-09-13 14:24:37 -07:00
|
|
|
"fmt"
|
2018-09-12 16:20:37 -07:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-09-13 14:24:37 -07:00
|
|
|
"sync"
|
2019-04-01 13:49:56 -07:00
|
|
|
"sync/atomic"
|
2018-09-12 16:20:37 -07:00
|
|
|
|
2019-05-13 23:55:40 -07:00
|
|
|
pvalue "google.golang.org/protobuf/internal/value"
|
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
piface "google.golang.org/protobuf/runtime/protoiface"
|
2018-09-12 16:20:37 -07:00
|
|
|
)
|
|
|
|
|
2019-05-22 05:12:36 -04:00
|
|
|
// MessageInfo provides protobuf related functionality for a given Go type
|
|
|
|
// that represents a message. A given instance of MessageInfo is tied to
|
2018-09-13 14:24:37 -07:00
|
|
|
// exactly one Go type, which must be a pointer to a struct type.
|
2019-05-22 05:12:36 -04:00
|
|
|
type MessageInfo struct {
|
internal/fileinit: generate reflect data structures from raw descriptors
This CL takes a significantly different approach to generating support
for protobuf reflection. The previous approach involved generating a
large number of Go literals to represent the reflection information.
While that approach was correct, it resulted in too much binary bloat.
The approach taken here initializes the reflection information from
the raw descriptor proto, which is a relatively dense representation
of the protobuf reflection information. In order to keep initialization
cost low, several measures were taken:
* At program init, the bare minimum is parsed in order to initialize
naming information for enums, messages, extensions, and services declared
in the file. This is done because those top-level declarations are often
relevant for registration.
* Only upon first are most of the other data structures for protobuf
reflection actually initialized.
* Instead of using proto.Unmarshal, a hand-written unmarshaler is used.
This allows us to avoid a dependendency on the descriptor proto and also
because the API for the descriptor proto is fundamentally non-performant
since it requires an allocation for every primitive field.
At a high-level, the new implementation lives in internal/fileinit.
Several changes were made to other parts of the repository:
* cmd/protoc-gen-go:
* Stop compressing the raw descriptors. While compression does reduce
the size of the descriptors by approximately 2x, it is a pre-mature
optimization since the descriptors themselves are around 1% of the total
binary bloat that is due to generated protobufs.
* Seeding protobuf reflection from the raw descriptor significantly
simplifies the generator implementation since it is no longer responsible
for constructing a tree of Go literals to represent the same information.
* We remove the generation of the shadow types and instead call
protoimpl.MessageType.MessageOf. Unfortunately, this incurs an allocation
for every call to ProtoReflect since we need to allocate a tuple that wraps
a pointer to the message value, and a pointer to message type.
* internal/impl:
* We add a MessageType.GoType field and make it required that it is
set prior to first use. This is done so that we can avoid calling
MessageType.init except for when it is actually needed. The allows code
to call (*FooMessage)(nil).ProtoReflect().Type() without fearing that the
init code will run, possibly triggering a recursive deadlock (where the
init code depends on getting the Type of some dependency which may be
declared within the same file).
* internal/cmd/generate-types:
* The code to generate reflect/prototype/protofile_list_gen.go was copied
and altered to generated internal/fileinit.desc_list_gen.go.
At a high-level this CL adds significant technical complexity.
However, this is offset by several possible future changes:
* The prototype package can be drastically simplified. We can probably
reimplement internal/legacy to use internal/fileinit instead, allowing us
to drop another dependency on the prototype package. As a result, we can
probably delete most of the constructor types in that package.
* With the prototype package significantly pruned, and the fact that generated
code no longer depend on depends on that package, we can consider merging
what's left of prototype into protodesc.
Change-Id: I6090f023f2e1b6afaf62bd3ae883566242e30715
Reviewed-on: https://go-review.googlesource.com/c/158539
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-01-18 09:32:24 -08:00
|
|
|
// GoType is the underlying message Go type and must be populated.
|
2018-09-13 14:24:37 -07:00
|
|
|
// Once set, this field must never be mutated.
|
internal/fileinit: generate reflect data structures from raw descriptors
This CL takes a significantly different approach to generating support
for protobuf reflection. The previous approach involved generating a
large number of Go literals to represent the reflection information.
While that approach was correct, it resulted in too much binary bloat.
The approach taken here initializes the reflection information from
the raw descriptor proto, which is a relatively dense representation
of the protobuf reflection information. In order to keep initialization
cost low, several measures were taken:
* At program init, the bare minimum is parsed in order to initialize
naming information for enums, messages, extensions, and services declared
in the file. This is done because those top-level declarations are often
relevant for registration.
* Only upon first are most of the other data structures for protobuf
reflection actually initialized.
* Instead of using proto.Unmarshal, a hand-written unmarshaler is used.
This allows us to avoid a dependendency on the descriptor proto and also
because the API for the descriptor proto is fundamentally non-performant
since it requires an allocation for every primitive field.
At a high-level, the new implementation lives in internal/fileinit.
Several changes were made to other parts of the repository:
* cmd/protoc-gen-go:
* Stop compressing the raw descriptors. While compression does reduce
the size of the descriptors by approximately 2x, it is a pre-mature
optimization since the descriptors themselves are around 1% of the total
binary bloat that is due to generated protobufs.
* Seeding protobuf reflection from the raw descriptor significantly
simplifies the generator implementation since it is no longer responsible
for constructing a tree of Go literals to represent the same information.
* We remove the generation of the shadow types and instead call
protoimpl.MessageType.MessageOf. Unfortunately, this incurs an allocation
for every call to ProtoReflect since we need to allocate a tuple that wraps
a pointer to the message value, and a pointer to message type.
* internal/impl:
* We add a MessageType.GoType field and make it required that it is
set prior to first use. This is done so that we can avoid calling
MessageType.init except for when it is actually needed. The allows code
to call (*FooMessage)(nil).ProtoReflect().Type() without fearing that the
init code will run, possibly triggering a recursive deadlock (where the
init code depends on getting the Type of some dependency which may be
declared within the same file).
* internal/cmd/generate-types:
* The code to generate reflect/prototype/protofile_list_gen.go was copied
and altered to generated internal/fileinit.desc_list_gen.go.
At a high-level this CL adds significant technical complexity.
However, this is offset by several possible future changes:
* The prototype package can be drastically simplified. We can probably
reimplement internal/legacy to use internal/fileinit instead, allowing us
to drop another dependency on the prototype package. As a result, we can
probably delete most of the constructor types in that package.
* With the prototype package significantly pruned, and the fact that generated
code no longer depend on depends on that package, we can consider merging
what's left of prototype into protodesc.
Change-Id: I6090f023f2e1b6afaf62bd3ae883566242e30715
Reviewed-on: https://go-review.googlesource.com/c/158539
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-01-18 09:32:24 -08:00
|
|
|
GoType reflect.Type // pointer to struct
|
2018-09-13 14:24:37 -07:00
|
|
|
|
internal/fileinit: generate reflect data structures from raw descriptors
This CL takes a significantly different approach to generating support
for protobuf reflection. The previous approach involved generating a
large number of Go literals to represent the reflection information.
While that approach was correct, it resulted in too much binary bloat.
The approach taken here initializes the reflection information from
the raw descriptor proto, which is a relatively dense representation
of the protobuf reflection information. In order to keep initialization
cost low, several measures were taken:
* At program init, the bare minimum is parsed in order to initialize
naming information for enums, messages, extensions, and services declared
in the file. This is done because those top-level declarations are often
relevant for registration.
* Only upon first are most of the other data structures for protobuf
reflection actually initialized.
* Instead of using proto.Unmarshal, a hand-written unmarshaler is used.
This allows us to avoid a dependendency on the descriptor proto and also
because the API for the descriptor proto is fundamentally non-performant
since it requires an allocation for every primitive field.
At a high-level, the new implementation lives in internal/fileinit.
Several changes were made to other parts of the repository:
* cmd/protoc-gen-go:
* Stop compressing the raw descriptors. While compression does reduce
the size of the descriptors by approximately 2x, it is a pre-mature
optimization since the descriptors themselves are around 1% of the total
binary bloat that is due to generated protobufs.
* Seeding protobuf reflection from the raw descriptor significantly
simplifies the generator implementation since it is no longer responsible
for constructing a tree of Go literals to represent the same information.
* We remove the generation of the shadow types and instead call
protoimpl.MessageType.MessageOf. Unfortunately, this incurs an allocation
for every call to ProtoReflect since we need to allocate a tuple that wraps
a pointer to the message value, and a pointer to message type.
* internal/impl:
* We add a MessageType.GoType field and make it required that it is
set prior to first use. This is done so that we can avoid calling
MessageType.init except for when it is actually needed. The allows code
to call (*FooMessage)(nil).ProtoReflect().Type() without fearing that the
init code will run, possibly triggering a recursive deadlock (where the
init code depends on getting the Type of some dependency which may be
declared within the same file).
* internal/cmd/generate-types:
* The code to generate reflect/prototype/protofile_list_gen.go was copied
and altered to generated internal/fileinit.desc_list_gen.go.
At a high-level this CL adds significant technical complexity.
However, this is offset by several possible future changes:
* The prototype package can be drastically simplified. We can probably
reimplement internal/legacy to use internal/fileinit instead, allowing us
to drop another dependency on the prototype package. As a result, we can
probably delete most of the constructor types in that package.
* With the prototype package significantly pruned, and the fact that generated
code no longer depend on depends on that package, we can consider merging
what's left of prototype into protodesc.
Change-Id: I6090f023f2e1b6afaf62bd3ae883566242e30715
Reviewed-on: https://go-review.googlesource.com/c/158539
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-01-18 09:32:24 -08:00
|
|
|
// PBType is the underlying message descriptor type and must be populated.
|
|
|
|
// Once set, this field must never be mutated.
|
|
|
|
PBType pref.MessageType
|
2018-09-13 14:24:37 -07:00
|
|
|
|
2019-04-01 13:49:56 -07:00
|
|
|
initMu sync.Mutex // protects all unexported fields
|
|
|
|
initDone uint32
|
|
|
|
|
2019-06-20 10:12:23 -07:00
|
|
|
fields map[pref.FieldNumber]*fieldInfo
|
2019-04-03 13:40:53 -07:00
|
|
|
oneofs map[pref.Name]*oneofInfo
|
2018-10-23 18:31:18 -07:00
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
getUnknown func(pointer) pref.RawFields
|
|
|
|
setUnknown func(pointer, pref.RawFields)
|
|
|
|
|
|
|
|
extensionMap func(pointer) *extensionMap
|
|
|
|
|
2019-06-20 10:12:23 -07:00
|
|
|
// Information used by the fast-path methods.
|
2019-06-17 12:30:25 -07:00
|
|
|
methods piface.Methods
|
2019-06-20 10:12:23 -07:00
|
|
|
coderMessageInfo
|
2019-04-01 13:49:56 -07:00
|
|
|
|
|
|
|
extensionFieldInfosMu sync.RWMutex
|
2019-06-04 16:20:00 -07:00
|
|
|
extensionFieldInfos map[pref.ExtensionType]*extensionFieldInfo
|
2019-04-01 13:49:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var prefMessageType = reflect.TypeOf((*pref.Message)(nil)).Elem()
|
|
|
|
|
2019-05-22 05:12:36 -04:00
|
|
|
// getMessageInfo returns the MessageInfo (if any) for a type.
|
2019-04-01 13:49:56 -07:00
|
|
|
//
|
2019-05-22 05:12:36 -04:00
|
|
|
// We find the MessageInfo by calling the ProtoReflect method on the type's
|
2019-04-01 13:49:56 -07:00
|
|
|
// zero value and looking at the returned type to see if it is a
|
2019-05-22 05:12:36 -04:00
|
|
|
// messageReflectWrapper. Note that the MessageInfo may still be uninitialized
|
2019-04-01 13:49:56 -07:00
|
|
|
// at this point.
|
2019-05-22 05:12:36 -04:00
|
|
|
func getMessageInfo(mt reflect.Type) (mi *MessageInfo, ok bool) {
|
2019-04-01 13:49:56 -07:00
|
|
|
method, ok := mt.MethodByName("ProtoReflect")
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if method.Type.NumIn() != 1 || method.Type.NumOut() != 1 || method.Type.Out(0) != prefMessageType {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
ret := reflect.Zero(mt).Method(method.Index).Call(nil)
|
|
|
|
m, ok := ret[0].Elem().Interface().(*messageReflectWrapper)
|
|
|
|
if !ok {
|
|
|
|
return nil, ok
|
|
|
|
}
|
|
|
|
return m.mi, true
|
2018-09-12 16:20:37 -07:00
|
|
|
}
|
|
|
|
|
2019-05-22 05:12:36 -04:00
|
|
|
func (mi *MessageInfo) init() {
|
2019-04-01 13:49:56 -07:00
|
|
|
// This function is called in the hot path. Inline the sync.Once
|
|
|
|
// logic, since allocating a closure for Once.Do is expensive.
|
|
|
|
// Keep init small to ensure that it can be inlined.
|
|
|
|
if atomic.LoadUint32(&mi.initDone) == 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mi.initOnce()
|
|
|
|
}
|
2018-09-13 14:24:37 -07:00
|
|
|
|
2019-05-22 05:12:36 -04:00
|
|
|
func (mi *MessageInfo) initOnce() {
|
2019-04-01 13:49:56 -07:00
|
|
|
mi.initMu.Lock()
|
|
|
|
defer mi.initMu.Unlock()
|
|
|
|
if mi.initDone == 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t := mi.GoType
|
|
|
|
if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
|
|
|
|
panic(fmt.Sprintf("got %v, want *struct kind", t))
|
|
|
|
}
|
|
|
|
|
|
|
|
si := mi.makeStructInfo(t.Elem())
|
|
|
|
mi.makeKnownFieldsFunc(si)
|
2019-07-06 13:02:14 -07:00
|
|
|
mi.makeUnknownFieldsFunc(t.Elem(), si)
|
|
|
|
mi.makeExtensionFieldsFunc(t.Elem(), si)
|
2019-06-20 10:12:23 -07:00
|
|
|
mi.makeMethods(t.Elem(), si)
|
2019-04-01 13:49:56 -07:00
|
|
|
|
|
|
|
atomic.StoreUint32(&mi.initDone, 1)
|
|
|
|
}
|
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
type (
|
|
|
|
SizeCache = int32
|
|
|
|
UnknownFields = []byte
|
|
|
|
ExtensionFields = map[int32]ExtensionField
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
sizecacheType = reflect.TypeOf(SizeCache(0))
|
|
|
|
unknownFieldsType = reflect.TypeOf(UnknownFields(nil))
|
|
|
|
extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
|
|
|
|
)
|
2019-04-01 13:49:56 -07:00
|
|
|
|
2019-05-09 11:33:55 -07:00
|
|
|
type structInfo struct {
|
2019-07-06 13:02:14 -07:00
|
|
|
sizecacheOffset offset
|
|
|
|
extensionOffset offset
|
|
|
|
unknownOffset offset
|
|
|
|
|
2019-05-09 11:33:55 -07:00
|
|
|
fieldsByNumber map[pref.FieldNumber]reflect.StructField
|
|
|
|
oneofsByName map[pref.Name]reflect.StructField
|
|
|
|
oneofWrappersByType map[reflect.Type]pref.FieldNumber
|
|
|
|
oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
|
|
|
|
}
|
|
|
|
|
2019-05-22 05:12:36 -04:00
|
|
|
func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
|
2019-05-09 11:33:55 -07:00
|
|
|
si := structInfo{
|
2019-07-06 13:02:14 -07:00
|
|
|
sizecacheOffset: invalidOffset,
|
|
|
|
extensionOffset: invalidOffset,
|
|
|
|
unknownOffset: invalidOffset,
|
|
|
|
|
2019-05-09 11:33:55 -07:00
|
|
|
fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
|
|
|
|
oneofsByName: map[pref.Name]reflect.StructField{},
|
|
|
|
oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
|
|
|
|
oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
|
|
|
|
}
|
2019-07-06 13:02:14 -07:00
|
|
|
|
|
|
|
if f, _ := t.FieldByName("XXX_sizecache"); f.Type == sizecacheType {
|
|
|
|
si.sizecacheOffset = offsetOf(f)
|
|
|
|
}
|
|
|
|
if f, _ := t.FieldByName("XXX_InternalExtensions"); f.Type == extensionFieldsType {
|
|
|
|
si.extensionOffset = offsetOf(f)
|
|
|
|
}
|
|
|
|
if f, _ := t.FieldByName("XXX_extensions"); f.Type == extensionFieldsType {
|
|
|
|
si.extensionOffset = offsetOf(f)
|
|
|
|
}
|
|
|
|
if f, _ := t.FieldByName("XXX_unrecognized"); f.Type == unknownFieldsType {
|
|
|
|
si.unknownOffset = offsetOf(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a mapping of field numbers and names to Go struct field or type.
|
2018-09-12 16:20:37 -07:00
|
|
|
fieldLoop:
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
f := t.Field(i)
|
|
|
|
for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
|
|
|
|
if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
|
|
|
|
n, _ := strconv.ParseUint(s, 10, 64)
|
2019-05-09 11:33:55 -07:00
|
|
|
si.fieldsByNumber[pref.FieldNumber(n)] = f
|
2018-09-12 16:20:37 -07:00
|
|
|
continue fieldLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
|
2019-05-09 11:33:55 -07:00
|
|
|
si.oneofsByName[pref.Name(s)] = f
|
2018-09-12 16:20:37 -07:00
|
|
|
continue fieldLoop
|
|
|
|
}
|
|
|
|
}
|
2019-07-06 13:02:14 -07:00
|
|
|
|
|
|
|
// Derive a mapping of oneof wrappers to fields.
|
2018-11-26 12:57:27 -08:00
|
|
|
var oneofWrappers []interface{}
|
2018-10-17 11:46:52 -07:00
|
|
|
if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
|
2018-11-26 12:57:27 -08:00
|
|
|
oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
|
|
|
|
}
|
|
|
|
if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
|
|
|
|
oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
|
|
|
|
}
|
|
|
|
for _, v := range oneofWrappers {
|
|
|
|
tf := reflect.TypeOf(v).Elem()
|
|
|
|
f := tf.Field(0)
|
|
|
|
for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
|
|
|
|
if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
|
|
|
|
n, _ := strconv.ParseUint(s, 10, 64)
|
2019-05-09 11:33:55 -07:00
|
|
|
si.oneofWrappersByType[tf] = pref.FieldNumber(n)
|
|
|
|
si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
|
2018-11-26 12:57:27 -08:00
|
|
|
break
|
2018-09-12 16:20:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-06 13:02:14 -07:00
|
|
|
|
2019-05-09 11:33:55 -07:00
|
|
|
return si
|
|
|
|
}
|
2018-09-12 16:20:37 -07:00
|
|
|
|
2019-05-09 11:33:55 -07:00
|
|
|
// makeKnownFieldsFunc generates functions for operations that can be performed
|
|
|
|
// on each protobuf message field. It takes in a reflect.Type representing the
|
|
|
|
// Go struct and matches message fields with struct fields.
|
|
|
|
//
|
|
|
|
// This code assumes that the struct is well-formed and panics if there are
|
|
|
|
// any discrepancies.
|
2019-05-22 05:12:36 -04:00
|
|
|
func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
|
2018-09-12 16:20:37 -07:00
|
|
|
mi.fields = map[pref.FieldNumber]*fieldInfo{}
|
2019-05-01 12:29:25 -07:00
|
|
|
for i := 0; i < mi.PBType.Descriptor().Fields().Len(); i++ {
|
|
|
|
fd := mi.PBType.Descriptor().Fields().Get(i)
|
2019-05-09 11:33:55 -07:00
|
|
|
fs := si.fieldsByNumber[fd.Number()]
|
2018-09-12 16:20:37 -07:00
|
|
|
var fi fieldInfo
|
|
|
|
switch {
|
2019-05-13 14:32:56 -07:00
|
|
|
case fd.ContainingOneof() != nil:
|
|
|
|
fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], si.oneofWrappersByNumber[fd.Number()])
|
2018-09-12 16:20:37 -07:00
|
|
|
case fd.IsMap():
|
|
|
|
fi = fieldInfoForMap(fd, fs)
|
2019-05-13 14:32:56 -07:00
|
|
|
case fd.IsList():
|
all: rename Vector as List
The terminology Vector does not occur in protobuf documentation at all,
so we should rename the Go use of the term to something more recognizable.
As such, all instances that match the regexp "[Vv]ect(or)?" were replaced.
The C++ documentation uses the term "Repeated", which is a reasonable name.
However, the term became overloaded in 2014, when maps were added as a feature
and implementated under the hood as repeated fields. This is confusing as it
means "repeated" could either refer to repeated fields proper (i.e., explicitly
marked with the "repeated" label in the proto file) or map fields. In the case
of the C++ reflective API, this is not a problem since repeated fields proper
and map fields are interacted with through the same RepeatedField type.
In Go, we do not use a single type to handle both types of repeated fields:
1) We are coming up with the Go protobuf reflection API for the first time
and so do not need to piggy-back on the repeated fields API to remain backwards
compatible since no former usages of Go protobuf reflection exists.
2) Map fields are commonly represented in Go as the Go map type, which do not
preserve ordering information. As such it is fundamentally impossible to present
an unordered map as a consistently ordered list. Thus, Go needs two different
interfaces for lists and maps.
Given the above situation, "Repeated" is not a great term to use since it
refers to two different things (when we only want one of the meanings).
To distinguish between the two, we'll use the terms "List" and "Map" instead.
There is some precedence for the term "List" in the protobuf codebase
(e.g., "getRepeatedInt32List").
Change-Id: Iddcdb6b78e1e60c14fa4ca213c15f45e214b967b
Reviewed-on: https://go-review.googlesource.com/c/149657
Reviewed-by: Damien Neil <dneil@google.com>
2018-11-14 14:05:19 -08:00
|
|
|
fi = fieldInfoForList(fd, fs)
|
2018-09-13 14:24:37 -07:00
|
|
|
case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
|
2018-09-12 16:20:37 -07:00
|
|
|
fi = fieldInfoForMessage(fd, fs)
|
2018-09-13 14:24:37 -07:00
|
|
|
default:
|
|
|
|
fi = fieldInfoForScalar(fd, fs)
|
2018-09-12 16:20:37 -07:00
|
|
|
}
|
|
|
|
mi.fields[fd.Number()] = &fi
|
|
|
|
}
|
2019-04-03 13:40:53 -07:00
|
|
|
|
|
|
|
mi.oneofs = map[pref.Name]*oneofInfo{}
|
2019-05-01 12:29:25 -07:00
|
|
|
for i := 0; i < mi.PBType.Descriptor().Oneofs().Len(); i++ {
|
|
|
|
od := mi.PBType.Descriptor().Oneofs().Get(i)
|
2019-05-09 11:33:55 -07:00
|
|
|
mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], si.oneofWrappersByType)
|
2019-04-03 13:40:53 -07:00
|
|
|
}
|
2018-09-12 16:20:37 -07:00
|
|
|
}
|
2018-09-13 14:24:37 -07:00
|
|
|
|
2019-07-06 13:02:14 -07:00
|
|
|
func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) {
|
2019-04-25 23:48:08 -07:00
|
|
|
mi.getUnknown = func(pointer) pref.RawFields { return nil }
|
|
|
|
mi.setUnknown = func(pointer, pref.RawFields) { return }
|
2019-07-06 13:02:14 -07:00
|
|
|
if si.unknownOffset.IsValid() {
|
2019-04-25 23:48:08 -07:00
|
|
|
mi.getUnknown = func(p pointer) pref.RawFields {
|
|
|
|
if p.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-06 13:02:14 -07:00
|
|
|
rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
|
2019-04-25 23:48:08 -07:00
|
|
|
return pref.RawFields(*rv.Interface().(*[]byte))
|
|
|
|
}
|
|
|
|
mi.setUnknown = func(p pointer, b pref.RawFields) {
|
|
|
|
if p.IsNil() {
|
|
|
|
panic("invalid SetUnknown on nil Message")
|
|
|
|
}
|
2019-07-06 13:02:14 -07:00
|
|
|
rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
|
2019-04-25 23:48:08 -07:00
|
|
|
*rv.Interface().(*[]byte) = []byte(b)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mi.getUnknown = func(pointer) pref.RawFields {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
mi.setUnknown = func(p pointer, _ pref.RawFields) {
|
|
|
|
if p.IsNil() {
|
|
|
|
panic("invalid SetUnknown on nil Message")
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 18:31:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 13:02:14 -07:00
|
|
|
func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type, si structInfo) {
|
|
|
|
if si.extensionOffset.IsValid() {
|
2019-04-25 23:48:08 -07:00
|
|
|
mi.extensionMap = func(p pointer) *extensionMap {
|
2019-06-06 13:01:53 -07:00
|
|
|
if p.IsNil() {
|
|
|
|
return (*extensionMap)(nil)
|
|
|
|
}
|
2019-07-06 13:02:14 -07:00
|
|
|
v := p.Apply(si.extensionOffset).AsValueOf(extensionFieldsType)
|
2019-04-25 23:48:08 -07:00
|
|
|
return (*extensionMap)(v.Interface().(*map[int32]ExtensionField))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mi.extensionMap = func(pointer) *extensionMap {
|
|
|
|
return (*extensionMap)(nil)
|
|
|
|
}
|
2018-10-23 18:31:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 05:12:36 -04:00
|
|
|
func (mi *MessageInfo) MessageOf(p interface{}) pref.Message {
|
2019-03-11 13:45:14 -07:00
|
|
|
return (*messageReflectWrapper)(mi.dataTypeOf(p))
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
2019-05-22 05:12:36 -04:00
|
|
|
func (mi *MessageInfo) Methods() *piface.Methods {
|
2019-04-01 13:49:56 -07:00
|
|
|
mi.init()
|
|
|
|
return &mi.methods
|
2019-04-01 13:31:55 -07:00
|
|
|
}
|
|
|
|
|
2019-05-22 05:12:36 -04:00
|
|
|
func (mi *MessageInfo) dataTypeOf(p interface{}) *messageDataType {
|
internal/fileinit: generate reflect data structures from raw descriptors
This CL takes a significantly different approach to generating support
for protobuf reflection. The previous approach involved generating a
large number of Go literals to represent the reflection information.
While that approach was correct, it resulted in too much binary bloat.
The approach taken here initializes the reflection information from
the raw descriptor proto, which is a relatively dense representation
of the protobuf reflection information. In order to keep initialization
cost low, several measures were taken:
* At program init, the bare minimum is parsed in order to initialize
naming information for enums, messages, extensions, and services declared
in the file. This is done because those top-level declarations are often
relevant for registration.
* Only upon first are most of the other data structures for protobuf
reflection actually initialized.
* Instead of using proto.Unmarshal, a hand-written unmarshaler is used.
This allows us to avoid a dependendency on the descriptor proto and also
because the API for the descriptor proto is fundamentally non-performant
since it requires an allocation for every primitive field.
At a high-level, the new implementation lives in internal/fileinit.
Several changes were made to other parts of the repository:
* cmd/protoc-gen-go:
* Stop compressing the raw descriptors. While compression does reduce
the size of the descriptors by approximately 2x, it is a pre-mature
optimization since the descriptors themselves are around 1% of the total
binary bloat that is due to generated protobufs.
* Seeding protobuf reflection from the raw descriptor significantly
simplifies the generator implementation since it is no longer responsible
for constructing a tree of Go literals to represent the same information.
* We remove the generation of the shadow types and instead call
protoimpl.MessageType.MessageOf. Unfortunately, this incurs an allocation
for every call to ProtoReflect since we need to allocate a tuple that wraps
a pointer to the message value, and a pointer to message type.
* internal/impl:
* We add a MessageType.GoType field and make it required that it is
set prior to first use. This is done so that we can avoid calling
MessageType.init except for when it is actually needed. The allows code
to call (*FooMessage)(nil).ProtoReflect().Type() without fearing that the
init code will run, possibly triggering a recursive deadlock (where the
init code depends on getting the Type of some dependency which may be
declared within the same file).
* internal/cmd/generate-types:
* The code to generate reflect/prototype/protofile_list_gen.go was copied
and altered to generated internal/fileinit.desc_list_gen.go.
At a high-level this CL adds significant technical complexity.
However, this is offset by several possible future changes:
* The prototype package can be drastically simplified. We can probably
reimplement internal/legacy to use internal/fileinit instead, allowing us
to drop another dependency on the prototype package. As a result, we can
probably delete most of the constructor types in that package.
* With the prototype package significantly pruned, and the fact that generated
code no longer depend on depends on that package, we can consider merging
what's left of prototype into protodesc.
Change-Id: I6090f023f2e1b6afaf62bd3ae883566242e30715
Reviewed-on: https://go-review.googlesource.com/c/158539
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-01-18 09:32:24 -08:00
|
|
|
// TODO: Remove this check? This API is primarily used by generated code,
|
|
|
|
// and should not violate this assumption. Leave this check in for now to
|
|
|
|
// provide some sanity checks during development. This can be removed if
|
|
|
|
// it proves to be detrimental to performance.
|
|
|
|
if reflect.TypeOf(p) != mi.GoType {
|
|
|
|
panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.GoType))
|
|
|
|
}
|
2018-12-01 04:57:09 -08:00
|
|
|
return &messageDataType{pointerOfIface(p), mi}
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// messageDataType is a tuple of a pointer to the message data and
|
|
|
|
// a pointer to the message type.
|
|
|
|
//
|
2019-05-22 05:12:36 -04:00
|
|
|
// TODO: Unfortunately, we need to close over a pointer and MessageInfo,
|
2018-09-13 14:24:37 -07:00
|
|
|
// which incurs an an allocation. This pair is similar to a Go interface,
|
|
|
|
// which is essentially a tuple of the same thing. We can make this efficient
|
|
|
|
// with reflect.NamedOf (see https://golang.org/issues/16522).
|
|
|
|
//
|
|
|
|
// With that hypothetical API, we could dynamically create a new named type
|
2019-05-22 05:12:36 -04:00
|
|
|
// that has the same underlying type as MessageInfo.GoType, and
|
|
|
|
// dynamically create methods that close over MessageInfo.
|
2018-09-13 14:24:37 -07:00
|
|
|
// Since the new type would have the same underlying type, we could directly
|
|
|
|
// convert between pointers of those types, giving us an efficient way to swap
|
|
|
|
// out the method set.
|
|
|
|
//
|
|
|
|
// Barring the ability to dynamically create named types, the workaround is
|
|
|
|
// 1. either to accept the cost of an allocation for this wrapper struct or
|
|
|
|
// 2. generate more types and methods, at the expense of binary size increase.
|
|
|
|
type messageDataType struct {
|
|
|
|
p pointer
|
2019-05-22 05:12:36 -04:00
|
|
|
mi *MessageInfo
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
|
|
|
|
2019-03-11 13:45:14 -07:00
|
|
|
type messageReflectWrapper messageDataType
|
2018-11-26 22:32:06 -08:00
|
|
|
|
2019-05-01 12:29:25 -07:00
|
|
|
func (m *messageReflectWrapper) Descriptor() pref.MessageDescriptor {
|
|
|
|
return m.mi.PBType.Descriptor()
|
|
|
|
}
|
|
|
|
func (m *messageReflectWrapper) New() pref.Message {
|
|
|
|
return m.mi.PBType.New()
|
|
|
|
}
|
2019-03-11 13:45:14 -07:00
|
|
|
func (m *messageReflectWrapper) Interface() pref.ProtoMessage {
|
2018-11-26 22:32:06 -08:00
|
|
|
if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
|
|
|
|
return m
|
|
|
|
}
|
2019-03-11 13:45:14 -07:00
|
|
|
return (*messageIfaceWrapper)(m)
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
2019-03-11 13:45:14 -07:00
|
|
|
func (m *messageReflectWrapper) ProtoUnwrap() interface{} {
|
internal/fileinit: generate reflect data structures from raw descriptors
This CL takes a significantly different approach to generating support
for protobuf reflection. The previous approach involved generating a
large number of Go literals to represent the reflection information.
While that approach was correct, it resulted in too much binary bloat.
The approach taken here initializes the reflection information from
the raw descriptor proto, which is a relatively dense representation
of the protobuf reflection information. In order to keep initialization
cost low, several measures were taken:
* At program init, the bare minimum is parsed in order to initialize
naming information for enums, messages, extensions, and services declared
in the file. This is done because those top-level declarations are often
relevant for registration.
* Only upon first are most of the other data structures for protobuf
reflection actually initialized.
* Instead of using proto.Unmarshal, a hand-written unmarshaler is used.
This allows us to avoid a dependendency on the descriptor proto and also
because the API for the descriptor proto is fundamentally non-performant
since it requires an allocation for every primitive field.
At a high-level, the new implementation lives in internal/fileinit.
Several changes were made to other parts of the repository:
* cmd/protoc-gen-go:
* Stop compressing the raw descriptors. While compression does reduce
the size of the descriptors by approximately 2x, it is a pre-mature
optimization since the descriptors themselves are around 1% of the total
binary bloat that is due to generated protobufs.
* Seeding protobuf reflection from the raw descriptor significantly
simplifies the generator implementation since it is no longer responsible
for constructing a tree of Go literals to represent the same information.
* We remove the generation of the shadow types and instead call
protoimpl.MessageType.MessageOf. Unfortunately, this incurs an allocation
for every call to ProtoReflect since we need to allocate a tuple that wraps
a pointer to the message value, and a pointer to message type.
* internal/impl:
* We add a MessageType.GoType field and make it required that it is
set prior to first use. This is done so that we can avoid calling
MessageType.init except for when it is actually needed. The allows code
to call (*FooMessage)(nil).ProtoReflect().Type() without fearing that the
init code will run, possibly triggering a recursive deadlock (where the
init code depends on getting the Type of some dependency which may be
declared within the same file).
* internal/cmd/generate-types:
* The code to generate reflect/prototype/protofile_list_gen.go was copied
and altered to generated internal/fileinit.desc_list_gen.go.
At a high-level this CL adds significant technical complexity.
However, this is offset by several possible future changes:
* The prototype package can be drastically simplified. We can probably
reimplement internal/legacy to use internal/fileinit instead, allowing us
to drop another dependency on the prototype package. As a result, we can
probably delete most of the constructor types in that package.
* With the prototype package significantly pruned, and the fact that generated
code no longer depend on depends on that package, we can consider merging
what's left of prototype into protodesc.
Change-Id: I6090f023f2e1b6afaf62bd3ae883566242e30715
Reviewed-on: https://go-review.googlesource.com/c/158539
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-01-18 09:32:24 -08:00
|
|
|
return m.p.AsIfaceOf(m.mi.GoType.Elem())
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
2019-03-11 13:45:14 -07:00
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *messageReflectWrapper) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
|
|
|
|
m.mi.init()
|
|
|
|
for _, fi := range m.mi.fields {
|
|
|
|
if fi.has(m.p) {
|
|
|
|
if !f(fi.fieldDesc, fi.get(m.p)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.mi.extensionMap(m.p).Range(f)
|
2019-03-11 13:45:14 -07:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *messageReflectWrapper) Has(fd pref.FieldDescriptor) bool {
|
|
|
|
if fi, xt := m.checkField(fd); fi != nil {
|
|
|
|
return fi.has(m.p)
|
|
|
|
} else {
|
|
|
|
return m.mi.extensionMap(m.p).Has(xt)
|
|
|
|
}
|
2019-04-01 13:49:56 -07:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *messageReflectWrapper) Clear(fd pref.FieldDescriptor) {
|
|
|
|
if fi, xt := m.checkField(fd); fi != nil {
|
|
|
|
fi.clear(m.p)
|
|
|
|
} else {
|
|
|
|
m.mi.extensionMap(m.p).Clear(xt)
|
|
|
|
}
|
2019-04-01 13:49:56 -07:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *messageReflectWrapper) Get(fd pref.FieldDescriptor) pref.Value {
|
|
|
|
if fi, xt := m.checkField(fd); fi != nil {
|
|
|
|
return fi.get(m.p)
|
|
|
|
} else {
|
|
|
|
return m.mi.extensionMap(m.p).Get(xt)
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *messageReflectWrapper) Set(fd pref.FieldDescriptor, v pref.Value) {
|
|
|
|
if fi, xt := m.checkField(fd); fi != nil {
|
|
|
|
fi.set(m.p, v)
|
|
|
|
} else {
|
|
|
|
m.mi.extensionMap(m.p).Set(xt, v)
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *messageReflectWrapper) Mutable(fd pref.FieldDescriptor) pref.Value {
|
|
|
|
if fi, xt := m.checkField(fd); fi != nil {
|
|
|
|
return fi.mutable(m.p)
|
|
|
|
} else {
|
|
|
|
return m.mi.extensionMap(m.p).Mutable(xt)
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *messageReflectWrapper) NewMessage(fd pref.FieldDescriptor) pref.Message {
|
|
|
|
if fi, xt := m.checkField(fd); fi != nil {
|
|
|
|
return fi.newMessage()
|
|
|
|
} else {
|
|
|
|
return xt.New().Message()
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
}
|
|
|
|
func (m *messageReflectWrapper) WhichOneof(od pref.OneofDescriptor) pref.FieldDescriptor {
|
|
|
|
m.mi.init()
|
|
|
|
if oi := m.mi.oneofs[od.Name()]; oi != nil && oi.oneofDesc == od {
|
|
|
|
return od.Fields().ByNumber(oi.which(m.p))
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
panic("invalid oneof descriptor")
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *messageReflectWrapper) GetUnknown() pref.RawFields {
|
|
|
|
m.mi.init()
|
|
|
|
return m.mi.getUnknown(m.p)
|
|
|
|
}
|
|
|
|
func (m *messageReflectWrapper) SetUnknown(b pref.RawFields) {
|
|
|
|
m.mi.init()
|
|
|
|
m.mi.setUnknown(m.p, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkField verifies that the provided field descriptor is valid.
|
|
|
|
// Exactly one of the returned values is populated.
|
|
|
|
func (m *messageReflectWrapper) checkField(fd pref.FieldDescriptor) (*fieldInfo, pref.ExtensionType) {
|
|
|
|
m.mi.init()
|
|
|
|
if fi := m.mi.fields[fd.Number()]; fi != nil {
|
|
|
|
if fi.fieldDesc != fd {
|
|
|
|
panic("mismatching field descriptor")
|
|
|
|
}
|
|
|
|
return fi, nil
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
if fd.IsExtension() {
|
|
|
|
if fd.ContainingMessage().FullName() != m.mi.PBType.FullName() {
|
|
|
|
// TODO: Should this be exact containing message descriptor match?
|
|
|
|
panic("mismatching containing message")
|
|
|
|
}
|
|
|
|
if !m.mi.PBType.ExtensionRanges().Has(fd.Number()) {
|
|
|
|
panic("invalid extension field")
|
|
|
|
}
|
|
|
|
return nil, fd.(pref.ExtensionType)
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
panic("invalid field descriptor")
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
|
|
|
|
type extensionMap map[int32]ExtensionField
|
|
|
|
|
|
|
|
func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
|
|
|
|
if m != nil {
|
|
|
|
for _, x := range *m {
|
|
|
|
xt := x.GetType()
|
|
|
|
if !f(xt, xt.ValueOf(x.GetValue())) {
|
2018-09-13 14:24:37 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) {
|
|
|
|
if m != nil {
|
|
|
|
_, ok = (*m)[int32(xt.Number())]
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
func (m *extensionMap) Clear(xt pref.ExtensionType) {
|
|
|
|
delete(*m, int32(xt.Number()))
|
|
|
|
}
|
|
|
|
func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value {
|
|
|
|
if m != nil {
|
|
|
|
if x, ok := (*m)[int32(xt.Number())]; ok {
|
|
|
|
return xt.ValueOf(x.GetValue())
|
|
|
|
}
|
2018-12-07 14:28:33 -08:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
if !isComposite(xt) {
|
|
|
|
return defaultValueOf(xt)
|
2018-12-07 14:28:33 -08:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
return frozenValueOf(xt.New())
|
2018-12-07 14:28:33 -08:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) {
|
|
|
|
if *m == nil {
|
|
|
|
*m = make(map[int32]ExtensionField)
|
|
|
|
}
|
|
|
|
var x ExtensionField
|
|
|
|
x.SetType(xt)
|
|
|
|
x.SetEagerValue(xt.InterfaceOf(v))
|
|
|
|
(*m)[int32(xt.Number())] = x
|
2018-10-23 18:31:18 -07:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value {
|
|
|
|
if !isComposite(xt) {
|
|
|
|
panic("invalid Mutable on field with non-composite type")
|
|
|
|
}
|
|
|
|
if x, ok := (*m)[int32(xt.Number())]; ok {
|
|
|
|
return xt.ValueOf(x.GetValue())
|
|
|
|
}
|
|
|
|
v := xt.New()
|
|
|
|
m.Set(xt, v)
|
|
|
|
return v
|
2018-09-13 14:24:37 -07:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
func isComposite(fd pref.FieldDescriptor) bool {
|
|
|
|
return fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind || fd.IsList() || fd.IsMap()
|
|
|
|
}
|
2018-10-23 18:31:18 -07:00
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil)
|
2018-10-23 18:31:18 -07:00
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
type messageIfaceWrapper messageDataType
|
2018-09-13 14:24:37 -07:00
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
|
|
|
|
return (*messageReflectWrapper)(m)
|
|
|
|
}
|
|
|
|
func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods {
|
|
|
|
// TODO: Consider not recreating this on every call.
|
|
|
|
m.mi.init()
|
|
|
|
return &piface.Methods{
|
|
|
|
Flags: piface.MethodFlagDeterministicMarshal,
|
|
|
|
MarshalAppend: m.marshalAppend,
|
|
|
|
Size: m.size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (m *messageIfaceWrapper) ProtoUnwrap() interface{} {
|
|
|
|
return m.p.AsIfaceOf(m.mi.GoType.Elem())
|
|
|
|
}
|
|
|
|
func (m *messageIfaceWrapper) marshalAppend(b []byte, _ pref.ProtoMessage, opts piface.MarshalOptions) ([]byte, error) {
|
|
|
|
return m.mi.marshalAppendPointer(b, m.p, newMarshalOptions(opts))
|
|
|
|
}
|
|
|
|
func (m *messageIfaceWrapper) size(msg pref.ProtoMessage) (size int) {
|
|
|
|
return m.mi.sizePointer(m.p, 0)
|
2018-12-07 14:28:33 -08:00
|
|
|
}
|