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/gogo/protobuf/proto" 46 ) 47 48 // extractFile extracts a FileDescriptorProto from a gzip'd buffer. 49 func extractFile(gz []byte) (*FileDescriptorProto, error) { 50 r, err := gzip.NewReader(bytes.NewReader(gz)) 51 if err != nil { 52 return nil, fmt.Errorf("failed to open gzip reader: %v", err) 53 } 54 defer r.Close() 55 56 b, err := ioutil.ReadAll(r) 57 if err != nil { 58 return nil, fmt.Errorf("failed to uncompress descriptor: %v", err) 59 } 60 61 fd := new(FileDescriptorProto) 62 if err := proto.Unmarshal(b, fd); err != nil { 63 return nil, fmt.Errorf("malformed FileDescriptorProto: %v", err) 64 } 65 66 return fd, nil 67 } 68 69 // Message is a proto.Message with a method to return its descriptor. 70 // 71 // Message types generated by the protocol compiler always satisfy 72 // the Message interface. 73 type Message interface { 74 proto.Message 75 Descriptor() ([]byte, []int) 76 } 77 78 // ForMessage returns a FileDescriptorProto and a DescriptorProto from within it 79 // describing the given message. 80 func ForMessage(msg Message) (fd *FileDescriptorProto, md *DescriptorProto) { 81 gz, path := msg.Descriptor() 82 fd, err := extractFile(gz) 83 if err != nil { 84 panic(fmt.Sprintf("invalid FileDescriptorProto for %T: %v", msg, err)) 85 } 86 87 md = fd.MessageType[path[0]] 88 for _, i := range path[1:] { 89 md = md.NestedType[i] 90 } 91 return fd, md 92 } 93 94 // Is this field a scalar numeric type? 95 func (field *FieldDescriptorProto) IsScalar() bool { 96 if field.Type == nil { 97 return false 98 } 99 switch *field.Type { 100 case FieldDescriptorProto_TYPE_DOUBLE, 101 FieldDescriptorProto_TYPE_FLOAT, 102 FieldDescriptorProto_TYPE_INT64, 103 FieldDescriptorProto_TYPE_UINT64, 104 FieldDescriptorProto_TYPE_INT32, 105 FieldDescriptorProto_TYPE_FIXED64, 106 FieldDescriptorProto_TYPE_FIXED32, 107 FieldDescriptorProto_TYPE_BOOL, 108 FieldDescriptorProto_TYPE_UINT32, 109 FieldDescriptorProto_TYPE_ENUM, 110 FieldDescriptorProto_TYPE_SFIXED32, 111 FieldDescriptorProto_TYPE_SFIXED64, 112 FieldDescriptorProto_TYPE_SINT32, 113 FieldDescriptorProto_TYPE_SINT64: 114 return true 115 default: 116 return false 117 } 118 } 119