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