blob: 9ab091086c9664c603dfc38c4bbb4ff610c74602 [file] [log] [blame]
Joe Tsai90fe9962018-10-18 11:06:29 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Joe Tsai21ade492019-05-22 13:42:54 -04005package impl
Joe Tsai90fe9962018-10-18 11:06:29 -07006
7import (
8 "bytes"
9 "compress/gzip"
10 "io/ioutil"
11 "sync"
Joe Tsaid8881392019-06-06 13:01:53 -070012
13 "google.golang.org/protobuf/internal/filedesc"
14 "google.golang.org/protobuf/reflect/protoreflect"
15 "google.golang.org/protobuf/reflect/protoregistry"
Joe Tsai90fe9962018-10-18 11:06:29 -070016)
17
18// Every enum and message type generated by protoc-gen-go since commit 2fc053c5
19// on February 25th, 2016 has had a method to get the raw descriptor.
20// Types that were not generated by protoc-gen-go or were generated prior
21// to that version are not supported.
22//
23// The []byte returned is the encoded form of a FileDescriptorProto message
24// compressed using GZIP. The []int is the path from the top-level file
25// to the specific message or enum declaration.
26type (
Joe Tsai6dbffb72018-12-04 14:06:19 -080027 enumV1 interface {
Joe Tsai90fe9962018-10-18 11:06:29 -070028 EnumDescriptor() ([]byte, []int)
29 }
Joe Tsai6dbffb72018-12-04 14:06:19 -080030 messageV1 interface {
Joe Tsai90fe9962018-10-18 11:06:29 -070031 Descriptor() ([]byte, []int)
32 }
33)
34
Joe Tsaid8881392019-06-06 13:01:53 -070035var legacyFileDescCache sync.Map // map[*byte]protoreflect.FileDescriptor
Joe Tsai90fe9962018-10-18 11:06:29 -070036
Joe Tsai21ade492019-05-22 13:42:54 -040037// legacyLoadFileDesc unmarshals b as a compressed FileDescriptorProto message.
Joe Tsai90fe9962018-10-18 11:06:29 -070038//
39// This assumes that b is immutable and that b does not refer to part of a
40// concatenated series of GZIP files (which would require shenanigans that
41// rely on the concatenation properties of both protobufs and GZIP).
42// File descriptors generated by protoc-gen-go do not rely on that property.
Joe Tsaid8881392019-06-06 13:01:53 -070043func legacyLoadFileDesc(b []byte) protoreflect.FileDescriptor {
Joe Tsai90fe9962018-10-18 11:06:29 -070044 // Fast-path: check whether we already have a cached file descriptor.
Joe Tsai21ade492019-05-22 13:42:54 -040045 if fd, ok := legacyFileDescCache.Load(&b[0]); ok {
Joe Tsaid8881392019-06-06 13:01:53 -070046 return fd.(protoreflect.FileDescriptor)
Joe Tsai90fe9962018-10-18 11:06:29 -070047 }
48
49 // Slow-path: decompress and unmarshal the file descriptor proto.
Joe Tsai90fe9962018-10-18 11:06:29 -070050 zr, err := gzip.NewReader(bytes.NewReader(b))
51 if err != nil {
52 panic(err)
53 }
Joe Tsaid8881392019-06-06 13:01:53 -070054 b2, err := ioutil.ReadAll(zr)
Joe Tsai90fe9962018-10-18 11:06:29 -070055 if err != nil {
56 panic(err)
57 }
Joe Tsaid8881392019-06-06 13:01:53 -070058
Joe Tsai52ec1752019-08-05 15:49:29 -070059 fd := filedesc.Builder{
Joe Tsaid8881392019-06-06 13:01:53 -070060 RawDescriptor: b2,
61 FileRegistry: resolverOnly{protoregistry.GlobalFiles}, // do not register back to global registry
62 }.Build().File
Joe Tsai21ade492019-05-22 13:42:54 -040063 if fd, ok := legacyFileDescCache.LoadOrStore(&b[0], fd); ok {
Joe Tsaid8881392019-06-06 13:01:53 -070064 return fd.(protoreflect.FileDescriptor)
Joe Tsaib9365042019-03-19 14:14:29 -070065 }
66 return fd
Joe Tsai90fe9962018-10-18 11:06:29 -070067}
Joe Tsaid8881392019-06-06 13:01:53 -070068
69type resolverOnly struct {
Damien Neila2684f42019-11-04 15:30:28 -080070 reg *protoregistry.Files
Joe Tsaid8881392019-06-06 13:01:53 -070071}
72
Damien Neila2684f42019-11-04 15:30:28 -080073func (r resolverOnly) FindFileByPath(path string) (protoreflect.FileDescriptor, error) {
74 return r.reg.FindFileByPath(path)
75}
76func (r resolverOnly) FindDescriptorByName(name protoreflect.FullName) (protoreflect.Descriptor, error) {
77 return r.reg.FindDescriptorByName(name)
78}
79func (resolverOnly) RegisterFile(protoreflect.FileDescriptor) error {
80 return nil
81}