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 | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame^] | 5 | package impl |
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" |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | // Every enum and message type generated by protoc-gen-go since commit 2fc053c5 |
| 15 | // on February 25th, 2016 has had a method to get the raw descriptor. |
| 16 | // Types that were not generated by protoc-gen-go or were generated prior |
| 17 | // to that version are not supported. |
| 18 | // |
| 19 | // The []byte returned is the encoded form of a FileDescriptorProto message |
| 20 | // compressed using GZIP. The []int is the path from the top-level file |
| 21 | // to the specific message or enum declaration. |
| 22 | type ( |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 23 | enumV1 interface { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 24 | EnumDescriptor() ([]byte, []int) |
| 25 | } |
Joe Tsai | 6dbffb7 | 2018-12-04 14:06:19 -0800 | [diff] [blame] | 26 | messageV1 interface { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 27 | Descriptor() ([]byte, []int) |
| 28 | } |
| 29 | ) |
| 30 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame^] | 31 | var legacyFileDescCache sync.Map // map[*byte]*descriptorpb.FileDescriptorProto |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 32 | |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame^] | 33 | // legacyLoadFileDesc unmarshals b as a compressed FileDescriptorProto message. |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 34 | // |
| 35 | // This assumes that b is immutable and that b does not refer to part of a |
| 36 | // concatenated series of GZIP files (which would require shenanigans that |
| 37 | // rely on the concatenation properties of both protobufs and GZIP). |
| 38 | // File descriptors generated by protoc-gen-go do not rely on that property. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame^] | 39 | func legacyLoadFileDesc(b []byte) *legacyFileDescriptorProto { |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 40 | // Fast-path: check whether we already have a cached file descriptor. |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame^] | 41 | if fd, ok := legacyFileDescCache.Load(&b[0]); ok { |
| 42 | return fd.(*legacyFileDescriptorProto) |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Slow-path: decompress and unmarshal the file descriptor proto. |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 46 | zr, err := gzip.NewReader(bytes.NewReader(b)) |
| 47 | if err != nil { |
| 48 | panic(err) |
| 49 | } |
| 50 | b, err = ioutil.ReadAll(zr) |
| 51 | if err != nil { |
| 52 | panic(err) |
| 53 | } |
Joe Tsai | 21ade49 | 2019-05-22 13:42:54 -0400 | [diff] [blame^] | 54 | fd := legacyParseFileDescProto(b) |
| 55 | if fd, ok := legacyFileDescCache.LoadOrStore(&b[0], fd); ok { |
| 56 | return fd.(*legacyFileDescriptorProto) |
Joe Tsai | b936504 | 2019-03-19 14:14:29 -0700 | [diff] [blame] | 57 | } |
| 58 | return fd |
Joe Tsai | 90fe996 | 2018-10-18 11:06:29 -0700 | [diff] [blame] | 59 | } |