legacy_enum.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 impl
  5. import (
  6. "fmt"
  7. "math"
  8. "reflect"
  9. "sync"
  10. descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
  11. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  12. ptype "github.com/golang/protobuf/v2/reflect/prototype"
  13. )
  14. var enumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
  15. // loadEnumDesc returns an EnumDescriptor derived from the Go type,
  16. // which must be an int32 kind and not implement the v2 API already.
  17. func loadEnumDesc(t reflect.Type) pref.EnumDescriptor {
  18. // Fast-path: check if an EnumDescriptor is cached for this concrete type.
  19. if v, ok := enumDescCache.Load(t); ok {
  20. return v.(pref.EnumDescriptor)
  21. }
  22. // Slow-path: initialize EnumDescriptor from the proto descriptor.
  23. if t.Kind() != reflect.Int32 {
  24. panic(fmt.Sprintf("got %v, want int32 kind", t))
  25. }
  26. // Derive the enum descriptor from the raw descriptor proto.
  27. e := new(ptype.StandaloneEnum)
  28. ev := reflect.Zero(t).Interface()
  29. if _, ok := ev.(pref.ProtoEnum); ok {
  30. panic(fmt.Sprintf("%v already implements proto.Enum", t))
  31. }
  32. if ed, ok := ev.(legacyEnum); ok {
  33. b, idxs := ed.EnumDescriptor()
  34. fd := loadFileDesc(b)
  35. // Derive syntax.
  36. switch fd.GetSyntax() {
  37. case "proto2", "":
  38. e.Syntax = pref.Proto2
  39. case "proto3":
  40. e.Syntax = pref.Proto3
  41. }
  42. // Derive the full name and correct enum descriptor.
  43. var ed *descriptorV1.EnumDescriptorProto
  44. e.FullName = pref.FullName(fd.GetPackage())
  45. if len(idxs) == 1 {
  46. ed = fd.EnumType[idxs[0]]
  47. e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
  48. } else {
  49. md := fd.MessageType[idxs[0]]
  50. e.FullName = e.FullName.Append(pref.Name(md.GetName()))
  51. for _, i := range idxs[1 : len(idxs)-1] {
  52. md = md.NestedType[i]
  53. e.FullName = e.FullName.Append(pref.Name(md.GetName()))
  54. }
  55. ed = md.EnumType[idxs[len(idxs)-1]]
  56. e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
  57. }
  58. // Derive the enum values.
  59. for _, vd := range ed.GetValue() {
  60. e.Values = append(e.Values, ptype.EnumValue{
  61. Name: pref.Name(vd.GetName()),
  62. Number: pref.EnumNumber(vd.GetNumber()),
  63. })
  64. }
  65. } else {
  66. // If the type does not implement legacyEnum, then there is no reliable
  67. // way to derive the original protobuf type information.
  68. // We are unable to use the global enum registry since it is
  69. // unfortunately keyed by the full name, which we do not know.
  70. // Furthermore, some generated enums register with a fork of
  71. // golang/protobuf so the enum may not even be found in the registry.
  72. //
  73. // Instead, create a bogus enum descriptor to ensure that
  74. // most operations continue to work. For example, textpb and jsonpb
  75. // will be unable to parse a message with an enum value by name.
  76. e.Syntax = pref.Proto2
  77. e.FullName = deriveFullName(t)
  78. e.Values = []ptype.EnumValue{{Name: "INVALID", Number: math.MinInt32}}
  79. }
  80. ed, err := ptype.NewEnum(e)
  81. if err != nil {
  82. panic(err)
  83. }
  84. enumDescCache.Store(t, ed)
  85. return ed
  86. }