blob: 59f04ce53d000ae5f8efc466d4646a52a9dd3e9b [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"
Damien Neilc37adef2019-04-01 13:49:56 -070013 "sync/atomic"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070014
Damien Neile89e6242019-05-13 23:55:40 -070015 pref "google.golang.org/protobuf/reflect/protoreflect"
16 piface "google.golang.org/protobuf/runtime/protoiface"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070017)
18
Joe Tsai4fe96632019-05-22 05:12:36 -040019// MessageInfo provides protobuf related functionality for a given Go type
20// that represents a message. A given instance of MessageInfo is tied to
Joe Tsaic6b75612018-09-13 14:24:37 -070021// exactly one Go type, which must be a pointer to a struct type.
Joe Tsai4fe96632019-05-22 05:12:36 -040022type MessageInfo 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
Joe Tsaic0e4bb22019-07-06 13:05:11 -070031 // Exporter must be provided in a purego environment in order to provide
32 // access to unexported fields.
33 Exporter exporter
34
Joe Tsai09912272019-07-08 10:38:11 -070035 // OneofWrappers is list of pointers to oneof wrapper struct types.
36 OneofWrappers []interface{}
37
Damien Neilc37adef2019-04-01 13:49:56 -070038 initMu sync.Mutex // protects all unexported fields
39 initDone uint32
Joe Tsaic6b75612018-09-13 14:24:37 -070040
Joe Tsai82760ce2019-06-20 03:09:57 -070041 reflectMessageInfo
Joe Tsai378c1322019-04-25 23:48:08 -070042
Damien Neil4ae30bb2019-06-20 10:12:23 -070043 // Information used by the fast-path methods.
Joe Tsai4a539f42019-06-17 12:30:25 -070044 methods piface.Methods
Damien Neil4ae30bb2019-06-20 10:12:23 -070045 coderMessageInfo
Damien Neilc37adef2019-04-01 13:49:56 -070046
Damien Neilc37adef2019-04-01 13:49:56 -070047 extensionFieldInfosMu sync.RWMutex
Joe Tsai89d49632019-06-04 16:20:00 -070048 extensionFieldInfos map[pref.ExtensionType]*extensionFieldInfo
Damien Neilc37adef2019-04-01 13:49:56 -070049}
50
Joe Tsai82760ce2019-06-20 03:09:57 -070051type reflectMessageInfo struct {
52 fields map[pref.FieldNumber]*fieldInfo
53 oneofs map[pref.Name]*oneofInfo
54
55 getUnknown func(pointer) pref.RawFields
56 setUnknown func(pointer, pref.RawFields)
57 extensionMap func(pointer) *extensionMap
58
59 nilMessage atomicNilMessage
60}
61
Joe Tsaic0e4bb22019-07-06 13:05:11 -070062// exporter is a function that returns a reference to the ith field of v,
63// where v is a pointer to a struct. It returns nil if it does not support
64// exporting the requested field (e.g., already exported).
65type exporter func(v interface{}, i int) interface{}
66
Damien Neilc37adef2019-04-01 13:49:56 -070067var prefMessageType = reflect.TypeOf((*pref.Message)(nil)).Elem()
68
Joe Tsai070c1012019-07-26 23:45:58 -070069// getMessageInfo returns the MessageInfo for any message type that
70// is generated by our implementation of protoc-gen-go (for v2 and on).
71// If it is unable to obtain a MessageInfo, it returns nil.
72func getMessageInfo(mt reflect.Type) *MessageInfo {
73 m, ok := reflect.Zero(mt).Interface().(pref.ProtoMessage)
Damien Neilc37adef2019-04-01 13:49:56 -070074 if !ok {
Joe Tsai070c1012019-07-26 23:45:58 -070075 return nil
Damien Neilc37adef2019-04-01 13:49:56 -070076 }
Joe Tsai070c1012019-07-26 23:45:58 -070077 mr, ok := m.ProtoReflect().(interface{ ProtoMessageInfo() *MessageInfo })
Damien Neilc37adef2019-04-01 13:49:56 -070078 if !ok {
Joe Tsai070c1012019-07-26 23:45:58 -070079 return nil
Damien Neilc37adef2019-04-01 13:49:56 -070080 }
Joe Tsai070c1012019-07-26 23:45:58 -070081 return mr.ProtoMessageInfo()
Joe Tsaifa02f4e2018-09-12 16:20:37 -070082}
83
Joe Tsai4fe96632019-05-22 05:12:36 -040084func (mi *MessageInfo) init() {
Damien Neilc37adef2019-04-01 13:49:56 -070085 // This function is called in the hot path. Inline the sync.Once
86 // logic, since allocating a closure for Once.Do is expensive.
87 // Keep init small to ensure that it can be inlined.
Joe Tsai82760ce2019-06-20 03:09:57 -070088 if atomic.LoadUint32(&mi.initDone) == 0 {
89 mi.initOnce()
Damien Neilc37adef2019-04-01 13:49:56 -070090 }
Damien Neilc37adef2019-04-01 13:49:56 -070091}
Joe Tsaic6b75612018-09-13 14:24:37 -070092
Joe Tsai4fe96632019-05-22 05:12:36 -040093func (mi *MessageInfo) initOnce() {
Damien Neilc37adef2019-04-01 13:49:56 -070094 mi.initMu.Lock()
95 defer mi.initMu.Unlock()
96 if mi.initDone == 1 {
97 return
98 }
99
100 t := mi.GoType
101 if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
102 panic(fmt.Sprintf("got %v, want *struct kind", t))
103 }
104
105 si := mi.makeStructInfo(t.Elem())
106 mi.makeKnownFieldsFunc(si)
Joe Tsai93fd9682019-07-06 13:02:14 -0700107 mi.makeUnknownFieldsFunc(t.Elem(), si)
108 mi.makeExtensionFieldsFunc(t.Elem(), si)
Damien Neil4ae30bb2019-06-20 10:12:23 -0700109 mi.makeMethods(t.Elem(), si)
Damien Neilc37adef2019-04-01 13:49:56 -0700110
111 atomic.StoreUint32(&mi.initDone, 1)
112}
113
Joe Tsai378c1322019-04-25 23:48:08 -0700114type (
115 SizeCache = int32
Joe Tsai3d8e3692019-04-08 13:52:14 -0700116 WeakFields = map[int32]piface.MessageV1
Joe Tsai378c1322019-04-25 23:48:08 -0700117 UnknownFields = []byte
118 ExtensionFields = map[int32]ExtensionField
119)
120
121var (
122 sizecacheType = reflect.TypeOf(SizeCache(0))
Joe Tsai3d8e3692019-04-08 13:52:14 -0700123 weakFieldsType = reflect.TypeOf(WeakFields(nil))
Joe Tsai378c1322019-04-25 23:48:08 -0700124 unknownFieldsType = reflect.TypeOf(UnknownFields(nil))
125 extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
126)
Damien Neilc37adef2019-04-01 13:49:56 -0700127
Damien Neil3eaddf02019-05-09 11:33:55 -0700128type structInfo struct {
Joe Tsai93fd9682019-07-06 13:02:14 -0700129 sizecacheOffset offset
Joe Tsai3d8e3692019-04-08 13:52:14 -0700130 weakOffset offset
Joe Tsai93fd9682019-07-06 13:02:14 -0700131 unknownOffset offset
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700132 extensionOffset offset
Joe Tsai93fd9682019-07-06 13:02:14 -0700133
Damien Neil3eaddf02019-05-09 11:33:55 -0700134 fieldsByNumber map[pref.FieldNumber]reflect.StructField
135 oneofsByName map[pref.Name]reflect.StructField
136 oneofWrappersByType map[reflect.Type]pref.FieldNumber
137 oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
138}
139
Joe Tsai4fe96632019-05-22 05:12:36 -0400140func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
Damien Neil3eaddf02019-05-09 11:33:55 -0700141 si := structInfo{
Joe Tsai93fd9682019-07-06 13:02:14 -0700142 sizecacheOffset: invalidOffset,
Joe Tsai3d8e3692019-04-08 13:52:14 -0700143 weakOffset: invalidOffset,
Joe Tsai93fd9682019-07-06 13:02:14 -0700144 unknownOffset: invalidOffset,
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700145 extensionOffset: invalidOffset,
Joe Tsai93fd9682019-07-06 13:02:14 -0700146
Damien Neil3eaddf02019-05-09 11:33:55 -0700147 fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
148 oneofsByName: map[pref.Name]reflect.StructField{},
149 oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
150 oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
151 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700152
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700153 if f, _ := t.FieldByName("sizeCache"); f.Type == sizecacheType {
154 si.sizecacheOffset = offsetOf(f, mi.Exporter)
155 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700156 if f, _ := t.FieldByName("XXX_sizecache"); f.Type == sizecacheType {
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700157 si.sizecacheOffset = offsetOf(f, mi.Exporter)
Joe Tsai93fd9682019-07-06 13:02:14 -0700158 }
Joe Tsai3d8e3692019-04-08 13:52:14 -0700159 if f, _ := t.FieldByName("XXX_weak"); f.Type == weakFieldsType {
160 si.weakOffset = offsetOf(f, mi.Exporter)
161 }
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700162 if f, _ := t.FieldByName("unknownFields"); f.Type == unknownFieldsType {
163 si.unknownOffset = offsetOf(f, mi.Exporter)
Joe Tsai93fd9682019-07-06 13:02:14 -0700164 }
165 if f, _ := t.FieldByName("XXX_unrecognized"); f.Type == unknownFieldsType {
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700166 si.unknownOffset = offsetOf(f, mi.Exporter)
167 }
168 if f, _ := t.FieldByName("extensionFields"); f.Type == extensionFieldsType {
169 si.extensionOffset = offsetOf(f, mi.Exporter)
170 }
171 if f, _ := t.FieldByName("XXX_InternalExtensions"); f.Type == extensionFieldsType {
172 si.extensionOffset = offsetOf(f, mi.Exporter)
173 }
174 if f, _ := t.FieldByName("XXX_extensions"); f.Type == extensionFieldsType {
175 si.extensionOffset = offsetOf(f, mi.Exporter)
Joe Tsai93fd9682019-07-06 13:02:14 -0700176 }
177
178 // Generate a mapping of field numbers and names to Go struct field or type.
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700179fieldLoop:
180 for i := 0; i < t.NumField(); i++ {
181 f := t.Field(i)
182 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
183 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
184 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700185 si.fieldsByNumber[pref.FieldNumber(n)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700186 continue fieldLoop
187 }
188 }
189 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
Damien Neil3eaddf02019-05-09 11:33:55 -0700190 si.oneofsByName[pref.Name(s)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700191 continue fieldLoop
192 }
193 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700194
195 // Derive a mapping of oneof wrappers to fields.
Joe Tsai09912272019-07-08 10:38:11 -0700196 oneofWrappers := mi.OneofWrappers
Joe Tsai2c870bb2018-10-17 11:46:52 -0700197 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800198 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
199 }
200 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
201 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
202 }
203 for _, v := range oneofWrappers {
204 tf := reflect.TypeOf(v).Elem()
205 f := tf.Field(0)
206 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
207 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
208 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700209 si.oneofWrappersByType[tf] = pref.FieldNumber(n)
210 si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800211 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700212 }
213 }
214 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700215
Damien Neil3eaddf02019-05-09 11:33:55 -0700216 return si
217}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700218
Damien Neil3eaddf02019-05-09 11:33:55 -0700219// makeKnownFieldsFunc generates functions for operations that can be performed
220// on each protobuf message field. It takes in a reflect.Type representing the
221// Go struct and matches message fields with struct fields.
222//
223// This code assumes that the struct is well-formed and panics if there are
224// any discrepancies.
Joe Tsai4fe96632019-05-22 05:12:36 -0400225func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700226 mi.fields = map[pref.FieldNumber]*fieldInfo{}
Joe Tsaid4211502019-07-02 14:58:02 -0700227 for i := 0; i < mi.PBType.Fields().Len(); i++ {
228 fd := mi.PBType.Fields().Get(i)
Damien Neil3eaddf02019-05-09 11:33:55 -0700229 fs := si.fieldsByNumber[fd.Number()]
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700230 var fi fieldInfo
231 switch {
Joe Tsaiac31a352019-05-13 14:32:56 -0700232 case fd.ContainingOneof() != nil:
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700233 fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], mi.Exporter, si.oneofWrappersByNumber[fd.Number()])
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700234 case fd.IsMap():
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700235 fi = fieldInfoForMap(fd, fs, mi.Exporter)
Joe Tsaiac31a352019-05-13 14:32:56 -0700236 case fd.IsList():
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700237 fi = fieldInfoForList(fd, fs, mi.Exporter)
Joe Tsai3d8e3692019-04-08 13:52:14 -0700238 case fd.IsWeak():
239 fi = fieldInfoForWeakMessage(fd, si.weakOffset)
Joe Tsaic6b75612018-09-13 14:24:37 -0700240 case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700241 fi = fieldInfoForMessage(fd, fs, mi.Exporter)
Joe Tsaic6b75612018-09-13 14:24:37 -0700242 default:
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700243 fi = fieldInfoForScalar(fd, fs, mi.Exporter)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700244 }
245 mi.fields[fd.Number()] = &fi
246 }
Joe Tsai4ec39c72019-04-03 13:40:53 -0700247
248 mi.oneofs = map[pref.Name]*oneofInfo{}
Joe Tsaid4211502019-07-02 14:58:02 -0700249 for i := 0; i < mi.PBType.Oneofs().Len(); i++ {
250 od := mi.PBType.Oneofs().Get(i)
Joe Tsaic0e4bb22019-07-06 13:05:11 -0700251 mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], mi.Exporter, si.oneofWrappersByType)
Joe Tsai4ec39c72019-04-03 13:40:53 -0700252 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700253}
Joe Tsaic6b75612018-09-13 14:24:37 -0700254
Joe Tsai93fd9682019-07-06 13:02:14 -0700255func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) {
Joe Tsai378c1322019-04-25 23:48:08 -0700256 mi.getUnknown = func(pointer) pref.RawFields { return nil }
257 mi.setUnknown = func(pointer, pref.RawFields) { return }
Joe Tsai93fd9682019-07-06 13:02:14 -0700258 if si.unknownOffset.IsValid() {
Joe Tsai378c1322019-04-25 23:48:08 -0700259 mi.getUnknown = func(p pointer) pref.RawFields {
260 if p.IsNil() {
261 return nil
262 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700263 rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
Joe Tsai378c1322019-04-25 23:48:08 -0700264 return pref.RawFields(*rv.Interface().(*[]byte))
265 }
266 mi.setUnknown = func(p pointer, b pref.RawFields) {
267 if p.IsNil() {
268 panic("invalid SetUnknown on nil Message")
269 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700270 rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
Joe Tsai378c1322019-04-25 23:48:08 -0700271 *rv.Interface().(*[]byte) = []byte(b)
272 }
273 } else {
274 mi.getUnknown = func(pointer) pref.RawFields {
275 return nil
276 }
277 mi.setUnknown = func(p pointer, _ pref.RawFields) {
278 if p.IsNil() {
279 panic("invalid SetUnknown on nil Message")
280 }
281 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700282 }
283}
284
Joe Tsai93fd9682019-07-06 13:02:14 -0700285func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type, si structInfo) {
286 if si.extensionOffset.IsValid() {
Joe Tsai378c1322019-04-25 23:48:08 -0700287 mi.extensionMap = func(p pointer) *extensionMap {
Joe Tsaid8881392019-06-06 13:01:53 -0700288 if p.IsNil() {
289 return (*extensionMap)(nil)
290 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700291 v := p.Apply(si.extensionOffset).AsValueOf(extensionFieldsType)
Joe Tsai378c1322019-04-25 23:48:08 -0700292 return (*extensionMap)(v.Interface().(*map[int32]ExtensionField))
293 }
294 } else {
295 mi.extensionMap = func(pointer) *extensionMap {
296 return (*extensionMap)(nil)
297 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700298 }
299}