blob: f4984c819a30619f383bd95de32ae2dd64969993 [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
Damien Neil4be2fb42018-12-17 11:16:16 -080013 "github.com/golang/protobuf/v2/proto"
Joe Tsaieeca8bb2018-12-04 16:24:22 -080014 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsaie1f8d502018-11-26 18:55:29 -080015 descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
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 Tsaie1f8d502018-11-26 18:55:29 -080035var fileDescCache sync.Map // map[*byte]*descriptorpb.FileDescriptorProto
Joe Tsai90fe9962018-10-18 11:06:29 -070036
Joe Tsai35ec98f2019-03-25 14:41:32 -070037// LoadFileDesc 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 Tsai35ec98f2019-03-25 14:41:32 -070043//
44// This is exported for testing purposes.
45func LoadFileDesc(b []byte) *descriptorpb.FileDescriptorProto {
Joe Tsai90fe9962018-10-18 11:06:29 -070046 // Fast-path: check whether we already have a cached file descriptor.
Joe Tsaib9365042019-03-19 14:14:29 -070047 if fd, ok := fileDescCache.Load(&b[0]); ok {
48 return fd.(*descriptorpb.FileDescriptorProto)
Joe Tsai90fe9962018-10-18 11:06:29 -070049 }
50
51 // Slow-path: decompress and unmarshal the file descriptor proto.
Joe Tsaib9365042019-03-19 14:14:29 -070052 fd := new(descriptorpb.FileDescriptorProto)
Joe Tsai90fe9962018-10-18 11:06:29 -070053 zr, err := gzip.NewReader(bytes.NewReader(b))
54 if err != nil {
55 panic(err)
56 }
57 b, err = ioutil.ReadAll(zr)
58 if err != nil {
59 panic(err)
60 }
Joe Tsaib9365042019-03-19 14:14:29 -070061 err = proto.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, fd)
Joe Tsai69996ba2019-03-14 14:06:05 -070062 if err != nil {
Joe Tsai90fe9962018-10-18 11:06:29 -070063 panic(err)
64 }
Joe Tsaib9365042019-03-19 14:14:29 -070065 if fd, ok := fileDescCache.LoadOrStore(&b[0], fd); ok {
66 return fd.(*descriptorpb.FileDescriptorProto)
67 }
68 return fd
Joe Tsai90fe9962018-10-18 11:06:29 -070069}
Joe Tsai08e00302018-11-26 22:32:06 -080070
71// parentFileDescriptor returns the parent protoreflect.FileDescriptor for the
72// provide descriptor. It returns nil if there is no parent.
73func parentFileDescriptor(d pref.Descriptor) pref.FileDescriptor {
74 for ok := true; ok; d, ok = d.Parent() {
75 if fd, _ := d.(pref.FileDescriptor); fd != nil {
76 return fd
77 }
78 }
79 return nil
80}