registry.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // Copyright 2019 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 proto
  5. import (
  6. "bytes"
  7. "compress/gzip"
  8. "fmt"
  9. "io/ioutil"
  10. "reflect"
  11. "strings"
  12. "sync"
  13. "google.golang.org/protobuf/reflect/protodesc"
  14. "google.golang.org/protobuf/reflect/protoreflect"
  15. "google.golang.org/protobuf/reflect/protoregistry"
  16. "google.golang.org/protobuf/runtime/protoimpl"
  17. )
  18. // filePath is the path to the proto source file.
  19. type filePath = string // e.g., "google/protobuf/descriptor.proto"
  20. // fileDescGZIP is the compressed contents of the encoded FileDescriptorProto.
  21. type fileDescGZIP = []byte
  22. var fileCache sync.Map // map[filePath]fileDescGZIP
  23. // RegisterFile is called from generated code to register the compressed
  24. // FileDescriptorProto with the file path for a proto source file.
  25. //
  26. // Deprecated: Use protoregistry.GlobalFiles.RegisterFile instead.
  27. func RegisterFile(s filePath, d fileDescGZIP) {
  28. // Decompress the descriptor.
  29. zr, err := gzip.NewReader(bytes.NewReader(d))
  30. if err != nil {
  31. panic(fmt.Sprintf("proto: invalid compressed file descriptor: %v", err))
  32. }
  33. b, err := ioutil.ReadAll(zr)
  34. if err != nil {
  35. panic(fmt.Sprintf("proto: invalid compressed file descriptor: %v", err))
  36. }
  37. // Construct a protoreflect.FileDescriptor from the raw descriptor.
  38. // Note that DescBuilder.Build automatically registers the constructed
  39. // file descriptor with the v2 registry.
  40. protoimpl.DescBuilder{RawDescriptor: b}.Build()
  41. // Locally cache the raw descriptor form for the file.
  42. fileCache.Store(s, d)
  43. }
  44. // FileDescriptor returns the compressed FileDescriptorProto given the file path
  45. // for a proto source file. It returns nil if not found.
  46. //
  47. // Deprecated: Use protoregistry.GlobalFiles.FindFileByPath instead.
  48. func FileDescriptor(s filePath) fileDescGZIP {
  49. if v, ok := fileCache.Load(s); ok {
  50. return v.(fileDescGZIP)
  51. }
  52. // Find the descriptor in the v2 registry.
  53. var b []byte
  54. if fd, _ := protoregistry.GlobalFiles.FindFileByPath(s); fd != nil {
  55. b, _ = Marshal(protodesc.ToFileDescriptorProto(fd))
  56. }
  57. // Locally cache the raw descriptor form for the file.
  58. if len(b) > 0 {
  59. v, _ := fileCache.LoadOrStore(s, protoimpl.X.CompressGZIP(b))
  60. return v.(fileDescGZIP)
  61. }
  62. return nil
  63. }
  64. // enumName is the name of an enum. For historical reasons, the enum name is
  65. // neither the full Go name nor the full protobuf name of the enum.
  66. // The name is the dot-separated combination of just the proto package that the
  67. // enum is declared within followed by the Go type name of the generated enum.
  68. type enumName = string // e.g., "my.proto.package.GoMessage_GoEnum"
  69. // enumsByName maps enum values by name to their numeric counterpart.
  70. type enumsByName = map[string]int32
  71. // enumsByNumber maps enum values by number to their name counterpart.
  72. type enumsByNumber = map[int32]string
  73. var enumCache sync.Map // map[enumName]enumsByName
  74. var numFilesCache sync.Map // map[protoreflect.FullName]int
  75. // RegisterEnum is called from the generated code to register the mapping of
  76. // enum value names to enum numbers for the enum identified by s.
  77. //
  78. // Deprecated: Use protoregistry.GlobalTypes.RegisterEnum instead.
  79. func RegisterEnum(s enumName, _ enumsByNumber, m enumsByName) {
  80. if _, ok := enumCache.Load(s); ok {
  81. panic("proto: duplicate enum registered: " + s)
  82. }
  83. enumCache.Store(s, m)
  84. // This does not forward registration to the v2 registry since this API
  85. // lacks sufficient information to construct a complete v2 enum descriptor.
  86. }
  87. // EnumValueMap returns the mapping from enum value names to enum numbers for
  88. // the enum of the given name. It returns nil if not found.
  89. //
  90. // Deprecated: Use protoregistry.GlobalTypes.FindEnumByName instead.
  91. func EnumValueMap(s enumName) enumsByName {
  92. if v, ok := enumCache.Load(s); ok {
  93. return v.(enumsByName)
  94. }
  95. // Check whether the cache is stale. If the number of files in the current
  96. // package differs, then it means that some enums may have been recently
  97. // registered upstream that we do not know about.
  98. var protoPkg protoreflect.FullName
  99. if i := strings.LastIndexByte(s, '.'); i >= 0 {
  100. protoPkg = protoreflect.FullName(s[:i])
  101. }
  102. v, _ := numFilesCache.Load(protoPkg)
  103. numFiles, _ := v.(int)
  104. if protoregistry.GlobalFiles.NumFilesByPackage(protoPkg) == numFiles {
  105. return nil // cache is up-to-date; was not found earlier
  106. }
  107. // Update the enum cache for all enums declared in the given proto package.
  108. numFiles = 0
  109. protoregistry.GlobalFiles.RangeFilesByPackage(protoPkg, func(fd protoreflect.FileDescriptor) bool {
  110. walkEnums(fd, func(ed protoreflect.EnumDescriptor) {
  111. name := protoimpl.X.LegacyEnumName(ed)
  112. if _, ok := enumCache.Load(name); !ok {
  113. m := make(enumsByName)
  114. evs := ed.Values()
  115. for i := evs.Len() - 1; i >= 0; i-- {
  116. ev := evs.Get(i)
  117. m[string(ev.Name())] = int32(ev.Number())
  118. }
  119. enumCache.LoadOrStore(name, m)
  120. }
  121. })
  122. numFiles++
  123. return true
  124. })
  125. numFilesCache.Store(protoPkg, numFiles)
  126. // Check cache again for enum map.
  127. if v, ok := enumCache.Load(s); ok {
  128. return v.(enumsByName)
  129. }
  130. return nil
  131. }
  132. // walkEnums recursively walks all enums declared in d.
  133. func walkEnums(d interface {
  134. Enums() protoreflect.EnumDescriptors
  135. Messages() protoreflect.MessageDescriptors
  136. }, f func(protoreflect.EnumDescriptor)) {
  137. eds := d.Enums()
  138. for i := eds.Len() - 1; i >= 0; i-- {
  139. f(eds.Get(i))
  140. }
  141. mds := d.Messages()
  142. for i := mds.Len() - 1; i >= 0; i-- {
  143. walkEnums(mds.Get(i), f)
  144. }
  145. }
  146. // messageName is the full name of protobuf message.
  147. type messageName = string
  148. var messageTypeCache sync.Map // map[messageName]reflect.Type
  149. // RegisterType is called from generated code to register the message Go type
  150. // for a message of the given name.
  151. //
  152. // Deprecated: Use protoregistry.GlobalTypes.RegisterMessage instead.
  153. func RegisterType(m Message, s messageName) {
  154. mt := protoimpl.X.LegacyMessageTypeOf(m, protoreflect.FullName(s))
  155. if err := protoregistry.GlobalTypes.RegisterMessage(mt); err != nil {
  156. panic(err)
  157. }
  158. messageTypeCache.Store(s, reflect.TypeOf(m))
  159. }
  160. // RegisterMapType is called from generated code to register the Go map type
  161. // for a protobuf message representing a map entry.
  162. //
  163. // Deprecated: Do not use.
  164. func RegisterMapType(m interface{}, s messageName) {
  165. t := reflect.TypeOf(m)
  166. if t.Kind() != reflect.Map {
  167. panic(fmt.Sprintf("invalid map kind: %v", t))
  168. }
  169. if _, ok := messageTypeCache.Load(s); ok {
  170. panic(fmt.Errorf("proto: duplicate proto message registered: %s", s))
  171. }
  172. messageTypeCache.Store(s, t)
  173. }
  174. // MessageType returns the message type for a named message.
  175. // It returns nil if not found.
  176. //
  177. // Deprecated: Use protoregistry.GlobalTypes.FindMessageByName instead.
  178. func MessageType(s messageName) reflect.Type {
  179. if v, ok := messageTypeCache.Load(s); ok {
  180. return v.(reflect.Type)
  181. }
  182. // Derive the message type from the v2 registry.
  183. var t reflect.Type
  184. if mt, _ := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(s)); mt != nil {
  185. t = messageGoType(mt)
  186. }
  187. // If we could not get a concrete type, it is possible that it is a
  188. // pseudo-message for a map entry.
  189. if t == nil {
  190. d, _ := protoregistry.GlobalFiles.FindDescriptorByName(protoreflect.FullName(s))
  191. if md, _ := d.(protoreflect.MessageDescriptor); md != nil && md.IsMapEntry() {
  192. kt := goTypeForField(md.Fields().ByNumber(1))
  193. vt := goTypeForField(md.Fields().ByNumber(2))
  194. t = reflect.MapOf(kt, vt)
  195. }
  196. }
  197. // Locally cache the message type for the given name.
  198. if t != nil {
  199. v, _ := messageTypeCache.LoadOrStore(s, t)
  200. return v.(reflect.Type)
  201. }
  202. return nil
  203. }
  204. func goTypeForField(fd protoreflect.FieldDescriptor) reflect.Type {
  205. switch k := fd.Kind(); k {
  206. case protoreflect.EnumKind:
  207. if et, _ := protoregistry.GlobalTypes.FindEnumByName(fd.Enum().FullName()); et != nil {
  208. return enumGoType(et)
  209. }
  210. return reflect.TypeOf(protoreflect.EnumNumber(0))
  211. case protoreflect.MessageKind, protoreflect.GroupKind:
  212. if mt, _ := protoregistry.GlobalTypes.FindMessageByName(fd.Message().FullName()); mt != nil {
  213. return messageGoType(mt)
  214. }
  215. return reflect.TypeOf((*protoreflect.Message)(nil)).Elem()
  216. default:
  217. return reflect.TypeOf(fd.Default().Interface())
  218. }
  219. }
  220. func enumGoType(et protoreflect.EnumType) reflect.Type {
  221. return reflect.TypeOf(et.New(0))
  222. }
  223. func messageGoType(mt protoreflect.MessageType) reflect.Type {
  224. return reflect.TypeOf(MessageV1(mt.Zero().Interface()))
  225. }
  226. // MessageName returns the full protobuf name for the given message type.
  227. //
  228. // Deprecated: Use protoreflect.MessageDescriptor.FullName instead.
  229. func MessageName(m Message) messageName {
  230. if m == nil {
  231. return ""
  232. }
  233. if m, ok := m.(interface{ XXX_MessageName() messageName }); ok {
  234. return m.XXX_MessageName()
  235. }
  236. return messageName(protoimpl.X.MessageDescriptorOf(m).FullName())
  237. }
  238. // RegisterExtension is called from the generated code to register
  239. // the extension descriptor.
  240. //
  241. // Deprecated: Use protoregistry.GlobalTypes.RegisterExtension instead.
  242. func RegisterExtension(d *ExtensionDesc) {
  243. if err := protoregistry.GlobalTypes.RegisterExtension(d); err != nil {
  244. panic(err)
  245. }
  246. }
  247. type extensionsByNumber = map[int32]*ExtensionDesc
  248. var extensionCache sync.Map // map[messageName]extensionsByNumber
  249. // RegisteredExtensions returns a map of the registered extensions for the
  250. // provided protobuf message, indexed by the extension field number.
  251. //
  252. // Deprecated: Use protoregistry.GlobalTypes.RangeExtensionsByMessage instead.
  253. func RegisteredExtensions(m Message) extensionsByNumber {
  254. // Check whether the cache is stale. If the number of extensions for
  255. // the given message differs, then it means that some extensions were
  256. // recently registered upstream that we do not know about.
  257. s := MessageName(m)
  258. v, _ := extensionCache.Load(s)
  259. xs, _ := v.(extensionsByNumber)
  260. if protoregistry.GlobalTypes.NumExtensionsByMessage(protoreflect.FullName(s)) == len(xs) {
  261. return xs // cache is up-to-date
  262. }
  263. // Cache is stale, re-compute the extensions map.
  264. xs = make(extensionsByNumber)
  265. protoregistry.GlobalTypes.RangeExtensionsByMessage(protoreflect.FullName(s), func(xt protoreflect.ExtensionType) bool {
  266. if xd, ok := xt.(*ExtensionDesc); ok {
  267. xs[int32(xt.TypeDescriptor().Number())] = xd
  268. } else {
  269. // TODO: This implies that the protoreflect.ExtensionType is a
  270. // custom type not generated by protoc-gen-go. We could try and
  271. // convert the type to an ExtensionDesc.
  272. }
  273. return true
  274. })
  275. extensionCache.Store(s, xs)
  276. return xs
  277. }