api_export.go 3.1 KB

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