blob: 161f1941ef800722add26f4d76f3780d3b60f9a5 [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"
Damien Neilc37adef2019-04-01 13:49:56 -070010 "sort"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070011 "strconv"
12 "strings"
Joe Tsaic6b75612018-09-13 14:24:37 -070013 "sync"
Damien Neilc37adef2019-04-01 13:49:56 -070014 "sync/atomic"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070015
Damien Neile89e6242019-05-13 23:55:40 -070016 pvalue "google.golang.org/protobuf/internal/value"
17 pref "google.golang.org/protobuf/reflect/protoreflect"
18 piface "google.golang.org/protobuf/runtime/protoiface"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070019)
20
Joe Tsai4fe96632019-05-22 05:12:36 -040021// MessageInfo provides protobuf related functionality for a given Go type
22// that represents a message. A given instance of MessageInfo is tied to
Joe Tsaic6b75612018-09-13 14:24:37 -070023// exactly one Go type, which must be a pointer to a struct type.
Joe Tsai4fe96632019-05-22 05:12:36 -040024type MessageInfo struct {
Damien Neil8012b442019-01-18 09:32:24 -080025 // GoType is the underlying message Go type and must be populated.
Joe Tsaic6b75612018-09-13 14:24:37 -070026 // Once set, this field must never be mutated.
Damien Neil8012b442019-01-18 09:32:24 -080027 GoType reflect.Type // pointer to struct
28
29 // PBType is the underlying message descriptor type and must be populated.
30 // Once set, this field must never be mutated.
31 PBType pref.MessageType
Joe Tsaic6b75612018-09-13 14:24:37 -070032
Damien Neilc37adef2019-04-01 13:49:56 -070033 initMu sync.Mutex // protects all unexported fields
34 initDone uint32
Joe Tsaic6b75612018-09-13 14:24:37 -070035
Damien Neilc37adef2019-04-01 13:49:56 -070036 // Keep a separate slice of fields for efficient field encoding in tag order
37 // and because iterating over a slice is substantially faster than a map.
38 fields map[pref.FieldNumber]*fieldInfo
39 fieldsOrdered []*fieldInfo
40
Joe Tsai4ec39c72019-04-03 13:40:53 -070041 oneofs map[pref.Name]*oneofInfo
Joe Tsaibe5348c2018-10-23 18:31:18 -070042
Joe Tsai378c1322019-04-25 23:48:08 -070043 getUnknown func(pointer) pref.RawFields
44 setUnknown func(pointer, pref.RawFields)
45
46 extensionMap func(pointer) *extensionMap
47
Joe Tsai4a539f42019-06-17 12:30:25 -070048 methods piface.Methods
Damien Neilc37adef2019-04-01 13:49:56 -070049
Damien Neilc37adef2019-04-01 13:49:56 -070050 sizecacheOffset offset
Joe Tsai378c1322019-04-25 23:48:08 -070051 extensionOffset offset
Damien Neilc37adef2019-04-01 13:49:56 -070052 unknownOffset offset
53 extensionFieldInfosMu sync.RWMutex
Joe Tsai89d49632019-06-04 16:20:00 -070054 extensionFieldInfos map[pref.ExtensionType]*extensionFieldInfo
Damien Neilc37adef2019-04-01 13:49:56 -070055}
56
57var prefMessageType = reflect.TypeOf((*pref.Message)(nil)).Elem()
58
Joe Tsai4fe96632019-05-22 05:12:36 -040059// getMessageInfo returns the MessageInfo (if any) for a type.
Damien Neilc37adef2019-04-01 13:49:56 -070060//
Joe Tsai4fe96632019-05-22 05:12:36 -040061// We find the MessageInfo by calling the ProtoReflect method on the type's
Damien Neilc37adef2019-04-01 13:49:56 -070062// zero value and looking at the returned type to see if it is a
Joe Tsai4fe96632019-05-22 05:12:36 -040063// messageReflectWrapper. Note that the MessageInfo may still be uninitialized
Damien Neilc37adef2019-04-01 13:49:56 -070064// at this point.
Joe Tsai4fe96632019-05-22 05:12:36 -040065func getMessageInfo(mt reflect.Type) (mi *MessageInfo, ok bool) {
Damien Neilc37adef2019-04-01 13:49:56 -070066 method, ok := mt.MethodByName("ProtoReflect")
67 if !ok {
68 return nil, false
69 }
70 if method.Type.NumIn() != 1 || method.Type.NumOut() != 1 || method.Type.Out(0) != prefMessageType {
71 return nil, false
72 }
73 ret := reflect.Zero(mt).Method(method.Index).Call(nil)
74 m, ok := ret[0].Elem().Interface().(*messageReflectWrapper)
75 if !ok {
76 return nil, ok
77 }
78 return m.mi, true
Joe Tsaifa02f4e2018-09-12 16:20:37 -070079}
80
Joe Tsai4fe96632019-05-22 05:12:36 -040081func (mi *MessageInfo) init() {
Damien Neilc37adef2019-04-01 13:49:56 -070082 // This function is called in the hot path. Inline the sync.Once
83 // logic, since allocating a closure for Once.Do is expensive.
84 // Keep init small to ensure that it can be inlined.
85 if atomic.LoadUint32(&mi.initDone) == 1 {
86 return
87 }
88 mi.initOnce()
89}
Joe Tsaic6b75612018-09-13 14:24:37 -070090
Joe Tsai4fe96632019-05-22 05:12:36 -040091func (mi *MessageInfo) initOnce() {
Damien Neilc37adef2019-04-01 13:49:56 -070092 mi.initMu.Lock()
93 defer mi.initMu.Unlock()
94 if mi.initDone == 1 {
95 return
96 }
97
98 t := mi.GoType
99 if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
100 panic(fmt.Sprintf("got %v, want *struct kind", t))
101 }
102
103 si := mi.makeStructInfo(t.Elem())
104 mi.makeKnownFieldsFunc(si)
105 mi.makeUnknownFieldsFunc(t.Elem())
106 mi.makeExtensionFieldsFunc(t.Elem())
107 mi.makeMethods(t.Elem())
108
109 atomic.StoreUint32(&mi.initDone, 1)
110}
111
Joe Tsai378c1322019-04-25 23:48:08 -0700112type (
113 SizeCache = int32
114 UnknownFields = []byte
115 ExtensionFields = map[int32]ExtensionField
116)
117
118var (
119 sizecacheType = reflect.TypeOf(SizeCache(0))
120 unknownFieldsType = reflect.TypeOf(UnknownFields(nil))
121 extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
122)
Damien Neilc37adef2019-04-01 13:49:56 -0700123
Joe Tsai4fe96632019-05-22 05:12:36 -0400124func (mi *MessageInfo) makeMethods(t reflect.Type) {
Damien Neilc37adef2019-04-01 13:49:56 -0700125 mi.sizecacheOffset = invalidOffset
126 if fx, _ := t.FieldByName("XXX_sizecache"); fx.Type == sizecacheType {
127 mi.sizecacheOffset = offsetOf(fx)
128 }
129 mi.unknownOffset = invalidOffset
Joe Tsai378c1322019-04-25 23:48:08 -0700130 if fx, _ := t.FieldByName("XXX_unrecognized"); fx.Type == unknownFieldsType {
Damien Neilc37adef2019-04-01 13:49:56 -0700131 mi.unknownOffset = offsetOf(fx)
132 }
Joe Tsai378c1322019-04-25 23:48:08 -0700133 mi.extensionOffset = invalidOffset
134 if fx, _ := t.FieldByName("XXX_InternalExtensions"); fx.Type == extensionFieldsType {
135 mi.extensionOffset = offsetOf(fx)
136 } else if fx, _ = t.FieldByName("XXX_extensions"); fx.Type == extensionFieldsType {
137 mi.extensionOffset = offsetOf(fx)
138 }
Damien Neilc37adef2019-04-01 13:49:56 -0700139 mi.methods.Flags = piface.MethodFlagDeterministicMarshal
140 mi.methods.MarshalAppend = mi.marshalAppend
141 mi.methods.Size = mi.size
Joe Tsaic6b75612018-09-13 14:24:37 -0700142}
143
Damien Neil3eaddf02019-05-09 11:33:55 -0700144type structInfo struct {
145 fieldsByNumber map[pref.FieldNumber]reflect.StructField
146 oneofsByName map[pref.Name]reflect.StructField
147 oneofWrappersByType map[reflect.Type]pref.FieldNumber
148 oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
149}
150
Joe Tsai4fe96632019-05-22 05:12:36 -0400151func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700152 // Generate a mapping of field numbers and names to Go struct field or type.
Damien Neil3eaddf02019-05-09 11:33:55 -0700153 si := structInfo{
154 fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
155 oneofsByName: map[pref.Name]reflect.StructField{},
156 oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
157 oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
158 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700159fieldLoop:
160 for i := 0; i < t.NumField(); i++ {
161 f := t.Field(i)
162 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
163 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
164 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700165 si.fieldsByNumber[pref.FieldNumber(n)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700166 continue fieldLoop
167 }
168 }
169 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
Damien Neil3eaddf02019-05-09 11:33:55 -0700170 si.oneofsByName[pref.Name(s)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700171 continue fieldLoop
172 }
173 }
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800174 var oneofWrappers []interface{}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700175 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800176 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
177 }
178 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
179 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
180 }
181 for _, v := range oneofWrappers {
182 tf := reflect.TypeOf(v).Elem()
183 f := tf.Field(0)
184 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
185 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
186 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700187 si.oneofWrappersByType[tf] = pref.FieldNumber(n)
188 si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800189 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700190 }
191 }
192 }
Damien Neil3eaddf02019-05-09 11:33:55 -0700193 return si
194}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700195
Damien Neil3eaddf02019-05-09 11:33:55 -0700196// makeKnownFieldsFunc generates functions for operations that can be performed
197// on each protobuf message field. It takes in a reflect.Type representing the
198// Go struct and matches message fields with struct fields.
199//
200// This code assumes that the struct is well-formed and panics if there are
201// any discrepancies.
Joe Tsai4fe96632019-05-22 05:12:36 -0400202func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700203 mi.fields = map[pref.FieldNumber]*fieldInfo{}
Damien Neilc37adef2019-04-01 13:49:56 -0700204 mi.fieldsOrdered = make([]*fieldInfo, 0, mi.PBType.Fields().Len())
Joe Tsai0fc49f82019-05-01 12:29:25 -0700205 for i := 0; i < mi.PBType.Descriptor().Fields().Len(); i++ {
206 fd := mi.PBType.Descriptor().Fields().Get(i)
Damien Neil3eaddf02019-05-09 11:33:55 -0700207 fs := si.fieldsByNumber[fd.Number()]
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700208 var fi fieldInfo
209 switch {
Joe Tsaiac31a352019-05-13 14:32:56 -0700210 case fd.ContainingOneof() != nil:
211 fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], si.oneofWrappersByNumber[fd.Number()])
Damien Neilc37adef2019-04-01 13:49:56 -0700212 // There is one fieldInfo for each proto message field, but only one struct
213 // field for all message fields in a oneof. We install the encoder functions
214 // on the fieldInfo for the first field in the oneof.
215 //
216 // A slightly simpler approach would be to have each fieldInfo's encoder
217 // handle the case where that field is set, but this would require more
218 // checks against the current oneof type than a single map lookup.
219 if fd.ContainingOneof().Fields().Get(0).Name() == fd.Name() {
220 fi.funcs = makeOneofFieldCoder(si.oneofsByName[fd.ContainingOneof().Name()], fd.ContainingOneof(), si.fieldsByNumber, si.oneofWrappersByNumber)
221 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700222 case fd.IsMap():
223 fi = fieldInfoForMap(fd, fs)
Joe Tsaiac31a352019-05-13 14:32:56 -0700224 case fd.IsList():
Joe Tsai4b7aff62018-11-14 14:05:19 -0800225 fi = fieldInfoForList(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700226 case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700227 fi = fieldInfoForMessage(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700228 default:
229 fi = fieldInfoForScalar(fd, fs)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700230 }
Damien Neilc37adef2019-04-01 13:49:56 -0700231 fi.num = fd.Number()
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700232 mi.fields[fd.Number()] = &fi
Damien Neilc37adef2019-04-01 13:49:56 -0700233 mi.fieldsOrdered = append(mi.fieldsOrdered, &fi)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700234 }
Damien Neilc37adef2019-04-01 13:49:56 -0700235 sort.Slice(mi.fieldsOrdered, func(i, j int) bool {
236 return mi.fieldsOrdered[i].num < mi.fieldsOrdered[j].num
237 })
Joe Tsai4ec39c72019-04-03 13:40:53 -0700238
239 mi.oneofs = map[pref.Name]*oneofInfo{}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700240 for i := 0; i < mi.PBType.Descriptor().Oneofs().Len(); i++ {
241 od := mi.PBType.Descriptor().Oneofs().Get(i)
Damien Neil3eaddf02019-05-09 11:33:55 -0700242 mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], si.oneofWrappersByType)
Joe Tsai4ec39c72019-04-03 13:40:53 -0700243 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700244}
Joe Tsaic6b75612018-09-13 14:24:37 -0700245
Joe Tsai4fe96632019-05-22 05:12:36 -0400246func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type) {
Joe Tsai378c1322019-04-25 23:48:08 -0700247 mi.getUnknown = func(pointer) pref.RawFields { return nil }
248 mi.setUnknown = func(pointer, pref.RawFields) { return }
249 fu, _ := t.FieldByName("XXX_unrecognized")
250 if fu.Type == unknownFieldsType {
251 fieldOffset := offsetOf(fu)
252 mi.getUnknown = func(p pointer) pref.RawFields {
253 if p.IsNil() {
254 return nil
255 }
256 rv := p.Apply(fieldOffset).AsValueOf(unknownFieldsType)
257 return pref.RawFields(*rv.Interface().(*[]byte))
258 }
259 mi.setUnknown = func(p pointer, b pref.RawFields) {
260 if p.IsNil() {
261 panic("invalid SetUnknown on nil Message")
262 }
263 rv := p.Apply(fieldOffset).AsValueOf(unknownFieldsType)
264 *rv.Interface().(*[]byte) = []byte(b)
265 }
266 } else {
267 mi.getUnknown = func(pointer) pref.RawFields {
268 return nil
269 }
270 mi.setUnknown = func(p pointer, _ pref.RawFields) {
271 if p.IsNil() {
272 panic("invalid SetUnknown on nil Message")
273 }
274 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700275 }
276}
277
Joe Tsai4fe96632019-05-22 05:12:36 -0400278func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type) {
Joe Tsai378c1322019-04-25 23:48:08 -0700279 fx, _ := t.FieldByName("XXX_extensions")
280 if fx.Type != extensionFieldsType {
281 fx, _ = t.FieldByName("XXX_InternalExtensions")
Joe Tsaif0c01e42018-11-06 13:05:20 -0800282 }
Joe Tsai378c1322019-04-25 23:48:08 -0700283 if fx.Type == extensionFieldsType {
284 fieldOffset := offsetOf(fx)
285 mi.extensionMap = func(p pointer) *extensionMap {
Joe Tsaid8881392019-06-06 13:01:53 -0700286 if p.IsNil() {
287 return (*extensionMap)(nil)
288 }
Joe Tsai378c1322019-04-25 23:48:08 -0700289 v := p.Apply(fieldOffset).AsValueOf(extensionFieldsType)
290 return (*extensionMap)(v.Interface().(*map[int32]ExtensionField))
291 }
292 } else {
293 mi.extensionMap = func(pointer) *extensionMap {
294 return (*extensionMap)(nil)
295 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700296 }
297}
298
Joe Tsai4fe96632019-05-22 05:12:36 -0400299func (mi *MessageInfo) MessageOf(p interface{}) pref.Message {
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700300 return (*messageReflectWrapper)(mi.dataTypeOf(p))
Joe Tsai08e00302018-11-26 22:32:06 -0800301}
302
Joe Tsai4fe96632019-05-22 05:12:36 -0400303func (mi *MessageInfo) Methods() *piface.Methods {
Damien Neilc37adef2019-04-01 13:49:56 -0700304 mi.init()
305 return &mi.methods
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700306}
307
Joe Tsai4fe96632019-05-22 05:12:36 -0400308func (mi *MessageInfo) dataTypeOf(p interface{}) *messageDataType {
Damien Neil8012b442019-01-18 09:32:24 -0800309 // TODO: Remove this check? This API is primarily used by generated code,
310 // and should not violate this assumption. Leave this check in for now to
311 // provide some sanity checks during development. This can be removed if
312 // it proves to be detrimental to performance.
313 if reflect.TypeOf(p) != mi.GoType {
314 panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.GoType))
315 }
Joe Tsai6cf80c42018-12-01 04:57:09 -0800316 return &messageDataType{pointerOfIface(p), mi}
Joe Tsaic6b75612018-09-13 14:24:37 -0700317}
318
319// messageDataType is a tuple of a pointer to the message data and
320// a pointer to the message type.
321//
Joe Tsai4fe96632019-05-22 05:12:36 -0400322// TODO: Unfortunately, we need to close over a pointer and MessageInfo,
Joe Tsaic6b75612018-09-13 14:24:37 -0700323// which incurs an an allocation. This pair is similar to a Go interface,
324// which is essentially a tuple of the same thing. We can make this efficient
325// with reflect.NamedOf (see https://golang.org/issues/16522).
326//
327// With that hypothetical API, we could dynamically create a new named type
Joe Tsai4fe96632019-05-22 05:12:36 -0400328// that has the same underlying type as MessageInfo.GoType, and
329// dynamically create methods that close over MessageInfo.
Joe Tsaic6b75612018-09-13 14:24:37 -0700330// Since the new type would have the same underlying type, we could directly
331// convert between pointers of those types, giving us an efficient way to swap
332// out the method set.
333//
334// Barring the ability to dynamically create named types, the workaround is
335// 1. either to accept the cost of an allocation for this wrapper struct or
336// 2. generate more types and methods, at the expense of binary size increase.
337type messageDataType struct {
338 p pointer
Joe Tsai4fe96632019-05-22 05:12:36 -0400339 mi *MessageInfo
Joe Tsaic6b75612018-09-13 14:24:37 -0700340}
341
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700342type messageReflectWrapper messageDataType
Joe Tsai08e00302018-11-26 22:32:06 -0800343
Joe Tsai0fc49f82019-05-01 12:29:25 -0700344func (m *messageReflectWrapper) Descriptor() pref.MessageDescriptor {
345 return m.mi.PBType.Descriptor()
346}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700347func (m *messageReflectWrapper) New() pref.Message {
348 return m.mi.PBType.New()
349}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700350func (m *messageReflectWrapper) Interface() pref.ProtoMessage {
Joe Tsai08e00302018-11-26 22:32:06 -0800351 if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
352 return m
353 }
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700354 return (*messageIfaceWrapper)(m)
Joe Tsai08e00302018-11-26 22:32:06 -0800355}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700356func (m *messageReflectWrapper) ProtoUnwrap() interface{} {
Damien Neil8012b442019-01-18 09:32:24 -0800357 return m.p.AsIfaceOf(m.mi.GoType.Elem())
Joe Tsai08e00302018-11-26 22:32:06 -0800358}
Joe Tsai08e00302018-11-26 22:32:06 -0800359
Joe Tsai378c1322019-04-25 23:48:08 -0700360func (m *messageReflectWrapper) Len() (cnt int) {
361 m.mi.init()
362 for _, fi := range m.mi.fields {
363 if fi.has(m.p) {
364 cnt++
365 }
366 }
367 return cnt + m.mi.extensionMap(m.p).Len()
368}
369func (m *messageReflectWrapper) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
370 m.mi.init()
371 for _, fi := range m.mi.fields {
372 if fi.has(m.p) {
373 if !f(fi.fieldDesc, fi.get(m.p)) {
374 return
375 }
376 }
377 }
378 m.mi.extensionMap(m.p).Range(f)
379}
380func (m *messageReflectWrapper) Has(fd pref.FieldDescriptor) bool {
381 if fi, xt := m.checkField(fd); fi != nil {
382 return fi.has(m.p)
383 } else {
384 return m.mi.extensionMap(m.p).Has(xt)
385 }
386}
387func (m *messageReflectWrapper) Clear(fd pref.FieldDescriptor) {
388 if fi, xt := m.checkField(fd); fi != nil {
389 fi.clear(m.p)
390 } else {
391 m.mi.extensionMap(m.p).Clear(xt)
392 }
393}
394func (m *messageReflectWrapper) Get(fd pref.FieldDescriptor) pref.Value {
395 if fi, xt := m.checkField(fd); fi != nil {
396 return fi.get(m.p)
397 } else {
398 return m.mi.extensionMap(m.p).Get(xt)
399 }
400}
401func (m *messageReflectWrapper) Set(fd pref.FieldDescriptor, v pref.Value) {
402 if fi, xt := m.checkField(fd); fi != nil {
403 fi.set(m.p, v)
404 } else {
405 m.mi.extensionMap(m.p).Set(xt, v)
406 }
407}
408func (m *messageReflectWrapper) Mutable(fd pref.FieldDescriptor) pref.Value {
409 if fi, xt := m.checkField(fd); fi != nil {
410 return fi.mutable(m.p)
411 } else {
412 return m.mi.extensionMap(m.p).Mutable(xt)
413 }
414}
415func (m *messageReflectWrapper) NewMessage(fd pref.FieldDescriptor) pref.Message {
416 if fi, xt := m.checkField(fd); fi != nil {
417 return fi.newMessage()
418 } else {
419 return xt.New().Message()
420 }
421}
422func (m *messageReflectWrapper) WhichOneof(od pref.OneofDescriptor) pref.FieldDescriptor {
423 m.mi.init()
424 if oi := m.mi.oneofs[od.Name()]; oi != nil && oi.oneofDesc == od {
425 return od.Fields().ByNumber(oi.which(m.p))
426 }
427 panic("invalid oneof descriptor")
428}
429func (m *messageReflectWrapper) GetUnknown() pref.RawFields {
430 m.mi.init()
431 return m.mi.getUnknown(m.p)
432}
433func (m *messageReflectWrapper) SetUnknown(b pref.RawFields) {
434 m.mi.init()
435 m.mi.setUnknown(m.p, b)
436}
437
438// checkField verifies that the provided field descriptor is valid.
439// Exactly one of the returned values is populated.
440func (m *messageReflectWrapper) checkField(fd pref.FieldDescriptor) (*fieldInfo, pref.ExtensionType) {
441 m.mi.init()
442 if fi := m.mi.fields[fd.Number()]; fi != nil {
443 if fi.fieldDesc != fd {
444 panic("mismatching field descriptor")
445 }
446 return fi, nil
447 }
448 if fd.IsExtension() {
449 if fd.ContainingMessage().FullName() != m.mi.PBType.FullName() {
450 // TODO: Should this be exact containing message descriptor match?
451 panic("mismatching containing message")
452 }
453 if !m.mi.PBType.ExtensionRanges().Has(fd.Number()) {
454 panic("invalid extension field")
455 }
456 return nil, fd.(pref.ExtensionType)
457 }
458 panic("invalid field descriptor")
459}
460
461type extensionMap map[int32]ExtensionField
462
463func (m *extensionMap) Len() int {
464 if m != nil {
465 return len(*m)
466 }
467 return 0
468}
469func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
470 if m != nil {
471 for _, x := range *m {
472 xt := x.GetType()
473 if !f(xt, xt.ValueOf(x.GetValue())) {
474 return
475 }
476 }
477 }
478}
479func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) {
480 if m != nil {
481 _, ok = (*m)[int32(xt.Number())]
482 }
483 return ok
484}
485func (m *extensionMap) Clear(xt pref.ExtensionType) {
486 delete(*m, int32(xt.Number()))
487}
488func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value {
489 if m != nil {
490 if x, ok := (*m)[int32(xt.Number())]; ok {
491 return xt.ValueOf(x.GetValue())
492 }
493 }
494 if !isComposite(xt) {
495 return defaultValueOf(xt)
496 }
497 return frozenValueOf(xt.New())
498}
499func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) {
500 if *m == nil {
501 *m = make(map[int32]ExtensionField)
502 }
503 var x ExtensionField
504 x.SetType(xt)
505 x.SetEagerValue(xt.InterfaceOf(v))
506 (*m)[int32(xt.Number())] = x
507}
508func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value {
509 if !isComposite(xt) {
510 panic("invalid Mutable on field with non-composite type")
511 }
512 if x, ok := (*m)[int32(xt.Number())]; ok {
513 return xt.ValueOf(x.GetValue())
514 }
515 v := xt.New()
516 m.Set(xt, v)
517 return v
518}
519
520func isComposite(fd pref.FieldDescriptor) bool {
521 return fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind || fd.IsList() || fd.IsMap()
522}
523
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700524var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil)
525
526type messageIfaceWrapper messageDataType
527
528func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
529 return (*messageReflectWrapper)(m)
530}
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700531func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods {
Damien Neilc37adef2019-04-01 13:49:56 -0700532 // TODO: Consider not recreating this on every call.
533 m.mi.init()
534 return &piface.Methods{
535 Flags: piface.MethodFlagDeterministicMarshal,
536 MarshalAppend: m.marshalAppend,
537 Size: m.size,
538 }
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700539}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700540func (m *messageIfaceWrapper) ProtoUnwrap() interface{} {
541 return m.p.AsIfaceOf(m.mi.GoType.Elem())
542}
Damien Neilc37adef2019-04-01 13:49:56 -0700543func (m *messageIfaceWrapper) marshalAppend(b []byte, _ pref.ProtoMessage, opts piface.MarshalOptions) ([]byte, error) {
544 return m.mi.marshalAppendPointer(b, m.p, newMarshalOptions(opts))
545}
546func (m *messageIfaceWrapper) size(msg pref.ProtoMessage) (size int) {
547 return m.mi.sizePointer(m.p, 0)
548}