message.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "github.com/golang/protobuf/v2/internal/value"
  12. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  13. piface "github.com/golang/protobuf/v2/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.Fields().Len(); i++ {
  104. fd := mi.PBType.Fields().Get(i)
  105. fs := si.fieldsByNumber[fd.Number()]
  106. var fi fieldInfo
  107. switch {
  108. case fd.Oneof() != nil:
  109. fi = fieldInfoForOneof(fd, si.oneofsByName[fd.Oneof().Name()], si.oneofWrappersByNumber[fd.Number()])
  110. case fd.IsMap():
  111. fi = fieldInfoForMap(fd, fs)
  112. case fd.Cardinality() == pref.Repeated:
  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.Oneofs().Len(); i++ {
  123. od := mi.PBType.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. func (m *messageReflectWrapper) Type() pref.MessageType {
  185. return m.mi.PBType
  186. }
  187. func (m *messageReflectWrapper) KnownFields() pref.KnownFields {
  188. m.mi.init()
  189. return (*knownFields)(m)
  190. }
  191. func (m *messageReflectWrapper) UnknownFields() pref.UnknownFields {
  192. m.mi.init()
  193. return m.mi.unknownFields((*messageDataType)(m))
  194. }
  195. func (m *messageReflectWrapper) Interface() pref.ProtoMessage {
  196. if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
  197. return m
  198. }
  199. return (*messageIfaceWrapper)(m)
  200. }
  201. func (m *messageReflectWrapper) ProtoUnwrap() interface{} {
  202. return m.p.AsIfaceOf(m.mi.GoType.Elem())
  203. }
  204. func (m *messageReflectWrapper) ProtoMutable() {}
  205. var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil)
  206. type messageIfaceWrapper messageDataType
  207. func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
  208. return (*messageReflectWrapper)(m)
  209. }
  210. func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods {
  211. return m.mi.Methods()
  212. }
  213. func (m *messageIfaceWrapper) ProtoUnwrap() interface{} {
  214. return m.p.AsIfaceOf(m.mi.GoType.Elem())
  215. }
  216. type knownFields messageDataType
  217. func (fs *knownFields) Len() (cnt int) {
  218. for _, fi := range fs.mi.fields {
  219. if fi.has(fs.p) {
  220. cnt++
  221. }
  222. }
  223. return cnt + fs.extensionFields().Len()
  224. }
  225. func (fs *knownFields) Has(n pref.FieldNumber) bool {
  226. if fi := fs.mi.fields[n]; fi != nil {
  227. return fi.has(fs.p)
  228. }
  229. return fs.extensionFields().Has(n)
  230. }
  231. func (fs *knownFields) Get(n pref.FieldNumber) pref.Value {
  232. if fi := fs.mi.fields[n]; fi != nil {
  233. return fi.get(fs.p)
  234. }
  235. return fs.extensionFields().Get(n)
  236. }
  237. func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) {
  238. if fi := fs.mi.fields[n]; fi != nil {
  239. fi.set(fs.p, v)
  240. return
  241. }
  242. if fs.mi.PBType.ExtensionRanges().Has(n) {
  243. fs.extensionFields().Set(n, v)
  244. return
  245. }
  246. panic(fmt.Sprintf("invalid field: %d", n))
  247. }
  248. func (fs *knownFields) Clear(n pref.FieldNumber) {
  249. if fi := fs.mi.fields[n]; fi != nil {
  250. fi.clear(fs.p)
  251. return
  252. }
  253. if fs.mi.PBType.ExtensionRanges().Has(n) {
  254. fs.extensionFields().Clear(n)
  255. return
  256. }
  257. }
  258. func (fs *knownFields) WhichOneof(s pref.Name) pref.FieldNumber {
  259. if oi := fs.mi.oneofs[s]; oi != nil {
  260. return oi.which(fs.p)
  261. }
  262. return 0
  263. }
  264. func (fs *knownFields) Range(f func(pref.FieldNumber, pref.Value) bool) {
  265. for n, fi := range fs.mi.fields {
  266. if fi.has(fs.p) {
  267. if !f(n, fi.get(fs.p)) {
  268. return
  269. }
  270. }
  271. }
  272. fs.extensionFields().Range(f)
  273. }
  274. func (fs *knownFields) NewMessage(n pref.FieldNumber) pref.Message {
  275. if fi := fs.mi.fields[n]; fi != nil {
  276. return fi.newMessage()
  277. }
  278. if fs.mi.PBType.ExtensionRanges().Has(n) {
  279. return fs.extensionFields().NewMessage(n)
  280. }
  281. panic(fmt.Sprintf("invalid field: %d", n))
  282. }
  283. func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes {
  284. return fs.extensionFields().ExtensionTypes()
  285. }
  286. func (fs *knownFields) extensionFields() pref.KnownFields {
  287. return fs.mi.extensionFields((*messageDataType)(fs))
  288. }
  289. type emptyUnknownFields struct{}
  290. func (emptyUnknownFields) Len() int { return 0 }
  291. func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil }
  292. func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop
  293. func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return }
  294. func (emptyUnknownFields) IsSupported() bool { return false }
  295. type emptyExtensionFields struct{}
  296. func (emptyExtensionFields) Len() int { return 0 }
  297. func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false }
  298. func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} }
  299. func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") }
  300. func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop
  301. func (emptyExtensionFields) WhichOneof(pref.Name) pref.FieldNumber { return 0 }
  302. func (emptyExtensionFields) Range(func(pref.FieldNumber, pref.Value) bool) { return }
  303. func (emptyExtensionFields) NewMessage(pref.FieldNumber) pref.Message {
  304. panic("extensions not supported")
  305. }
  306. func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} }
  307. type emptyExtensionTypes struct{}
  308. func (emptyExtensionTypes) Len() int { return 0 }
  309. func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") }
  310. func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop
  311. func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil }
  312. func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil }
  313. func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return }