2018-10-18 11:06:29 -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.
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
package impl
|
2018-10-18 11:06:29 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"unicode"
|
|
|
|
|
2019-05-13 23:55:40 -07:00
|
|
|
ptag "google.golang.org/protobuf/internal/encoding/tag"
|
|
|
|
ptype "google.golang.org/protobuf/internal/prototype"
|
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
2019-05-22 00:42:45 -04:00
|
|
|
"google.golang.org/protobuf/reflect/prototype"
|
2018-10-18 11:06:29 -07:00
|
|
|
)
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
// legacyWrapMessage wraps v as a protoreflect.ProtoMessage,
|
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
|
|
|
// where v must be a *struct kind and not implement the v2 API already.
|
2019-05-22 13:42:54 -04:00
|
|
|
func legacyWrapMessage(v reflect.Value) pref.ProtoMessage {
|
|
|
|
mt := legacyLoadMessageInfo(v.Type())
|
2018-11-26 22:32:06 -08:00
|
|
|
return mt.MessageOf(v.Interface()).Interface()
|
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-05-22 13:42:54 -04:00
|
|
|
var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo
|
2018-10-19 16:27:46 -07:00
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
// legacyLoadMessageInfo dynamically loads a *MessageInfo for t,
|
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
|
|
|
// where t must be a *struct kind and not implement the v2 API already.
|
2019-05-22 13:42:54 -04:00
|
|
|
func legacyLoadMessageInfo(t reflect.Type) *MessageInfo {
|
2019-05-22 05:12:36 -04:00
|
|
|
// Fast-path: check if a MessageInfo is cached for this concrete type.
|
2019-05-22 13:42:54 -04:00
|
|
|
if mt, ok := legacyMessageTypeCache.Load(t); ok {
|
|
|
|
return mt.(*MessageInfo)
|
2018-10-19 16:27:46 -07:00
|
|
|
}
|
|
|
|
|
2019-05-22 05:12:36 -04:00
|
|
|
// Slow-path: derive message descriptor and initialize MessageInfo.
|
2019-05-22 13:42:54 -04:00
|
|
|
md := LegacyLoadMessageDesc(t)
|
|
|
|
mt := new(MessageInfo)
|
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
|
|
|
mt.GoType = t
|
2019-05-22 00:42:45 -04:00
|
|
|
mt.PBType = &prototype.Message{
|
|
|
|
MessageDescriptor: md,
|
|
|
|
NewMessage: func() pref.Message {
|
|
|
|
return mt.MessageOf(reflect.New(t.Elem()).Interface())
|
|
|
|
},
|
|
|
|
}
|
2019-05-22 13:42:54 -04:00
|
|
|
if mt, ok := legacyMessageTypeCache.LoadOrStore(t, mt); ok {
|
|
|
|
return mt.(*MessageInfo)
|
2019-03-19 14:14:29 -07:00
|
|
|
}
|
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
|
|
|
return mt
|
2018-10-19 16:27:46 -07:00
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
var (
|
|
|
|
legacyMessageDescLock sync.Mutex
|
|
|
|
legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
|
|
|
|
)
|
2018-10-18 11:06:29 -07:00
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
// LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
|
2018-10-19 16:27:46 -07:00
|
|
|
// which must be a *struct kind and not implement the v2 API already.
|
2019-03-25 14:41:32 -07:00
|
|
|
//
|
|
|
|
// This is exported for testing purposes.
|
2019-05-22 13:42:54 -04:00
|
|
|
func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
|
|
|
|
return legacyMessageDescSet{}.Load(t)
|
2018-10-18 11:06:29 -07:00
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
type legacyMessageDescSet struct {
|
2018-10-18 11:06:29 -07:00
|
|
|
visited map[reflect.Type]*ptype.StandaloneMessage
|
|
|
|
descs []*ptype.StandaloneMessage
|
|
|
|
types []reflect.Type
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
func (ms legacyMessageDescSet) Load(t reflect.Type) pref.MessageDescriptor {
|
2018-10-18 11:06:29 -07:00
|
|
|
// Fast-path: check if a MessageDescriptor is cached for this concrete type.
|
2019-05-22 13:42:54 -04:00
|
|
|
if mi, ok := legacyMessageDescCache.Load(t); ok {
|
2018-10-18 11:06:29 -07:00
|
|
|
return mi.(pref.MessageDescriptor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow-path: initialize MessageDescriptor from the Go type.
|
2019-03-19 14:14:29 -07:00
|
|
|
//
|
|
|
|
// Hold a global lock during message creation to ensure that each Go type
|
|
|
|
// maps to exactly one MessageDescriptor. After obtaining the lock, we must
|
|
|
|
// check again whether the message has already been handled.
|
2019-05-22 13:42:54 -04:00
|
|
|
legacyMessageDescLock.Lock()
|
|
|
|
defer legacyMessageDescLock.Unlock()
|
|
|
|
if mi, ok := legacyMessageDescCache.Load(t); ok {
|
2019-03-19 14:14:29 -07:00
|
|
|
return mi.(pref.MessageDescriptor)
|
|
|
|
}
|
2018-10-18 11:06:29 -07:00
|
|
|
|
|
|
|
// Processing t recursively populates descs and types with all sub-messages.
|
|
|
|
// The descriptor for the first type is guaranteed to be at the front.
|
|
|
|
ms.processMessage(t)
|
|
|
|
|
|
|
|
// Within a proto file it is possible for cyclic dependencies to exist
|
|
|
|
// between multiple message types. When these cases arise, the set of
|
|
|
|
// message descriptors must be created together.
|
|
|
|
mds, err := ptype.NewMessages(ms.descs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for i, md := range mds {
|
|
|
|
// Protobuf semantics represents map entries under-the-hood as
|
|
|
|
// pseudo-messages (has a descriptor, but no generated Go type).
|
|
|
|
// Avoid caching these fake messages.
|
|
|
|
if t := ms.types[i]; t.Kind() != reflect.Map {
|
2019-05-22 13:42:54 -04:00
|
|
|
legacyMessageDescCache.Store(t, md)
|
2018-10-18 11:06:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mds[0]
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
func (ms *legacyMessageDescSet) processMessage(t reflect.Type) pref.MessageDescriptor {
|
2018-10-18 11:06:29 -07:00
|
|
|
// Fast-path: Obtain a placeholder if the message is already processed.
|
|
|
|
if m, ok := ms.visited[t]; ok {
|
|
|
|
return ptype.PlaceholderMessage(m.FullName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow-path: Walk over the struct fields to derive the message descriptor.
|
2018-11-10 14:12:21 -08:00
|
|
|
if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct || t.Elem().PkgPath() == "" {
|
|
|
|
panic(fmt.Sprintf("got %v, want named *struct kind", t))
|
2018-10-18 11:06:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Derive name and syntax from the raw descriptor.
|
|
|
|
m := new(ptype.StandaloneMessage)
|
|
|
|
mv := reflect.New(t.Elem()).Interface()
|
|
|
|
if _, ok := mv.(pref.ProtoMessage); ok {
|
|
|
|
panic(fmt.Sprintf("%v already implements proto.Message", t))
|
|
|
|
}
|
2018-12-04 14:06:19 -08:00
|
|
|
if md, ok := mv.(messageV1); ok {
|
2018-10-18 11:06:29 -07:00
|
|
|
b, idxs := md.Descriptor()
|
2019-05-22 13:42:54 -04:00
|
|
|
fd := legacyLoadFileDesc(b)
|
2018-10-18 11:06:29 -07:00
|
|
|
|
|
|
|
// Derive syntax.
|
|
|
|
switch fd.GetSyntax() {
|
|
|
|
case "proto2", "":
|
|
|
|
m.Syntax = pref.Proto2
|
|
|
|
case "proto3":
|
|
|
|
m.Syntax = pref.Proto3
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive full name.
|
|
|
|
md := fd.MessageType[idxs[0]]
|
|
|
|
m.FullName = pref.FullName(fd.GetPackage()).Append(pref.Name(md.GetName()))
|
|
|
|
for _, i := range idxs[1:] {
|
|
|
|
md = md.NestedType[i]
|
|
|
|
m.FullName = m.FullName.Append(pref.Name(md.GetName()))
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-04 14:06:19 -08:00
|
|
|
// If the type does not implement messageV1, then the only way to
|
2018-10-18 11:06:29 -07:00
|
|
|
// obtain the full name is through the registry. However, this is
|
|
|
|
// unreliable as some generated messages register with a fork of
|
|
|
|
// golang/protobuf, so the registry may not have this information.
|
2019-05-22 13:42:54 -04:00
|
|
|
m.FullName = legacyDeriveFullName(t.Elem())
|
2018-10-18 11:06:29 -07:00
|
|
|
m.Syntax = pref.Proto2
|
|
|
|
|
|
|
|
// Try to determine if the message is using proto3 by checking scalars.
|
|
|
|
for i := 0; i < t.Elem().NumField(); i++ {
|
|
|
|
f := t.Elem().Field(i)
|
|
|
|
if tag := f.Tag.Get("protobuf"); tag != "" {
|
|
|
|
switch f.Type.Kind() {
|
|
|
|
case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
|
|
|
|
m.Syntax = pref.Proto3
|
|
|
|
}
|
|
|
|
for _, s := range strings.Split(tag, ",") {
|
|
|
|
if s == "proto3" {
|
|
|
|
m.Syntax = pref.Proto3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ms.visit(m, t)
|
|
|
|
|
|
|
|
// Obtain a list of oneof wrapper types.
|
|
|
|
var oneofWrappers []reflect.Type
|
|
|
|
if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok {
|
|
|
|
vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
|
|
|
|
for _, v := range vs.Interface().([]interface{}) {
|
|
|
|
oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 23:43:49 -08:00
|
|
|
if fn, ok := t.MethodByName("XXX_OneofWrappers"); ok {
|
|
|
|
vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
|
|
|
|
for _, v := range vs.Interface().([]interface{}) {
|
|
|
|
oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 11:06:29 -07:00
|
|
|
|
|
|
|
// Obtain a list of the extension ranges.
|
|
|
|
if fn, ok := t.MethodByName("ExtensionRangeArray"); ok {
|
|
|
|
vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
|
|
|
|
for i := 0; i < vs.Len(); i++ {
|
|
|
|
v := vs.Index(i)
|
|
|
|
m.ExtensionRanges = append(m.ExtensionRanges, [2]pref.FieldNumber{
|
|
|
|
pref.FieldNumber(v.FieldByName("Start").Int()),
|
|
|
|
pref.FieldNumber(v.FieldByName("End").Int() + 1),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive the message fields by inspecting the struct fields.
|
|
|
|
for i := 0; i < t.Elem().NumField(); i++ {
|
|
|
|
f := t.Elem().Field(i)
|
|
|
|
if tag := f.Tag.Get("protobuf"); tag != "" {
|
|
|
|
tagKey := f.Tag.Get("protobuf_key")
|
|
|
|
tagVal := f.Tag.Get("protobuf_val")
|
|
|
|
m.Fields = append(m.Fields, ms.parseField(tag, tagKey, tagVal, f.Type, m))
|
|
|
|
}
|
|
|
|
if tag := f.Tag.Get("protobuf_oneof"); tag != "" {
|
|
|
|
name := pref.Name(tag)
|
|
|
|
m.Oneofs = append(m.Oneofs, ptype.Oneof{Name: name})
|
|
|
|
for _, t := range oneofWrappers {
|
|
|
|
if t.Implements(f.Type) {
|
|
|
|
f := t.Elem().Field(0)
|
|
|
|
if tag := f.Tag.Get("protobuf"); tag != "" {
|
|
|
|
ft := ms.parseField(tag, "", "", f.Type, m)
|
|
|
|
ft.OneofName = name
|
|
|
|
m.Fields = append(m.Fields, ft)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptype.PlaceholderMessage(m.FullName)
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
func (ms *legacyMessageDescSet) parseField(tag, tagKey, tagVal string, goType reflect.Type, parent *ptype.StandaloneMessage) ptype.Field {
|
2018-11-01 13:52:16 -07:00
|
|
|
t := goType
|
2018-10-18 11:06:29 -07:00
|
|
|
isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
|
|
|
|
isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
|
|
|
|
if isOptional || isRepeated {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
2018-11-01 13:52:16 -07:00
|
|
|
f := ptag.Unmarshal(tag, t)
|
2018-10-18 11:06:29 -07:00
|
|
|
|
|
|
|
// Populate EnumType and MessageType.
|
|
|
|
if f.EnumType == nil && f.Kind == pref.EnumKind {
|
2019-01-08 16:18:07 -08:00
|
|
|
if ev, ok := reflect.Zero(t).Interface().(pref.Enum); ok {
|
2019-05-01 12:29:25 -07:00
|
|
|
f.EnumType = ev.Descriptor()
|
2018-10-18 11:06:29 -07:00
|
|
|
} else {
|
2019-05-22 13:42:54 -04:00
|
|
|
f.EnumType = LegacyLoadEnumDesc(t)
|
2018-10-18 11:06:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if f.MessageType == nil && (f.Kind == pref.MessageKind || f.Kind == pref.GroupKind) {
|
|
|
|
if mv, ok := reflect.Zero(t).Interface().(pref.ProtoMessage); ok {
|
2019-05-01 12:29:25 -07:00
|
|
|
f.MessageType = mv.ProtoReflect().Descriptor()
|
2018-10-18 11:06:29 -07:00
|
|
|
} else if t.Kind() == reflect.Map {
|
|
|
|
m := &ptype.StandaloneMessage{
|
2018-12-10 15:14:36 -08:00
|
|
|
Syntax: parent.Syntax,
|
2019-05-22 13:42:54 -04:00
|
|
|
FullName: parent.FullName.Append(legacyMapEntryName(f.Name)),
|
2018-12-10 15:14:36 -08:00
|
|
|
IsMapEntry: true,
|
2018-10-18 11:06:29 -07:00
|
|
|
Fields: []ptype.Field{
|
|
|
|
ms.parseField(tagKey, "", "", t.Key(), nil),
|
|
|
|
ms.parseField(tagVal, "", "", t.Elem(), nil),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ms.visit(m, t)
|
|
|
|
f.MessageType = ptype.PlaceholderMessage(m.FullName)
|
2019-05-22 13:42:54 -04:00
|
|
|
} else if mv, ok := legacyMessageDescCache.Load(t); ok {
|
2018-10-18 11:06:29 -07:00
|
|
|
f.MessageType = mv.(pref.MessageDescriptor)
|
|
|
|
} else {
|
|
|
|
f.MessageType = ms.processMessage(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
func (ms *legacyMessageDescSet) visit(m *ptype.StandaloneMessage, t reflect.Type) {
|
2018-10-18 11:06:29 -07:00
|
|
|
if ms.visited == nil {
|
|
|
|
ms.visited = make(map[reflect.Type]*ptype.StandaloneMessage)
|
|
|
|
}
|
|
|
|
if t.Kind() != reflect.Map {
|
|
|
|
ms.visited[t] = m
|
|
|
|
}
|
|
|
|
ms.descs = append(ms.descs, m)
|
|
|
|
ms.types = append(ms.types, t)
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
// legacyDeriveFullName derives a fully qualified protobuf name for the given Go type
|
2018-10-18 11:06:29 -07:00
|
|
|
// The provided name is not guaranteed to be stable nor universally unique.
|
|
|
|
// It should be sufficiently unique within a program.
|
2019-05-22 13:42:54 -04:00
|
|
|
func legacyDeriveFullName(t reflect.Type) pref.FullName {
|
2018-10-18 11:06:29 -07:00
|
|
|
sanitize := func(r rune) rune {
|
|
|
|
switch {
|
|
|
|
case r == '/':
|
|
|
|
return '.'
|
|
|
|
case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z', '0' <= r && r <= '9':
|
|
|
|
return r
|
|
|
|
default:
|
|
|
|
return '_'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prefix := strings.Map(sanitize, t.PkgPath())
|
|
|
|
suffix := strings.Map(sanitize, t.Name())
|
|
|
|
if suffix == "" {
|
|
|
|
suffix = fmt.Sprintf("UnknownX%X", reflect.ValueOf(t).Pointer())
|
|
|
|
}
|
|
|
|
|
|
|
|
ss := append(strings.Split(prefix, "."), suffix)
|
|
|
|
for i, s := range ss {
|
|
|
|
if s == "" || ('0' <= s[0] && s[0] <= '9') {
|
|
|
|
ss[i] = "x" + s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pref.FullName(strings.Join(ss, "."))
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
// legacyMapEntryName derives the message name for a map field of a given name.
|
2018-10-18 11:06:29 -07:00
|
|
|
// This is identical to MapEntryName from parser.cc in the protoc source.
|
2019-05-22 13:42:54 -04:00
|
|
|
func legacyMapEntryName(s pref.Name) pref.Name {
|
2018-10-18 11:06:29 -07:00
|
|
|
var b []byte
|
|
|
|
nextUpper := true
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if c := s[i]; c == '_' {
|
|
|
|
nextUpper = true
|
|
|
|
} else {
|
|
|
|
if nextUpper {
|
|
|
|
c = byte(unicode.ToUpper(rune(c)))
|
|
|
|
nextUpper = false
|
|
|
|
}
|
|
|
|
b = append(b, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pref.Name(append(b, "Entry"...))
|
|
|
|
}
|