message.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. "strings"
  10. "sync"
  11. "sync/atomic"
  12. pref "google.golang.org/protobuf/reflect/protoreflect"
  13. piface "google.golang.org/protobuf/runtime/protoiface"
  14. )
  15. // MessageInfo provides protobuf related functionality for a given Go type
  16. // that represents a message. A given instance of MessageInfo is tied to
  17. // exactly one Go type, which must be a pointer to a struct type.
  18. type MessageInfo struct {
  19. // GoType is the underlying message Go type and must be populated.
  20. // Once set, this field must never be mutated.
  21. GoType reflect.Type // pointer to struct
  22. // PBType is the underlying message descriptor type and must be populated.
  23. // Once set, this field must never be mutated.
  24. PBType pref.MessageType
  25. // Exporter must be provided in a purego environment in order to provide
  26. // access to unexported fields.
  27. Exporter exporter
  28. // OneofWrappers is list of pointers to oneof wrapper struct types.
  29. OneofWrappers []interface{}
  30. initMu sync.Mutex // protects all unexported fields
  31. initDone uint32
  32. reflectMessageInfo
  33. // Information used by the fast-path methods.
  34. methods piface.Methods
  35. coderMessageInfo
  36. extensionFieldInfosMu sync.RWMutex
  37. extensionFieldInfos map[pref.ExtensionType]*extensionFieldInfo
  38. }
  39. type reflectMessageInfo struct {
  40. fields map[pref.FieldNumber]*fieldInfo
  41. oneofs map[pref.Name]*oneofInfo
  42. getUnknown func(pointer) pref.RawFields
  43. setUnknown func(pointer, pref.RawFields)
  44. extensionMap func(pointer) *extensionMap
  45. nilMessage atomicNilMessage
  46. }
  47. // exporter is a function that returns a reference to the ith field of v,
  48. // where v is a pointer to a struct. It returns nil if it does not support
  49. // exporting the requested field (e.g., already exported).
  50. type exporter func(v interface{}, i int) interface{}
  51. // getMessageInfo returns the MessageInfo for any message type that
  52. // is generated by our implementation of protoc-gen-go (for v2 and on).
  53. // If it is unable to obtain a MessageInfo, it returns nil.
  54. func getMessageInfo(mt reflect.Type) *MessageInfo {
  55. m, ok := reflect.Zero(mt).Interface().(pref.ProtoMessage)
  56. if !ok {
  57. return nil
  58. }
  59. mr, ok := m.ProtoReflect().(interface{ ProtoMessageInfo() *MessageInfo })
  60. if !ok {
  61. return nil
  62. }
  63. return mr.ProtoMessageInfo()
  64. }
  65. func (mi *MessageInfo) init() {
  66. // This function is called in the hot path. Inline the sync.Once
  67. // logic, since allocating a closure for Once.Do is expensive.
  68. // Keep init small to ensure that it can be inlined.
  69. if atomic.LoadUint32(&mi.initDone) == 0 {
  70. mi.initOnce()
  71. }
  72. }
  73. func (mi *MessageInfo) initOnce() {
  74. mi.initMu.Lock()
  75. defer mi.initMu.Unlock()
  76. if mi.initDone == 1 {
  77. return
  78. }
  79. t := mi.GoType
  80. if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
  81. panic(fmt.Sprintf("got %v, want *struct kind", t))
  82. }
  83. si := mi.makeStructInfo(t.Elem())
  84. mi.makeKnownFieldsFunc(si)
  85. mi.makeUnknownFieldsFunc(t.Elem(), si)
  86. mi.makeExtensionFieldsFunc(t.Elem(), si)
  87. mi.makeMethods(t.Elem(), si)
  88. atomic.StoreUint32(&mi.initDone, 1)
  89. }
  90. type (
  91. SizeCache = int32
  92. WeakFields = map[int32]piface.MessageV1
  93. UnknownFields = []byte
  94. ExtensionFields = map[int32]ExtensionField
  95. )
  96. var (
  97. sizecacheType = reflect.TypeOf(SizeCache(0))
  98. weakFieldsType = reflect.TypeOf(WeakFields(nil))
  99. unknownFieldsType = reflect.TypeOf(UnknownFields(nil))
  100. extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
  101. )
  102. type structInfo struct {
  103. sizecacheOffset offset
  104. weakOffset offset
  105. unknownOffset offset
  106. extensionOffset offset
  107. fieldsByNumber map[pref.FieldNumber]reflect.StructField
  108. oneofsByName map[pref.Name]reflect.StructField
  109. oneofWrappersByType map[reflect.Type]pref.FieldNumber
  110. oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
  111. }
  112. func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
  113. si := structInfo{
  114. sizecacheOffset: invalidOffset,
  115. weakOffset: invalidOffset,
  116. unknownOffset: invalidOffset,
  117. extensionOffset: invalidOffset,
  118. fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
  119. oneofsByName: map[pref.Name]reflect.StructField{},
  120. oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
  121. oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
  122. }
  123. if f, _ := t.FieldByName("sizeCache"); f.Type == sizecacheType {
  124. si.sizecacheOffset = offsetOf(f, mi.Exporter)
  125. }
  126. if f, _ := t.FieldByName("XXX_sizecache"); f.Type == sizecacheType {
  127. si.sizecacheOffset = offsetOf(f, mi.Exporter)
  128. }
  129. if f, _ := t.FieldByName("XXX_weak"); f.Type == weakFieldsType {
  130. si.weakOffset = offsetOf(f, mi.Exporter)
  131. }
  132. if f, _ := t.FieldByName("unknownFields"); f.Type == unknownFieldsType {
  133. si.unknownOffset = offsetOf(f, mi.Exporter)
  134. }
  135. if f, _ := t.FieldByName("XXX_unrecognized"); f.Type == unknownFieldsType {
  136. si.unknownOffset = offsetOf(f, mi.Exporter)
  137. }
  138. if f, _ := t.FieldByName("extensionFields"); f.Type == extensionFieldsType {
  139. si.extensionOffset = offsetOf(f, mi.Exporter)
  140. }
  141. if f, _ := t.FieldByName("XXX_InternalExtensions"); f.Type == extensionFieldsType {
  142. si.extensionOffset = offsetOf(f, mi.Exporter)
  143. }
  144. if f, _ := t.FieldByName("XXX_extensions"); f.Type == extensionFieldsType {
  145. si.extensionOffset = offsetOf(f, mi.Exporter)
  146. }
  147. // Generate a mapping of field numbers and names to Go struct field or type.
  148. fieldLoop:
  149. for i := 0; i < t.NumField(); i++ {
  150. f := t.Field(i)
  151. for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
  152. if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
  153. n, _ := strconv.ParseUint(s, 10, 64)
  154. si.fieldsByNumber[pref.FieldNumber(n)] = f
  155. continue fieldLoop
  156. }
  157. }
  158. if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
  159. si.oneofsByName[pref.Name(s)] = f
  160. continue fieldLoop
  161. }
  162. }
  163. // Derive a mapping of oneof wrappers to fields.
  164. oneofWrappers := mi.OneofWrappers
  165. if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
  166. oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
  167. }
  168. if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
  169. oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
  170. }
  171. for _, v := range oneofWrappers {
  172. tf := reflect.TypeOf(v).Elem()
  173. f := tf.Field(0)
  174. for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
  175. if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
  176. n, _ := strconv.ParseUint(s, 10, 64)
  177. si.oneofWrappersByType[tf] = pref.FieldNumber(n)
  178. si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
  179. break
  180. }
  181. }
  182. }
  183. return si
  184. }
  185. // makeKnownFieldsFunc generates functions for operations that can be performed
  186. // on each protobuf message field. It takes in a reflect.Type representing the
  187. // Go struct and matches message fields with struct fields.
  188. //
  189. // This code assumes that the struct is well-formed and panics if there are
  190. // any discrepancies.
  191. func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
  192. mi.fields = map[pref.FieldNumber]*fieldInfo{}
  193. for i := 0; i < mi.PBType.Fields().Len(); i++ {
  194. fd := mi.PBType.Fields().Get(i)
  195. fs := si.fieldsByNumber[fd.Number()]
  196. var fi fieldInfo
  197. switch {
  198. case fd.ContainingOneof() != nil:
  199. fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], mi.Exporter, si.oneofWrappersByNumber[fd.Number()])
  200. case fd.IsMap():
  201. fi = fieldInfoForMap(fd, fs, mi.Exporter)
  202. case fd.IsList():
  203. fi = fieldInfoForList(fd, fs, mi.Exporter)
  204. case fd.IsWeak():
  205. fi = fieldInfoForWeakMessage(fd, si.weakOffset)
  206. case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
  207. fi = fieldInfoForMessage(fd, fs, mi.Exporter)
  208. default:
  209. fi = fieldInfoForScalar(fd, fs, mi.Exporter)
  210. }
  211. mi.fields[fd.Number()] = &fi
  212. }
  213. mi.oneofs = map[pref.Name]*oneofInfo{}
  214. for i := 0; i < mi.PBType.Oneofs().Len(); i++ {
  215. od := mi.PBType.Oneofs().Get(i)
  216. mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], mi.Exporter, si.oneofWrappersByType)
  217. }
  218. }
  219. func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) {
  220. mi.getUnknown = func(pointer) pref.RawFields { return nil }
  221. mi.setUnknown = func(pointer, pref.RawFields) { return }
  222. if si.unknownOffset.IsValid() {
  223. mi.getUnknown = func(p pointer) pref.RawFields {
  224. if p.IsNil() {
  225. return nil
  226. }
  227. rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
  228. return pref.RawFields(*rv.Interface().(*[]byte))
  229. }
  230. mi.setUnknown = func(p pointer, b pref.RawFields) {
  231. if p.IsNil() {
  232. panic("invalid SetUnknown on nil Message")
  233. }
  234. rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
  235. *rv.Interface().(*[]byte) = []byte(b)
  236. }
  237. } else {
  238. mi.getUnknown = func(pointer) pref.RawFields {
  239. return nil
  240. }
  241. mi.setUnknown = func(p pointer, _ pref.RawFields) {
  242. if p.IsNil() {
  243. panic("invalid SetUnknown on nil Message")
  244. }
  245. }
  246. }
  247. }
  248. func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type, si structInfo) {
  249. if si.extensionOffset.IsValid() {
  250. mi.extensionMap = func(p pointer) *extensionMap {
  251. if p.IsNil() {
  252. return (*extensionMap)(nil)
  253. }
  254. v := p.Apply(si.extensionOffset).AsValueOf(extensionFieldsType)
  255. return (*extensionMap)(v.Interface().(*map[int32]ExtensionField))
  256. }
  257. } else {
  258. mi.extensionMap = func(pointer) *extensionMap {
  259. return (*extensionMap)(nil)
  260. }
  261. }
  262. }