blob: d64ee7faf7a903d9b741a02a81bb043cb0940b0b [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
43 unknownFields func(*messageDataType) pref.UnknownFields
44 extensionFields func(*messageDataType) pref.KnownFields
Damien Neilc37adef2019-04-01 13:49:56 -070045 methods piface.Methods
46
47 extensionOffset offset
48 sizecacheOffset offset
49 unknownOffset offset
50 extensionFieldInfosMu sync.RWMutex
51 extensionFieldInfos map[*piface.ExtensionDescV1]*extensionFieldInfo
52}
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)
102 mi.makeUnknownFieldsFunc(t.Elem())
103 mi.makeExtensionFieldsFunc(t.Elem())
104 mi.makeMethods(t.Elem())
105
106 atomic.StoreUint32(&mi.initDone, 1)
107}
108
109var sizecacheType = reflect.TypeOf(int32(0))
110
Joe Tsai4fe96632019-05-22 05:12:36 -0400111func (mi *MessageInfo) makeMethods(t reflect.Type) {
Damien Neilc37adef2019-04-01 13:49:56 -0700112 mi.extensionOffset = invalidOffset
113 if fx, _ := t.FieldByName("XXX_InternalExtensions"); fx.Type == extType {
114 mi.extensionOffset = offsetOf(fx)
115 } else if fx, _ = t.FieldByName("XXX_extensions"); fx.Type == extType {
116 mi.extensionOffset = offsetOf(fx)
117 }
118 mi.sizecacheOffset = invalidOffset
119 if fx, _ := t.FieldByName("XXX_sizecache"); fx.Type == sizecacheType {
120 mi.sizecacheOffset = offsetOf(fx)
121 }
122 mi.unknownOffset = invalidOffset
123 if fx, _ := t.FieldByName("XXX_unrecognized"); fx.Type == bytesType {
124 mi.unknownOffset = offsetOf(fx)
125 }
126 mi.methods.Flags = piface.MethodFlagDeterministicMarshal
127 mi.methods.MarshalAppend = mi.marshalAppend
128 mi.methods.Size = mi.size
Joe Tsaic6b75612018-09-13 14:24:37 -0700129}
130
Damien Neil3eaddf02019-05-09 11:33:55 -0700131type structInfo struct {
132 fieldsByNumber map[pref.FieldNumber]reflect.StructField
133 oneofsByName map[pref.Name]reflect.StructField
134 oneofWrappersByType map[reflect.Type]pref.FieldNumber
135 oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
136}
137
Joe Tsai4fe96632019-05-22 05:12:36 -0400138func (mi *MessageInfo) makeStructInfo(t reflect.Type) structInfo {
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700139 // Generate a mapping of field numbers and names to Go struct field or type.
Damien Neil3eaddf02019-05-09 11:33:55 -0700140 si := structInfo{
141 fieldsByNumber: map[pref.FieldNumber]reflect.StructField{},
142 oneofsByName: map[pref.Name]reflect.StructField{},
143 oneofWrappersByType: map[reflect.Type]pref.FieldNumber{},
144 oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
145 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700146fieldLoop:
147 for i := 0; i < t.NumField(); i++ {
148 f := t.Field(i)
149 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
150 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
151 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700152 si.fieldsByNumber[pref.FieldNumber(n)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700153 continue fieldLoop
154 }
155 }
156 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
Damien Neil3eaddf02019-05-09 11:33:55 -0700157 si.oneofsByName[pref.Name(s)] = f
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700158 continue fieldLoop
159 }
160 }
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800161 var oneofWrappers []interface{}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700162 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800163 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
164 }
165 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
166 oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
167 }
168 for _, v := range oneofWrappers {
169 tf := reflect.TypeOf(v).Elem()
170 f := tf.Field(0)
171 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
172 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
173 n, _ := strconv.ParseUint(s, 10, 64)
Damien Neil3eaddf02019-05-09 11:33:55 -0700174 si.oneofWrappersByType[tf] = pref.FieldNumber(n)
175 si.oneofWrappersByNumber[pref.FieldNumber(n)] = tf
Joe Tsaid7e97bc2018-11-26 12:57:27 -0800176 break
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700177 }
178 }
179 }
Damien Neil3eaddf02019-05-09 11:33:55 -0700180 return si
181}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700182
Damien Neil3eaddf02019-05-09 11:33:55 -0700183// makeKnownFieldsFunc generates functions for operations that can be performed
184// on each protobuf message field. It takes in a reflect.Type representing the
185// Go struct and matches message fields with struct fields.
186//
187// This code assumes that the struct is well-formed and panics if there are
188// any discrepancies.
Joe Tsai4fe96632019-05-22 05:12:36 -0400189func (mi *MessageInfo) makeKnownFieldsFunc(si structInfo) {
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700190 mi.fields = map[pref.FieldNumber]*fieldInfo{}
Damien Neilc37adef2019-04-01 13:49:56 -0700191 mi.fieldsOrdered = make([]*fieldInfo, 0, mi.PBType.Fields().Len())
Joe Tsai0fc49f82019-05-01 12:29:25 -0700192 for i := 0; i < mi.PBType.Descriptor().Fields().Len(); i++ {
193 fd := mi.PBType.Descriptor().Fields().Get(i)
Damien Neil3eaddf02019-05-09 11:33:55 -0700194 fs := si.fieldsByNumber[fd.Number()]
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700195 var fi fieldInfo
196 switch {
Joe Tsaiac31a352019-05-13 14:32:56 -0700197 case fd.ContainingOneof() != nil:
198 fi = fieldInfoForOneof(fd, si.oneofsByName[fd.ContainingOneof().Name()], si.oneofWrappersByNumber[fd.Number()])
Damien Neilc37adef2019-04-01 13:49:56 -0700199 // There is one fieldInfo for each proto message field, but only one struct
200 // field for all message fields in a oneof. We install the encoder functions
201 // on the fieldInfo for the first field in the oneof.
202 //
203 // A slightly simpler approach would be to have each fieldInfo's encoder
204 // handle the case where that field is set, but this would require more
205 // checks against the current oneof type than a single map lookup.
206 if fd.ContainingOneof().Fields().Get(0).Name() == fd.Name() {
207 fi.funcs = makeOneofFieldCoder(si.oneofsByName[fd.ContainingOneof().Name()], fd.ContainingOneof(), si.fieldsByNumber, si.oneofWrappersByNumber)
208 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700209 case fd.IsMap():
210 fi = fieldInfoForMap(fd, fs)
Joe Tsaiac31a352019-05-13 14:32:56 -0700211 case fd.IsList():
Joe Tsai4b7aff62018-11-14 14:05:19 -0800212 fi = fieldInfoForList(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700213 case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700214 fi = fieldInfoForMessage(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700215 default:
216 fi = fieldInfoForScalar(fd, fs)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700217 }
Damien Neilc37adef2019-04-01 13:49:56 -0700218 fi.num = fd.Number()
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700219 mi.fields[fd.Number()] = &fi
Damien Neilc37adef2019-04-01 13:49:56 -0700220 mi.fieldsOrdered = append(mi.fieldsOrdered, &fi)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700221 }
Damien Neilc37adef2019-04-01 13:49:56 -0700222 sort.Slice(mi.fieldsOrdered, func(i, j int) bool {
223 return mi.fieldsOrdered[i].num < mi.fieldsOrdered[j].num
224 })
Joe Tsai4ec39c72019-04-03 13:40:53 -0700225
226 mi.oneofs = map[pref.Name]*oneofInfo{}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700227 for i := 0; i < mi.PBType.Descriptor().Oneofs().Len(); i++ {
228 od := mi.PBType.Descriptor().Oneofs().Get(i)
Damien Neil3eaddf02019-05-09 11:33:55 -0700229 mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], si.oneofWrappersByType)
Joe Tsai4ec39c72019-04-03 13:40:53 -0700230 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700231}
Joe Tsaic6b75612018-09-13 14:24:37 -0700232
Joe Tsai4fe96632019-05-22 05:12:36 -0400233func (mi *MessageInfo) makeUnknownFieldsFunc(t reflect.Type) {
Joe Tsai95b02902018-10-31 18:23:42 -0700234 if f := makeLegacyUnknownFieldsFunc(t); f != nil {
Joe Tsaie2afdc22018-10-25 14:06:56 -0700235 mi.unknownFields = f
236 return
237 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700238 mi.unknownFields = func(*messageDataType) pref.UnknownFields {
239 return emptyUnknownFields{}
240 }
241}
242
Joe Tsai4fe96632019-05-22 05:12:36 -0400243func (mi *MessageInfo) makeExtensionFieldsFunc(t reflect.Type) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800244 if f := makeLegacyExtensionFieldsFunc(t); f != nil {
245 mi.extensionFields = f
246 return
247 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700248 mi.extensionFields = func(*messageDataType) pref.KnownFields {
249 return emptyExtensionFields{}
250 }
251}
252
Joe Tsai4fe96632019-05-22 05:12:36 -0400253func (mi *MessageInfo) MessageOf(p interface{}) pref.Message {
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700254 return (*messageReflectWrapper)(mi.dataTypeOf(p))
Joe Tsai08e00302018-11-26 22:32:06 -0800255}
256
Joe Tsai4fe96632019-05-22 05:12:36 -0400257func (mi *MessageInfo) Methods() *piface.Methods {
Damien Neilc37adef2019-04-01 13:49:56 -0700258 mi.init()
259 return &mi.methods
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700260}
261
Joe Tsai4fe96632019-05-22 05:12:36 -0400262func (mi *MessageInfo) dataTypeOf(p interface{}) *messageDataType {
Damien Neil8012b442019-01-18 09:32:24 -0800263 // TODO: Remove this check? This API is primarily used by generated code,
264 // and should not violate this assumption. Leave this check in for now to
265 // provide some sanity checks during development. This can be removed if
266 // it proves to be detrimental to performance.
267 if reflect.TypeOf(p) != mi.GoType {
268 panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.GoType))
269 }
Joe Tsai6cf80c42018-12-01 04:57:09 -0800270 return &messageDataType{pointerOfIface(p), mi}
Joe Tsaic6b75612018-09-13 14:24:37 -0700271}
272
273// messageDataType is a tuple of a pointer to the message data and
274// a pointer to the message type.
275//
Joe Tsai4fe96632019-05-22 05:12:36 -0400276// TODO: Unfortunately, we need to close over a pointer and MessageInfo,
Joe Tsaic6b75612018-09-13 14:24:37 -0700277// which incurs an an allocation. This pair is similar to a Go interface,
278// which is essentially a tuple of the same thing. We can make this efficient
279// with reflect.NamedOf (see https://golang.org/issues/16522).
280//
281// With that hypothetical API, we could dynamically create a new named type
Joe Tsai4fe96632019-05-22 05:12:36 -0400282// that has the same underlying type as MessageInfo.GoType, and
283// dynamically create methods that close over MessageInfo.
Joe Tsaic6b75612018-09-13 14:24:37 -0700284// Since the new type would have the same underlying type, we could directly
285// convert between pointers of those types, giving us an efficient way to swap
286// out the method set.
287//
288// Barring the ability to dynamically create named types, the workaround is
289// 1. either to accept the cost of an allocation for this wrapper struct or
290// 2. generate more types and methods, at the expense of binary size increase.
291type messageDataType struct {
292 p pointer
Joe Tsai4fe96632019-05-22 05:12:36 -0400293 mi *MessageInfo
Joe Tsaic6b75612018-09-13 14:24:37 -0700294}
295
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700296type messageReflectWrapper messageDataType
Joe Tsai08e00302018-11-26 22:32:06 -0800297
Joe Tsai0fc49f82019-05-01 12:29:25 -0700298// TODO: Remove this.
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700299func (m *messageReflectWrapper) Type() pref.MessageType {
Damien Neil8012b442019-01-18 09:32:24 -0800300 return m.mi.PBType
Joe Tsai08e00302018-11-26 22:32:06 -0800301}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700302func (m *messageReflectWrapper) Descriptor() pref.MessageDescriptor {
303 return m.mi.PBType.Descriptor()
304}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700305func (m *messageReflectWrapper) KnownFields() pref.KnownFields {
Damien Neil8012b442019-01-18 09:32:24 -0800306 m.mi.init()
Joe Tsai08e00302018-11-26 22:32:06 -0800307 return (*knownFields)(m)
308}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700309func (m *messageReflectWrapper) UnknownFields() pref.UnknownFields {
Damien Neil8012b442019-01-18 09:32:24 -0800310 m.mi.init()
Joe Tsai08e00302018-11-26 22:32:06 -0800311 return m.mi.unknownFields((*messageDataType)(m))
312}
Joe Tsai0fc49f82019-05-01 12:29:25 -0700313func (m *messageReflectWrapper) New() pref.Message {
314 return m.mi.PBType.New()
315}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700316func (m *messageReflectWrapper) Interface() pref.ProtoMessage {
Joe Tsai08e00302018-11-26 22:32:06 -0800317 if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
318 return m
319 }
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700320 return (*messageIfaceWrapper)(m)
Joe Tsai08e00302018-11-26 22:32:06 -0800321}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700322func (m *messageReflectWrapper) ProtoUnwrap() interface{} {
Damien Neil8012b442019-01-18 09:32:24 -0800323 return m.p.AsIfaceOf(m.mi.GoType.Elem())
Joe Tsai08e00302018-11-26 22:32:06 -0800324}
Joe Tsai08e00302018-11-26 22:32:06 -0800325
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700326var _ pvalue.Unwrapper = (*messageReflectWrapper)(nil)
327
328type messageIfaceWrapper messageDataType
329
330func (m *messageIfaceWrapper) ProtoReflect() pref.Message {
331 return (*messageReflectWrapper)(m)
332}
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700333func (m *messageIfaceWrapper) XXX_Methods() *piface.Methods {
Damien Neilc37adef2019-04-01 13:49:56 -0700334 // TODO: Consider not recreating this on every call.
335 m.mi.init()
336 return &piface.Methods{
337 Flags: piface.MethodFlagDeterministicMarshal,
338 MarshalAppend: m.marshalAppend,
339 Size: m.size,
340 }
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700341}
Joe Tsai22b1ebd2019-03-11 13:45:14 -0700342func (m *messageIfaceWrapper) ProtoUnwrap() interface{} {
343 return m.p.AsIfaceOf(m.mi.GoType.Elem())
344}
Damien Neilc37adef2019-04-01 13:49:56 -0700345func (m *messageIfaceWrapper) marshalAppend(b []byte, _ pref.ProtoMessage, opts piface.MarshalOptions) ([]byte, error) {
346 return m.mi.marshalAppendPointer(b, m.p, newMarshalOptions(opts))
347}
348func (m *messageIfaceWrapper) size(msg pref.ProtoMessage) (size int) {
349 return m.mi.sizePointer(m.p, 0)
350}
Joe Tsai08e00302018-11-26 22:32:06 -0800351
Joe Tsaic6b75612018-09-13 14:24:37 -0700352type knownFields messageDataType
353
Joe Tsaic6b75612018-09-13 14:24:37 -0700354func (fs *knownFields) Len() (cnt int) {
355 for _, fi := range fs.mi.fields {
356 if fi.has(fs.p) {
357 cnt++
358 }
359 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700360 return cnt + fs.extensionFields().Len()
Joe Tsaic6b75612018-09-13 14:24:37 -0700361}
362func (fs *knownFields) Has(n pref.FieldNumber) bool {
363 if fi := fs.mi.fields[n]; fi != nil {
364 return fi.has(fs.p)
365 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700366 return fs.extensionFields().Has(n)
Joe Tsaic6b75612018-09-13 14:24:37 -0700367}
368func (fs *knownFields) Get(n pref.FieldNumber) pref.Value {
369 if fi := fs.mi.fields[n]; fi != nil {
370 return fi.get(fs.p)
371 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700372 return fs.extensionFields().Get(n)
Joe Tsaic6b75612018-09-13 14:24:37 -0700373}
374func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) {
375 if fi := fs.mi.fields[n]; fi != nil {
376 fi.set(fs.p, v)
377 return
378 }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700379 if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800380 fs.extensionFields().Set(n, v)
381 return
382 }
383 panic(fmt.Sprintf("invalid field: %d", n))
Joe Tsaic6b75612018-09-13 14:24:37 -0700384}
385func (fs *knownFields) Clear(n pref.FieldNumber) {
386 if fi := fs.mi.fields[n]; fi != nil {
387 fi.clear(fs.p)
388 return
389 }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700390 if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800391 fs.extensionFields().Clear(n)
392 return
393 }
Joe Tsaic6b75612018-09-13 14:24:37 -0700394}
Joe Tsai4ec39c72019-04-03 13:40:53 -0700395func (fs *knownFields) WhichOneof(s pref.Name) pref.FieldNumber {
396 if oi := fs.mi.oneofs[s]; oi != nil {
397 return oi.which(fs.p)
398 }
399 return 0
400}
Joe Tsaic6b75612018-09-13 14:24:37 -0700401func (fs *knownFields) Range(f func(pref.FieldNumber, pref.Value) bool) {
402 for n, fi := range fs.mi.fields {
403 if fi.has(fs.p) {
404 if !f(n, fi.get(fs.p)) {
405 return
406 }
407 }
408 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700409 fs.extensionFields().Range(f)
Joe Tsaic6b75612018-09-13 14:24:37 -0700410}
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800411func (fs *knownFields) NewMessage(n pref.FieldNumber) pref.Message {
Damien Neil97e7f572018-12-07 14:28:33 -0800412 if fi := fs.mi.fields[n]; fi != nil {
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800413 return fi.newMessage()
Damien Neil97e7f572018-12-07 14:28:33 -0800414 }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700415 if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) {
Damien Neil97e7f572018-12-07 14:28:33 -0800416 return fs.extensionFields().NewMessage(n)
417 }
418 panic(fmt.Sprintf("invalid field: %d", n))
419}
Joe Tsaic6b75612018-09-13 14:24:37 -0700420func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes {
Joe Tsaibe5348c2018-10-23 18:31:18 -0700421 return fs.extensionFields().ExtensionTypes()
422}
423func (fs *knownFields) extensionFields() pref.KnownFields {
424 return fs.mi.extensionFields((*messageDataType)(fs))
Joe Tsaic6b75612018-09-13 14:24:37 -0700425}
426
Joe Tsaibe5348c2018-10-23 18:31:18 -0700427type emptyUnknownFields struct{}
Joe Tsaic6b75612018-09-13 14:24:37 -0700428
Joe Tsaibe5348c2018-10-23 18:31:18 -0700429func (emptyUnknownFields) Len() int { return 0 }
430func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800431func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop
432func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700433func (emptyUnknownFields) IsSupported() bool { return false }
Joe Tsaic6b75612018-09-13 14:24:37 -0700434
Joe Tsaibe5348c2018-10-23 18:31:18 -0700435type emptyExtensionFields struct{}
Joe Tsaic6b75612018-09-13 14:24:37 -0700436
Joe Tsaif0c01e42018-11-06 13:05:20 -0800437func (emptyExtensionFields) Len() int { return 0 }
438func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false }
439func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} }
440func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") }
441func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop
Joe Tsai4ec39c72019-04-03 13:40:53 -0700442func (emptyExtensionFields) WhichOneof(pref.Name) pref.FieldNumber { return 0 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800443func (emptyExtensionFields) Range(func(pref.FieldNumber, pref.Value) bool) { return }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800444func (emptyExtensionFields) NewMessage(pref.FieldNumber) pref.Message {
Damien Neil97e7f572018-12-07 14:28:33 -0800445 panic("extensions not supported")
446}
447func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700448
449type emptyExtensionTypes struct{}
450
451func (emptyExtensionTypes) Len() int { return 0 }
452func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800453func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop
Joe Tsaibe5348c2018-10-23 18:31:18 -0700454func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil }
455func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800456func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return }