descriptor.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // Package descriptor provides functions for obtaining protocol buffer
  32. // descriptors for generated Go types.
  33. //
  34. // These functions cannot go in package proto because they depend on the
  35. // generated protobuf descriptor messages, which themselves depend on proto.
  36. package descriptor
  37. import (
  38. "bytes"
  39. "compress/gzip"
  40. "fmt"
  41. "io/ioutil"
  42. "github.com/golang/protobuf/proto"
  43. protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor"
  44. )
  45. // extractFile extracts a FileDescriptorProto from a gzip'd buffer.
  46. func extractFile(gz []byte) (*protobuf.FileDescriptorProto, error) {
  47. r, err := gzip.NewReader(bytes.NewReader(gz))
  48. if err != nil {
  49. return nil, fmt.Errorf("failed to open gzip reader: %v", err)
  50. }
  51. defer r.Close()
  52. b, err := ioutil.ReadAll(r)
  53. if err != nil {
  54. return nil, fmt.Errorf("failed to uncompress descriptor: %v", err)
  55. }
  56. fd := new(protobuf.FileDescriptorProto)
  57. if err := proto.Unmarshal(b, fd); err != nil {
  58. return nil, fmt.Errorf("malformed FileDescriptorProto: %v", err)
  59. }
  60. return fd, nil
  61. }
  62. // Message is a proto.Message with a method to return its descriptor.
  63. //
  64. // Message types generated by the protocol compiler always satisfy
  65. // the Message interface.
  66. type Message interface {
  67. proto.Message
  68. Descriptor() ([]byte, []int)
  69. }
  70. // ForMessage returns a FileDescriptorProto and a DescriptorProto from within it
  71. // describing the given message.
  72. func ForMessage(msg Message) (fd *protobuf.FileDescriptorProto, md *protobuf.DescriptorProto) {
  73. gz, path := msg.Descriptor()
  74. fd, err := extractFile(gz)
  75. if err != nil {
  76. panic(fmt.Sprintf("invalid FileDescriptorProto for %T: %v", msg, err))
  77. }
  78. md = fd.MessageType[path[0]]
  79. for _, i := range path[1:] {
  80. md = md.NestedType[i]
  81. }
  82. return fd, md
  83. }