message.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
  94. vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
  95. oneofLoop:
  96. for _, v := range vs.Interface().([]interface{}) {
  97. tf := reflect.TypeOf(v).Elem()
  98. f := tf.Field(0)
  99. for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
  100. if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
  101. n, _ := strconv.ParseUint(s, 10, 64)
  102. oneofFields[pref.FieldNumber(n)] = tf
  103. continue oneofLoop
  104. }
  105. }
  106. }
  107. }
  108. mi.fields = map[pref.FieldNumber]*fieldInfo{}
  109. for i := 0; i < mi.Type.Fields().Len(); i++ {
  110. fd := mi.Type.Fields().Get(i)
  111. fs := fields[fd.Number()]
  112. var fi fieldInfo
  113. switch {
  114. case fd.IsWeak():
  115. fi = fieldInfoForWeak(fd, special["XXX_weak"])
  116. case fd.OneofType() != nil:
  117. fi = fieldInfoForOneof(fd, oneofs[fd.OneofType().Name()], oneofFields[fd.Number()])
  118. case fd.IsMap():
  119. fi = fieldInfoForMap(fd, fs)
  120. case fd.Cardinality() == pref.Repeated:
  121. fi = fieldInfoForList(fd, fs)
  122. case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
  123. fi = fieldInfoForMessage(fd, fs)
  124. default:
  125. fi = fieldInfoForScalar(fd, fs)
  126. }
  127. mi.fields[fd.Number()] = &fi
  128. }
  129. }
  130. func (mi *MessageType) makeUnknownFieldsFunc(t reflect.Type) {
  131. if f := makeLegacyUnknownFieldsFunc(t); f != nil {
  132. mi.unknownFields = f
  133. return
  134. }
  135. mi.unknownFields = func(*messageDataType) pref.UnknownFields {
  136. return emptyUnknownFields{}
  137. }
  138. }
  139. func (mi *MessageType) makeExtensionFieldsFunc(t reflect.Type) {
  140. if f := makeLegacyExtensionFieldsFunc(t); f != nil {
  141. mi.extensionFields = f
  142. return
  143. }
  144. mi.extensionFields = func(*messageDataType) pref.KnownFields {
  145. return emptyExtensionFields{}
  146. }
  147. }
  148. func (mi *MessageType) KnownFieldsOf(p interface{}) pref.KnownFields {
  149. return (*knownFields)(mi.dataTypeOf(p))
  150. }
  151. func (mi *MessageType) UnknownFieldsOf(p interface{}) pref.UnknownFields {
  152. return mi.unknownFields(mi.dataTypeOf(p))
  153. }
  154. func (mi *MessageType) dataTypeOf(p interface{}) *messageDataType {
  155. mi.init(p)
  156. return &messageDataType{pointerOfIface(&p), mi}
  157. }
  158. // messageDataType is a tuple of a pointer to the message data and
  159. // a pointer to the message type.
  160. //
  161. // TODO: Unfortunately, we need to close over a pointer and MessageType,
  162. // which incurs an an allocation. This pair is similar to a Go interface,
  163. // which is essentially a tuple of the same thing. We can make this efficient
  164. // with reflect.NamedOf (see https://golang.org/issues/16522).
  165. //
  166. // With that hypothetical API, we could dynamically create a new named type
  167. // that has the same underlying type as MessageType.goType, and
  168. // dynamically create methods that close over MessageType.
  169. // Since the new type would have the same underlying type, we could directly
  170. // convert between pointers of those types, giving us an efficient way to swap
  171. // out the method set.
  172. //
  173. // Barring the ability to dynamically create named types, the workaround is
  174. // 1. either to accept the cost of an allocation for this wrapper struct or
  175. // 2. generate more types and methods, at the expense of binary size increase.
  176. type messageDataType struct {
  177. p pointer
  178. mi *MessageType
  179. }
  180. type knownFields messageDataType
  181. func (fs *knownFields) Len() (cnt int) {
  182. for _, fi := range fs.mi.fields {
  183. if fi.has(fs.p) {
  184. cnt++
  185. }
  186. }
  187. return cnt + fs.extensionFields().Len()
  188. }
  189. func (fs *knownFields) Has(n pref.FieldNumber) bool {
  190. if fi := fs.mi.fields[n]; fi != nil {
  191. return fi.has(fs.p)
  192. }
  193. return fs.extensionFields().Has(n)
  194. }
  195. func (fs *knownFields) Get(n pref.FieldNumber) pref.Value {
  196. if fi := fs.mi.fields[n]; fi != nil {
  197. return fi.get(fs.p)
  198. }
  199. return fs.extensionFields().Get(n)
  200. }
  201. func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) {
  202. if fi := fs.mi.fields[n]; fi != nil {
  203. fi.set(fs.p, v)
  204. return
  205. }
  206. if fs.mi.Type.ExtensionRanges().Has(n) {
  207. fs.extensionFields().Set(n, v)
  208. return
  209. }
  210. panic(fmt.Sprintf("invalid field: %d", n))
  211. }
  212. func (fs *knownFields) Clear(n pref.FieldNumber) {
  213. if fi := fs.mi.fields[n]; fi != nil {
  214. fi.clear(fs.p)
  215. return
  216. }
  217. if fs.mi.Type.ExtensionRanges().Has(n) {
  218. fs.extensionFields().Clear(n)
  219. return
  220. }
  221. }
  222. func (fs *knownFields) Mutable(n pref.FieldNumber) pref.Mutable {
  223. if fi := fs.mi.fields[n]; fi != nil {
  224. return fi.mutable(fs.p)
  225. }
  226. if fs.mi.Type.ExtensionRanges().Has(n) {
  227. return fs.extensionFields().Mutable(n)
  228. }
  229. panic(fmt.Sprintf("invalid field: %d", n))
  230. }
  231. func (fs *knownFields) Range(f func(pref.FieldNumber, pref.Value) bool) {
  232. for n, fi := range fs.mi.fields {
  233. if fi.has(fs.p) {
  234. if !f(n, fi.get(fs.p)) {
  235. return
  236. }
  237. }
  238. }
  239. fs.extensionFields().Range(f)
  240. }
  241. func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes {
  242. return fs.extensionFields().ExtensionTypes()
  243. }
  244. func (fs *knownFields) extensionFields() pref.KnownFields {
  245. return fs.mi.extensionFields((*messageDataType)(fs))
  246. }
  247. type emptyUnknownFields struct{}
  248. func (emptyUnknownFields) Len() int { return 0 }
  249. func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil }
  250. func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop
  251. func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return }
  252. func (emptyUnknownFields) IsSupported() bool { return false }
  253. type emptyExtensionFields struct{}
  254. func (emptyExtensionFields) Len() int { return 0 }
  255. func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false }
  256. func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} }
  257. func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") }
  258. func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop
  259. func (emptyExtensionFields) Mutable(pref.FieldNumber) pref.Mutable { panic("extensions not supported") }
  260. func (emptyExtensionFields) Range(func(pref.FieldNumber, pref.Value) bool) { return }
  261. func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} }
  262. type emptyExtensionTypes struct{}
  263. func (emptyExtensionTypes) Len() int { return 0 }
  264. func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") }
  265. func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop
  266. func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil }
  267. func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil }
  268. func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return }