blob: 3d94a94e3fc568513c10c9a81ef128c1b7e20dd5 [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"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070016)
17
Joe Tsaic6b75612018-09-13 14:24:37 -070018// MessageType provides protobuf related functionality for a given Go type
19// that represents a message. A given instance of MessageType is tied to
20// exactly one Go type, which must be a pointer to a struct type.
21type MessageType struct {
Joe Tsaif0c01e42018-11-06 13:05:20 -080022 // Type is the underlying message type and must be populated.
Joe Tsaic6b75612018-09-13 14:24:37 -070023 // Once set, this field must never be mutated.
Joe Tsaif0c01e42018-11-06 13:05:20 -080024 Type pref.MessageType
Joe Tsaic6b75612018-09-13 14:24:37 -070025
26 once sync.Once // protects all unexported fields
27
Joe Tsaif0c01e42018-11-06 13:05:20 -080028 goType reflect.Type // pointer to struct
Joe Tsaic6b75612018-09-13 14:24:37 -070029
Joe Tsaifa02f4e2018-09-12 16:20:37 -070030 // TODO: Split fields into dense and sparse maps similar to the current
31 // table-driven implementation in v1?
32 fields map[pref.FieldNumber]*fieldInfo
Joe Tsaibe5348c2018-10-23 18:31:18 -070033
34 unknownFields func(*messageDataType) pref.UnknownFields
35 extensionFields func(*messageDataType) pref.KnownFields
Joe Tsaifa02f4e2018-09-12 16:20:37 -070036}
37
Joe Tsaic6b75612018-09-13 14:24:37 -070038// 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.
42func (mi *MessageType) init(p interface{}) {
43 mi.once.Do(func() {
Joe Tsaif0c01e42018-11-06 13:05:20 -080044 t := reflect.TypeOf(p)
Joe Tsaic6b75612018-09-13 14:24:37 -070045 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
Joe Tsai95b02902018-10-31 18:23:42 -070050 mi.makeKnownFieldsFunc(t.Elem())
51 mi.makeUnknownFieldsFunc(t.Elem())
52 mi.makeExtensionFieldsFunc(t.Elem())
Joe Tsaic6b75612018-09-13 14:24:37 -070053 })
54
55 // TODO: Remove this check? This API is primarily used by generated code,
56 // and should not violate this assumption. Leave this check in for now to
57 // provide some sanity checks during development. This can be removed if
58 // it proves to be detrimental to performance.
59 if reflect.TypeOf(p) != mi.goType {
60 panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.goType))
61 }
62}
63
Joe Tsaif0c01e42018-11-06 13:05:20 -080064// makeKnownFieldsFunc generates functions for operations that can be performed
65// on each protobuf message field. It takes in a reflect.Type representing the
66// Go struct and matches message fields with struct fields.
Joe Tsaifa02f4e2018-09-12 16:20:37 -070067//
68// This code assumes that the struct is well-formed and panics if there are
69// any discrepancies.
Joe Tsai95b02902018-10-31 18:23:42 -070070func (mi *MessageType) makeKnownFieldsFunc(t reflect.Type) {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070071 // Generate a mapping of field numbers and names to Go struct field or type.
72 fields := map[pref.FieldNumber]reflect.StructField{}
73 oneofs := map[pref.Name]reflect.StructField{}
74 oneofFields := map[pref.FieldNumber]reflect.Type{}
75 special := map[string]reflect.StructField{}
76fieldLoop:
77 for i := 0; i < t.NumField(); i++ {
78 f := t.Field(i)
79 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
80 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
81 n, _ := strconv.ParseUint(s, 10, 64)
82 fields[pref.FieldNumber(n)] = f
83 continue fieldLoop
84 }
85 }
86 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
87 oneofs[pref.Name(s)] = f
88 continue fieldLoop
89 }
90 switch f.Name {
91 case "XXX_weak", "XXX_unrecognized", "XXX_sizecache", "XXX_extensions", "XXX_InternalExtensions":
92 special[f.Name] = f
93 continue fieldLoop
94 }
95 }
Joe Tsaid7e97bc2018-11-26 12:57:27 -080096 var oneofWrappers []interface{}
Joe Tsai2c870bb2018-10-17 11:46:52 -070097 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
Joe Tsaid7e97bc2018-11-26 12:57:27 -080098 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
99 }
100 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
101 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
102 }
103 for _, v := range oneofWrappers {
104 tf := reflect.TypeOf(v).Elem()
105 f := tf.Field(0)
106 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
107 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
108 n, _ := strconv.ParseUint(s, 10, 64)
109 oneofFields[pref.FieldNumber(n)] = tf
110 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700111 }
112 }
113 }
114
115 mi.fields = map[pref.FieldNumber]*fieldInfo{}
Joe Tsaif0c01e42018-11-06 13:05:20 -0800116 for i := 0; i < mi.Type.Fields().Len(); i++ {
117 fd := mi.Type.Fields().Get(i)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700118 fs := fields[fd.Number()]
119 var fi fieldInfo
120 switch {
121 case fd.IsWeak():
122 fi = fieldInfoForWeak(fd, special["XXX_weak"])
123 case fd.OneofType() != nil:
124 fi = fieldInfoForOneof(fd, oneofs[fd.OneofType().Name()], oneofFields[fd.Number()])
125 case fd.IsMap():
126 fi = fieldInfoForMap(fd, fs)
127 case fd.Cardinality() == pref.Repeated:
Joe Tsai4b7aff62018-11-14 14:05:19 -0800128 fi = fieldInfoForList(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700129 case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700130 fi = fieldInfoForMessage(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700131 default:
132 fi = fieldInfoForScalar(fd, fs)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700133 }
134 mi.fields[fd.Number()] = &fi
135 }
136}
Joe Tsaic6b75612018-09-13 14:24:37 -0700137
Joe Tsai95b02902018-10-31 18:23:42 -0700138func (mi *MessageType) makeUnknownFieldsFunc(t reflect.Type) {
139 if f := makeLegacyUnknownFieldsFunc(t); f != nil {
Joe Tsaie2afdc22018-10-25 14:06:56 -0700140 mi.unknownFields = f
141 return
142 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700143 mi.unknownFields = func(*messageDataType) pref.UnknownFields {
144 return emptyUnknownFields{}
145 }
146}
147
Joe Tsai95b02902018-10-31 18:23:42 -0700148func (mi *MessageType) makeExtensionFieldsFunc(t reflect.Type) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800149 if f := makeLegacyExtensionFieldsFunc(t); f != nil {
150 mi.extensionFields = f
151 return
152 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700153 mi.extensionFields = func(*messageDataType) pref.KnownFields {
154 return emptyExtensionFields{}
155 }
156}
157
Joe Tsai08e00302018-11-26 22:32:06 -0800158func (mi *MessageType) MessageOf(p interface{}) pref.Message {
159 return (*messageWrapper)(mi.dataTypeOf(p))
160}
161
Joe Tsaic6b75612018-09-13 14:24:37 -0700162func (mi *MessageType) KnownFieldsOf(p interface{}) pref.KnownFields {
Joe Tsaic6b75612018-09-13 14:24:37 -0700163 return (*knownFields)(mi.dataTypeOf(p))
164}
165
166func (mi *MessageType) UnknownFieldsOf(p interface{}) pref.UnknownFields {
Joe Tsaibe5348c2018-10-23 18:31:18 -0700167 return mi.unknownFields(mi.dataTypeOf(p))
Joe Tsaic6b75612018-09-13 14:24:37 -0700168}
169
170func (mi *MessageType) dataTypeOf(p interface{}) *messageDataType {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800171 mi.init(p)
Joe Tsai6cf80c42018-12-01 04:57:09 -0800172 return &messageDataType{pointerOfIface(p), mi}
Joe Tsaic6b75612018-09-13 14:24:37 -0700173}
174
175// messageDataType is a tuple of a pointer to the message data and
176// a pointer to the message type.
177//
178// TODO: Unfortunately, we need to close over a pointer and MessageType,
179// which incurs an an allocation. This pair is similar to a Go interface,
180// which is essentially a tuple of the same thing. We can make this efficient
181// with reflect.NamedOf (see https://golang.org/issues/16522).
182//
183// With that hypothetical API, we could dynamically create a new named type
184// that has the same underlying type as MessageType.goType, and
185// dynamically create methods that close over MessageType.
186// Since the new type would have the same underlying type, we could directly
187// convert between pointers of those types, giving us an efficient way to swap
188// out the method set.
189//
190// Barring the ability to dynamically create named types, the workaround is
191// 1. either to accept the cost of an allocation for this wrapper struct or
192// 2. generate more types and methods, at the expense of binary size increase.
193type messageDataType struct {
194 p pointer
195 mi *MessageType
196}
197
Joe Tsai08e00302018-11-26 22:32:06 -0800198type messageWrapper messageDataType
199
200func (m *messageWrapper) Type() pref.MessageType {
201 return m.mi.Type
202}
203func (m *messageWrapper) KnownFields() pref.KnownFields {
204 return (*knownFields)(m)
205}
206func (m *messageWrapper) UnknownFields() pref.UnknownFields {
207 return m.mi.unknownFields((*messageDataType)(m))
208}
209func (m *messageWrapper) Interface() pref.ProtoMessage {
210 if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
211 return m
212 }
213 return m
214}
215func (m *messageWrapper) ProtoReflect() pref.Message {
216 return m
217}
218func (m *messageWrapper) ProtoUnwrap() interface{} {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800219 return m.p.AsIfaceOf(m.mi.goType.Elem())
Joe Tsai08e00302018-11-26 22:32:06 -0800220}
221func (m *messageWrapper) ProtoMutable() {}
222
223var _ pvalue.Unwrapper = (*messageWrapper)(nil)
224
Joe Tsaic6b75612018-09-13 14:24:37 -0700225type knownFields messageDataType
226
Joe Tsaic6b75612018-09-13 14:24:37 -0700227func (fs *knownFields) Len() (cnt int) {
228 for _, fi := range fs.mi.fields {
229 if fi.has(fs.p) {
230 cnt++
231 }
232 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700233 return cnt + fs.extensionFields().Len()
Joe Tsaic6b75612018-09-13 14:24:37 -0700234}
235func (fs *knownFields) Has(n pref.FieldNumber) bool {
236 if fi := fs.mi.fields[n]; fi != nil {
237 return fi.has(fs.p)
238 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700239 return fs.extensionFields().Has(n)
Joe Tsaic6b75612018-09-13 14:24:37 -0700240}
241func (fs *knownFields) Get(n pref.FieldNumber) pref.Value {
242 if fi := fs.mi.fields[n]; fi != nil {
243 return fi.get(fs.p)
244 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700245 return fs.extensionFields().Get(n)
Joe Tsaic6b75612018-09-13 14:24:37 -0700246}
247func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) {
248 if fi := fs.mi.fields[n]; fi != nil {
249 fi.set(fs.p, v)
250 return
251 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800252 if fs.mi.Type.ExtensionRanges().Has(n) {
253 fs.extensionFields().Set(n, v)
254 return
255 }
256 panic(fmt.Sprintf("invalid field: %d", n))
Joe Tsaic6b75612018-09-13 14:24:37 -0700257}
258func (fs *knownFields) Clear(n pref.FieldNumber) {
259 if fi := fs.mi.fields[n]; fi != nil {
260 fi.clear(fs.p)
261 return
262 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800263 if fs.mi.Type.ExtensionRanges().Has(n) {
264 fs.extensionFields().Clear(n)
265 return
266 }
Joe Tsaic6b75612018-09-13 14:24:37 -0700267}
Joe Tsaic6b75612018-09-13 14:24:37 -0700268func (fs *knownFields) Range(f func(pref.FieldNumber, pref.Value) bool) {
269 for n, fi := range fs.mi.fields {
270 if fi.has(fs.p) {
271 if !f(n, fi.get(fs.p)) {
272 return
273 }
274 }
275 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700276 fs.extensionFields().Range(f)
Joe Tsaic6b75612018-09-13 14:24:37 -0700277}
Damien Neil97e7f572018-12-07 14:28:33 -0800278func (fs *knownFields) NewMessage(n pref.FieldNumber) pref.ProtoMessage {
279 if fi := fs.mi.fields[n]; fi != nil {
280 return fi.newMessage().Interface()
281 }
282 if fs.mi.Type.ExtensionRanges().Has(n) {
283 return fs.extensionFields().NewMessage(n)
284 }
285 panic(fmt.Sprintf("invalid field: %d", n))
286}
Joe Tsaic6b75612018-09-13 14:24:37 -0700287func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes {
Joe Tsaibe5348c2018-10-23 18:31:18 -0700288 return fs.extensionFields().ExtensionTypes()
289}
290func (fs *knownFields) extensionFields() pref.KnownFields {
291 return fs.mi.extensionFields((*messageDataType)(fs))
Joe Tsaic6b75612018-09-13 14:24:37 -0700292}
293
Joe Tsaibe5348c2018-10-23 18:31:18 -0700294type emptyUnknownFields struct{}
Joe Tsaic6b75612018-09-13 14:24:37 -0700295
Joe Tsaibe5348c2018-10-23 18:31:18 -0700296func (emptyUnknownFields) Len() int { return 0 }
297func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800298func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop
299func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700300func (emptyUnknownFields) IsSupported() bool { return false }
Joe Tsaic6b75612018-09-13 14:24:37 -0700301
Joe Tsaibe5348c2018-10-23 18:31:18 -0700302type emptyExtensionFields struct{}
Joe Tsaic6b75612018-09-13 14:24:37 -0700303
Joe Tsaif0c01e42018-11-06 13:05:20 -0800304func (emptyExtensionFields) Len() int { return 0 }
305func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false }
306func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} }
307func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") }
308func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop
Joe Tsaif0c01e42018-11-06 13:05:20 -0800309func (emptyExtensionFields) Range(func(pref.FieldNumber, pref.Value) bool) { return }
Damien Neil97e7f572018-12-07 14:28:33 -0800310func (emptyExtensionFields) NewMessage(pref.FieldNumber) pref.ProtoMessage {
311 panic("extensions not supported")
312}
313func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700314
315type emptyExtensionTypes struct{}
316
317func (emptyExtensionTypes) Len() int { return 0 }
318func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800319func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop
Joe Tsaibe5348c2018-10-23 18:31:18 -0700320func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil }
321func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800322func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return }