2018-10-18 18:06:29 +00: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 17:42:54 +00:00
|
|
|
package impl
|
2018-10-18 18:06:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2019-07-01 20:45:52 +00:00
|
|
|
"strings"
|
2018-10-18 18:06:29 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-07-01 20:45:52 +00:00
|
|
|
"google.golang.org/protobuf/internal/filedesc"
|
2019-08-23 19:18:57 +00:00
|
|
|
"google.golang.org/protobuf/internal/strs"
|
2019-06-06 20:01:53 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2019-05-14 06:55:40 +00:00
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
2018-10-18 18:06:29 +00:00
|
|
|
)
|
|
|
|
|
2019-08-23 19:18:57 +00:00
|
|
|
// legacyEnumName returns the name of enums used in legacy code.
|
|
|
|
// It is neither the protobuf full name nor the qualified Go name,
|
|
|
|
// but rather an odd hybrid of both.
|
|
|
|
func legacyEnumName(ed pref.EnumDescriptor) string {
|
|
|
|
var protoPkg string
|
|
|
|
enumName := string(ed.FullName())
|
|
|
|
if fd := ed.ParentFile(); fd != nil {
|
|
|
|
protoPkg = string(fd.Package())
|
|
|
|
enumName = strings.TrimPrefix(enumName, protoPkg+".")
|
|
|
|
}
|
|
|
|
if protoPkg == "" {
|
|
|
|
return strs.GoCamelCase(enumName)
|
|
|
|
}
|
|
|
|
return protoPkg + "." + strs.GoCamelCase(enumName)
|
|
|
|
}
|
|
|
|
|
2019-05-22 17:42:54 +00:00
|
|
|
// legacyWrapEnum wraps v as a protoreflect.Enum,
|
2018-11-27 06:32:06 +00:00
|
|
|
// where v must be a int32 kind and not implement the v2 API already.
|
2019-05-22 17:42:54 +00:00
|
|
|
func legacyWrapEnum(v reflect.Value) pref.Enum {
|
|
|
|
et := legacyLoadEnumType(v.Type())
|
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 21:05:20 +00:00
|
|
|
return et.New(pref.EnumNumber(v.Int()))
|
|
|
|
}
|
|
|
|
|
2019-05-22 17:42:54 +00:00
|
|
|
var legacyEnumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType
|
2018-11-10 22:12:21 +00:00
|
|
|
|
2019-05-22 17:42:54 +00:00
|
|
|
// legacyLoadEnumType dynamically loads a protoreflect.EnumType 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 21:05:20 +00:00
|
|
|
// where t must be an int32 kind and not implement the v2 API already.
|
2019-05-22 17:42:54 +00:00
|
|
|
func legacyLoadEnumType(t reflect.Type) pref.EnumType {
|
2018-11-10 22:12:21 +00:00
|
|
|
// Fast-path: check if a EnumType is cached for this concrete type.
|
2019-05-22 17:42:54 +00:00
|
|
|
if et, ok := legacyEnumTypeCache.Load(t); ok {
|
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 21:05:20 +00:00
|
|
|
return et.(pref.EnumType)
|
2018-11-10 22:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Slow-path: derive enum descriptor and initialize EnumType.
|
2019-05-22 04:42:45 +00:00
|
|
|
var et pref.EnumType
|
2019-05-22 17:42:54 +00:00
|
|
|
ed := LegacyLoadEnumDesc(t)
|
2019-08-06 22:43:25 +00:00
|
|
|
et = &legacyEnumType{
|
|
|
|
desc: ed,
|
|
|
|
goType: t,
|
2019-05-22 04:42:45 +00:00
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
if et, ok := legacyEnumTypeCache.LoadOrStore(t, et); ok {
|
2019-03-19 21:14:29 +00:00
|
|
|
return et.(pref.EnumType)
|
|
|
|
}
|
|
|
|
return et
|
2018-11-10 22:12:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 22:43:25 +00:00
|
|
|
type legacyEnumType struct {
|
|
|
|
desc pref.EnumDescriptor
|
|
|
|
goType reflect.Type
|
|
|
|
m sync.Map // map[protoreflect.EnumNumber]proto.Enum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *legacyEnumType) New(n pref.EnumNumber) pref.Enum {
|
|
|
|
if e, ok := t.m.Load(n); ok {
|
|
|
|
return e.(pref.Enum)
|
|
|
|
}
|
|
|
|
e := &legacyEnumWrapper{num: n, pbTyp: t, goTyp: t.goType}
|
|
|
|
t.m.Store(n, e)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
func (t *legacyEnumType) GoType() reflect.Type {
|
|
|
|
return t.goType
|
|
|
|
}
|
|
|
|
func (t *legacyEnumType) Descriptor() pref.EnumDescriptor {
|
|
|
|
return t.desc
|
|
|
|
}
|
|
|
|
|
2019-05-22 17:42:54 +00:00
|
|
|
type legacyEnumWrapper struct {
|
2018-11-10 22:12:21 +00:00
|
|
|
num pref.EnumNumber
|
|
|
|
pbTyp pref.EnumType
|
|
|
|
goTyp reflect.Type
|
|
|
|
}
|
|
|
|
|
2019-05-22 17:42:54 +00:00
|
|
|
func (e *legacyEnumWrapper) Descriptor() pref.EnumDescriptor {
|
2019-05-01 19:29:25 +00:00
|
|
|
return e.pbTyp.Descriptor()
|
|
|
|
}
|
2019-07-02 21:58:02 +00:00
|
|
|
func (e *legacyEnumWrapper) Type() pref.EnumType {
|
|
|
|
return e.pbTyp
|
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
func (e *legacyEnumWrapper) Number() pref.EnumNumber {
|
2019-05-01 19:29:25 +00:00
|
|
|
return e.num
|
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
func (e *legacyEnumWrapper) ProtoReflect() pref.Enum {
|
2018-11-10 22:12:21 +00:00
|
|
|
return e
|
|
|
|
}
|
2019-09-03 23:30:39 +00:00
|
|
|
func (e *legacyEnumWrapper) protoUnwrap() interface{} {
|
2018-11-10 22:12:21 +00:00
|
|
|
v := reflect.New(e.goTyp).Elem()
|
|
|
|
v.SetInt(int64(e.num))
|
|
|
|
return v.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-07-17 23:52:10 +00:00
|
|
|
_ pref.Enum = (*legacyEnumWrapper)(nil)
|
2019-09-03 23:30:39 +00:00
|
|
|
_ unwrapper = (*legacyEnumWrapper)(nil)
|
2018-11-10 22:12:21 +00:00
|
|
|
)
|
|
|
|
|
2019-05-22 17:42:54 +00:00
|
|
|
var legacyEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
|
2018-10-18 18:06:29 +00:00
|
|
|
|
2019-05-22 17:42:54 +00:00
|
|
|
// LegacyLoadEnumDesc returns an EnumDescriptor derived from the Go type,
|
2018-10-18 18:06:29 +00:00
|
|
|
// which must be an int32 kind and not implement the v2 API already.
|
2019-03-25 21:41:32 +00:00
|
|
|
//
|
|
|
|
// This is exported for testing purposes.
|
2019-05-22 17:42:54 +00:00
|
|
|
func LegacyLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
|
2018-10-18 18:06:29 +00:00
|
|
|
// Fast-path: check if an EnumDescriptor is cached for this concrete type.
|
2019-05-22 17:42:54 +00:00
|
|
|
if ed, ok := legacyEnumDescCache.Load(t); ok {
|
2019-03-19 21:14:29 +00:00
|
|
|
return ed.(pref.EnumDescriptor)
|
2018-10-18 18:06:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 20:01:53 +00:00
|
|
|
// Slow-path: initialize EnumDescriptor from the raw descriptor.
|
2018-10-18 18:06:29 +00:00
|
|
|
ev := reflect.Zero(t).Interface()
|
2019-01-09 00:18:07 +00:00
|
|
|
if _, ok := ev.(pref.Enum); ok {
|
2018-10-18 18:06:29 +00:00
|
|
|
panic(fmt.Sprintf("%v already implements proto.Enum", t))
|
|
|
|
}
|
2019-06-06 20:01:53 +00:00
|
|
|
edV1, ok := ev.(enumV1)
|
|
|
|
if !ok {
|
2019-07-01 20:45:52 +00:00
|
|
|
return aberrantLoadEnumDesc(t)
|
2018-10-18 18:06:29 +00:00
|
|
|
}
|
2019-06-06 20:01:53 +00:00
|
|
|
b, idxs := edV1.EnumDescriptor()
|
2018-10-18 18:06:29 +00:00
|
|
|
|
2019-06-06 20:01:53 +00:00
|
|
|
var ed pref.EnumDescriptor
|
|
|
|
if len(idxs) == 1 {
|
|
|
|
ed = legacyLoadFileDesc(b).Enums().Get(idxs[0])
|
|
|
|
} else {
|
|
|
|
md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
|
|
|
|
for _, i := range idxs[1 : len(idxs)-1] {
|
|
|
|
md = md.Messages().Get(i)
|
|
|
|
}
|
|
|
|
ed = md.Enums().Get(idxs[len(idxs)-1])
|
2018-10-18 18:06:29 +00:00
|
|
|
}
|
2019-05-22 17:42:54 +00:00
|
|
|
if ed, ok := legacyEnumDescCache.LoadOrStore(t, ed); ok {
|
2019-06-06 20:01:53 +00:00
|
|
|
return ed.(protoreflect.EnumDescriptor)
|
2019-03-19 21:14:29 +00:00
|
|
|
}
|
2018-10-18 18:06:29 +00:00
|
|
|
return ed
|
|
|
|
}
|
2019-07-01 20:45:52 +00:00
|
|
|
|
|
|
|
var aberrantEnumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
|
|
|
|
|
|
|
|
// aberrantLoadEnumDesc returns an EnumDescriptor derived from the Go type,
|
|
|
|
// which must not implement protoreflect.Enum or enumV1.
|
|
|
|
//
|
|
|
|
// If the type does not implement enumV1, then there is no reliable
|
|
|
|
// way to derive the original protobuf type information.
|
|
|
|
// We are unable to use the global enum registry since it is
|
|
|
|
// unfortunately keyed by the protobuf full name, which we also do not know.
|
|
|
|
// Thus, this produces some bogus enum descriptor based on the Go type name.
|
|
|
|
func aberrantLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
|
|
|
|
// Fast-path: check if an EnumDescriptor is cached for this concrete type.
|
|
|
|
if ed, ok := aberrantEnumDescCache.Load(t); ok {
|
|
|
|
return ed.(pref.EnumDescriptor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow-path: construct a bogus, but unique EnumDescriptor.
|
|
|
|
ed := &filedesc.Enum{L2: new(filedesc.EnumL2)}
|
|
|
|
ed.L0.FullName = aberrantDeriveFullName(t) // e.g., github_com.user.repo.MyEnum
|
|
|
|
ed.L0.ParentFile = filedesc.SurrogateProto3
|
|
|
|
ed.L2.Values.List = append(ed.L2.Values.List, filedesc.EnumValue{})
|
|
|
|
|
|
|
|
// TODO: Use the presence of a UnmarshalJSON method to determine proto2?
|
|
|
|
|
|
|
|
vd := &ed.L2.Values.List[0]
|
|
|
|
vd.L0.FullName = ed.L0.FullName + "_UNKNOWN" // e.g., github_com.user.repo.MyEnum_UNKNOWN
|
|
|
|
vd.L0.ParentFile = ed.L0.ParentFile
|
|
|
|
vd.L0.Parent = ed
|
|
|
|
|
|
|
|
// TODO: We could use the String method to obtain some enum value names by
|
|
|
|
// starting at 0 and print the enum until it produces invalid identifiers.
|
|
|
|
// An exhaustive query is clearly impractical, but can be best-effort.
|
|
|
|
|
|
|
|
if ed, ok := aberrantEnumDescCache.LoadOrStore(t, ed); ok {
|
|
|
|
return ed.(pref.EnumDescriptor)
|
|
|
|
}
|
|
|
|
return ed
|
|
|
|
}
|
|
|
|
|
|
|
|
// aberrantDeriveFullName derives a fully qualified protobuf name for the given Go type
|
|
|
|
// The provided name is not guaranteed to be stable nor universally unique.
|
|
|
|
// It should be sufficiently unique within a program.
|
|
|
|
func aberrantDeriveFullName(t reflect.Type) pref.FullName {
|
|
|
|
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, "."))
|
|
|
|
}
|