export.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/textpb"
  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 legacyWrapper.EnumOf(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 &enumType{ev.Descriptor(), reflect.TypeOf(e)}
  28. }
  29. return legacyWrapper.EnumTypeOf(e)
  30. }
  31. // TODO: This needs to be centralized in a package.
  32. type enumType struct {
  33. // TODO: Remove me as an embedded field.
  34. pref.EnumDescriptor
  35. typ reflect.Type // must implement protoreflect.Enum
  36. }
  37. func (t *enumType) Descriptor() pref.EnumDescriptor { return t.EnumDescriptor }
  38. func (t *enumType) GoType() reflect.Type { return t.typ }
  39. func (t *enumType) New(n pref.EnumNumber) pref.Enum {
  40. return reflect.ValueOf(n).Convert(t.typ).Interface().(pref.Enum)
  41. }
  42. // EnumDescriptorOf returns the protoreflect.EnumDescriptor for e.
  43. func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor {
  44. if ev, ok := e.(pref.Enum); ok {
  45. return ev.Descriptor()
  46. }
  47. return legacyWrapper.EnumDescriptorOf(e)
  48. }
  49. // EnumStringOf returns the enum value as a string, either as the name if
  50. // the number is resolvable, or the number formatted as a string.
  51. func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
  52. ev := ed.Values().ByNumber(n)
  53. if ev != nil {
  54. return string(ev.Name())
  55. }
  56. return strconv.Itoa(int(n))
  57. }
  58. // message is any message type generated by protoc-gen-go
  59. // and must be a pointer to a named struct type.
  60. type message = interface{}
  61. // MessageOf returns the protoreflect.Message interface over m.
  62. func (Export) MessageOf(m message) pref.Message {
  63. if mv, ok := m.(pref.ProtoMessage); ok {
  64. return mv.ProtoReflect()
  65. }
  66. return legacyWrapper.MessageOf(m)
  67. }
  68. // MessageTypeOf returns the protoreflect.MessageType for m.
  69. func (Export) MessageTypeOf(m message) pref.MessageType {
  70. if mv, ok := m.(pref.ProtoMessage); ok {
  71. return &messageType{mv.ProtoReflect().Descriptor(), reflect.TypeOf(m)}
  72. }
  73. return legacyWrapper.MessageTypeOf(m)
  74. }
  75. // TODO: This needs to be centralized in a package.
  76. type messageType struct {
  77. // TODO: Remove me as an embedded field.
  78. pref.MessageDescriptor
  79. typ reflect.Type // must implement protoreflect.ProtoMessage
  80. }
  81. func (t *messageType) Descriptor() pref.MessageDescriptor { return t.MessageDescriptor }
  82. func (t *messageType) GoType() reflect.Type { return t.typ }
  83. func (t *messageType) New() pref.Message {
  84. return reflect.New(t.typ.Elem()).Interface().(pref.ProtoMessage).ProtoReflect()
  85. }
  86. // MessageDescriptorOf returns the protoreflect.MessageDescriptor for m.
  87. func (Export) MessageDescriptorOf(m message) pref.MessageDescriptor {
  88. if mv, ok := m.(pref.ProtoMessage); ok {
  89. return mv.ProtoReflect().Descriptor()
  90. }
  91. return legacyWrapper.MessageDescriptorOf(m)
  92. }
  93. // MessageStringOf returns the message value as a string,
  94. // which is the message serialized in the protobuf text format.
  95. func (Export) MessageStringOf(m pref.ProtoMessage) string {
  96. b, _ := textpb.MarshalOptions{AllowPartial: true}.Marshal(m)
  97. return string(b)
  98. }