legacy_message.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. "strings"
  9. "sync"
  10. "google.golang.org/protobuf/internal/descopts"
  11. ptag "google.golang.org/protobuf/internal/encoding/tag"
  12. "google.golang.org/protobuf/internal/errors"
  13. "google.golang.org/protobuf/internal/filedesc"
  14. "google.golang.org/protobuf/internal/strs"
  15. "google.golang.org/protobuf/reflect/protoreflect"
  16. pref "google.golang.org/protobuf/reflect/protoreflect"
  17. piface "google.golang.org/protobuf/runtime/protoiface"
  18. )
  19. // legacyWrapMessage wraps v as a protoreflect.ProtoMessage,
  20. // where v must be a *struct kind and not implement the v2 API already.
  21. func legacyWrapMessage(v reflect.Value) pref.ProtoMessage {
  22. typ := v.Type()
  23. if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Struct {
  24. return aberrantMessage{v: v}
  25. }
  26. mt := legacyLoadMessageInfo(typ, "")
  27. return mt.MessageOf(v.Interface()).Interface()
  28. }
  29. var legacyMessageTypeCache sync.Map // map[reflect.Type]*MessageInfo
  30. // legacyLoadMessageInfo dynamically loads a *MessageInfo for t,
  31. // where t must be a *struct kind and not implement the v2 API already.
  32. // The provided name is used if it cannot be determined from the message.
  33. func legacyLoadMessageInfo(t reflect.Type, name pref.FullName) *MessageInfo {
  34. // Fast-path: check if a MessageInfo is cached for this concrete type.
  35. if mt, ok := legacyMessageTypeCache.Load(t); ok {
  36. return mt.(*MessageInfo)
  37. }
  38. // Slow-path: derive message descriptor and initialize MessageInfo.
  39. mi := &MessageInfo{
  40. Desc: legacyLoadMessageDesc(t, name),
  41. GoReflectType: t,
  42. }
  43. v := reflect.Zero(t).Interface()
  44. if _, ok := v.(legacyMarshaler); ok {
  45. mi.methods.MarshalAppend = legacyMarshalAppend
  46. mi.methods.Size = legacySize
  47. }
  48. if _, ok := v.(legacyUnmarshaler); ok {
  49. mi.methods.Unmarshal = legacyUnmarshal
  50. }
  51. if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok {
  52. return mi.(*MessageInfo)
  53. }
  54. return mi
  55. }
  56. var legacyMessageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
  57. // LegacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
  58. // which must be a *struct kind and not implement the v2 API already.
  59. //
  60. // This is exported for testing purposes.
  61. func LegacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
  62. return legacyLoadMessageDesc(t, "")
  63. }
  64. func legacyLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
  65. // Fast-path: check if a MessageDescriptor is cached for this concrete type.
  66. if mi, ok := legacyMessageDescCache.Load(t); ok {
  67. return mi.(pref.MessageDescriptor)
  68. }
  69. // Slow-path: initialize MessageDescriptor from the raw descriptor.
  70. mv := reflect.Zero(t).Interface()
  71. if _, ok := mv.(pref.ProtoMessage); ok {
  72. panic(fmt.Sprintf("%v already implements proto.Message", t))
  73. }
  74. mdV1, ok := mv.(messageV1)
  75. if !ok {
  76. return aberrantLoadMessageDesc(t, name)
  77. }
  78. b, idxs := mdV1.Descriptor()
  79. md := legacyLoadFileDesc(b).Messages().Get(idxs[0])
  80. for _, i := range idxs[1:] {
  81. md = md.Messages().Get(i)
  82. }
  83. if name != "" && md.FullName() != name {
  84. panic(fmt.Sprintf("mismatching message name: got %v, want %v", md.FullName(), name))
  85. }
  86. if md, ok := legacyMessageDescCache.LoadOrStore(t, md); ok {
  87. return md.(protoreflect.MessageDescriptor)
  88. }
  89. return md
  90. }
  91. var (
  92. aberrantMessageDescLock sync.Mutex
  93. aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor
  94. )
  95. // aberrantLoadMessageDesc returns an MessageDescriptor derived from the Go type,
  96. // which must not implement protoreflect.ProtoMessage or messageV1.
  97. //
  98. // This is a best-effort derivation of the message descriptor using the protobuf
  99. // tags on the struct fields.
  100. func aberrantLoadMessageDesc(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
  101. aberrantMessageDescLock.Lock()
  102. defer aberrantMessageDescLock.Unlock()
  103. if aberrantMessageDescCache == nil {
  104. aberrantMessageDescCache = make(map[reflect.Type]protoreflect.MessageDescriptor)
  105. }
  106. return aberrantLoadMessageDescReentrant(t, name)
  107. }
  108. func aberrantLoadMessageDescReentrant(t reflect.Type, name pref.FullName) pref.MessageDescriptor {
  109. // Fast-path: check if an MessageDescriptor is cached for this concrete type.
  110. if md, ok := aberrantMessageDescCache[t]; ok {
  111. return md
  112. }
  113. // Slow-path: construct a descriptor from the Go struct type (best-effort).
  114. // Cache the MessageDescriptor early on so that we can resolve internal
  115. // cyclic references.
  116. md := &filedesc.Message{L2: new(filedesc.MessageL2)}
  117. md.L0.FullName = aberrantDeriveMessageName(t, name)
  118. md.L0.ParentFile = filedesc.SurrogateProto2
  119. aberrantMessageDescCache[t] = md
  120. if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
  121. return md
  122. }
  123. // Try to determine if the message is using proto3 by checking scalars.
  124. for i := 0; i < t.Elem().NumField(); i++ {
  125. f := t.Elem().Field(i)
  126. if tag := f.Tag.Get("protobuf"); tag != "" {
  127. switch f.Type.Kind() {
  128. case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
  129. md.L0.ParentFile = filedesc.SurrogateProto3
  130. }
  131. for _, s := range strings.Split(tag, ",") {
  132. if s == "proto3" {
  133. md.L0.ParentFile = filedesc.SurrogateProto3
  134. }
  135. }
  136. }
  137. }
  138. // Obtain a list of oneof wrapper types.
  139. var oneofWrappers []reflect.Type
  140. if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok {
  141. vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
  142. for _, v := range vs.Interface().([]interface{}) {
  143. oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
  144. }
  145. }
  146. if fn, ok := t.MethodByName("XXX_OneofWrappers"); ok {
  147. vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
  148. for _, v := range vs.Interface().([]interface{}) {
  149. oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
  150. }
  151. }
  152. // Obtain a list of the extension ranges.
  153. if fn, ok := t.MethodByName("ExtensionRangeArray"); ok {
  154. vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
  155. for i := 0; i < vs.Len(); i++ {
  156. v := vs.Index(i)
  157. md.L2.ExtensionRanges.List = append(md.L2.ExtensionRanges.List, [2]pref.FieldNumber{
  158. pref.FieldNumber(v.FieldByName("Start").Int()),
  159. pref.FieldNumber(v.FieldByName("End").Int() + 1),
  160. })
  161. md.L2.ExtensionRangeOptions = append(md.L2.ExtensionRangeOptions, nil)
  162. }
  163. }
  164. // Derive the message fields by inspecting the struct fields.
  165. for i := 0; i < t.Elem().NumField(); i++ {
  166. f := t.Elem().Field(i)
  167. if tag := f.Tag.Get("protobuf"); tag != "" {
  168. tagKey := f.Tag.Get("protobuf_key")
  169. tagVal := f.Tag.Get("protobuf_val")
  170. aberrantAppendField(md, f.Type, tag, tagKey, tagVal)
  171. }
  172. if tag := f.Tag.Get("protobuf_oneof"); tag != "" {
  173. n := len(md.L2.Oneofs.List)
  174. md.L2.Oneofs.List = append(md.L2.Oneofs.List, filedesc.Oneof{})
  175. od := &md.L2.Oneofs.List[n]
  176. od.L0.FullName = md.FullName().Append(pref.Name(tag))
  177. od.L0.ParentFile = md.L0.ParentFile
  178. od.L0.Parent = md
  179. od.L0.Index = n
  180. for _, t := range oneofWrappers {
  181. if t.Implements(f.Type) {
  182. f := t.Elem().Field(0)
  183. if tag := f.Tag.Get("protobuf"); tag != "" {
  184. aberrantAppendField(md, f.Type, tag, "", "")
  185. fd := &md.L2.Fields.List[len(md.L2.Fields.List)-1]
  186. fd.L1.ContainingOneof = od
  187. od.L1.Fields.List = append(od.L1.Fields.List, fd)
  188. }
  189. }
  190. }
  191. }
  192. }
  193. return md
  194. }
  195. func aberrantDeriveMessageName(t reflect.Type, name pref.FullName) pref.FullName {
  196. if name.IsValid() {
  197. return name
  198. }
  199. func() {
  200. defer func() { recover() }() // swallow possible nil panics
  201. if m, ok := reflect.Zero(t).Interface().(interface{ XXX_MessageName() string }); ok {
  202. name = pref.FullName(m.XXX_MessageName())
  203. }
  204. }()
  205. if name.IsValid() {
  206. return name
  207. }
  208. if t.Kind() == reflect.Ptr {
  209. t = t.Elem()
  210. }
  211. return aberrantDeriveFullName(t)
  212. }
  213. func aberrantAppendField(md *filedesc.Message, goType reflect.Type, tag, tagKey, tagVal string) {
  214. t := goType
  215. isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
  216. isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
  217. if isOptional || isRepeated {
  218. t = t.Elem()
  219. }
  220. fd := ptag.Unmarshal(tag, t, placeholderEnumValues{}).(*filedesc.Field)
  221. // Append field descriptor to the message.
  222. n := len(md.L2.Fields.List)
  223. md.L2.Fields.List = append(md.L2.Fields.List, *fd)
  224. fd = &md.L2.Fields.List[n]
  225. fd.L0.FullName = md.FullName().Append(fd.Name())
  226. fd.L0.ParentFile = md.L0.ParentFile
  227. fd.L0.Parent = md
  228. fd.L0.Index = n
  229. if fd.L1.IsWeak || fd.L1.HasPacked {
  230. fd.L1.Options = func() pref.ProtoMessage {
  231. opts := descopts.Field.ProtoReflect().New()
  232. if fd.L1.IsWeak {
  233. opts.Set(opts.Descriptor().Fields().ByName("weak"), protoreflect.ValueOfBool(true))
  234. }
  235. if fd.L1.HasPacked {
  236. opts.Set(opts.Descriptor().Fields().ByName("packed"), protoreflect.ValueOfBool(fd.L1.IsPacked))
  237. }
  238. return opts.Interface()
  239. }
  240. }
  241. // Populate Enum and Message.
  242. if fd.Enum() == nil && fd.Kind() == pref.EnumKind {
  243. switch v := reflect.Zero(t).Interface().(type) {
  244. case pref.Enum:
  245. fd.L1.Enum = v.Descriptor()
  246. default:
  247. fd.L1.Enum = LegacyLoadEnumDesc(t)
  248. }
  249. }
  250. if fd.Message() == nil && (fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind) {
  251. switch v := reflect.Zero(t).Interface().(type) {
  252. case pref.ProtoMessage:
  253. fd.L1.Message = v.ProtoReflect().Descriptor()
  254. case messageV1:
  255. fd.L1.Message = LegacyLoadMessageDesc(t)
  256. default:
  257. if t.Kind() == reflect.Map {
  258. n := len(md.L1.Messages.List)
  259. md.L1.Messages.List = append(md.L1.Messages.List, filedesc.Message{L2: new(filedesc.MessageL2)})
  260. md2 := &md.L1.Messages.List[n]
  261. md2.L0.FullName = md.FullName().Append(pref.Name(strs.MapEntryName(string(fd.Name()))))
  262. md2.L0.ParentFile = md.L0.ParentFile
  263. md2.L0.Parent = md
  264. md2.L0.Index = n
  265. md2.L2.IsMapEntry = true
  266. md2.L2.Options = func() pref.ProtoMessage {
  267. opts := descopts.Message.ProtoReflect().New()
  268. opts.Set(opts.Descriptor().Fields().ByName("map_entry"), protoreflect.ValueOfBool(true))
  269. return opts.Interface()
  270. }
  271. aberrantAppendField(md2, t.Key(), tagKey, "", "")
  272. aberrantAppendField(md2, t.Elem(), tagVal, "", "")
  273. fd.L1.Message = md2
  274. break
  275. }
  276. fd.L1.Message = aberrantLoadMessageDescReentrant(t, "")
  277. }
  278. }
  279. }
  280. type placeholderEnumValues struct {
  281. protoreflect.EnumValueDescriptors
  282. }
  283. func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor {
  284. return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n)))
  285. }
  286. // legacyMarshaler is the proto.Marshaler interface superseded by protoiface.Methoder.
  287. type legacyMarshaler interface {
  288. Marshal() ([]byte, error)
  289. }
  290. // legacyUnmarshaler is the proto.Unmarshaler interface superseded by protoiface.Methoder.
  291. type legacyUnmarshaler interface {
  292. Unmarshal([]byte) error
  293. }
  294. var legacyProtoMethods = &piface.Methods{
  295. Size: legacySize,
  296. MarshalAppend: legacyMarshalAppend,
  297. Unmarshal: legacyUnmarshal,
  298. }
  299. func legacySize(m protoreflect.Message, opts piface.MarshalOptions) int {
  300. b, _ := legacyMarshalAppend(nil, m, opts)
  301. return len(b)
  302. }
  303. func legacyMarshalAppend(b []byte, m protoreflect.Message, opts piface.MarshalOptions) ([]byte, error) {
  304. v := m.(unwrapper).protoUnwrap()
  305. marshaler, ok := v.(legacyMarshaler)
  306. if !ok {
  307. return nil, errors.New("%T does not implement Marshal", v)
  308. }
  309. out, err := marshaler.Marshal()
  310. if b != nil {
  311. out = append(b, out...)
  312. }
  313. return out, err
  314. }
  315. func legacyUnmarshal(b []byte, m protoreflect.Message, opts piface.UnmarshalOptions) error {
  316. v := m.(unwrapper).protoUnwrap()
  317. unmarshaler, ok := v.(legacyUnmarshaler)
  318. if !ok {
  319. return errors.New("%T does not implement Marshal", v)
  320. }
  321. return unmarshaler.Unmarshal(b)
  322. }
  323. // aberrantMessageType implements MessageType for all types other than pointer-to-struct.
  324. type aberrantMessageType struct {
  325. t reflect.Type
  326. }
  327. func (mt aberrantMessageType) New() pref.Message {
  328. return aberrantMessage{reflect.Zero(mt.t)}
  329. }
  330. func (mt aberrantMessageType) Zero() pref.Message {
  331. return aberrantMessage{reflect.Zero(mt.t)}
  332. }
  333. func (mt aberrantMessageType) GoType() reflect.Type {
  334. return mt.t
  335. }
  336. func (mt aberrantMessageType) Descriptor() pref.MessageDescriptor {
  337. return LegacyLoadMessageDesc(mt.t)
  338. }
  339. // aberrantMessage implements Message for all types other than pointer-to-struct.
  340. //
  341. // When the underlying type implements legacyMarshaler or legacyUnmarshaler,
  342. // the aberrant Message can be marshaled or unmarshaled. Otherwise, there is
  343. // not much that can be done with values of this type.
  344. type aberrantMessage struct {
  345. v reflect.Value
  346. }
  347. func (m aberrantMessage) ProtoReflect() pref.Message {
  348. return m
  349. }
  350. func (m aberrantMessage) Descriptor() pref.MessageDescriptor {
  351. return LegacyLoadMessageDesc(m.v.Type())
  352. }
  353. func (m aberrantMessage) Type() pref.MessageType {
  354. return aberrantMessageType{m.v.Type()}
  355. }
  356. func (m aberrantMessage) New() pref.Message {
  357. return aberrantMessage{reflect.Zero(m.v.Type())}
  358. }
  359. func (m aberrantMessage) Interface() pref.ProtoMessage {
  360. return m
  361. }
  362. func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
  363. }
  364. func (m aberrantMessage) Has(pref.FieldDescriptor) bool {
  365. panic("invalid field descriptor")
  366. }
  367. func (m aberrantMessage) Clear(pref.FieldDescriptor) {
  368. panic("invalid field descriptor")
  369. }
  370. func (m aberrantMessage) Get(pref.FieldDescriptor) pref.Value {
  371. panic("invalid field descriptor")
  372. }
  373. func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) {
  374. panic("invalid field descriptor")
  375. }
  376. func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value {
  377. panic("invalid field descriptor")
  378. }
  379. func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value {
  380. panic("invalid field descriptor")
  381. }
  382. func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor {
  383. panic("invalid oneof descriptor")
  384. }
  385. func (m aberrantMessage) GetUnknown() pref.RawFields {
  386. return nil
  387. }
  388. func (m aberrantMessage) SetUnknown(pref.RawFields) {
  389. // SetUnknown discards its input on messages which don't support unknown field storage.
  390. }
  391. func (m aberrantMessage) ProtoMethods() *piface.Methods {
  392. return legacyProtoMethods
  393. }
  394. func (m aberrantMessage) protoUnwrap() interface{} {
  395. return m.v.Interface()
  396. }