blob: fcf4b14bea0e5f096d73b0749de632978c0c7662 [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
Damien Neilc37adef2019-04-01 13:49:56 -070032 initMu sync.Mutex // protects all unexported fields
33 initDone uint32
Joe Tsaic6b75612018-09-13 14:24:37 -070034
Damien Neil4ae30bb2019-06-20 10:12:23 -070035 fields map[pref.FieldNumber]*fieldInfo
Joe Tsai4ec39c72019-04-03 13:40:53 -070036 oneofs map[pref.Name]*oneofInfo
Joe Tsaibe5348c2018-10-23 18:31:18 -070037
Joe Tsai378c1322019-04-25 23:48:08 -070038 getUnknown func(pointer) pref.RawFields
39 setUnknown func(pointer, pref.RawFields)
40
41 extensionMap func(pointer) *extensionMap
42
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
51var prefMessageType = reflect.TypeOf((*pref.Message)(nil)).Elem()
52
Joe Tsai4fe96632019-05-22 05:12:36 -040053// getMessageInfo returns the MessageInfo (if any) for a type.
Damien Neilc37adef2019-04-01 13:49:56 -070054//
Joe Tsai4fe96632019-05-22 05:12:36 -040055// We find the MessageInfo by calling the ProtoReflect method on the type's
Damien Neilc37adef2019-04-01 13:49:56 -070056// zero value and looking at the returned type to see if it is a
Joe Tsai4fe96632019-05-22 05:12:36 -040057// messageReflectWrapper. Note that the MessageInfo may still be uninitialized
Damien Neilc37adef2019-04-01 13:49:56 -070058// at this point.
Joe Tsai4fe96632019-05-22 05:12:36 -040059func getMessageInfo(mt reflect.Type) (mi *MessageInfo, ok bool) {
Damien Neilc37adef2019-04-01 13:49:56 -070060 method, ok := mt.MethodByName("ProtoReflect")
61 if !ok {
62 return nil, false
63 }
64 if method.Type.NumIn() != 1 || method.Type.NumOut() != 1 || method.Type.Out(0) != prefMessageType {
65 return nil, false
66 }
67 ret := reflect.Zero(mt).Method(method.Index).Call(nil)
68 m, ok := ret[0].Elem().Interface().(*messageReflectWrapper)
69 if !ok {
70 return nil, ok
71 }
72 return m.mi, true
Joe Tsaifa02f4e2018-09-12 16:20:37 -070073}
74
Joe Tsai4fe96632019-05-22 05:12:36 -040075func (mi *MessageInfo) init() {
Damien Neilc37adef2019-04-01 13:49:56 -070076 // This function is called in the hot path. Inline the sync.Once
77 // logic, since allocating a closure for Once.Do is expensive.
78 // Keep init small to ensure that it can be inlined.
79 if atomic.LoadUint32(&mi.initDone) == 1 {
80 return
81 }
82 mi.initOnce()
83}
Joe Tsaic6b75612018-09-13 14:24:37 -070084
Joe Tsai4fe96632019-05-22 05:12:36 -040085func (mi *MessageInfo) initOnce() {
Damien Neilc37adef2019-04-01 13:49:56 -070086 mi.initMu.Lock()
87 defer mi.initMu.Unlock()
88 if mi.initDone == 1 {
89 return
90 }
91
92 t := mi.GoType
93 if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
94 panic(fmt.Sprintf("got %v, want *struct kind", t))
95 }
96
97 si := mi.makeStructInfo(t.Elem())
98 mi.makeKnownFieldsFunc(si)
Joe Tsai93fd9682019-07-06 13:02:14 -070099 mi.makeUnknownFieldsFunc(t.Elem(), si)
100 mi.makeExtensionFieldsFunc(t.Elem(), si)
Damien Neil4ae30bb2019-06-20 10:12:23 -0700101 mi.makeMethods(t.Elem(), si)
Damien Neilc37adef2019-04-01 13:49:56 -0700102
103 atomic.StoreUint32(&mi.initDone, 1)
104}
105
Joe Tsai378c1322019-04-25 23:48:08 -0700106type (
107 SizeCache = int32
108 UnknownFields = []byte
109 ExtensionFields = map[int32]ExtensionField
110)
111
112var (
113 sizecacheType = reflect.TypeOf(SizeCache(0))
114 unknownFieldsType = reflect.TypeOf(UnknownFields(nil))
115 extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
116)
Damien Neilc37adef2019-04-01 13:49:56 -0700117
Damien Neil3eaddf02019-05-09 11:33:55 -0700118type structInfo struct {
Joe Tsai93fd9682019-07-06 13:02:14 -0700119 sizecacheOffset offset
120 extensionOffset offset
121 unknownOffset offset
122
Damien Neil3eaddf02019-05-09 11:33:55 -0700123 fieldsByNumber map[pref.FieldNumber]reflect.StructField
124 oneofsByName map[pref.Name]reflect.StructField
125 oneofWrappersByType map[reflect.Type]pref.FieldNumber
126 oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
127}
128
Joe Tsai4fe96632019-05-22 05:12:36 -0400129func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
Damien Neil3eaddf02019-05-09 11:33:55 -0700130 si := structInfo{
Joe Tsai93fd9682019-07-06 13:02:14 -0700131 sizecacheOffset: invalidOffset,
132 extensionOffset: invalidOffset,
133 unknownOffset: invalidOffset,
134
Damien Neil3eaddf02019-05-09 11:33:55 -0700135 fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
136 oneofsByName: map[pref.Name]reflect.StructField{},
137 oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
138 oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
139 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700140
141 if f, _ := t.FieldByName("XXX_sizecache"); f.Type == sizecacheType {
142 si.sizecacheOffset = offsetOf(f)
143 }
144 if f, _ := t.FieldByName("XXX_InternalExtensions"); f.Type == extensionFieldsType {
145 si.extensionOffset = offsetOf(f)
146 }
147 if f, _ := t.FieldByName("XXX_extensions"); f.Type == extensionFieldsType {
148 si.extensionOffset = offsetOf(f)
149 }
150 if f, _ := t.FieldByName("XXX_unrecognized"); f.Type == unknownFieldsType {
151 si.unknownOffset = offsetOf(f)
152 }
153
154 // Generate a mapping of field numbers and names to Go struct field or type.
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700155fieldLoop:
156 for i := 0; i < t.NumField(); i++ {
157 f := t.Field(i)
158 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
159 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
160 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700161 si.fieldsByNumber[pref.FieldNumber(n)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700162 continue fieldLoop
163 }
164 }
165 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
Damien Neil3eaddf02019-05-09 11:33:55 -0700166 si.oneofsByName[pref.Name(s)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700167 continue fieldLoop
168 }
169 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700170
171 // Derive a mapping of oneof wrappers to fields.
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800172 var oneofWrappers []interface{}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700173 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800174 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
175 }
176 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
177 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
178 }
179 for _, v := range oneofWrappers {
180 tf := reflect.TypeOf(v).Elem()
181 f := tf.Field(0)
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.oneofWrappersByType[tf] = pref.FieldNumber(n)
186 si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800187 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700188 }
189 }
190 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700191
Damien Neil3eaddf02019-05-09 11:33:55 -0700192 return si
193}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700194
Damien Neil3eaddf02019-05-09 11:33:55 -0700195// makeKnownFieldsFunc generates functions for operations that can be performed
196// on each protobuf message field. It takes in a reflect.Type representing the
197// Go struct and matches message fields with struct fields.
198//
199// This code assumes that the struct is well-formed and panics if there are
200// any discrepancies.
Joe Tsai4fe96632019-05-22 05:12:36 -0400201func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700202 mi.fields = map[pref.FieldNumber]*fieldInfo{}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700203 for i := 0; i < mi.PBType.Descriptor().Fields().Len(); i++ {
204 fd := mi.PBType.Descriptor().Fields().Get(i)
Damien Neil3eaddf02019-05-09 11:33:55 -0700205 fs := si.fieldsByNumber[fd.Number()]
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700206 var fi fieldInfo
207 switch {
Joe Tsaiac31a352019-05-13 14:32:56 -0700208 case fd.ContainingOneof() != nil:
209 fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], si.oneofWrappersByNumber[fd.Number()])
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700210 case fd.IsMap():
211 fi = fieldInfoForMap(fd, fs)
Joe Tsaiac31a352019-05-13 14:32:56 -0700212 case fd.IsList():
Joe Tsai4b7aff62018-11-14 14:05:19 -0800213 fi = fieldInfoForList(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700214 case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700215 fi = fieldInfoForMessage(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700216 default:
217 fi = fieldInfoForScalar(fd, fs)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700218 }
219 mi.fields[fd.Number()] = &fi
220 }
Joe Tsai4ec39c72019-04-03 13:40:53 -0700221
222 mi.oneofs = map[pref.Name]*oneofInfo{}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700223 for i := 0; i < mi.PBType.Descriptor().Oneofs().Len(); i++ {
224 od := mi.PBType.Descriptor().Oneofs().Get(i)
Damien Neil3eaddf02019-05-09 11:33:55 -0700225 mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], si.oneofWrappersByType)
Joe Tsai4ec39c72019-04-03 13:40:53 -0700226 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700227}
Joe Tsaic6b75612018-09-13 14:24:37 -0700228
Joe Tsai93fd9682019-07-06 13:02:14 -0700229func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type, si structInfo) {
Joe Tsai378c1322019-04-25 23:48:08 -0700230 mi.getUnknown = func(pointer) pref.RawFields { return nil }
231 mi.setUnknown = func(pointer, pref.RawFields) { return }
Joe Tsai93fd9682019-07-06 13:02:14 -0700232 if si.unknownOffset.IsValid() {
Joe Tsai378c1322019-04-25 23:48:08 -0700233 mi.getUnknown = func(p pointer) pref.RawFields {
234 if p.IsNil() {
235 return nil
236 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700237 rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
Joe Tsai378c1322019-04-25 23:48:08 -0700238 return pref.RawFields(*rv.Interface().(*[]byte))
239 }
240 mi.setUnknown = func(p pointer, b pref.RawFields) {
241 if p.IsNil() {
242 panic("invalid SetUnknown on nil Message")
243 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700244 rv := p.Apply(si.unknownOffset).AsValueOf(unknownFieldsType)
Joe Tsai378c1322019-04-25 23:48:08 -0700245 *rv.Interface().(*[]byte) = []byte(b)
246 }
247 } else {
248 mi.getUnknown = func(pointer) pref.RawFields {
249 return nil
250 }
251 mi.setUnknown = func(p pointer, _ pref.RawFields) {
252 if p.IsNil() {
253 panic("invalid SetUnknown on nil Message")
254 }
255 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700256 }
257}
258
Joe Tsai93fd9682019-07-06 13:02:14 -0700259func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type, si structInfo) {
260 if si.extensionOffset.IsValid() {
Joe Tsai378c1322019-04-25 23:48:08 -0700261 mi.extensionMap = func(p pointer) *extensionMap {
Joe Tsaid8881392019-06-06 13:01:53 -0700262 if p.IsNil() {
263 return (*extensionMap)(nil)
264 }
Joe Tsai93fd9682019-07-06 13:02:14 -0700265 v := p.Apply(si.extensionOffset).AsValueOf(extensionFieldsType)
Joe Tsai378c1322019-04-25 23:48:08 -0700266 return (*extensionMap)(v.Interface().(*map[int32]ExtensionField))
267 }
268 } else {
269 mi.extensionMap = func(pointer) *extensionMap {
270 return (*extensionMap)(nil)
271 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700272 }
273}
274
Joe Tsai4fe96632019-05-22 05:12:36 -0400275func (mi *MessageInfo) MessageOf(p interface{}) pref.Message {
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700276 return (*messageReflectWrapper)(mi.dataTypeOf(p))
Joe Tsai08e00302018-11-26 22:32:06 -0800277}
278
Joe Tsai4fe96632019-05-22 05:12:36 -0400279func (mi *MessageInfo) Methods() *piface.Methods {
Damien Neilc37adef2019-04-01 13:49:56 -0700280 mi.init()
281 return &mi.methods
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700282}
283
Joe Tsai4fe96632019-05-22 05:12:36 -0400284func (mi *MessageInfo) dataTypeOf(p interface{}) *messageDataType {
Damien Neil8012b442019-01-18 09:32:24 -0800285 // TODO: Remove this check? This API is primarily used by generated code,
286 // and should not violate this assumption. Leave this check in for now to
287 // provide some sanity checks during development. This can be removed if
288 // it proves to be detrimental to performance.
289 if reflect.TypeOf(p) != mi.GoType {
290 panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.GoType))
291 }
Joe Tsai6cf80c42018-12-01 04:57:09 -0800292 return &messageDataType{pointerOfIface(p), mi}
Joe Tsaic6b75612018-09-13 14:24:37 -0700293}
294
295// messageDataType is a tuple of a pointer to the message data and
296// a pointer to the message type.
297//
Joe Tsai4fe96632019-05-22 05:12:36 -0400298// TODO: Unfortunately, we need to close over a pointer and MessageInfo,
Joe Tsaic6b75612018-09-13 14:24:37 -0700299// which incurs an an allocation. This pair is similar to a Go interface,
300// which is essentially a tuple of the same thing. We can make this efficient
301// with reflect.NamedOf (see https://golang.org/issues/16522).
302//
303// With that hypothetical API, we could dynamically create a new named type
Joe Tsai4fe96632019-05-22 05:12:36 -0400304// that has the same underlying type as MessageInfo.GoType, and
305// dynamically create methods that close over MessageInfo.
Joe Tsaic6b75612018-09-13 14:24:37 -0700306// Since the new type would have the same underlying type, we could directly
307// convert between pointers of those types, giving us an efficient way to swap
308// out the method set.
309//
310// Barring the ability to dynamically create named types, the workaround is
311// 1. either to accept the cost of an allocation for this wrapper struct or
312// 2. generate more types and methods, at the expense of binary size increase.
313type messageDataType struct {
314 p pointer
Joe Tsai4fe96632019-05-22 05:12:36 -0400315 mi *MessageInfo
Joe Tsaic6b75612018-09-13 14:24:37 -0700316}
317
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700318type messageReflectWrapper messageDataType
Joe Tsai08e00302018-11-26 22:32:06 -0800319
Joe Tsai0fc49f82019-05-01 12:29:25 -0700320func (m *messageReflectWrapper) Descriptor() pref.MessageDescriptor {
321 return m.mi.PBType.Descriptor()
322}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700323func (m *messageReflectWrapper) New() pref.Message {
324 return m.mi.PBType.New()
325}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700326func (m *messageReflectWrapper) Interface() pref.ProtoMessage {
Joe Tsai08e00302018-11-26 22:32:06 -0800327 if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
328 return m
329 }
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700330 return (*messageIfaceWrapper)(m)
Joe Tsai08e00302018-11-26 22:32:06 -0800331}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700332func (m *messageReflectWrapper) ProtoUnwrap() interface{} {
Damien Neil8012b442019-01-18 09:32:24 -0800333 return m.p.AsIfaceOf(m.mi.GoType.Elem())
Joe Tsai08e00302018-11-26 22:32:06 -0800334}
Joe Tsai08e00302018-11-26 22:32:06 -0800335
Joe Tsai378c1322019-04-25 23:48:08 -0700336func (m *messageReflectWrapper) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
337 m.mi.init()
338 for _, fi := range m.mi.fields {
339 if fi.has(m.p) {
340 if !f(fi.fieldDesc, fi.get(m.p)) {
341 return
342 }
343 }
344 }
345 m.mi.extensionMap(m.p).Range(f)
346}
347func (m *messageReflectWrapper) Has(fd pref.FieldDescriptor) bool {
348 if fi, xt := m.checkField(fd); fi != nil {
349 return fi.has(m.p)
350 } else {
351 return m.mi.extensionMap(m.p).Has(xt)
352 }
353}
354func (m *messageReflectWrapper) Clear(fd pref.FieldDescriptor) {
355 if fi, xt := m.checkField(fd); fi != nil {
356 fi.clear(m.p)
357 } else {
358 m.mi.extensionMap(m.p).Clear(xt)
359 }
360}
361func (m *messageReflectWrapper) Get(fd pref.FieldDescriptor) pref.Value {
362 if fi, xt := m.checkField(fd); fi != nil {
363 return fi.get(m.p)
364 } else {
365 return m.mi.extensionMap(m.p).Get(xt)
366 }
367}
368func (m *messageReflectWrapper) Set(fd pref.FieldDescriptor, v pref.Value) {
369 if fi, xt := m.checkField(fd); fi != nil {
370 fi.set(m.p, v)
371 } else {
372 m.mi.extensionMap(m.p).Set(xt, v)
373 }
374}
375func (m *messageReflectWrapper) Mutable(fd pref.FieldDescriptor) pref.Value {
376 if fi, xt := m.checkField(fd); fi != nil {
377 return fi.mutable(m.p)
378 } else {
379 return m.mi.extensionMap(m.p).Mutable(xt)
380 }
381}
382func (m *messageReflectWrapper) NewMessage(fd pref.FieldDescriptor) pref.Message {
383 if fi, xt := m.checkField(fd); fi != nil {
384 return fi.newMessage()
385 } else {
386 return xt.New().Message()
387 }
388}
389func (m *messageReflectWrapper) WhichOneof(od pref.OneofDescriptor) pref.FieldDescriptor {
390 m.mi.init()
391 if oi := m.mi.oneofs[od.Name()]; oi != nil && oi.oneofDesc == od {
392 return od.Fields().ByNumber(oi.which(m.p))
393 }
394 panic("invalid oneof descriptor")
395}
396func (m *messageReflectWrapper) GetUnknown() pref.RawFields {
397 m.mi.init()
398 return m.mi.getUnknown(m.p)
399}
400func (m *messageReflectWrapper) SetUnknown(b pref.RawFields) {
401 m.mi.init()
402 m.mi.setUnknown(m.p, b)
403}
404
405// checkField verifies that the provided field descriptor is valid.
406// Exactly one of the returned values is populated.
407func (m *messageReflectWrapper) checkField(fd pref.FieldDescriptor) (*fieldInfo, pref.ExtensionType) {
408 m.mi.init()
409 if fi := m.mi.fields[fd.Number()]; fi != nil {
410 if fi.fieldDesc != fd {
411 panic("mismatching field descriptor")
412 }
413 return fi, nil
414 }
415 if fd.IsExtension() {
416 if fd.ContainingMessage().FullName() != m.mi.PBType.FullName() {
417 // TODO: Should this be exact containing message descriptor match?
418 panic("mismatching containing message")
419 }
420 if !m.mi.PBType.ExtensionRanges().Has(fd.Number()) {
421 panic("invalid extension field")
422 }
423 return nil, fd.(pref.ExtensionType)
424 }
425 panic("invalid field descriptor")
426}
427
428type extensionMap map[int32]ExtensionField
429
Joe Tsai378c1322019-04-25 23:48:08 -0700430func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
431 if m != nil {
432 for _, x := range *m {
433 xt := x.GetType()
434 if !f(xt, xt.ValueOf(x.GetValue())) {
435 return
436 }
437 }
438 }
439}
440func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) {
441 if m != nil {
442 _, ok = (*m)[int32(xt.Number())]
443 }
444 return ok
445}
446func (m *extensionMap) Clear(xt pref.ExtensionType) {
447 delete(*m, int32(xt.Number()))
448}
449func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value {
450 if m != nil {
451 if x, ok := (*m)[int32(xt.Number())]; ok {
452 return xt.ValueOf(x.GetValue())
453 }
454 }
455 if !isComposite(xt) {
456 return defaultValueOf(xt)
457 }
458 return frozenValueOf(xt.New())
459}
460func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) {
461 if *m == nil {
462 *m = make(map[int32]ExtensionField)
463 }
464 var x ExtensionField
465 x.SetType(xt)
466 x.SetEagerValue(xt.InterfaceOf(v))
467 (*m)[int32(xt.Number())] = x
468}
469func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value {
470 if !isComposite(xt) {
471 panic("invalid Mutable on field with non-composite type")
472 }
473 if x, ok := (*m)[int32(xt.Number())]; ok {
474 return xt.ValueOf(x.GetValue())
475 }
476 v := xt.New()
477 m.Set(xt, v)
478 return v
479}
480
481func isComposite(fd pref.FieldDescriptor) bool {
482 return fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind || fd.IsList() || fd.IsMap()
483}
484
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700485var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil)
486
487type messageIfaceWrapper messageDataType
488
489func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
490 return (*messageReflectWrapper)(m)
491}
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700492func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods {
Damien Neilc37adef2019-04-01 13:49:56 -0700493 // TODO: Consider not recreating this on every call.
494 m.mi.init()
495 return &piface.Methods{
496 Flags: piface.MethodFlagDeterministicMarshal,
497 MarshalAppend: m.marshalAppend,
498 Size: m.size,
499 }
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700500}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700501func (m *messageIfaceWrapper) ProtoUnwrap() interface{} {
502 return m.p.AsIfaceOf(m.mi.GoType.Elem())
503}
Damien Neilc37adef2019-04-01 13:49:56 -0700504func (m *messageIfaceWrapper) marshalAppend(b []byte, _ pref.ProtoMessage, opts piface.MarshalOptions) ([]byte, error) {
505 return m.mi.marshalAppendPointer(b, m.p, newMarshalOptions(opts))
506}
507func (m *messageIfaceWrapper) size(msg pref.ProtoMessage) (size int) {
508 return m.mi.sizePointer(m.p, 0)
509}