message.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. pvalue "google.golang.org/protobuf/internal/value"
  12. pref "google.golang.org/protobuf/reflect/protoreflect"
  13. piface "google.golang.org/protobuf/runtime/protoiface"
  14. )
  15. // MessageType provides protobuf related functionality for a given Go type
  16. // that represents a message. A given instance of MessageType is tied to
  17. // exactly one Go type, which must be a pointer to a struct type.
  18. type MessageType 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. once sync.Once // protects all unexported fields
  26. // TODO: Split fields into dense and sparse maps similar to the current
  27. // table-driven implementation in v1?
  28. fields map[pref.FieldNumber]*fieldInfo
  29. oneofs map[pref.Name]*oneofInfo
  30. unknownFields func(*messageDataType) pref.UnknownFields
  31. extensionFields func(*messageDataType) pref.KnownFields
  32. }
  33. func (mi *MessageType) init() {
  34. mi.once.Do(func() {
  35. t := mi.GoType
  36. if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
  37. panic(fmt.Sprintf("got %v, want *struct kind", t))
  38. }
  39. si := mi.makeStructInfo(t.Elem())
  40. mi.makeKnownFieldsFunc(si)
  41. mi.makeUnknownFieldsFunc(t.Elem())
  42. mi.makeExtensionFieldsFunc(t.Elem())
  43. })
  44. }
  45. type structInfo struct {
  46. fieldsByNumber map[pref.FieldNumber]reflect.StructField
  47. oneofsByName map[pref.Name]reflect.StructField
  48. oneofWrappersByType map[reflect.Type]pref.FieldNumber
  49. oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
  50. }
  51. func (mi *MessageType) makeStructInfo(t reflect.Type) structInfo {
  52. // Generate a mapping of field numbers and names to Go struct field or type.
  53. si := structInfo{
  54. fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
  55. oneofsByName: map[pref.Name]reflect.StructField{},
  56. oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
  57. oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
  58. }
  59. fieldLoop:
  60. for i := 0; i < t.NumField(); i++ {
  61. f := t.Field(i)
  62. for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
  63. if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
  64. n, _ := strconv.ParseUint(s, 10, 64)
  65. si.fieldsByNumber[pref.FieldNumber(n)] = f
  66. continue fieldLoop
  67. }
  68. }
  69. if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
  70. si.oneofsByName[pref.Name(s)] = f
  71. continue fieldLoop
  72. }
  73. }
  74. var oneofWrappers []interface{}
  75. if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
  76. oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
  77. }
  78. if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
  79. oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
  80. }
  81. for _, v := range oneofWrappers {
  82. tf := reflect.TypeOf(v).Elem()
  83. f := tf.Field(0)
  84. for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
  85. if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
  86. n, _ := strconv.ParseUint(s, 10, 64)
  87. si.oneofWrappersByType[tf] = pref.FieldNumber(n)
  88. si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
  89. break
  90. }
  91. }
  92. }
  93. return si
  94. }
  95. // makeKnownFieldsFunc generates functions for operations that can be performed
  96. // on each protobuf message field. It takes in a reflect.Type representing the
  97. // Go struct and matches message fields with struct fields.
  98. //
  99. // This code assumes that the struct is well-formed and panics if there are
  100. // any discrepancies.
  101. func (mi *MessageType) makeKnownFieldsFunc(si structInfo) {
  102. mi.fields = map[pref.FieldNumber]*fieldInfo{}
  103. for i := 0; i < mi.PBType.Descriptor().Fields().Len(); i++ {
  104. fd := mi.PBType.Descriptor().Fields().Get(i)
  105. fs := si.fieldsByNumber[fd.Number()]
  106. var fi fieldInfo
  107. switch {
  108. case fd.ContainingOneof() != nil:
  109. fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], si.oneofWrappersByNumber[fd.Number()])
  110. case fd.IsMap():
  111. fi = fieldInfoForMap(fd, fs)
  112. case fd.IsList():
  113. fi = fieldInfoForList(fd, fs)
  114. case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
  115. fi = fieldInfoForMessage(fd, fs)
  116. default:
  117. fi = fieldInfoForScalar(fd, fs)
  118. }
  119. mi.fields[fd.Number()] = &fi
  120. }
  121. mi.oneofs = map[pref.Name]*oneofInfo{}
  122. for i := 0; i < mi.PBType.Descriptor().Oneofs().Len(); i++ {
  123. od := mi.PBType.Descriptor().Oneofs().Get(i)
  124. mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], si.oneofWrappersByType)
  125. }
  126. }
  127. func (mi *MessageType) makeUnknownFieldsFunc(t reflect.Type) {
  128. if f := makeLegacyUnknownFieldsFunc(t); f != nil {
  129. mi.unknownFields = f
  130. return
  131. }
  132. mi.unknownFields = func(*messageDataType) pref.UnknownFields {
  133. return emptyUnknownFields{}
  134. }
  135. }
  136. func (mi *MessageType) makeExtensionFieldsFunc(t reflect.Type) {
  137. if f := makeLegacyExtensionFieldsFunc(t); f != nil {
  138. mi.extensionFields = f
  139. return
  140. }
  141. mi.extensionFields = func(*messageDataType) pref.KnownFields {
  142. return emptyExtensionFields{}
  143. }
  144. }
  145. func (mi *MessageType) MessageOf(p interface{}) pref.Message {
  146. return (*messageReflectWrapper)(mi.dataTypeOf(p))
  147. }
  148. func (mi *MessageType) Methods() *piface.Methods {
  149. return nil
  150. }
  151. func (mi *MessageType) dataTypeOf(p interface{}) *messageDataType {
  152. // TODO: Remove this check? This API is primarily used by generated code,
  153. // and should not violate this assumption. Leave this check in for now to
  154. // provide some sanity checks during development. This can be removed if
  155. // it proves to be detrimental to performance.
  156. if reflect.TypeOf(p) != mi.GoType {
  157. panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.GoType))
  158. }
  159. return &messageDataType{pointerOfIface(p), mi}
  160. }
  161. // messageDataType is a tuple of a pointer to the message data and
  162. // a pointer to the message type.
  163. //
  164. // TODO: Unfortunately, we need to close over a pointer and MessageType,
  165. // which incurs an an allocation. This pair is similar to a Go interface,
  166. // which is essentially a tuple of the same thing. We can make this efficient
  167. // with reflect.NamedOf (see https://golang.org/issues/16522).
  168. //
  169. // With that hypothetical API, we could dynamically create a new named type
  170. // that has the same underlying type as MessageType.GoType, and
  171. // dynamically create methods that close over MessageType.
  172. // Since the new type would have the same underlying type, we could directly
  173. // convert between pointers of those types, giving us an efficient way to swap
  174. // out the method set.
  175. //
  176. // Barring the ability to dynamically create named types, the workaround is
  177. // 1. either to accept the cost of an allocation for this wrapper struct or
  178. // 2. generate more types and methods, at the expense of binary size increase.
  179. type messageDataType struct {
  180. p pointer
  181. mi *MessageType
  182. }
  183. type messageReflectWrapper messageDataType
  184. // TODO: Remove this.
  185. func (m *messageReflectWrapper) Type() pref.MessageType {
  186. return m.mi.PBType
  187. }
  188. func (m *messageReflectWrapper) Descriptor() pref.MessageDescriptor {
  189. return m.mi.PBType.Descriptor()
  190. }
  191. func (m *messageReflectWrapper) KnownFields() pref.KnownFields {
  192. m.mi.init()
  193. return (*knownFields)(m)
  194. }
  195. func (m *messageReflectWrapper) UnknownFields() pref.UnknownFields {
  196. m.mi.init()
  197. return m.mi.unknownFields((*messageDataType)(m))
  198. }
  199. func (m *messageReflectWrapper) New() pref.Message {
  200. return m.mi.PBType.New()
  201. }
  202. func (m *messageReflectWrapper) Interface() pref.ProtoMessage {
  203. if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
  204. return m
  205. }
  206. return (*messageIfaceWrapper)(m)
  207. }
  208. func (m *messageReflectWrapper) ProtoUnwrap() interface{} {
  209. return m.p.AsIfaceOf(m.mi.GoType.Elem())
  210. }
  211. func (m *messageReflectWrapper) ProtoMutable() {}
  212. var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil)
  213. type messageIfaceWrapper messageDataType
  214. func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
  215. return (*messageReflectWrapper)(m)
  216. }
  217. func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods {
  218. return m.mi.Methods()
  219. }
  220. func (m *messageIfaceWrapper) ProtoUnwrap() interface{} {
  221. return m.p.AsIfaceOf(m.mi.GoType.Elem())
  222. }
  223. type knownFields messageDataType
  224. func (fs *knownFields) Len() (cnt int) {
  225. for _, fi := range fs.mi.fields {
  226. if fi.has(fs.p) {
  227. cnt++
  228. }
  229. }
  230. return cnt + fs.extensionFields().Len()
  231. }
  232. func (fs *knownFields) Has(n pref.FieldNumber) bool {
  233. if fi := fs.mi.fields[n]; fi != nil {
  234. return fi.has(fs.p)
  235. }
  236. return fs.extensionFields().Has(n)
  237. }
  238. func (fs *knownFields) Get(n pref.FieldNumber) pref.Value {
  239. if fi := fs.mi.fields[n]; fi != nil {
  240. return fi.get(fs.p)
  241. }
  242. return fs.extensionFields().Get(n)
  243. }
  244. func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) {
  245. if fi := fs.mi.fields[n]; fi != nil {
  246. fi.set(fs.p, v)
  247. return
  248. }
  249. if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) {
  250. fs.extensionFields().Set(n, v)
  251. return
  252. }
  253. panic(fmt.Sprintf("invalid field: %d", n))
  254. }
  255. func (fs *knownFields) Clear(n pref.FieldNumber) {
  256. if fi := fs.mi.fields[n]; fi != nil {
  257. fi.clear(fs.p)
  258. return
  259. }
  260. if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) {
  261. fs.extensionFields().Clear(n)
  262. return
  263. }
  264. }
  265. func (fs *knownFields) WhichOneof(s pref.Name) pref.FieldNumber {
  266. if oi := fs.mi.oneofs[s]; oi != nil {
  267. return oi.which(fs.p)
  268. }
  269. return 0
  270. }
  271. func (fs *knownFields) Range(f func(pref.FieldNumber, pref.Value) bool) {
  272. for n, fi := range fs.mi.fields {
  273. if fi.has(fs.p) {
  274. if !f(n, fi.get(fs.p)) {
  275. return
  276. }
  277. }
  278. }
  279. fs.extensionFields().Range(f)
  280. }
  281. func (fs *knownFields) NewMessage(n pref.FieldNumber) pref.Message {
  282. if fi := fs.mi.fields[n]; fi != nil {
  283. return fi.newMessage()
  284. }
  285. if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) {
  286. return fs.extensionFields().NewMessage(n)
  287. }
  288. panic(fmt.Sprintf("invalid field: %d", n))
  289. }
  290. func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes {
  291. return fs.extensionFields().ExtensionTypes()
  292. }
  293. func (fs *knownFields) extensionFields() pref.KnownFields {
  294. return fs.mi.extensionFields((*messageDataType)(fs))
  295. }
  296. type emptyUnknownFields struct{}
  297. func (emptyUnknownFields) Len() int { return 0 }
  298. func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil }
  299. func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop
  300. func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return }
  301. func (emptyUnknownFields) IsSupported() bool { return false }
  302. type emptyExtensionFields struct{}
  303. func (emptyExtensionFields) Len() int { return 0 }
  304. func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false }
  305. func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} }
  306. func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") }
  307. func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop
  308. func (emptyExtensionFields) WhichOneof(pref.Name) pref.FieldNumber { return 0 }
  309. func (emptyExtensionFields) Range(func(pref.FieldNumber, pref.Value) bool) { return }
  310. func (emptyExtensionFields) NewMessage(pref.FieldNumber) pref.Message {
  311. panic("extensions not supported")
  312. }
  313. func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} }
  314. type emptyExtensionTypes struct{}
  315. func (emptyExtensionTypes) Len() int { return 0 }
  316. func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") }
  317. func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop
  318. func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil }
  319. func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil }
  320. func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return }