Home | History | Annotate | Download | only in descriptor
      1 // Go support for Protocol Buffers - Google's data interchange format
      2 //
      3 // Copyright 2016 The Go Authors.  All rights reserved.
      4 // https://github.com/golang/protobuf
      5 //
      6 // Redistribution and use in source and binary forms, with or without
      7 // modification, are permitted provided that the following conditions are
      8 // met:
      9 //
     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
     15 // distribution.
     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.
     19 //
     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.
     31 
     32 // Package descriptor provides functions for obtaining protocol buffer
     33 // descriptors for generated Go types.
     34 //
     35 // These functions cannot go in package proto because they depend on the
     36 // generated protobuf descriptor messages, which themselves depend on proto.
     37 package descriptor
     38 
     39 import (
     40 	"bytes"
     41 	"compress/gzip"
     42 	"fmt"
     43 	"io/ioutil"
     44 
     45 	"github.com/golang/protobuf/proto"
     46 	protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor"
     47 )
     48 
     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))
     52 	if err != nil {
     53 		return nil, fmt.Errorf("failed to open gzip reader: %v", err)
     54 	}
     55 	defer r.Close()
     56 
     57 	b, err := ioutil.ReadAll(r)
     58 	if err != nil {
     59 		return nil, fmt.Errorf("failed to uncompress descriptor: %v", err)
     60 	}
     61 
     62 	fd := new(protobuf.FileDescriptorProto)
     63 	if err := proto.Unmarshal(b, fd); err != nil {
     64 		return nil, fmt.Errorf("malformed FileDescriptorProto: %v", err)
     65 	}
     66 
     67 	return fd, nil
     68 }
     69 
     70 // Message is a proto.Message with a method to return its descriptor.
     71 //
     72 // Message types generated by the protocol compiler always satisfy
     73 // the Message interface.
     74 type Message interface {
     75 	proto.Message
     76 	Descriptor() ([]byte, []int)
     77 }
     78 
     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)
     84 	if err != nil {
     85 		panic(fmt.Sprintf("invalid FileDescriptorProto for %T: %v", msg, err))
     86 	}
     87 
     88 	md = fd.MessageType[path[0]]
     89 	for _, i := range path[1:] {
     90 		md = md.NestedType[i]
     91 	}
     92 	return fd, md
     93 }
     94