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>
diff --git a/internal/impl/legacy_enum.go b/internal/impl/legacy_enum.go
index 87b4921..bba130d 100644
--- a/internal/impl/legacy_enum.go
+++ b/internal/impl/legacy_enum.go
@@ -16,29 +16,36 @@
ptype "github.com/golang/protobuf/v2/reflect/prototype"
)
+// legacyWrapEnum wraps v as a protoreflect.ProtoEnum,
+// where v must be a *struct kind and not implement the v2 API already.
+func legacyWrapEnum(v reflect.Value) pref.ProtoEnum {
+ et := legacyLoadEnumType(v.Type())
+ return et.New(pref.EnumNumber(v.Int()))
+}
+
var enumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType
-// wrapLegacyEnum wraps v as a protoreflect.ProtoEnum,
-// where v must be an int32 kind and not implement the v2 API already.
-func wrapLegacyEnum(v reflect.Value) pref.ProtoEnum {
+// legacyLoadEnumType dynamically loads a protoreflect.EnumType for t,
+// where t must be an int32 kind and not implement the v2 API already.
+func legacyLoadEnumType(t reflect.Type) pref.EnumType {
// Fast-path: check if a EnumType is cached for this concrete type.
- if et, ok := enumTypeCache.Load(v.Type()); ok {
- return et.(pref.EnumType).New(pref.EnumNumber(v.Int()))
+ if et, ok := enumTypeCache.Load(t); ok {
+ return et.(pref.EnumType)
}
// Slow-path: derive enum descriptor and initialize EnumType.
var m sync.Map // map[protoreflect.EnumNumber]proto.Enum
- ed := loadEnumDesc(v.Type())
+ ed := legacyLoadEnumDesc(t)
et := ptype.GoEnum(ed, func(et pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
if e, ok := m.Load(n); ok {
return e.(pref.ProtoEnum)
}
- e := &legacyEnumWrapper{num: n, pbTyp: et, goTyp: v.Type()}
+ e := &legacyEnumWrapper{num: n, pbTyp: et, goTyp: t}
m.Store(n, e)
return e
})
- enumTypeCache.Store(v.Type(), et)
- return et.(pref.EnumType).New(pref.EnumNumber(v.Int()))
+ enumTypeCache.Store(t, et)
+ return et.(pref.EnumType)
}
type legacyEnumWrapper struct {
@@ -70,9 +77,11 @@
var enumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
-// loadEnumDesc returns an EnumDescriptor derived from the Go type,
+var enumNumberType = reflect.TypeOf(pref.EnumNumber(0))
+
+// legacyLoadEnumDesc returns an EnumDescriptor derived from the Go type,
// which must be an int32 kind and not implement the v2 API already.
-func loadEnumDesc(t reflect.Type) pref.EnumDescriptor {
+func legacyLoadEnumDesc(t reflect.Type) pref.EnumDescriptor {
// Fast-path: check if an EnumDescriptor is cached for this concrete type.
if v, ok := enumDescCache.Load(t); ok {
return v.(pref.EnumDescriptor)
@@ -82,6 +91,9 @@
if t.Kind() != reflect.Int32 || t.PkgPath() == "" {
panic(fmt.Sprintf("got %v, want named int32 kind", t))
}
+ if t == enumNumberType {
+ panic(fmt.Sprintf("cannot be %v", t))
+ }
// Derive the enum descriptor from the raw descriptor proto.
e := new(ptype.StandaloneEnum)
@@ -91,7 +103,7 @@
}
if ed, ok := ev.(legacyEnum); ok {
b, idxs := ed.EnumDescriptor()
- fd := loadFileDesc(b)
+ fd := legacyLoadFileDesc(b)
// Derive syntax.
switch fd.GetSyntax() {