export.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "strconv"
  7. "github.com/golang/protobuf/v2/encoding/textpb"
  8. ptype "github.com/golang/protobuf/v2/internal/prototype"
  9. pref "github.com/golang/protobuf/v2/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. // EnumOf returns the protoreflect.Enum interface over e.
  15. // If e already implements proto.Enum, then it directly calls the
  16. // ProtoReflect method, otherwise it wraps the v1 enum to implement
  17. // the v2 reflective interface.
  18. func (Export) EnumOf(e interface{}) 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. // If e already implements proto.Enum, then it obtains the type by directly
  26. // calling the ProtoReflect.Type method, otherwise it derives an enum type
  27. // from the v1 named int32 type.
  28. func (Export) EnumTypeOf(e interface{}) pref.EnumType {
  29. if ev, ok := e.(pref.Enum); ok {
  30. return ev.Type()
  31. }
  32. return legacyWrapper.EnumTypeOf(e)
  33. }
  34. // EnumStringOf returns the enum value as a string, either as the name if
  35. // the number is resolvable, or the number formatted as a string.
  36. func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
  37. ev := ed.Values().ByNumber(n)
  38. if ev != nil {
  39. return string(ev.Name())
  40. }
  41. return strconv.Itoa(int(n))
  42. }
  43. // MessageOf returns the protoreflect.Message interface over m.
  44. // If m already implements proto.Message, then it directly calls the
  45. // ProtoReflect method, otherwise it wraps the v1 message to implement
  46. // the v2 reflective interface.
  47. func (Export) MessageOf(m interface{}) pref.Message {
  48. if mv, ok := m.(pref.ProtoMessage); ok {
  49. return mv.ProtoReflect()
  50. }
  51. return legacyWrapper.MessageOf(m)
  52. }
  53. // MessageTypeOf returns the protoreflect.MessageType for m.
  54. // If m already implements proto.Message, then it obtains the type by directly
  55. // calling the ProtoReflect.Type method, otherwise it derives a message type
  56. // from the v1 message struct.
  57. func (Export) MessageTypeOf(m interface{}) pref.MessageType {
  58. if mv, ok := m.(pref.ProtoMessage); ok {
  59. return mv.ProtoReflect().Type()
  60. }
  61. return legacyWrapper.MessageTypeOf(m)
  62. }
  63. // ExtensionTypeOf returns a protoreflect.ExtensionType where the type of the
  64. // field is t. The type t must be provided if the field is an enum or message.
  65. // If t already implements proto.Enum or proto.Message, then this returns
  66. // an extension type by directly calling prototype.GoExtension.
  67. // Otherwise, it derives an extension type by wrapping the enum or message
  68. // using EnumOf or MessageOf.
  69. func (Export) ExtensionTypeOf(d pref.ExtensionDescriptor, t interface{}) pref.ExtensionType {
  70. switch t := t.(type) {
  71. case nil:
  72. return ptype.GoExtension(d, nil, nil)
  73. case pref.Enum:
  74. return ptype.GoExtension(d, t.Type(), nil)
  75. case pref.ProtoMessage:
  76. return ptype.GoExtension(d, nil, t.ProtoReflect().Type())
  77. }
  78. return legacyWrapper.ExtensionTypeOf(d, t)
  79. }
  80. // MessageStringOf returns the message value as a string,
  81. // which is the message serialized in the protobuf text format.
  82. func (Export) MessageStringOf(m pref.ProtoMessage) string {
  83. b, _ := textpb.MarshalOptions{AllowPartial: true}.Marshal(m)
  84. return string(b)
  85. }