api_export.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "reflect"
  7. "strconv"
  8. "google.golang.org/protobuf/encoding/prototext"
  9. pref "google.golang.org/protobuf/reflect/protoreflect"
  10. "google.golang.org/protobuf/reflect/prototype"
  11. piface "google.golang.org/protobuf/runtime/protoiface"
  12. )
  13. // Export is a zero-length named type that exists only to export a set of
  14. // functions that we do not want to appear in godoc.
  15. type Export struct{}
  16. // enum is any enum type generated by protoc-gen-go
  17. // and must be a named int32 type.
  18. type enum = interface{}
  19. // EnumOf returns the protoreflect.Enum interface over e.
  20. func (Export) EnumOf(e enum) pref.Enum {
  21. if ev, ok := e.(pref.Enum); ok {
  22. return ev
  23. }
  24. return legacyWrapEnum(reflect.ValueOf(e))
  25. }
  26. // EnumTypeOf returns the protoreflect.EnumType for e.
  27. func (Export) EnumTypeOf(e enum) pref.EnumType {
  28. if ev, ok := e.(pref.Enum); ok {
  29. return &prototype.Enum{
  30. EnumDescriptor: ev.Descriptor(),
  31. NewEnum: func(n pref.EnumNumber) pref.Enum {
  32. return reflect.ValueOf(n).Convert(reflect.TypeOf(e)).Interface().(pref.Enum)
  33. },
  34. }
  35. }
  36. return legacyLoadEnumType(reflect.TypeOf(e))
  37. }
  38. // EnumDescriptorOf returns the protoreflect.EnumDescriptor for e.
  39. func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor {
  40. if ev, ok := e.(pref.Enum); ok {
  41. return ev.Descriptor()
  42. }
  43. return LegacyLoadEnumDesc(reflect.TypeOf(e))
  44. }
  45. // EnumStringOf returns the enum value as a string, either as the name if
  46. // the number is resolvable, or the number formatted as a string.
  47. func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
  48. ev := ed.Values().ByNumber(n)
  49. if ev != nil {
  50. return string(ev.Name())
  51. }
  52. return strconv.Itoa(int(n))
  53. }
  54. // message is any message type generated by protoc-gen-go
  55. // and must be a pointer to a named struct type.
  56. type message = interface{}
  57. // MessageOf returns the protoreflect.Message interface over m.
  58. func (Export) MessageOf(m message) pref.Message {
  59. if mv, ok := m.(pref.ProtoMessage); ok {
  60. return mv.ProtoReflect()
  61. }
  62. return legacyWrapMessage(reflect.ValueOf(m)).ProtoReflect()
  63. }
  64. // MessageTypeOf returns the protoreflect.MessageType for m.
  65. func (Export) MessageTypeOf(m message) pref.MessageType {
  66. if mv, ok := m.(pref.ProtoMessage); ok {
  67. return &prototype.Message{
  68. MessageDescriptor: mv.ProtoReflect().Descriptor(),
  69. NewMessage: func() pref.Message {
  70. return reflect.New(reflect.TypeOf(m).Elem()).Interface().(pref.ProtoMessage).ProtoReflect()
  71. },
  72. }
  73. }
  74. return legacyLoadMessageInfo(reflect.TypeOf(m)).PBType
  75. }
  76. // MessageDescriptorOf returns the protoreflect.MessageDescriptor for m.
  77. func (Export) MessageDescriptorOf(m message) pref.MessageDescriptor {
  78. if mv, ok := m.(pref.ProtoMessage); ok {
  79. return mv.ProtoReflect().Descriptor()
  80. }
  81. return LegacyLoadMessageDesc(reflect.TypeOf(m))
  82. }
  83. // MessageStringOf returns the message value as a string,
  84. // which is the message serialized in the protobuf text format.
  85. func (Export) MessageStringOf(m pref.ProtoMessage) string {
  86. b, _ := prototext.MarshalOptions{AllowPartial: true}.Marshal(m)
  87. return string(b)
  88. }
  89. // ExtensionDescFromType returns the legacy protoiface.ExtensionDescV1 for t.
  90. func (Export) ExtensionDescFromType(t pref.ExtensionType) *piface.ExtensionDescV1 {
  91. return legacyExtensionDescFromType(t)
  92. }
  93. // ExtensionTypeFromDesc returns the v2 protoreflect.ExtensionType for d.
  94. func (Export) ExtensionTypeFromDesc(d *piface.ExtensionDescV1) pref.ExtensionType {
  95. return legacyExtensionTypeFromDesc(d)
  96. }