export.go 2.8 KB

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