api_export.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/internal/errors"
  11. "google.golang.org/protobuf/proto"
  12. pref "google.golang.org/protobuf/reflect/protoreflect"
  13. piface "google.golang.org/protobuf/runtime/protoiface"
  14. )
  15. // Export is a zero-length named type that exists only to export a set of
  16. // functions that we do not want to appear in godoc.
  17. type Export struct{}
  18. // NewError formats a string according to the format specifier and arguments and
  19. // returns an error that has a "proto" prefix.
  20. func (Export) NewError(f string, x ...interface{}) error {
  21. return errors.New(f, x...)
  22. }
  23. // enum is any enum type generated by protoc-gen-go
  24. // and must be a named int32 type.
  25. type enum = interface{}
  26. // EnumOf returns the protoreflect.Enum interface over e.
  27. // It returns nil if e is nil.
  28. func (Export) EnumOf(e enum) pref.Enum {
  29. switch e := e.(type) {
  30. case nil:
  31. return nil
  32. case pref.Enum:
  33. return e
  34. default:
  35. return legacyWrapEnum(reflect.ValueOf(e))
  36. }
  37. }
  38. // EnumDescriptorOf returns the protoreflect.EnumDescriptor for e.
  39. // It returns nil if e is nil.
  40. func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor {
  41. switch e := e.(type) {
  42. case nil:
  43. return nil
  44. case pref.Enum:
  45. return e.Descriptor()
  46. default:
  47. return LegacyLoadEnumDesc(reflect.TypeOf(e))
  48. }
  49. }
  50. // EnumTypeOf returns the protoreflect.EnumType for e.
  51. // It returns nil if e is nil.
  52. func (Export) EnumTypeOf(e enum) pref.EnumType {
  53. switch e := e.(type) {
  54. case nil:
  55. return nil
  56. case pref.Enum:
  57. return e.Type()
  58. default:
  59. return legacyLoadEnumType(reflect.TypeOf(e))
  60. }
  61. }
  62. // EnumStringOf returns the enum value as a string, either as the name if
  63. // the number is resolvable, or the number formatted as a string.
  64. func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
  65. ev := ed.Values().ByNumber(n)
  66. if ev != nil {
  67. return string(ev.Name())
  68. }
  69. return strconv.Itoa(int(n))
  70. }
  71. // message is any message type generated by protoc-gen-go
  72. // and must be a pointer to a named struct type.
  73. type message = interface{}
  74. // legacyMessageWrapper wraps a v2 message as a v1 message.
  75. type legacyMessageWrapper struct{ m pref.ProtoMessage }
  76. func (m legacyMessageWrapper) Reset() { proto.Reset(m.m) }
  77. func (m legacyMessageWrapper) String() string { return Export{}.MessageStringOf(m.m) }
  78. func (m legacyMessageWrapper) ProtoMessage() {}
  79. // ProtoMessageV1Of converts either a v1 or v2 message to a v1 message.
  80. // It returns nil if m is nil.
  81. func (Export) ProtoMessageV1Of(m message) piface.MessageV1 {
  82. switch mv := m.(type) {
  83. case nil:
  84. return nil
  85. case piface.MessageV1:
  86. return mv
  87. case unwrapper:
  88. return Export{}.ProtoMessageV1Of(mv.protoUnwrap())
  89. case pref.ProtoMessage:
  90. return legacyMessageWrapper{mv}
  91. default:
  92. panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m))
  93. }
  94. }
  95. func (Export) protoMessageV2Of(m message) pref.ProtoMessage {
  96. switch mv := m.(type) {
  97. case nil:
  98. return nil
  99. case pref.ProtoMessage:
  100. return mv
  101. case legacyMessageWrapper:
  102. return mv.m
  103. case piface.MessageV1:
  104. return nil
  105. default:
  106. panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m))
  107. }
  108. }
  109. // ProtoMessageV2Of converts either a v1 or v2 message to a v2 message.
  110. // It returns nil if m is nil.
  111. func (Export) ProtoMessageV2Of(m message) pref.ProtoMessage {
  112. if m == nil {
  113. return nil
  114. }
  115. if mv := (Export{}).protoMessageV2Of(m); mv != nil {
  116. return mv
  117. }
  118. return legacyWrapMessage(reflect.ValueOf(m)).Interface()
  119. }
  120. // MessageOf returns the protoreflect.Message interface over m.
  121. // It returns nil if m is nil.
  122. func (Export) MessageOf(m message) pref.Message {
  123. if m == nil {
  124. return nil
  125. }
  126. if mv := (Export{}).protoMessageV2Of(m); mv != nil {
  127. return mv.ProtoReflect()
  128. }
  129. return legacyWrapMessage(reflect.ValueOf(m))
  130. }
  131. // MessageDescriptorOf returns the protoreflect.MessageDescriptor for m.
  132. // It returns nil if m is nil.
  133. func (Export) MessageDescriptorOf(m message) pref.MessageDescriptor {
  134. if m == nil {
  135. return nil
  136. }
  137. if mv := (Export{}).protoMessageV2Of(m); mv != nil {
  138. return mv.ProtoReflect().Descriptor()
  139. }
  140. return LegacyLoadMessageDesc(reflect.TypeOf(m))
  141. }
  142. // MessageTypeOf returns the protoreflect.MessageType for m.
  143. // It returns nil if m is nil.
  144. func (Export) MessageTypeOf(m message) pref.MessageType {
  145. if m == nil {
  146. return nil
  147. }
  148. if mv := (Export{}).protoMessageV2Of(m); mv != nil {
  149. return mv.ProtoReflect().Type()
  150. }
  151. return legacyLoadMessageType(reflect.TypeOf(m), "")
  152. }
  153. // MessageStringOf returns the message value as a string,
  154. // which is the message serialized in the protobuf text format.
  155. func (Export) MessageStringOf(m pref.ProtoMessage) string {
  156. return prototext.MarshalOptions{Multiline: false}.Format(m)
  157. }