export.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. ptype "github.com/golang/protobuf/v2/internal/prototype"
  8. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  9. )
  10. // Export is a zero-length named type that exists only to export a set of
  11. // functions that we do not want to appear in godoc.
  12. type Export struct{}
  13. // EnumOf returns the protoreflect.Enum interface over e.
  14. // If e already implements proto.Enum, then it directly calls the
  15. // ProtoReflect method, otherwise it wraps the v1 enum to implement
  16. // the v2 reflective interface.
  17. func (Export) EnumOf(e interface{}) pref.Enum {
  18. if ev, ok := e.(pref.Enum); ok {
  19. return ev
  20. }
  21. return legacyWrapper.EnumOf(e)
  22. }
  23. // EnumTypeOf returns the protoreflect.EnumType for e.
  24. // If e already implements proto.Enum, then it obtains the type by directly
  25. // calling the ProtoReflect.Type method, otherwise it derives an enum type
  26. // from the v1 named int32 type.
  27. func (Export) EnumTypeOf(e interface{}) pref.EnumType {
  28. if ev, ok := e.(pref.Enum); ok {
  29. return ev.Type()
  30. }
  31. return legacyWrapper.EnumTypeOf(e)
  32. }
  33. // EnumStringOf returns the enum value as a string, either as the name if
  34. // the number is resolvable, or the number formatted as a string.
  35. func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
  36. ev := ed.Values().ByNumber(n)
  37. if ev != nil {
  38. return string(ev.Name())
  39. }
  40. return strconv.Itoa(int(n))
  41. }
  42. // MessageOf returns the protoreflect.Message interface over m.
  43. // If m already implements proto.Message, then it directly calls the
  44. // ProtoReflect method, otherwise it wraps the v1 message to implement
  45. // the v2 reflective interface.
  46. func (Export) MessageOf(m interface{}) pref.Message {
  47. if mv, ok := m.(pref.ProtoMessage); ok {
  48. return mv.ProtoReflect()
  49. }
  50. return legacyWrapper.MessageOf(m)
  51. }
  52. // MessageTypeOf returns the protoreflect.MessageType for m.
  53. // If m already implements proto.Message, then it obtains the type by directly
  54. // calling the ProtoReflect.Type method, otherwise it derives a message type
  55. // from the v1 message struct.
  56. func (Export) MessageTypeOf(m interface{}) pref.MessageType {
  57. if mv, ok := m.(pref.ProtoMessage); ok {
  58. return mv.ProtoReflect().Type()
  59. }
  60. return legacyWrapper.MessageTypeOf(m)
  61. }
  62. // ExtensionTypeOf returns a protoreflect.ExtensionType where the type of the
  63. // field is t. The type t must be provided if the field is an enum or message.
  64. // If t already implements proto.Enum or proto.Message, then this returns
  65. // an extension type by directly calling prototype.GoExtension.
  66. // Otherwise, it derives an extension type by wrapping the enum or message
  67. // using EnumOf or MessageOf.
  68. func (Export) ExtensionTypeOf(d pref.ExtensionDescriptor, t interface{}) pref.ExtensionType {
  69. switch t := t.(type) {
  70. case nil:
  71. return ptype.GoExtension(d, nil, nil)
  72. case pref.Enum:
  73. return ptype.GoExtension(d, t.Type(), nil)
  74. case pref.ProtoMessage:
  75. return ptype.GoExtension(d, nil, t.ProtoReflect().Type())
  76. }
  77. return legacyWrapper.ExtensionTypeOf(d, t)
  78. }