descriptor.go 5.8 KB

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