reflect/protodesc: split descriptor related functionality from prototype
In order to generate descriptor.proto, the generated code would want to depend
on the prototype package to construct the reflection data structures.
However, this is a problem since descriptor itself is one of the dependencies
for prototype. To break this dependency, we do the following:
* Avoid using concrete *descriptorpb.XOptions messages in the public API, and
instead just use protoreflect.ProtoMessage. We do lose some type safety here
as a result.
* Use protobuf reflection to interpret the Options message.
* Split out NewFileFromDescriptorProto into a separate protodesc package since
constructing protobuf reflection from the descriptor proto obviously depends
on the descriptor protos themselves.
As part of this CL, we check in a pre-generated version of descriptor and plugin
that supports protobuf reflection natively and switchover all usages of those
protos to the new definitions. These files were generated by protoc-gen-go
from CL/150074, but hand-modified to remove dependencies on the v1 proto runtime.
Change-Id: I81e03c42eeab480b03764e2fcbe1aae0e058fc57
Reviewed-on: https://go-review.googlesource.com/c/152020
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/legacy/file.go b/internal/legacy/file.go
index 5f09273..d76255b 100644
--- a/internal/legacy/file.go
+++ b/internal/legacy/file.go
@@ -14,8 +14,9 @@
// chicken and egg problem where we need the descriptor protos to implement
// the new API.
protoV1 "github.com/golang/protobuf/proto"
- descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
pref "github.com/golang/protobuf/v2/reflect/protoreflect"
+
+ descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
)
// Every enum and message type generated by protoc-gen-go since commit 2fc053c5
@@ -35,7 +36,7 @@
}
)
-var fileDescCache sync.Map // map[*byte]*descriptorV1.FileDescriptorProto
+var fileDescCache sync.Map // map[*byte]*descriptorpb.FileDescriptorProto
// loadFileDesc unmarshals b as a compressed FileDescriptorProto message.
//
@@ -43,14 +44,14 @@
// concatenated series of GZIP files (which would require shenanigans that
// rely on the concatenation properties of both protobufs and GZIP).
// File descriptors generated by protoc-gen-go do not rely on that property.
-func loadFileDesc(b []byte) *descriptorV1.FileDescriptorProto {
+func loadFileDesc(b []byte) *descriptorpb.FileDescriptorProto {
// Fast-path: check whether we already have a cached file descriptor.
if v, ok := fileDescCache.Load(&b[0]); ok {
- return v.(*descriptorV1.FileDescriptorProto)
+ return v.(*descriptorpb.FileDescriptorProto)
}
// Slow-path: decompress and unmarshal the file descriptor proto.
- m := new(descriptorV1.FileDescriptorProto)
+ m := new(descriptorpb.FileDescriptorProto)
zr, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
panic(err)