1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2016 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // Package descriptor provides functions for obtaining protocol buffer
33 // descriptors for generated Go types.
35 // These functions cannot go in package proto because they depend on the
36 // generated protobuf descriptor messages, which themselves depend on proto.
45 "github.com/golang/protobuf/proto"
46 protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor"
49 // extractFile extracts a FileDescriptorProto from a gzip'd buffer.
50 func extractFile(gz []byte) (*protobuf.FileDescriptorProto, error) {
51 r, err := gzip.NewReader(bytes.NewReader(gz))
53 return nil, fmt.Errorf("failed to open gzip reader: %v", err)
57 b, err := ioutil.ReadAll(r)
59 return nil, fmt.Errorf("failed to uncompress descriptor: %v", err)
62 fd := new(protobuf.FileDescriptorProto)
63 if err := proto.Unmarshal(b, fd); err != nil {
64 return nil, fmt.Errorf("malformed FileDescriptorProto: %v", err)
70 // Message is a proto.Message with a method to return its descriptor.
72 // Message types generated by the protocol compiler always satisfy
73 // the Message interface.
74 type Message interface {
76 Descriptor() ([]byte, []int)
79 // ForMessage returns a FileDescriptorProto and a DescriptorProto from within it
80 // describing the given message.
81 func ForMessage(msg Message) (fd *protobuf.FileDescriptorProto, md *protobuf.DescriptorProto) {
82 gz, path := msg.Descriptor()
83 fd, err := extractFile(gz)
85 panic(fmt.Sprintf("invalid FileDescriptorProto for %T: %v", msg, err))
88 md = fd.MessageType[path[0]]
89 for _, i := range path[1:] {