api_export.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "fmt"
  7. "reflect"
  8. "strconv"
  9. "google.golang.org/protobuf/encoding/prototext"
  10. "google.golang.org/protobuf/proto"
  11. pref "google.golang.org/protobuf/reflect/protoreflect"
  12. piface "google.golang.org/protobuf/runtime/protoiface"
  13. )
  14. // Export is a zero-length named type that exists only to export a set of
  15. // functions that we do not want to appear in godoc.
  16. type Export struct{}
  17. // enum is any enum type generated by protoc-gen-go
  18. // and must be a named int32 type.
  19. type enum = interface{}
  20. // EnumOf returns the protoreflect.Enum interface over e.
  21. func (Export) EnumOf(e enum) pref.Enum {
  22. if ev, ok := e.(pref.Enum); ok {
  23. return ev
  24. }
  25. return legacyWrapEnum(reflect.ValueOf(e))
  26. }
  27. // EnumDescriptorOf returns the protoreflect.EnumDescriptor for e.
  28. func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor {
  29. if ev, ok := e.(pref.Enum); ok {
  30. return ev.Descriptor()
  31. }
  32. return LegacyLoadEnumDesc(reflect.TypeOf(e))
  33. }
  34. // EnumTypeOf returns the protoreflect.EnumType for e.
  35. func (Export) EnumTypeOf(e enum) pref.EnumType {
  36. if ev, ok := e.(pref.Enum); ok {
  37. return ev.Type()
  38. }
  39. return legacyLoadEnumType(reflect.TypeOf(e))
  40. }
  41. // EnumStringOf returns the enum value as a string, either as the name if
  42. // the number is resolvable, or the number formatted as a string.
  43. func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
  44. ev := ed.Values().ByNumber(n)
  45. if ev != nil {
  46. return string(ev.Name())
  47. }
  48. return strconv.Itoa(int(n))
  49. }
  50. // message is any message type generated by protoc-gen-go
  51. // and must be a pointer to a named struct type.
  52. type message = interface{}
  53. // legacyMessageWrapper wraps a v2 message as a v1 message.
  54. type legacyMessageWrapper struct{ m pref.ProtoMessage }
  55. func (m legacyMessageWrapper) Reset() { proto.Reset(m.m) }
  56. func (m legacyMessageWrapper) String() string { return Export{}.MessageStringOf(m.m) }
  57. func (m legacyMessageWrapper) ProtoMessage() {}
  58. // ProtoMessageV1Of converts either a v1 or v2 message to a v1 message.
  59. func (Export) ProtoMessageV1Of(m message) piface.MessageV1 {
  60. switch mv := m.(type) {
  61. case nil:
  62. return nil
  63. case piface.MessageV1:
  64. return mv
  65. case unwrapper:
  66. return Export{}.ProtoMessageV1Of(mv.protoUnwrap())
  67. case pref.ProtoMessage:
  68. return legacyMessageWrapper{mv}
  69. default:
  70. panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m))
  71. }
  72. }
  73. func (Export) protoMessageV2Of(m message) pref.ProtoMessage {
  74. switch mv := m.(type) {
  75. case nil:
  76. return nil
  77. case pref.ProtoMessage:
  78. return mv
  79. case legacyMessageWrapper:
  80. return mv.m
  81. case piface.MessageV1:
  82. return nil
  83. default:
  84. panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m))
  85. }
  86. }
  87. // ProtoMessageV2Of converts either a v1 or v2 message to a v2 message.
  88. func (Export) ProtoMessageV2Of(m message) pref.ProtoMessage {
  89. if mv := (Export{}).protoMessageV2Of(m); mv != nil || m == nil {
  90. return mv
  91. }
  92. return legacyWrapMessage(reflect.ValueOf(m))
  93. }
  94. // MessageOf returns the protoreflect.Message interface over m.
  95. func (Export) MessageOf(m message) pref.Message {
  96. if mv := (Export{}).protoMessageV2Of(m); mv != nil {
  97. return mv.ProtoReflect()
  98. }
  99. return legacyWrapMessage(reflect.ValueOf(m)).ProtoReflect()
  100. }
  101. // MessageDescriptorOf returns the protoreflect.MessageDescriptor for m.
  102. func (Export) MessageDescriptorOf(m message) pref.MessageDescriptor {
  103. if mv := (Export{}).protoMessageV2Of(m); mv != nil {
  104. return mv.ProtoReflect().Descriptor()
  105. }
  106. return LegacyLoadMessageDesc(reflect.TypeOf(m))
  107. }
  108. // MessageTypeOf returns the protoreflect.MessageType for m.
  109. func (Export) MessageTypeOf(m message) pref.MessageType {
  110. if mv := (Export{}).protoMessageV2Of(m); mv != nil {
  111. return mv.ProtoReflect().Type()
  112. }
  113. return legacyLoadMessageInfo(reflect.TypeOf(m), "")
  114. }
  115. // MessageStringOf returns the message value as a string,
  116. // which is the message serialized in the protobuf text format.
  117. func (Export) MessageStringOf(m pref.ProtoMessage) string {
  118. v := reflect.ValueOf(m)
  119. if m == nil || (v.Kind() == reflect.Ptr && v.IsNil()) {
  120. return "<nil>"
  121. }
  122. b, _ := prototext.MarshalOptions{
  123. AllowPartial: true,
  124. EmitUnknown: true,
  125. }.Marshal(m)
  126. return string(b)
  127. }
  128. // ExtensionDescFromType returns the legacy protoV1.ExtensionDesc for t.
  129. func (Export) ExtensionDescFromType(t pref.ExtensionType) *ExtensionInfo {
  130. // TODO: Delete this function when v1 directly does this assertion.
  131. if xt, ok := t.(*ExtensionInfo); ok {
  132. return xt
  133. }
  134. return nil
  135. }