message.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  12. )
  13. // MessageOf returns the protoreflect.Message interface over p.
  14. // If p already implements proto.Message, then it directly calls the
  15. // ProtoReflect method, otherwise it wraps the legacy v1 message to implement
  16. // the v2 reflective interface.
  17. func MessageOf(p interface{}) pref.Message {
  18. if m, ok := p.(pref.ProtoMessage); ok {
  19. return m.ProtoReflect()
  20. }
  21. return legacyWrapMessage(reflect.ValueOf(p)).ProtoReflect()
  22. }
  23. // MessageType provides protobuf related functionality for a given Go type
  24. // that represents a message. A given instance of MessageType is tied to
  25. // exactly one Go type, which must be a pointer to a struct type.
  26. type MessageType struct {
  27. // Type is the underlying message type and must be populated.
  28. // Once set, this field must never be mutated.
  29. Type pref.MessageType
  30. once sync.Once // protects all unexported fields
  31. goType reflect.Type // pointer to struct
  32. // TODO: Split fields into dense and sparse maps similar to the current
  33. // table-driven implementation in v1?
  34. fields map[pref.FieldNumber]*fieldInfo
  35. unknownFields func(*messageDataType) pref.UnknownFields
  36. extensionFields func(*messageDataType) pref.KnownFields
  37. }
  38. // init lazily initializes the MessageType upon first use and
  39. // also checks that the provided pointer p is of the correct Go type.
  40. //
  41. // It must be called at the start of every exported method.
  42. func (mi *MessageType) init(p interface{}) {
  43. mi.once.Do(func() {
  44. t := reflect.TypeOf(p)
  45. if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
  46. panic(fmt.Sprintf("got %v, want *struct kind", t))
  47. }
  48. mi.goType = t
  49. mi.makeKnownFieldsFunc(t.Elem())
  50. mi.makeUnknownFieldsFunc(t.Elem())
  51. mi.makeExtensionFieldsFunc(t.Elem())
  52. })
  53. // TODO: Remove this check? This API is primarily used by generated code,
  54. // and should not violate this assumption. Leave this check in for now to
  55. // provide some sanity checks during development. This can be removed if
  56. // it proves to be detrimental to performance.
  57. if reflect.TypeOf(p) != mi.goType {
  58. panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.goType))
  59. }
  60. }
  61. // makeKnownFieldsFunc generates functions for operations that can be performed
  62. // on each protobuf message field. It takes in a reflect.Type representing the
  63. // Go struct and matches message fields with struct fields.
  64. //
  65. // This code assumes that the struct is well-formed and panics if there are
  66. // any discrepancies.
  67. func (mi *MessageType) makeKnownFieldsFunc(t reflect.Type) {
  68. // Generate a mapping of field numbers and names to Go struct field or type.
  69. fields := map[pref.FieldNumber]reflect.StructField{}
  70. oneofs := map[pref.Name]reflect.StructField{}
  71. oneofFields := map[pref.FieldNumber]reflect.Type{}
  72. special := map[string]reflect.StructField{}
  73. fieldLoop:
  74. for i := 0; i < t.NumField(); i++ {
  75. f := t.Field(i)
  76. for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
  77. if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
  78. n, _ := strconv.ParseUint(s, 10, 64)
  79. fields[pref.FieldNumber(n)] = f
  80. continue fieldLoop
  81. }
  82. }
  83. if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
  84. oneofs[pref.Name(s)] = f
  85. continue fieldLoop
  86. }
  87. switch f.Name {
  88. case "XXX_weak", "XXX_unrecognized", "XXX_sizecache", "XXX_extensions", "XXX_InternalExtensions":
  89. special[f.Name] = f
  90. continue fieldLoop
  91. }
  92. }
  93. var oneofWrappers []interface{}
  94. if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
  95. oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
  96. }
  97. if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
  98. oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
  99. }
  100. for _, v := range oneofWrappers {
  101. tf := reflect.TypeOf(v).Elem()
  102. f := tf.Field(0)
  103. for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
  104. if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
  105. n, _ := strconv.ParseUint(s, 10, 64)
  106. oneofFields[pref.FieldNumber(n)] = tf
  107. break
  108. }
  109. }
  110. }
  111. mi.fields = map[pref.FieldNumber]*fieldInfo{}
  112. for i := 0; i < mi.Type.Fields().Len(); i++ {
  113. fd := mi.Type.Fields().Get(i)
  114. fs := fields[fd.Number()]
  115. var fi fieldInfo
  116. switch {
  117. case fd.IsWeak():
  118. fi = fieldInfoForWeak(fd, special["XXX_weak"])
  119. case fd.OneofType() != nil:
  120. fi = fieldInfoForOneof(fd, oneofs[fd.OneofType().Name()], oneofFields[fd.Number()])
  121. case fd.IsMap():
  122. fi = fieldInfoForMap(fd, fs)
  123. case fd.Cardinality() == pref.Repeated:
  124. fi = fieldInfoForList(fd, fs)
  125. case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
  126. fi = fieldInfoForMessage(fd, fs)
  127. default:
  128. fi = fieldInfoForScalar(fd, fs)
  129. }
  130. mi.fields[fd.Number()] = &fi
  131. }
  132. }
  133. func (mi *MessageType) makeUnknownFieldsFunc(t reflect.Type) {
  134. if f := makeLegacyUnknownFieldsFunc(t); f != nil {
  135. mi.unknownFields = f
  136. return
  137. }
  138. mi.unknownFields = func(*messageDataType) pref.UnknownFields {
  139. return emptyUnknownFields{}
  140. }
  141. }
  142. func (mi *MessageType) makeExtensionFieldsFunc(t reflect.Type) {
  143. if f := makeLegacyExtensionFieldsFunc(t); f != nil {
  144. mi.extensionFields = f
  145. return
  146. }
  147. mi.extensionFields = func(*messageDataType) pref.KnownFields {
  148. return emptyExtensionFields{}
  149. }
  150. }
  151. func (mi *MessageType) KnownFieldsOf(p interface{}) pref.KnownFields {
  152. return (*knownFields)(mi.dataTypeOf(p))
  153. }
  154. func (mi *MessageType) UnknownFieldsOf(p interface{}) pref.UnknownFields {
  155. return mi.unknownFields(mi.dataTypeOf(p))
  156. }
  157. func (mi *MessageType) dataTypeOf(p interface{}) *messageDataType {
  158. mi.init(p)
  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 knownFields messageDataType
  184. func (fs *knownFields) Len() (cnt int) {
  185. for _, fi := range fs.mi.fields {
  186. if fi.has(fs.p) {
  187. cnt++
  188. }
  189. }
  190. return cnt + fs.extensionFields().Len()
  191. }
  192. func (fs *knownFields) Has(n pref.FieldNumber) bool {
  193. if fi := fs.mi.fields[n]; fi != nil {
  194. return fi.has(fs.p)
  195. }
  196. return fs.extensionFields().Has(n)
  197. }
  198. func (fs *knownFields) Get(n pref.FieldNumber) pref.Value {
  199. if fi := fs.mi.fields[n]; fi != nil {
  200. return fi.get(fs.p)
  201. }
  202. return fs.extensionFields().Get(n)
  203. }
  204. func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) {
  205. if fi := fs.mi.fields[n]; fi != nil {
  206. fi.set(fs.p, v)
  207. return
  208. }
  209. if fs.mi.Type.ExtensionRanges().Has(n) {
  210. fs.extensionFields().Set(n, v)
  211. return
  212. }
  213. panic(fmt.Sprintf("invalid field: %d", n))
  214. }
  215. func (fs *knownFields) Clear(n pref.FieldNumber) {
  216. if fi := fs.mi.fields[n]; fi != nil {
  217. fi.clear(fs.p)
  218. return
  219. }
  220. if fs.mi.Type.ExtensionRanges().Has(n) {
  221. fs.extensionFields().Clear(n)
  222. return
  223. }
  224. }
  225. func (fs *knownFields) Mutable(n pref.FieldNumber) pref.Mutable {
  226. if fi := fs.mi.fields[n]; fi != nil {
  227. return fi.mutable(fs.p)
  228. }
  229. if fs.mi.Type.ExtensionRanges().Has(n) {
  230. return fs.extensionFields().Mutable(n)
  231. }
  232. panic(fmt.Sprintf("invalid field: %d", n))
  233. }
  234. func (fs *knownFields) Range(f func(pref.FieldNumber, pref.Value) bool) {
  235. for n, fi := range fs.mi.fields {
  236. if fi.has(fs.p) {
  237. if !f(n, fi.get(fs.p)) {
  238. return
  239. }
  240. }
  241. }
  242. fs.extensionFields().Range(f)
  243. }
  244. func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes {
  245. return fs.extensionFields().ExtensionTypes()
  246. }
  247. func (fs *knownFields) extensionFields() pref.KnownFields {
  248. return fs.mi.extensionFields((*messageDataType)(fs))
  249. }
  250. type emptyUnknownFields struct{}
  251. func (emptyUnknownFields) Len() int { return 0 }
  252. func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil }
  253. func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop
  254. func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return }
  255. func (emptyUnknownFields) IsSupported() bool { return false }
  256. type emptyExtensionFields struct{}
  257. func (emptyExtensionFields) Len() int { return 0 }
  258. func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false }
  259. func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} }
  260. func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") }
  261. func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop
  262. func (emptyExtensionFields) Mutable(pref.FieldNumber) pref.Mutable { panic("extensions not supported") }
  263. func (emptyExtensionFields) Range(func(pref.FieldNumber, pref.Value) bool) { return }
  264. func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} }
  265. type emptyExtensionTypes struct{}
  266. func (emptyExtensionTypes) Len() int { return 0 }
  267. func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") }
  268. func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop
  269. func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil }
  270. func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil }
  271. func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return }