api_export.go 3.1 KB

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