Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 1 | // 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 Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 5 | package legacy |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "compress/gzip" |
| 10 | "io/ioutil" |
| 11 | "sync" |
| 12 | |
Damien Neil | 4be2fb4 | 2018-12-17 11:16:16 -0800 | [diff] [blame] | 13 | "github.com/golang/protobuf/v2/proto" |
Joe Tsai | eeca8bb | 2018-12-04 16:24:22 -0800 | [diff] [blame] | 14 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 15 | descriptorpb "github.com/golang/protobuf/v2/types/descriptor" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 16 | ) |
| 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. |
| 26 | type ( |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 27 | enumV1 interface { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 28 | EnumDescriptor() ([]byte, []int) |
| 29 | } |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 30 | messageV1 interface { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 31 | Descriptor() ([]byte, []int) |
| 32 | } |
| 33 | ) |
| 34 | |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 35 | var fileDescCache sync.Map // map[*byte]*descriptorpb.FileDescriptorProto |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 36 | |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 37 | // loadFileDesc unmarshals b as a compressed FileDescriptorProto message. |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 38 | // |
| 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 Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 43 | func loadFileDesc(b []byte) *descriptorpb.FileDescriptorProto { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 44 | // Fast-path: check whether we already have a cached file descriptor. |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame^] | 45 | if fd, ok := fileDescCache.Load(&b[0]); ok { |
| 46 | return fd.(*descriptorpb.FileDescriptorProto) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Slow-path: decompress and unmarshal the file descriptor proto. |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame^] | 50 | fd := new(descriptorpb.FileDescriptorProto) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 51 | zr, err := gzip.NewReader(bytes.NewReader(b)) |
| 52 | if err != nil { |
| 53 | panic(err) |
| 54 | } |
| 55 | b, err = ioutil.ReadAll(zr) |
| 56 | if err != nil { |
| 57 | panic(err) |
| 58 | } |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame^] | 59 | err = proto.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, fd) |
Joe Tsai | 69996ba | 2019-03-14 14:06:05 -0700 | [diff] [blame] | 60 | if err != nil { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 61 | panic(err) |
| 62 | } |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame^] | 63 | if fd, ok := fileDescCache.LoadOrStore(&b[0], fd); ok { |
| 64 | return fd.(*descriptorpb.FileDescriptorProto) |
| 65 | } |
| 66 | return fd |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 67 | } |
Joe Tsai | 08e0030 | 2018-11-26 22:32:06 -0800 | [diff] [blame] | 68 | |
| 69 | // parentFileDescriptor returns the parent protoreflect.FileDescriptor for the |
| 70 | // provide descriptor. It returns nil if there is no parent. |
| 71 | func parentFileDescriptor(d pref.Descriptor) pref.FileDescriptor { |
| 72 | for ok := true; ok; d, ok = d.Parent() { |
| 73 | if fd, _ := d.(pref.FileDescriptor); fd != nil { |
| 74 | return fd |
| 75 | } |
| 76 | } |
| 77 | return nil |
| 78 | } |