blob: c3c4a2b87efa5b40baac3802d3193c860d68281e [file] [log] [blame]
amit.ghosh258d14c2020-10-02 15:13:38 +02001// Copyright 2016 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.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07004
amit.ghosh258d14c2020-10-02 15:13:38 +02005// Package descriptor provides functions for obtaining the protocol buffer
6// descriptors of generated Go types.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07007//
amit.ghosh258d14c2020-10-02 15:13:38 +02008// Deprecated: See the "google.golang.org/protobuf/reflect/protoreflect" package
9// for how to obtain an EnumDescriptor or MessageDescriptor in order to
10// programatically interact with the protobuf type system.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070011package descriptor
12
13import (
14 "bytes"
15 "compress/gzip"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070016 "io/ioutil"
amit.ghosh258d14c2020-10-02 15:13:38 +020017 "sync"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070018
19 "github.com/golang/protobuf/proto"
amit.ghosh258d14c2020-10-02 15:13:38 +020020 "google.golang.org/protobuf/reflect/protodesc"
21 "google.golang.org/protobuf/reflect/protoreflect"
22 "google.golang.org/protobuf/runtime/protoimpl"
23
24 descriptorpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070025)
26
amit.ghosh258d14c2020-10-02 15:13:38 +020027// Message is proto.Message with a method to return its descriptor.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070028//
amit.ghosh258d14c2020-10-02 15:13:38 +020029// Deprecated: The Descriptor method may not be generated by future
30// versions of protoc-gen-go, meaning that this interface may not
31// be implemented by many concrete message types.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070032type Message interface {
33 proto.Message
34 Descriptor() ([]byte, []int)
35}
36
amit.ghosh258d14c2020-10-02 15:13:38 +020037// ForMessage returns the file descriptor proto containing
38// the message and the message descriptor proto for the message itself.
39// The returned proto messages must not be mutated.
40//
41// Deprecated: Not all concrete message types satisfy the Message interface.
42// Use MessageDescriptorProto instead. If possible, the calling code should
43// be rewritten to use protobuf reflection instead.
44// See package "google.golang.org/protobuf/reflect/protoreflect" for details.
45func ForMessage(m Message) (*descriptorpb.FileDescriptorProto, *descriptorpb.DescriptorProto) {
46 return MessageDescriptorProto(m)
47}
48
49type rawDesc struct {
50 fileDesc []byte
51 indexes []int
52}
53
54var rawDescCache sync.Map // map[protoreflect.Descriptor]*rawDesc
55
56func deriveRawDescriptor(d protoreflect.Descriptor) ([]byte, []int) {
57 // Fast-path: check whether raw descriptors are already cached.
58 origDesc := d
59 if v, ok := rawDescCache.Load(origDesc); ok {
60 return v.(*rawDesc).fileDesc, v.(*rawDesc).indexes
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070061 }
62
amit.ghosh258d14c2020-10-02 15:13:38 +020063 // Slow-path: derive the raw descriptor from the v2 descriptor.
64
65 // Start with the leaf (a given enum or message declaration) and
66 // ascend upwards until we hit the parent file descriptor.
67 var idxs []int
68 for {
69 idxs = append(idxs, d.Index())
70 d = d.Parent()
71 if d == nil {
72 // TODO: We could construct a FileDescriptor stub for standalone
73 // descriptors to satisfy the API.
74 return nil, nil
75 }
76 if _, ok := d.(protoreflect.FileDescriptor); ok {
77 break
78 }
79 }
80
81 // Obtain the raw file descriptor.
82 var raw []byte
83 switch fd := d.(type) {
84 case interface{ ProtoLegacyRawDesc() []byte }:
85 raw = fd.ProtoLegacyRawDesc()
86 case protoreflect.FileDescriptor:
87 raw, _ = proto.Marshal(protodesc.ToFileDescriptorProto(fd))
88 }
89 file := protoimpl.X.CompressGZIP(raw)
90
91 // Reverse the indexes, since we populated it in reverse.
92 for i, j := 0, len(idxs)-1; i < j; i, j = i+1, j-1 {
93 idxs[i], idxs[j] = idxs[j], idxs[i]
94 }
95
96 if v, ok := rawDescCache.LoadOrStore(origDesc, &rawDesc{file, idxs}); ok {
97 return v.(*rawDesc).fileDesc, v.(*rawDesc).indexes
98 }
99 return file, idxs
100}
101
102// EnumRawDescriptor returns the GZIP'd raw file descriptor representing
103// the enum and the index path to reach the enum declaration.
104// The returned slices must not be mutated.
105func EnumRawDescriptor(e proto.GeneratedEnum) ([]byte, []int) {
106 if ev, ok := e.(interface{ EnumDescriptor() ([]byte, []int) }); ok {
107 return ev.EnumDescriptor()
108 }
109 ed := protoimpl.X.EnumTypeOf(e)
110 return deriveRawDescriptor(ed.Descriptor())
111}
112
113// MessageRawDescriptor returns the GZIP'd raw file descriptor representing
114// the message and the index path to reach the message declaration.
115// The returned slices must not be mutated.
116func MessageRawDescriptor(m proto.GeneratedMessage) ([]byte, []int) {
117 if mv, ok := m.(interface{ Descriptor() ([]byte, []int) }); ok {
118 return mv.Descriptor()
119 }
120 md := protoimpl.X.MessageTypeOf(m)
121 return deriveRawDescriptor(md.Descriptor())
122}
123
124var fileDescCache sync.Map // map[*byte]*descriptorpb.FileDescriptorProto
125
126func deriveFileDescriptor(rawDesc []byte) *descriptorpb.FileDescriptorProto {
127 // Fast-path: check whether descriptor protos are already cached.
128 if v, ok := fileDescCache.Load(&rawDesc[0]); ok {
129 return v.(*descriptorpb.FileDescriptorProto)
130 }
131
132 // Slow-path: derive the descriptor proto from the GZIP'd message.
133 zr, err := gzip.NewReader(bytes.NewReader(rawDesc))
134 if err != nil {
135 panic(err)
136 }
137 b, err := ioutil.ReadAll(zr)
138 if err != nil {
139 panic(err)
140 }
141 fd := new(descriptorpb.FileDescriptorProto)
142 if err := proto.Unmarshal(b, fd); err != nil {
143 panic(err)
144 }
145 if v, ok := fileDescCache.LoadOrStore(&rawDesc[0], fd); ok {
146 return v.(*descriptorpb.FileDescriptorProto)
147 }
148 return fd
149}
150
151// EnumDescriptorProto returns the file descriptor proto representing
152// the enum and the enum descriptor proto for the enum itself.
153// The returned proto messages must not be mutated.
154func EnumDescriptorProto(e proto.GeneratedEnum) (*descriptorpb.FileDescriptorProto, *descriptorpb.EnumDescriptorProto) {
155 rawDesc, idxs := EnumRawDescriptor(e)
156 if rawDesc == nil || idxs == nil {
157 return nil, nil
158 }
159 fd := deriveFileDescriptor(rawDesc)
160 if len(idxs) == 1 {
161 return fd, fd.EnumType[idxs[0]]
162 }
163 md := fd.MessageType[idxs[0]]
164 for _, i := range idxs[1 : len(idxs)-1] {
165 md = md.NestedType[i]
166 }
167 ed := md.EnumType[idxs[len(idxs)-1]]
168 return fd, ed
169}
170
171// MessageDescriptorProto returns the file descriptor proto representing
172// the message and the message descriptor proto for the message itself.
173// The returned proto messages must not be mutated.
174func MessageDescriptorProto(m proto.GeneratedMessage) (*descriptorpb.FileDescriptorProto, *descriptorpb.DescriptorProto) {
175 rawDesc, idxs := MessageRawDescriptor(m)
176 if rawDesc == nil || idxs == nil {
177 return nil, nil
178 }
179 fd := deriveFileDescriptor(rawDesc)
180 md := fd.MessageType[idxs[0]]
181 for _, i := range idxs[1:] {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700182 md = md.NestedType[i]
183 }
184 return fd, md
185}