blob: 0f871a06842bc222387c58faf489f773e7502c54 [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 Tsai08e00302018-11-26 22:32:06 -08005package legacy
Joe Tsai90fe9962018-10-18 11:06:29 -07006
7import (
8 "bytes"
9 "compress/gzip"
10 "io/ioutil"
11 "sync"
12
Joe Tsaieeca8bb2018-12-04 16:24:22 -080013 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsai90fe9962018-10-18 11:06:29 -070014)
15
16// Every enum and message type generated by protoc-gen-go since commit 2fc053c5
17// on February 25th, 2016 has had a method to get the raw descriptor.
18// Types that were not generated by protoc-gen-go or were generated prior
19// to that version are not supported.
20//
21// The []byte returned is the encoded form of a FileDescriptorProto message
22// compressed using GZIP. The []int is the path from the top-level file
23// to the specific message or enum declaration.
24type (
Joe Tsai6dbffb72018-12-04 14:06:19 -080025 enumV1 interface {
Joe Tsai90fe9962018-10-18 11:06:29 -070026 EnumDescriptor() ([]byte, []int)
27 }
Joe Tsai6dbffb72018-12-04 14:06:19 -080028 messageV1 interface {
Joe Tsai90fe9962018-10-18 11:06:29 -070029 Descriptor() ([]byte, []int)
30 }
31)
32
Joe Tsaie1f8d502018-11-26 18:55:29 -080033var fileDescCache sync.Map // map[*byte]*descriptorpb.FileDescriptorProto
Joe Tsai90fe9962018-10-18 11:06:29 -070034
Damien Neil987d5702019-04-10 11:06:53 -070035// loadFileDesc unmarshals b as a compressed FileDescriptorProto message.
Joe Tsai90fe9962018-10-18 11:06:29 -070036//
37// This assumes that b is immutable and that b does not refer to part of a
38// concatenated series of GZIP files (which would require shenanigans that
39// rely on the concatenation properties of both protobufs and GZIP).
40// File descriptors generated by protoc-gen-go do not rely on that property.
Damien Neil987d5702019-04-10 11:06:53 -070041func loadFileDesc(b []byte) *fileDescriptorProto {
Joe Tsai90fe9962018-10-18 11:06:29 -070042 // Fast-path: check whether we already have a cached file descriptor.
Joe Tsaib9365042019-03-19 14:14:29 -070043 if fd, ok := fileDescCache.Load(&b[0]); ok {
Damien Neil987d5702019-04-10 11:06:53 -070044 return fd.(*fileDescriptorProto)
Joe Tsai90fe9962018-10-18 11:06:29 -070045 }
46
47 // Slow-path: decompress and unmarshal the file descriptor proto.
Joe Tsai90fe9962018-10-18 11:06:29 -070048 zr, err := gzip.NewReader(bytes.NewReader(b))
49 if err != nil {
50 panic(err)
51 }
52 b, err = ioutil.ReadAll(zr)
53 if err != nil {
54 panic(err)
55 }
Damien Neil987d5702019-04-10 11:06:53 -070056 fd := parseFileDescProto(b)
Joe Tsaib9365042019-03-19 14:14:29 -070057 if fd, ok := fileDescCache.LoadOrStore(&b[0], fd); ok {
Damien Neil987d5702019-04-10 11:06:53 -070058 return fd.(*fileDescriptorProto)
Joe Tsaib9365042019-03-19 14:14:29 -070059 }
60 return fd
Joe Tsai90fe9962018-10-18 11:06:29 -070061}
Joe Tsai08e00302018-11-26 22:32:06 -080062
63// parentFileDescriptor returns the parent protoreflect.FileDescriptor for the
64// provide descriptor. It returns nil if there is no parent.
65func parentFileDescriptor(d pref.Descriptor) pref.FileDescriptor {
66 for ok := true; ok; d, ok = d.Parent() {
67 if fd, _ := d.(pref.FileDescriptor); fd != nil {
68 return fd
69 }
70 }
71 return nil
72}