internal/legacy: extract legacy support out from the impl package
The impl package currently supports wrapping legacy v1 enums and messages
so that they implement the v2 reflective APIs. This functionality is necessary
for v1 and v2 to interoperate. However, the existence of this functionality
presents several problems:
* A significant portion of the complexity in impl is for legacy wrapping.
* This complexity is linked into a Go binary even if all the other messages
in the binary natively support v2 reflection.
* It presents a cyclic dependency when trying to generate descriptor proto.
Suppose you are generating descriptor.proto. The generated code would want to
depend on the impl package because impl is the runtime implementation for
protobuf messages. However, impl currently depends depends on descriptor in
order to wrap legacy enum and messages since it needs the ability to dynamically
create new protobuf descriptor types. In the case of descriptor.proto, it would
presumably be generated with native reflection support, so the legacy wrapping
logic is unneccessary.
To break the dependency of impl on descriptor, we move the legacy support logic
to a different package and instead add hooks in impl so that legacy support could
be dynamically registered at runtime. This is dependency injection.
Change-Id: I01a582908ed5629993f6699e9bf2f4bee93857a4
Reviewed-on: https://go-review.googlesource.com/c/151877
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/internal/value/convert.go b/internal/value/convert.go
index cc9d94d..5f4109a 100644
--- a/internal/value/convert.go
+++ b/internal/value/convert.go
@@ -47,23 +47,41 @@
// protoc-gen-go historically generated to be able to automatically wrap some
// v1 messages generated by other forks of protoc-gen-go.
func NewConverter(t reflect.Type, k pref.Kind) Converter {
- return NewLegacyConverter(t, k, nil, nil, nil)
+ return NewLegacyConverter(t, k, nil)
}
-// Legacy enums and messages do not self-report their own protoreflect types.
-// Thus, the caller needs to provide functions for retrieving those when
-// a v1 enum or message is encountered.
+// LegacyWrapper is a set of wrapper methods that wraps legacy v1 Go types
+// to implement the v2 reflection APIs.
type (
- enumTypeOf = func(reflect.Type) pref.EnumType
- messageTypeOf = func(reflect.Type) pref.MessageType
- messageValueOf = func(reflect.Value) pref.ProtoMessage
+ LegacyWrapper interface {
+ EnumOf(interface{}) LegacyEnum
+ EnumTypeOf(interface{}) pref.EnumType
+
+ MessageOf(interface{}) LegacyMessage
+ MessageTypeOf(interface{}) pref.MessageType
+
+ ExtensionTypeOf(pref.ExtensionDescriptor, interface{}) pref.ExtensionType
+
+ // TODO: Remove these eventually. See the TODOs in protoapi.
+ ExtensionDescFromType(pref.ExtensionType) *papi.ExtensionDesc
+ ExtensionTypeFromDesc(*papi.ExtensionDesc) pref.ExtensionType
+ }
+
+ LegacyEnum = interface {
+ pref.Enum
+ ProtoUnwrap() interface{}
+ }
+
+ LegacyMessage = interface {
+ pref.Message
+ ProtoUnwrap() interface{}
+ }
)
// NewLegacyConverter is identical to NewConverter,
// but supports wrapping legacy v1 messages to implement the v2 message API
-// using the provided enumTypeOf, messageTypeOf and messageValueOf functions.
-// The wrapped message must implement Unwrapper.
-func NewLegacyConverter(t reflect.Type, k pref.Kind, etOf enumTypeOf, mtOf messageTypeOf, mvOf messageValueOf) Converter {
+// using the provided LegacyWrapper.
+func NewLegacyConverter(t reflect.Type, k pref.Kind, w LegacyWrapper) Converter {
switch k {
case pref.BoolKind:
if t.Kind() == reflect.Bool {
@@ -125,8 +143,8 @@
}
// Handle v1 enums, which we identify as simply a named int32 type.
- if etOf != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 {
- et := etOf(t)
+ if w != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 {
+ et := w.EnumTypeOf(reflect.Zero(t).Interface())
return Converter{
PBValueOf: func(v reflect.Value) pref.Value {
if v.Type() != t {
@@ -164,14 +182,14 @@
}
// Handle v1 messages, which we need to wrap as a v2 message.
- if mtOf != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
- mt := mtOf(t)
+ if w != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
+ mt := w.MessageTypeOf(reflect.Zero(t).Interface())
return Converter{
PBValueOf: func(v reflect.Value) pref.Value {
if v.Type() != t {
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
}
- return pref.ValueOf(mvOf(v).ProtoReflect())
+ return pref.ValueOf(w.MessageOf(v.Interface()))
},
GoValueOf: func(v pref.Value) reflect.Value {
rv := reflect.ValueOf(v.Message().(Unwrapper).ProtoUnwrap())