blob: 0b2c3fca5d989712c7a85c8e29bb7b4ec08f5819 [file] [log] [blame]
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001// 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
5package impl
6
7import (
Joe Tsaic6b75612018-09-13 14:24:37 -07008 "fmt"
Joe Tsaifa02f4e2018-09-12 16:20:37 -07009 "reflect"
10 "strconv"
11 "strings"
Joe Tsaic6b75612018-09-13 14:24:37 -070012 "sync"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070013
Joe Tsai08e00302018-11-26 22:32:06 -080014 pvalue "github.com/golang/protobuf/v2/internal/value"
Joe Tsai01ab2962018-09-21 17:44:00 -070015 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Damien Neil0d3e8cc2019-04-01 13:31:55 -070016 piface "github.com/golang/protobuf/v2/runtime/protoiface"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070017)
18
Joe Tsaic6b75612018-09-13 14:24:37 -070019// MessageType provides protobuf related functionality for a given Go type
20// that represents a message. A given instance of MessageType is tied to
21// exactly one Go type, which must be a pointer to a struct type.
22type MessageType struct {
Damien Neil8012b442019-01-18 09:32:24 -080023 // GoType is the underlying message Go type and must be populated.
Joe Tsaic6b75612018-09-13 14:24:37 -070024 // Once set, this field must never be mutated.
Damien Neil8012b442019-01-18 09:32:24 -080025 GoType reflect.Type // pointer to struct
26
27 // PBType is the underlying message descriptor type and must be populated.
28 // Once set, this field must never be mutated.
29 PBType pref.MessageType
Joe Tsaic6b75612018-09-13 14:24:37 -070030
31 once sync.Once // protects all unexported fields
32
Joe Tsaifa02f4e2018-09-12 16:20:37 -070033 // TODO: Split fields into dense and sparse maps similar to the current
34 // table-driven implementation in v1?
35 fields map[pref.FieldNumber]*fieldInfo
Joe Tsaibe5348c2018-10-23 18:31:18 -070036
37 unknownFields func(*messageDataType) pref.UnknownFields
38 extensionFields func(*messageDataType) pref.KnownFields
Joe Tsaifa02f4e2018-09-12 16:20:37 -070039}
40
Damien Neil8012b442019-01-18 09:32:24 -080041func (mi *MessageType) init() {
Joe Tsaic6b75612018-09-13 14:24:37 -070042 mi.once.Do(func() {
Damien Neil8012b442019-01-18 09:32:24 -080043 t := mi.GoType
Joe Tsaic6b75612018-09-13 14:24:37 -070044 if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
45 panic(fmt.Sprintf("got %v, want *struct kind", t))
46 }
Joe Tsaic6b75612018-09-13 14:24:37 -070047
Joe Tsai95b02902018-10-31 18:23:42 -070048 mi.makeKnownFieldsFunc(t.Elem())
49 mi.makeUnknownFieldsFunc(t.Elem())
50 mi.makeExtensionFieldsFunc(t.Elem())
Joe Tsaic6b75612018-09-13 14:24:37 -070051 })
Joe Tsaic6b75612018-09-13 14:24:37 -070052}
53
Joe Tsaif0c01e42018-11-06 13:05:20 -080054// makeKnownFieldsFunc generates functions for operations that can be performed
55// on each protobuf message field. It takes in a reflect.Type representing the
56// Go struct and matches message fields with struct fields.
Joe Tsaifa02f4e2018-09-12 16:20:37 -070057//
58// This code assumes that the struct is well-formed and panics if there are
59// any discrepancies.
Joe Tsai95b02902018-10-31 18:23:42 -070060func (mi *MessageType) makeKnownFieldsFunc(t reflect.Type) {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070061 // Generate a mapping of field numbers and names to Go struct field or type.
62 fields := map[pref.FieldNumber]reflect.StructField{}
63 oneofs := map[pref.Name]reflect.StructField{}
64 oneofFields := map[pref.FieldNumber]reflect.Type{}
65 special := map[string]reflect.StructField{}
66fieldLoop:
67 for i := 0; i < t.NumField(); i++ {
68 f := t.Field(i)
69 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
70 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
71 n, _ := strconv.ParseUint(s, 10, 64)
72 fields[pref.FieldNumber(n)] = f
73 continue fieldLoop
74 }
75 }
76 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
77 oneofs[pref.Name(s)] = f
78 continue fieldLoop
79 }
80 switch f.Name {
81 case "XXX_weak", "XXX_unrecognized", "XXX_sizecache", "XXX_extensions", "XXX_InternalExtensions":
82 special[f.Name] = f
83 continue fieldLoop
84 }
85 }
Joe Tsaid7e97bc2018-11-26 12:57:27 -080086 var oneofWrappers []interface{}
Joe Tsai2c870bb2018-10-17 11:46:52 -070087 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
Joe Tsaid7e97bc2018-11-26 12:57:27 -080088 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
89 }
90 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
91 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
92 }
93 for _, v := range oneofWrappers {
94 tf := reflect.TypeOf(v).Elem()
95 f := tf.Field(0)
96 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
97 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
98 n, _ := strconv.ParseUint(s, 10, 64)
99 oneofFields[pref.FieldNumber(n)] = tf
100 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700101 }
102 }
103 }
104
105 mi.fields = map[pref.FieldNumber]*fieldInfo{}
Damien Neil8012b442019-01-18 09:32:24 -0800106 for i := 0; i < mi.PBType.Fields().Len(); i++ {
107 fd := mi.PBType.Fields().Get(i)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700108 fs := fields[fd.Number()]
109 var fi fieldInfo
110 switch {
111 case fd.IsWeak():
112 fi = fieldInfoForWeak(fd, special["XXX_weak"])
113 case fd.OneofType() != nil:
114 fi = fieldInfoForOneof(fd, oneofs[fd.OneofType().Name()], oneofFields[fd.Number()])
115 case fd.IsMap():
116 fi = fieldInfoForMap(fd, fs)
117 case fd.Cardinality() == pref.Repeated:
Joe Tsai4b7aff62018-11-14 14:05:19 -0800118 fi = fieldInfoForList(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700119 case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700120 fi = fieldInfoForMessage(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700121 default:
122 fi = fieldInfoForScalar(fd, fs)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700123 }
124 mi.fields[fd.Number()] = &fi
125 }
126}
Joe Tsaic6b75612018-09-13 14:24:37 -0700127
Joe Tsai95b02902018-10-31 18:23:42 -0700128func (mi *MessageType) makeUnknownFieldsFunc(t reflect.Type) {
129 if f := makeLegacyUnknownFieldsFunc(t); f != nil {
Joe Tsaie2afdc22018-10-25 14:06:56 -0700130 mi.unknownFields = f
131 return
132 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700133 mi.unknownFields = func(*messageDataType) pref.UnknownFields {
134 return emptyUnknownFields{}
135 }
136}
137
Joe Tsai95b02902018-10-31 18:23:42 -0700138func (mi *MessageType) makeExtensionFieldsFunc(t reflect.Type) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800139 if f := makeLegacyExtensionFieldsFunc(t); f != nil {
140 mi.extensionFields = f
141 return
142 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700143 mi.extensionFields = func(*messageDataType) pref.KnownFields {
144 return emptyExtensionFields{}
145 }
146}
147
Joe Tsai08e00302018-11-26 22:32:06 -0800148func (mi *MessageType) MessageOf(p interface{}) pref.Message {
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700149 return (*messageReflectWrapper)(mi.dataTypeOf(p))
Joe Tsai08e00302018-11-26 22:32:06 -0800150}
151
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700152func (mi *MessageType) Methods() *piface.Methods {
153 return nil
154}
155
Joe Tsaic6b75612018-09-13 14:24:37 -0700156func (mi *MessageType) dataTypeOf(p interface{}) *messageDataType {
Damien Neil8012b442019-01-18 09:32:24 -0800157 // TODO: Remove this check? This API is primarily used by generated code,
158 // and should not violate this assumption. Leave this check in for now to
159 // provide some sanity checks during development. This can be removed if
160 // it proves to be detrimental to performance.
161 if reflect.TypeOf(p) != mi.GoType {
162 panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.GoType))
163 }
Joe Tsai6cf80c42018-12-01 04:57:09 -0800164 return &messageDataType{pointerOfIface(p), mi}
Joe Tsaic6b75612018-09-13 14:24:37 -0700165}
166
167// messageDataType is a tuple of a pointer to the message data and
168// a pointer to the message type.
169//
170// TODO: Unfortunately, we need to close over a pointer and MessageType,
171// which incurs an an allocation. This pair is similar to a Go interface,
172// which is essentially a tuple of the same thing. We can make this efficient
173// with reflect.NamedOf (see https://golang.org/issues/16522).
174//
175// With that hypothetical API, we could dynamically create a new named type
Damien Neil8012b442019-01-18 09:32:24 -0800176// that has the same underlying type as MessageType.GoType, and
Joe Tsaic6b75612018-09-13 14:24:37 -0700177// dynamically create methods that close over MessageType.
178// Since the new type would have the same underlying type, we could directly
179// convert between pointers of those types, giving us an efficient way to swap
180// out the method set.
181//
182// Barring the ability to dynamically create named types, the workaround is
183// 1. either to accept the cost of an allocation for this wrapper struct or
184// 2. generate more types and methods, at the expense of binary size increase.
185type messageDataType struct {
186 p pointer
187 mi *MessageType
188}
189
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700190type messageReflectWrapper messageDataType
Joe Tsai08e00302018-11-26 22:32:06 -0800191
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700192func (m *messageReflectWrapper) Type() pref.MessageType {
Damien Neil8012b442019-01-18 09:32:24 -0800193 return m.mi.PBType
Joe Tsai08e00302018-11-26 22:32:06 -0800194}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700195func (m *messageReflectWrapper) KnownFields() pref.KnownFields {
Damien Neil8012b442019-01-18 09:32:24 -0800196 m.mi.init()
Joe Tsai08e00302018-11-26 22:32:06 -0800197 return (*knownFields)(m)
198}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700199func (m *messageReflectWrapper) UnknownFields() pref.UnknownFields {
Damien Neil8012b442019-01-18 09:32:24 -0800200 m.mi.init()
Joe Tsai08e00302018-11-26 22:32:06 -0800201 return m.mi.unknownFields((*messageDataType)(m))
202}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700203func (m *messageReflectWrapper) Interface() pref.ProtoMessage {
Joe Tsai08e00302018-11-26 22:32:06 -0800204 if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
205 return m
206 }
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700207 return (*messageIfaceWrapper)(m)
Joe Tsai08e00302018-11-26 22:32:06 -0800208}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700209func (m *messageReflectWrapper) ProtoUnwrap() interface{} {
Damien Neil8012b442019-01-18 09:32:24 -0800210 return m.p.AsIfaceOf(m.mi.GoType.Elem())
Joe Tsai08e00302018-11-26 22:32:06 -0800211}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700212func (m *messageReflectWrapper) ProtoMutable() {}
Joe Tsai08e00302018-11-26 22:32:06 -0800213
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700214var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil)
215
216type messageIfaceWrapper messageDataType
217
218func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
219 return (*messageReflectWrapper)(m)
220}
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700221func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods {
222 return m.mi.Methods()
223}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700224func (m *messageIfaceWrapper) ProtoUnwrap() interface{} {
225 return m.p.AsIfaceOf(m.mi.GoType.Elem())
226}
Joe Tsai08e00302018-11-26 22:32:06 -0800227
Joe Tsaic6b75612018-09-13 14:24:37 -0700228type knownFields messageDataType
229
Joe Tsaic6b75612018-09-13 14:24:37 -0700230func (fs *knownFields) Len() (cnt int) {
231 for _, fi := range fs.mi.fields {
232 if fi.has(fs.p) {
233 cnt++
234 }
235 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700236 return cnt + fs.extensionFields().Len()
Joe Tsaic6b75612018-09-13 14:24:37 -0700237}
238func (fs *knownFields) Has(n pref.FieldNumber) bool {
239 if fi := fs.mi.fields[n]; fi != nil {
240 return fi.has(fs.p)
241 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700242 return fs.extensionFields().Has(n)
Joe Tsaic6b75612018-09-13 14:24:37 -0700243}
244func (fs *knownFields) Get(n pref.FieldNumber) pref.Value {
245 if fi := fs.mi.fields[n]; fi != nil {
246 return fi.get(fs.p)
247 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700248 return fs.extensionFields().Get(n)
Joe Tsaic6b75612018-09-13 14:24:37 -0700249}
250func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) {
251 if fi := fs.mi.fields[n]; fi != nil {
252 fi.set(fs.p, v)
253 return
254 }
Damien Neil8012b442019-01-18 09:32:24 -0800255 if fs.mi.PBType.ExtensionRanges().Has(n) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800256 fs.extensionFields().Set(n, v)
257 return
258 }
259 panic(fmt.Sprintf("invalid field: %d", n))
Joe Tsaic6b75612018-09-13 14:24:37 -0700260}
261func (fs *knownFields) Clear(n pref.FieldNumber) {
262 if fi := fs.mi.fields[n]; fi != nil {
263 fi.clear(fs.p)
264 return
265 }
Damien Neil8012b442019-01-18 09:32:24 -0800266 if fs.mi.PBType.ExtensionRanges().Has(n) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800267 fs.extensionFields().Clear(n)
268 return
269 }
Joe Tsaic6b75612018-09-13 14:24:37 -0700270}
Joe Tsaic6b75612018-09-13 14:24:37 -0700271func (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 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700279 fs.extensionFields().Range(f)
Joe Tsaic6b75612018-09-13 14:24:37 -0700280}
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800281func (fs *knownFields) NewMessage(n pref.FieldNumber) pref.Message {
Damien Neil97e7f572018-12-07 14:28:33 -0800282 if fi := fs.mi.fields[n]; fi != nil {
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800283 return fi.newMessage()
Damien Neil97e7f572018-12-07 14:28:33 -0800284 }
Damien Neil8012b442019-01-18 09:32:24 -0800285 if fs.mi.PBType.ExtensionRanges().Has(n) {
Damien Neil97e7f572018-12-07 14:28:33 -0800286 return fs.extensionFields().NewMessage(n)
287 }
288 panic(fmt.Sprintf("invalid field: %d", n))
289}
Joe Tsaic6b75612018-09-13 14:24:37 -0700290func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes {
Joe Tsaibe5348c2018-10-23 18:31:18 -0700291 return fs.extensionFields().ExtensionTypes()
292}
293func (fs *knownFields) extensionFields() pref.KnownFields {
294 return fs.mi.extensionFields((*messageDataType)(fs))
Joe Tsaic6b75612018-09-13 14:24:37 -0700295}
296
Joe Tsaibe5348c2018-10-23 18:31:18 -0700297type emptyUnknownFields struct{}
Joe Tsaic6b75612018-09-13 14:24:37 -0700298
Joe Tsaibe5348c2018-10-23 18:31:18 -0700299func (emptyUnknownFields) Len() int { return 0 }
300func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800301func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop
302func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700303func (emptyUnknownFields) IsSupported() bool { return false }
Joe Tsaic6b75612018-09-13 14:24:37 -0700304
Joe Tsaibe5348c2018-10-23 18:31:18 -0700305type emptyExtensionFields struct{}
Joe Tsaic6b75612018-09-13 14:24:37 -0700306
Joe Tsaif0c01e42018-11-06 13:05:20 -0800307func (emptyExtensionFields) Len() int { return 0 }
308func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false }
309func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} }
310func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") }
311func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop
Joe Tsaif0c01e42018-11-06 13:05:20 -0800312func (emptyExtensionFields) Range(func(pref.FieldNumber, pref.Value) bool) { return }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800313func (emptyExtensionFields) NewMessage(pref.FieldNumber) pref.Message {
Damien Neil97e7f572018-12-07 14:28:33 -0800314 panic("extensions not supported")
315}
316func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700317
318type emptyExtensionTypes struct{}
319
320func (emptyExtensionTypes) Len() int { return 0 }
321func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800322func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop
Joe Tsaibe5348c2018-10-23 18:31:18 -0700323func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil }
324func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800325func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return }