blob: 82107df018335f305a8d2d7d5e56d77451cf0b8d [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 pvalue "google.golang.org/protobuf/internal/value"
16 pref "google.golang.org/protobuf/reflect/protoreflect"
17 piface "google.golang.org/protobuf/runtime/protoiface"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070018)
19
Joe Tsai4fe96632019-05-22 05:12:36 -040020// MessageInfo provides protobuf related functionality for a given Go type
21// that represents a message. A given instance of MessageInfo is tied to
Joe Tsaic6b75612018-09-13 14:24:37 -070022// exactly one Go type, which must be a pointer to a struct type.
Joe Tsai4fe96632019-05-22 05:12:36 -040023type MessageInfo struct {
Damien Neil8012b442019-01-18 09:32:24 -080024 // GoType is the underlying message Go type and must be populated.
Joe Tsaic6b75612018-09-13 14:24:37 -070025 // Once set, this field must never be mutated.
Damien Neil8012b442019-01-18 09:32:24 -080026 GoType reflect.Type // pointer to struct
27
28 // PBType is the underlying message descriptor type and must be populated.
29 // Once set, this field must never be mutated.
30 PBType pref.MessageType
Joe Tsaic6b75612018-09-13 14:24:37 -070031
Joe Tsai09912272019-07-08 10:38:11 -070032 // OneofWrappers is list of pointers to oneof wrapper struct types.
33 OneofWrappers []interface{}
34
Damien Neilc37adef2019-04-01 13:49:56 -070035 initMu sync.Mutex // protects all unexported fields
36 initDone uint32
Joe Tsaic6b75612018-09-13 14:24:37 -070037
Damien Neil4ae30bb2019-06-20 10:12:23 -070038 fields map[pref.FieldNumber]*fieldInfo
Joe Tsai4ec39c72019-04-03 13:40:53 -070039 oneofs map[pref.Name]*oneofInfo
Joe Tsaibe5348c2018-10-23 18:31:18 -070040
Joe Tsai378c1322019-04-25 23:48:08 -070041 getUnknown func(pointer) pref.RawFields
42 setUnknown func(pointer, pref.RawFields)
43
44 extensionMap func(pointer) *extensionMap
45
Damien Neil4ae30bb2019-06-20 10:12:23 -070046 // Information used by the fast-path methods.
Joe Tsai4a539f42019-06-17 12:30:25 -070047 methods piface.Methods
Damien Neil4ae30bb2019-06-20 10:12:23 -070048 coderMessageInfo
Damien Neilc37adef2019-04-01 13:49:56 -070049
Damien Neilc37adef2019-04-01 13:49:56 -070050 extensionFieldInfosMu sync.RWMutex
Joe Tsai89d49632019-06-04 16:20:00 -070051 extensionFieldInfos map[pref.ExtensionType]*extensionFieldInfo
Damien Neilc37adef2019-04-01 13:49:56 -070052}
53
54var prefMessageType = reflect.TypeOf((*pref.Message)(nil)).Elem()
55
Joe Tsai4fe96632019-05-22 05:12:36 -040056// getMessageInfo returns the MessageInfo (if any) for a type.
Damien Neilc37adef2019-04-01 13:49:56 -070057//
Joe Tsai4fe96632019-05-22 05:12:36 -040058// We find the MessageInfo by calling the ProtoReflect method on the type's
Damien Neilc37adef2019-04-01 13:49:56 -070059// zero value and looking at the returned type to see if it is a
Joe Tsai4fe96632019-05-22 05:12:36 -040060// messageReflectWrapper. Note that the MessageInfo may still be uninitialized
Damien Neilc37adef2019-04-01 13:49:56 -070061// at this point.
Joe Tsai4fe96632019-05-22 05:12:36 -040062func getMessageInfo(mt reflect.Type) (mi *MessageInfo, ok bool) {
Damien Neilc37adef2019-04-01 13:49:56 -070063 method, ok := mt.MethodByName("ProtoReflect")
64 if !ok {
65 return nil, false
66 }
67 if method.Type.NumIn() != 1 || method.Type.NumOut() != 1 || method.Type.Out(0) != prefMessageType {
68 return nil, false
69 }
70 ret := reflect.Zero(mt).Method(method.Index).Call(nil)
71 m, ok := ret[0].Elem().Interface().(*messageReflectWrapper)
72 if !ok {
73 return nil, ok
74 }
75 return m.mi, true
Joe Tsaifa02f4e2018-09-12 16:20:37 -070076}
77
Joe Tsai4fe96632019-05-22 05:12:36 -040078func (mi *MessageInfo) init() {
Damien Neilc37adef2019-04-01 13:49:56 -070079 // This function is called in the hot path. Inline the sync.Once
80 // logic, since allocating a closure for Once.Do is expensive.
81 // Keep init small to ensure that it can be inlined.
82 if atomic.LoadUint32(&mi.initDone) == 1 {
83 return
84 }
85 mi.initOnce()
86}
Joe Tsaic6b75612018-09-13 14:24:37 -070087
Joe Tsai4fe96632019-05-22 05:12:36 -040088func (mi *MessageInfo) initOnce() {
Damien Neilc37adef2019-04-01 13:49:56 -070089 mi.initMu.Lock()
90 defer mi.initMu.Unlock()
91 if mi.initDone == 1 {
92 return
93 }
94
95 t := mi.GoType
96 if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
97 panic(fmt.Sprintf("got %v, want *struct kind", t))
98 }
99
100 si := mi.makeStructInfo(t.Elem())
101 mi.makeKnownFieldsFunc(si)
Joe Tsai93fd9682019-07-06 13:02:14 -0700102 mi.makeUnknownFieldsFunc(t.Elem(), si)
103 mi.makeExtensionFieldsFunc(t.Elem(), si)
Damien Neil4ae30bb2019-06-20 10:12:23 -0700104 mi.makeMethods(t.Elem(), si)
Damien Neilc37adef2019-04-01 13:49:56 -0700105
106 atomic.StoreUint32(&mi.initDone, 1)
107}
108
Joe Tsai378c1322019-04-25 23:48:08 -0700109type (
110 SizeCache = int32
111 UnknownFields = []byte
112 ExtensionFields = map[int32]ExtensionField
113)
114
115var (
116 sizecacheType = reflect.TypeOf(SizeCache(0))
117 unknownFieldsType = reflect.TypeOf(UnknownFields(nil))
118 extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
119)
Damien Neilc37adef2019-04-01 13:49:56 -0700120
Damien Neil3eaddf02019-05-09 11:33:55 -0700121type structInfo struct {
Joe Tsai93fd9682019-07-06 13:02:14 -0700122 sizecacheOffset offset
123 extensionOffset offset
124 unknownOffset offset
125
Damien Neil3eaddf02019-05-09 11:33:55 -0700126 fieldsByNumber map[pref.FieldNumber]reflect.StructField
127 oneofsByName map[pref.Name]reflect.StructField
128 oneofWrappersByType map[reflect.Type]pref.FieldNumber
129 oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
130}
131
Joe Tsai4fe96632019-05-22 05:12:36 -0400132func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
Damien Neil3eaddf02019-05-09 11:33:55 -0700133 si := structInfo{
Joe Tsai93fd9682019-07-06 13:02:14 -0700134 sizecacheOffset: invalidOffset,
135 extensionOffset: invalidOffset,
136 unknownOffset: invalidOffset,
137
Damien Neil3eaddf02019-05-09 11:33:55 -0700138 fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
139 oneofsByName: map[pref.Name]reflect.StructField{},
140 oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
141 oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
142 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700143
144 if f, _ := t.FieldByName("XXX_sizecache"); f.Type == sizecacheType {
145 si.sizecacheOffset = offsetOf(f)
146 }
147 if f, _ := t.FieldByName("XXX_InternalExtensions"); f.Type == extensionFieldsType {
148 si.extensionOffset = offsetOf(f)
149 }
150 if f, _ := t.FieldByName("XXX_extensions"); f.Type == extensionFieldsType {
151 si.extensionOffset = offsetOf(f)
152 }
153 if f, _ := t.FieldByName("XXX_unrecognized"); f.Type == unknownFieldsType {
154 si.unknownOffset = offsetOf(f)
155 }
156
157 // Generate a mapping of field numbers and names to Go struct field or type.
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700158fieldLoop:
159 for i := 0; i < t.NumField(); i++ {
160 f := t.Field(i)
161 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
162 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
163 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700164 si.fieldsByNumber[pref.FieldNumber(n)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700165 continue fieldLoop
166 }
167 }
168 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
Damien Neil3eaddf02019-05-09 11:33:55 -0700169 si.oneofsByName[pref.Name(s)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700170 continue fieldLoop
171 }
172 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700173
174 // Derive a mapping of oneof wrappers to fields.
Joe Tsai09912272019-07-08 10:38:11 -0700175 oneofWrappers := mi.OneofWrappers
Joe Tsai2c870bb2018-10-17 11:46:52 -0700176 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800177 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
178 }
179 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
180 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
181 }
182 for _, v := range oneofWrappers {
183 tf := reflect.TypeOf(v).Elem()
184 f := tf.Field(0)
185 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
186 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
187 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700188 si.oneofWrappersByType[tf] = pref.FieldNumber(n)
189 si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800190 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700191 }
192 }
193 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700194
Damien Neil3eaddf02019-05-09 11:33:55 -0700195 return si
196}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700197
Damien Neil3eaddf02019-05-09 11:33:55 -0700198// makeKnownFieldsFunc generates functions for operations that can be performed
199// on each protobuf message field. It takes in a reflect.Type representing the
200// Go struct and matches message fields with struct fields.
201//
202// This code assumes that the struct is well-formed and panics if there are
203// any discrepancies.
Joe Tsai4fe96632019-05-22 05:12:36 -0400204func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700205 mi.fields = map[pref.FieldNumber]*fieldInfo{}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700206 for i := 0; i < mi.PBType.Descriptor().Fields().Len(); i++ {
207 fd := mi.PBType.Descriptor().Fields().Get(i)
Damien Neil3eaddf02019-05-09 11:33:55 -0700208 fs := si.fieldsByNumber[fd.Number()]
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700209 var fi fieldInfo
210 switch {
Joe Tsaiac31a352019-05-13 14:32:56 -0700211 case fd.ContainingOneof() != nil:
212 fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], si.oneofWrappersByNumber[fd.Number()])
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700213 case fd.IsMap():
214 fi = fieldInfoForMap(fd, fs)
Joe Tsaiac31a352019-05-13 14:32:56 -0700215 case fd.IsList():
Joe Tsai4b7aff62018-11-14 14:05:19 -0800216 fi = fieldInfoForList(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700217 case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700218 fi = fieldInfoForMessage(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700219 default:
220 fi = fieldInfoForScalar(fd, fs)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700221 }
222 mi.fields[fd.Number()] = &fi
223 }
Joe Tsai4ec39c72019-04-03 13:40:53 -0700224
225 mi.oneofs = map[pref.Name]*oneofInfo{}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700226 for i := 0; i < mi.PBType.Descriptor().Oneofs().Len(); i++ {
227 od := mi.PBType.Descriptor().Oneofs().Get(i)
Damien Neil3eaddf02019-05-09 11:33:55 -0700228 mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], si.oneofWrappersByType)
Joe Tsai4ec39c72019-04-03 13:40:53 -0700229 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700230}
Joe Tsaic6b75612018-09-13 14:24:37 -0700231
Joe Tsai93fd9682019-07-06 13:02:14 -0700232func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) {
Joe Tsai378c1322019-04-25 23:48:08 -0700233 mi.getUnknown = func(pointer) pref.RawFields { return nil }
234 mi.setUnknown = func(pointer, pref.RawFields) { return }
Joe Tsai93fd9682019-07-06 13:02:14 -0700235 if si.unknownOffset.IsValid() {
Joe Tsai378c1322019-04-25 23:48:08 -0700236 mi.getUnknown = func(p pointer) pref.RawFields {
237 if p.IsNil() {
238 return nil
239 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700240 rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
Joe Tsai378c1322019-04-25 23:48:08 -0700241 return pref.RawFields(*rv.Interface().(*[]byte))
242 }
243 mi.setUnknown = func(p pointer, b pref.RawFields) {
244 if p.IsNil() {
245 panic("invalid SetUnknown on nil Message")
246 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700247 rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
Joe Tsai378c1322019-04-25 23:48:08 -0700248 *rv.Interface().(*[]byte) = []byte(b)
249 }
250 } else {
251 mi.getUnknown = func(pointer) pref.RawFields {
252 return nil
253 }
254 mi.setUnknown = func(p pointer, _ pref.RawFields) {
255 if p.IsNil() {
256 panic("invalid SetUnknown on nil Message")
257 }
258 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700259 }
260}
261
Joe Tsai93fd9682019-07-06 13:02:14 -0700262func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type, si structInfo) {
263 if si.extensionOffset.IsValid() {
Joe Tsai378c1322019-04-25 23:48:08 -0700264 mi.extensionMap = func(p pointer) *extensionMap {
Joe Tsaid8881392019-06-06 13:01:53 -0700265 if p.IsNil() {
266 return (*extensionMap)(nil)
267 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700268 v := p.Apply(si.extensionOffset).AsValueOf(extensionFieldsType)
Joe Tsai378c1322019-04-25 23:48:08 -0700269 return (*extensionMap)(v.Interface().(*map[int32]ExtensionField))
270 }
271 } else {
272 mi.extensionMap = func(pointer) *extensionMap {
273 return (*extensionMap)(nil)
274 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700275 }
276}
277
Joe Tsai4fe96632019-05-22 05:12:36 -0400278func (mi *MessageInfo) MessageOf(p interface{}) pref.Message {
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700279 return (*messageReflectWrapper)(mi.dataTypeOf(p))
Joe Tsai08e00302018-11-26 22:32:06 -0800280}
281
Joe Tsai4fe96632019-05-22 05:12:36 -0400282func (mi *MessageInfo) Methods() *piface.Methods {
Damien Neilc37adef2019-04-01 13:49:56 -0700283 mi.init()
284 return &mi.methods
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700285}
286
Joe Tsai4fe96632019-05-22 05:12:36 -0400287func (mi *MessageInfo) dataTypeOf(p interface{}) *messageDataType {
Damien Neil8012b442019-01-18 09:32:24 -0800288 // TODO: Remove this check? This API is primarily used by generated code,
289 // and should not violate this assumption. Leave this check in for now to
290 // provide some sanity checks during development. This can be removed if
291 // it proves to be detrimental to performance.
292 if reflect.TypeOf(p) != mi.GoType {
293 panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.GoType))
294 }
Joe Tsai6cf80c42018-12-01 04:57:09 -0800295 return &messageDataType{pointerOfIface(p), mi}
Joe Tsaic6b75612018-09-13 14:24:37 -0700296}
297
298// messageDataType is a tuple of a pointer to the message data and
299// a pointer to the message type.
300//
Joe Tsai4fe96632019-05-22 05:12:36 -0400301// TODO: Unfortunately, we need to close over a pointer and MessageInfo,
Joe Tsaic6b75612018-09-13 14:24:37 -0700302// which incurs an an allocation. This pair is similar to a Go interface,
303// which is essentially a tuple of the same thing. We can make this efficient
304// with reflect.NamedOf (see https://golang.org/issues/16522).
305//
306// With that hypothetical API, we could dynamically create a new named type
Joe Tsai4fe96632019-05-22 05:12:36 -0400307// that has the same underlying type as MessageInfo.GoType, and
308// dynamically create methods that close over MessageInfo.
Joe Tsaic6b75612018-09-13 14:24:37 -0700309// Since the new type would have the same underlying type, we could directly
310// convert between pointers of those types, giving us an efficient way to swap
311// out the method set.
312//
313// Barring the ability to dynamically create named types, the workaround is
314// 1. either to accept the cost of an allocation for this wrapper struct or
315// 2. generate more types and methods, at the expense of binary size increase.
316type messageDataType struct {
317 p pointer
Joe Tsai4fe96632019-05-22 05:12:36 -0400318 mi *MessageInfo
Joe Tsaic6b75612018-09-13 14:24:37 -0700319}
320
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700321type messageReflectWrapper messageDataType
Joe Tsai08e00302018-11-26 22:32:06 -0800322
Joe Tsai0fc49f82019-05-01 12:29:25 -0700323func (m *messageReflectWrapper) Descriptor() pref.MessageDescriptor {
324 return m.mi.PBType.Descriptor()
325}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700326func (m *messageReflectWrapper) New() pref.Message {
327 return m.mi.PBType.New()
328}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700329func (m *messageReflectWrapper) Interface() pref.ProtoMessage {
Joe Tsai08e00302018-11-26 22:32:06 -0800330 if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
331 return m
332 }
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700333 return (*messageIfaceWrapper)(m)
Joe Tsai08e00302018-11-26 22:32:06 -0800334}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700335func (m *messageReflectWrapper) ProtoUnwrap() interface{} {
Damien Neil8012b442019-01-18 09:32:24 -0800336 return m.p.AsIfaceOf(m.mi.GoType.Elem())
Joe Tsai08e00302018-11-26 22:32:06 -0800337}
Joe Tsai08e00302018-11-26 22:32:06 -0800338
Joe Tsai378c1322019-04-25 23:48:08 -0700339func (m *messageReflectWrapper) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
340 m.mi.init()
341 for _, fi := range m.mi.fields {
342 if fi.has(m.p) {
343 if !f(fi.fieldDesc, fi.get(m.p)) {
344 return
345 }
346 }
347 }
348 m.mi.extensionMap(m.p).Range(f)
349}
350func (m *messageReflectWrapper) Has(fd pref.FieldDescriptor) bool {
351 if fi, xt := m.checkField(fd); fi != nil {
352 return fi.has(m.p)
353 } else {
354 return m.mi.extensionMap(m.p).Has(xt)
355 }
356}
357func (m *messageReflectWrapper) Clear(fd pref.FieldDescriptor) {
358 if fi, xt := m.checkField(fd); fi != nil {
359 fi.clear(m.p)
360 } else {
361 m.mi.extensionMap(m.p).Clear(xt)
362 }
363}
364func (m *messageReflectWrapper) Get(fd pref.FieldDescriptor) pref.Value {
365 if fi, xt := m.checkField(fd); fi != nil {
366 return fi.get(m.p)
367 } else {
368 return m.mi.extensionMap(m.p).Get(xt)
369 }
370}
371func (m *messageReflectWrapper) Set(fd pref.FieldDescriptor, v pref.Value) {
372 if fi, xt := m.checkField(fd); fi != nil {
373 fi.set(m.p, v)
374 } else {
375 m.mi.extensionMap(m.p).Set(xt, v)
376 }
377}
378func (m *messageReflectWrapper) Mutable(fd pref.FieldDescriptor) pref.Value {
379 if fi, xt := m.checkField(fd); fi != nil {
380 return fi.mutable(m.p)
381 } else {
382 return m.mi.extensionMap(m.p).Mutable(xt)
383 }
384}
385func (m *messageReflectWrapper) NewMessage(fd pref.FieldDescriptor) pref.Message {
386 if fi, xt := m.checkField(fd); fi != nil {
387 return fi.newMessage()
388 } else {
389 return xt.New().Message()
390 }
391}
392func (m *messageReflectWrapper) WhichOneof(od pref.OneofDescriptor) pref.FieldDescriptor {
393 m.mi.init()
394 if oi := m.mi.oneofs[od.Name()]; oi != nil && oi.oneofDesc == od {
395 return od.Fields().ByNumber(oi.which(m.p))
396 }
397 panic("invalid oneof descriptor")
398}
399func (m *messageReflectWrapper) GetUnknown() pref.RawFields {
400 m.mi.init()
401 return m.mi.getUnknown(m.p)
402}
403func (m *messageReflectWrapper) SetUnknown(b pref.RawFields) {
404 m.mi.init()
405 m.mi.setUnknown(m.p, b)
406}
407
408// checkField verifies that the provided field descriptor is valid.
409// Exactly one of the returned values is populated.
410func (m *messageReflectWrapper) checkField(fd pref.FieldDescriptor) (*fieldInfo, pref.ExtensionType) {
411 m.mi.init()
412 if fi := m.mi.fields[fd.Number()]; fi != nil {
413 if fi.fieldDesc != fd {
414 panic("mismatching field descriptor")
415 }
416 return fi, nil
417 }
418 if fd.IsExtension() {
419 if fd.ContainingMessage().FullName() != m.mi.PBType.FullName() {
420 // TODO: Should this be exact containing message descriptor match?
421 panic("mismatching containing message")
422 }
423 if !m.mi.PBType.ExtensionRanges().Has(fd.Number()) {
424 panic("invalid extension field")
425 }
426 return nil, fd.(pref.ExtensionType)
427 }
428 panic("invalid field descriptor")
429}
430
431type extensionMap map[int32]ExtensionField
432
Joe Tsai378c1322019-04-25 23:48:08 -0700433func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
434 if m != nil {
435 for _, x := range *m {
436 xt := x.GetType()
437 if !f(xt, xt.ValueOf(x.GetValue())) {
438 return
439 }
440 }
441 }
442}
443func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) {
444 if m != nil {
445 _, ok = (*m)[int32(xt.Number())]
446 }
447 return ok
448}
449func (m *extensionMap) Clear(xt pref.ExtensionType) {
450 delete(*m, int32(xt.Number()))
451}
452func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value {
453 if m != nil {
454 if x, ok := (*m)[int32(xt.Number())]; ok {
455 return xt.ValueOf(x.GetValue())
456 }
457 }
458 if !isComposite(xt) {
459 return defaultValueOf(xt)
460 }
461 return frozenValueOf(xt.New())
462}
463func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) {
464 if *m == nil {
465 *m = make(map[int32]ExtensionField)
466 }
467 var x ExtensionField
468 x.SetType(xt)
469 x.SetEagerValue(xt.InterfaceOf(v))
470 (*m)[int32(xt.Number())] = x
471}
472func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value {
473 if !isComposite(xt) {
474 panic("invalid Mutable on field with non-composite type")
475 }
476 if x, ok := (*m)[int32(xt.Number())]; ok {
477 return xt.ValueOf(x.GetValue())
478 }
479 v := xt.New()
480 m.Set(xt, v)
481 return v
482}
483
484func isComposite(fd pref.FieldDescriptor) bool {
485 return fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind || fd.IsList() || fd.IsMap()
486}
487
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700488var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil)
489
490type messageIfaceWrapper messageDataType
491
492func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
493 return (*messageReflectWrapper)(m)
494}
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700495func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods {
Damien Neilc37adef2019-04-01 13:49:56 -0700496 // TODO: Consider not recreating this on every call.
497 m.mi.init()
498 return &piface.Methods{
499 Flags: piface.MethodFlagDeterministicMarshal,
500 MarshalAppend: m.marshalAppend,
501 Size: m.size,
502 }
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700503}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700504func (m *messageIfaceWrapper) ProtoUnwrap() interface{} {
505 return m.p.AsIfaceOf(m.mi.GoType.Elem())
506}
Damien Neilc37adef2019-04-01 13:49:56 -0700507func (m *messageIfaceWrapper) marshalAppend(b []byte, _ pref.ProtoMessage, opts piface.MarshalOptions) ([]byte, error) {
508 return m.mi.marshalAppendPointer(b, m.p, newMarshalOptions(opts))
509}
510func (m *messageIfaceWrapper) size(msg pref.ProtoMessage) (size int) {
511 return m.mi.sizePointer(m.p, 0)
512}