registry.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2018 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 protoapi
  5. import (
  6. "fmt"
  7. "log"
  8. "reflect"
  9. "strconv"
  10. )
  11. // TODO: This entire file should not exist and will eventually be deleted.
  12. // This is added for bootstrapping purposes so that a descriptor proto can be
  13. // generated that supports v2 reflection, but still registers into the
  14. // v1 registries for the time being.
  15. var enumValueMaps = make(map[string]map[string]int32)
  16. // RegisterEnum is called from the generated code to install the enum descriptor
  17. // maps into the global table to aid parsing text format protocol buffers.
  18. func RegisterEnum(typeName string, unusedNameMap map[int32]string, valueMap map[string]int32) {
  19. if _, ok := enumValueMaps[typeName]; ok {
  20. panic("proto: duplicate enum registered: " + typeName)
  21. }
  22. enumValueMaps[typeName] = valueMap
  23. }
  24. // EnumValueMap returns the mapping from names to integers of the
  25. // enum type enumType, or a nil if not found.
  26. func EnumValueMap(enumType string) map[string]int32 {
  27. return enumValueMaps[enumType]
  28. }
  29. // A registry of all linked message types.
  30. // The string is a fully-qualified proto name ("pkg.Message").
  31. var (
  32. protoTypedNils = make(map[string]Message) // a map from proto names to typed nil pointers
  33. protoMapTypes = make(map[string]reflect.Type) // a map from proto names to map types
  34. revProtoTypes = make(map[reflect.Type]string)
  35. )
  36. // RegisterType is called from generated code and maps from the fully qualified
  37. // proto name to the type (pointer to struct) of the protocol buffer.
  38. func RegisterType(x Message, name string) {
  39. if _, ok := protoTypedNils[name]; ok {
  40. // TODO: Some day, make this a panic.
  41. log.Printf("proto: duplicate proto type registered: %s", name)
  42. return
  43. }
  44. t := reflect.TypeOf(x)
  45. if v := reflect.ValueOf(x); v.Kind() == reflect.Ptr && v.Pointer() == 0 {
  46. // Generated code always calls RegisterType with nil x.
  47. // This check is just for extra safety.
  48. protoTypedNils[name] = x
  49. } else {
  50. protoTypedNils[name] = reflect.Zero(t).Interface().(Message)
  51. }
  52. revProtoTypes[t] = name
  53. }
  54. // RegisterMapType is called from generated code and maps from the fully qualified
  55. // proto name to the native map type of the proto map definition.
  56. func RegisterMapType(x interface{}, name string) {
  57. if reflect.TypeOf(x).Kind() != reflect.Map {
  58. panic(fmt.Sprintf("RegisterMapType(%T, %q); want map", x, name))
  59. }
  60. if _, ok := protoMapTypes[name]; ok {
  61. log.Printf("proto: duplicate proto type registered: %s", name)
  62. return
  63. }
  64. t := reflect.TypeOf(x)
  65. protoMapTypes[name] = t
  66. revProtoTypes[t] = name
  67. }
  68. // MessageName returns the fully-qualified proto name for the given message type.
  69. func MessageName(x Message) string {
  70. type xname interface {
  71. XXX_MessageName() string
  72. }
  73. if m, ok := x.(xname); ok {
  74. return m.XXX_MessageName()
  75. }
  76. return revProtoTypes[reflect.TypeOf(x)]
  77. }
  78. // MessageType returns the message type (pointer to struct) for a named message.
  79. // The type is not guaranteed to implement proto.Message if the name refers to a
  80. // map entry.
  81. func MessageType(name string) reflect.Type {
  82. if t, ok := protoTypedNils[name]; ok {
  83. return reflect.TypeOf(t)
  84. }
  85. return protoMapTypes[name]
  86. }
  87. // A registry of all linked proto files.
  88. var protoFiles = make(map[string][]byte) // file name => fileDescriptor
  89. // RegisterFile is called from generated code and maps from the
  90. // full file name of a .proto file to its compressed FileDescriptorProto.
  91. func RegisterFile(filename string, fileDescriptor []byte) {
  92. protoFiles[filename] = fileDescriptor
  93. }
  94. // FileDescriptor returns the compressed FileDescriptorProto for a .proto file.
  95. func FileDescriptor(filename string) []byte { return protoFiles[filename] }
  96. var extensionMaps = make(map[reflect.Type]map[int32]*ExtensionDesc)
  97. // RegisterExtension is called from the generated code.
  98. func RegisterExtension(desc *ExtensionDesc) {
  99. st := reflect.TypeOf(desc.ExtendedType).Elem()
  100. m := extensionMaps[st]
  101. if m == nil {
  102. m = make(map[int32]*ExtensionDesc)
  103. extensionMaps[st] = m
  104. }
  105. if _, ok := m[desc.Field]; ok {
  106. panic("proto: duplicate extension registered: " + st.String() + " " + strconv.Itoa(int(desc.Field)))
  107. }
  108. m[desc.Field] = desc
  109. }
  110. // RegisteredExtensions returns a map of the registered extensions of a
  111. // protocol buffer struct, indexed by the extension number.
  112. // The argument pb should be a nil pointer to the struct type.
  113. func RegisteredExtensions(pb Message) map[int32]*ExtensionDesc {
  114. return extensionMaps[reflect.TypeOf(pb).Elem()]
  115. }