message_reflect.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // Copyright 2019 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. "google.golang.org/protobuf/internal/pragma"
  9. pref "google.golang.org/protobuf/reflect/protoreflect"
  10. )
  11. type reflectMessageInfo struct {
  12. fields map[pref.FieldNumber]*fieldInfo
  13. oneofs map[pref.Name]*oneofInfo
  14. // denseFields is a subset of fields where:
  15. // 0 < fieldDesc.Number() < len(denseFields)
  16. // It provides faster access to the fieldInfo, but may be incomplete.
  17. denseFields []*fieldInfo
  18. // rangeInfos is a list of all fields (not belonging to a oneof) and oneofs.
  19. rangeInfos []interface{} // either *fieldInfo or *oneofInfo
  20. getUnknown func(pointer) pref.RawFields
  21. setUnknown func(pointer, pref.RawFields)
  22. extensionMap func(pointer) *extensionMap
  23. nilMessage atomicNilMessage
  24. }
  25. // makeReflectFuncs generates the set of functions to support reflection.
  26. func (mi *MessageInfo) makeReflectFuncs(t reflect.Type, si structInfo) {
  27. mi.makeKnownFieldsFunc(si)
  28. mi.makeUnknownFieldsFunc(t, si)
  29. mi.makeExtensionFieldsFunc(t, si)
  30. }
  31. // makeKnownFieldsFunc generates functions for operations that can be performed
  32. // on each protobuf message field. It takes in a reflect.Type representing the
  33. // Go struct and matches message fields with struct fields.
  34. //
  35. // This code assumes that the struct is well-formed and panics if there are
  36. // any discrepancies.
  37. func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
  38. mi.fields = map[pref.FieldNumber]*fieldInfo{}
  39. md := mi.Desc
  40. fds := md.Fields()
  41. for i := 0; i < fds.Len(); i++ {
  42. fd := fds.Get(i)
  43. fs := si.fieldsByNumber[fd.Number()]
  44. var fi fieldInfo
  45. switch {
  46. case fd.ContainingOneof() != nil:
  47. fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], mi.Exporter, si.oneofWrappersByNumber[fd.Number()])
  48. case fd.IsMap():
  49. fi = fieldInfoForMap(fd, fs, mi.Exporter)
  50. case fd.IsList():
  51. fi = fieldInfoForList(fd, fs, mi.Exporter)
  52. case fd.IsWeak():
  53. fi = fieldInfoForWeakMessage(fd, si.weakOffset)
  54. case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
  55. fi = fieldInfoForMessage(fd, fs, mi.Exporter)
  56. default:
  57. fi = fieldInfoForScalar(fd, fs, mi.Exporter)
  58. }
  59. mi.fields[fd.Number()] = &fi
  60. }
  61. mi.oneofs = map[pref.Name]*oneofInfo{}
  62. for i := 0; i < md.Oneofs().Len(); i++ {
  63. od := md.Oneofs().Get(i)
  64. mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], mi.Exporter, si.oneofWrappersByType)
  65. }
  66. mi.denseFields = make([]*fieldInfo, fds.Len()*2)
  67. for i := 0; i < fds.Len(); i++ {
  68. if fd := fds.Get(i); int(fd.Number()) < len(mi.denseFields) {
  69. mi.denseFields[fd.Number()] = mi.fields[fd.Number()]
  70. }
  71. }
  72. for i := 0; i < fds.Len(); {
  73. fd := fds.Get(i)
  74. if od := fd.ContainingOneof(); od != nil {
  75. mi.rangeInfos = append(mi.rangeInfos, mi.oneofs[od.Name()])
  76. i += od.Fields().Len()
  77. } else {
  78. mi.rangeInfos = append(mi.rangeInfos, mi.fields[fd.Number()])
  79. i++
  80. }
  81. }
  82. }
  83. func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) {
  84. mi.getUnknown = func(pointer) pref.RawFields { return nil }
  85. mi.setUnknown = func(pointer, pref.RawFields) { return }
  86. if si.unknownOffset.IsValid() {
  87. mi.getUnknown = func(p pointer) pref.RawFields {
  88. if p.IsNil() {
  89. return nil
  90. }
  91. rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
  92. return pref.RawFields(*rv.Interface().(*[]byte))
  93. }
  94. mi.setUnknown = func(p pointer, b pref.RawFields) {
  95. if p.IsNil() {
  96. panic("invalid SetUnknown on nil Message")
  97. }
  98. rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
  99. *rv.Interface().(*[]byte) = []byte(b)
  100. }
  101. } else {
  102. mi.getUnknown = func(pointer) pref.RawFields {
  103. return nil
  104. }
  105. mi.setUnknown = func(p pointer, _ pref.RawFields) {
  106. if p.IsNil() {
  107. panic("invalid SetUnknown on nil Message")
  108. }
  109. }
  110. }
  111. }
  112. func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type, si structInfo) {
  113. if si.extensionOffset.IsValid() {
  114. mi.extensionMap = func(p pointer) *extensionMap {
  115. if p.IsNil() {
  116. return (*extensionMap)(nil)
  117. }
  118. v := p.Apply(si.extensionOffset).AsValueOf(extensionFieldsType)
  119. return (*extensionMap)(v.Interface().(*map[int32]ExtensionField))
  120. }
  121. } else {
  122. mi.extensionMap = func(pointer) *extensionMap {
  123. return (*extensionMap)(nil)
  124. }
  125. }
  126. }
  127. type extensionMap map[int32]ExtensionField
  128. func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
  129. if m != nil {
  130. for _, x := range *m {
  131. xt := x.GetType()
  132. if !f(xt.TypeDescriptor(), x.Value()) {
  133. return
  134. }
  135. }
  136. }
  137. }
  138. func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) {
  139. if m != nil {
  140. _, ok = (*m)[int32(xt.TypeDescriptor().Number())]
  141. }
  142. return ok
  143. }
  144. func (m *extensionMap) Clear(xt pref.ExtensionType) {
  145. delete(*m, int32(xt.TypeDescriptor().Number()))
  146. }
  147. func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value {
  148. xd := xt.TypeDescriptor()
  149. if m != nil {
  150. if x, ok := (*m)[int32(xd.Number())]; ok {
  151. return x.Value()
  152. }
  153. }
  154. return xt.Zero()
  155. }
  156. func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) {
  157. if !xt.IsValidValue(v) {
  158. panic(fmt.Sprintf("%v: assigning invalid value", xt.TypeDescriptor().FullName()))
  159. }
  160. if *m == nil {
  161. *m = make(map[int32]ExtensionField)
  162. }
  163. var x ExtensionField
  164. x.Set(xt, v)
  165. (*m)[int32(xt.TypeDescriptor().Number())] = x
  166. }
  167. func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value {
  168. xd := xt.TypeDescriptor()
  169. if xd.Kind() != pref.MessageKind && xd.Kind() != pref.GroupKind && !xd.IsList() && !xd.IsMap() {
  170. panic("invalid Mutable on field with non-composite type")
  171. }
  172. if x, ok := (*m)[int32(xd.Number())]; ok {
  173. return x.Value()
  174. }
  175. v := xt.New()
  176. m.Set(xt, v)
  177. return v
  178. }
  179. // MessageState is a data structure that is nested as the first field in a
  180. // concrete message. It provides a way to implement the ProtoReflect method
  181. // in an allocation-free way without needing to have a shadow Go type generated
  182. // for every message type. This technique only works using unsafe.
  183. //
  184. //
  185. // Example generated code:
  186. //
  187. // type M struct {
  188. // state protoimpl.MessageState
  189. //
  190. // Field1 int32
  191. // Field2 string
  192. // Field3 *BarMessage
  193. // ...
  194. // }
  195. //
  196. // func (m *M) ProtoReflect() protoreflect.Message {
  197. // mi := &file_fizz_buzz_proto_msgInfos[5]
  198. // if protoimpl.UnsafeEnabled && m != nil {
  199. // ms := protoimpl.X.MessageStateOf(Pointer(m))
  200. // if ms.LoadMessageInfo() == nil {
  201. // ms.StoreMessageInfo(mi)
  202. // }
  203. // return ms
  204. // }
  205. // return mi.MessageOf(m)
  206. // }
  207. //
  208. // The MessageState type holds a *MessageInfo, which must be atomically set to
  209. // the message info associated with a given message instance.
  210. // By unsafely converting a *M into a *MessageState, the MessageState object
  211. // has access to all the information needed to implement protobuf reflection.
  212. // It has access to the message info as its first field, and a pointer to the
  213. // MessageState is identical to a pointer to the concrete message value.
  214. //
  215. //
  216. // Requirements:
  217. // • The type M must implement protoreflect.ProtoMessage.
  218. // • The address of m must not be nil.
  219. // • The address of m and the address of m.state must be equal,
  220. // even though they are different Go types.
  221. type MessageState struct {
  222. pragma.NoUnkeyedLiterals
  223. pragma.DoNotCompare
  224. pragma.DoNotCopy
  225. mi *MessageInfo
  226. }
  227. type messageState MessageState
  228. var (
  229. _ pref.Message = (*messageState)(nil)
  230. _ unwrapper = (*messageState)(nil)
  231. )
  232. // messageDataType is a tuple of a pointer to the message data and
  233. // a pointer to the message type. It is a generalized way of providing a
  234. // reflective view over a message instance. The disadvantage of this approach
  235. // is the need to allocate this tuple of 16B.
  236. type messageDataType struct {
  237. p pointer
  238. mi *MessageInfo
  239. }
  240. type (
  241. messageReflectWrapper messageDataType
  242. messageIfaceWrapper messageDataType
  243. )
  244. var (
  245. _ pref.Message = (*messageReflectWrapper)(nil)
  246. _ unwrapper = (*messageReflectWrapper)(nil)
  247. _ pref.ProtoMessage = (*messageIfaceWrapper)(nil)
  248. _ unwrapper = (*messageIfaceWrapper)(nil)
  249. )
  250. // MessageOf returns a reflective view over a message. The input must be a
  251. // pointer to a named Go struct. If the provided type has a ProtoReflect method,
  252. // it must be implemented by calling this method.
  253. func (mi *MessageInfo) MessageOf(m interface{}) pref.Message {
  254. // TODO: Switch the input to be an opaque Pointer.
  255. if reflect.TypeOf(m) != mi.GoReflectType {
  256. panic(fmt.Sprintf("type mismatch: got %T, want %v", m, mi.GoReflectType))
  257. }
  258. p := pointerOfIface(m)
  259. if p.IsNil() {
  260. return mi.nilMessage.Init(mi)
  261. }
  262. return &messageReflectWrapper{p, mi}
  263. }
  264. func (m *messageReflectWrapper) pointer() pointer { return m.p }
  265. func (m *messageReflectWrapper) messageInfo() *MessageInfo { return m.mi }
  266. func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
  267. return (*messageReflectWrapper)(m)
  268. }
  269. func (m *messageIfaceWrapper) protoUnwrap() interface{} {
  270. return m.p.AsIfaceOf(m.mi.GoReflectType.Elem())
  271. }
  272. // checkField verifies that the provided field descriptor is valid.
  273. // Exactly one of the returned values is populated.
  274. func (mi *MessageInfo) checkField(fd pref.FieldDescriptor) (*fieldInfo, pref.ExtensionType) {
  275. var fi *fieldInfo
  276. if n := fd.Number(); 0 < n && int(n) < len(mi.denseFields) {
  277. fi = mi.denseFields[n]
  278. } else {
  279. fi = mi.fields[n]
  280. }
  281. if fi != nil {
  282. if fi.fieldDesc != fd {
  283. panic("mismatching field descriptor")
  284. }
  285. return fi, nil
  286. }
  287. if fd.IsExtension() {
  288. if fd.ContainingMessage().FullName() != mi.Desc.FullName() {
  289. // TODO: Should this be exact containing message descriptor match?
  290. panic("mismatching containing message")
  291. }
  292. if !mi.Desc.ExtensionRanges().Has(fd.Number()) {
  293. panic("invalid extension field")
  294. }
  295. xtd, ok := fd.(pref.ExtensionTypeDescriptor)
  296. if !ok {
  297. panic("extension descriptor does not implement ExtensionTypeDescriptor")
  298. }
  299. return nil, xtd.Type()
  300. }
  301. panic("invalid field descriptor")
  302. }