blob: 57ca93a654882fab3605ccd7397c6dbf912d2920 [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"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070013
Joe Tsai01ab2962018-09-21 17:44:00 -070014 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070015)
16
Joe Tsaif0c01e42018-11-06 13:05:20 -080017// MessageOf returns the protoreflect.Message interface over p.
18// If p already implements proto.Message, then it directly calls the
19// ProtoReflect method, otherwise it wraps the legacy v1 message to implement
20// the v2 reflective interface.
21func MessageOf(p interface{}) pref.Message {
22 if m, ok := p.(pref.ProtoMessage); ok {
23 return m.ProtoReflect()
24 }
25 return legacyWrapMessage(reflect.ValueOf(p)).ProtoReflect()
26}
27
Joe Tsaic6b75612018-09-13 14:24:37 -070028// MessageType provides protobuf related functionality for a given Go type
29// that represents a message. A given instance of MessageType is tied to
30// exactly one Go type, which must be a pointer to a struct type.
31type MessageType struct {
Joe Tsaif0c01e42018-11-06 13:05:20 -080032 // Type is the underlying message type and must be populated.
Joe Tsaic6b75612018-09-13 14:24:37 -070033 // Once set, this field must never be mutated.
Joe Tsaif0c01e42018-11-06 13:05:20 -080034 Type pref.MessageType
Joe Tsaic6b75612018-09-13 14:24:37 -070035
36 once sync.Once // protects all unexported fields
37
Joe Tsaif0c01e42018-11-06 13:05:20 -080038 goType reflect.Type // pointer to struct
Joe Tsaic6b75612018-09-13 14:24:37 -070039
Joe Tsaifa02f4e2018-09-12 16:20:37 -070040 // TODO: Split fields into dense and sparse maps similar to the current
41 // table-driven implementation in v1?
42 fields map[pref.FieldNumber]*fieldInfo
Joe Tsaibe5348c2018-10-23 18:31:18 -070043
44 unknownFields func(*messageDataType) pref.UnknownFields
45 extensionFields func(*messageDataType) pref.KnownFields
Joe Tsaifa02f4e2018-09-12 16:20:37 -070046}
47
Joe Tsaic6b75612018-09-13 14:24:37 -070048// init lazily initializes the MessageType upon first use and
49// also checks that the provided pointer p is of the correct Go type.
50//
51// It must be called at the start of every exported method.
52func (mi *MessageType) init(p interface{}) {
53 mi.once.Do(func() {
Joe Tsaif0c01e42018-11-06 13:05:20 -080054 t := reflect.TypeOf(p)
Joe Tsaic6b75612018-09-13 14:24:37 -070055 if t.Kind() != reflect.Ptr && t.Elem().Kind() != reflect.Struct {
56 panic(fmt.Sprintf("got %v, want *struct kind", t))
57 }
58 mi.goType = t
59
Joe Tsai95b02902018-10-31 18:23:42 -070060 mi.makeKnownFieldsFunc(t.Elem())
61 mi.makeUnknownFieldsFunc(t.Elem())
62 mi.makeExtensionFieldsFunc(t.Elem())
Joe Tsaic6b75612018-09-13 14:24:37 -070063 })
64
65 // TODO: Remove this check? This API is primarily used by generated code,
66 // and should not violate this assumption. Leave this check in for now to
67 // provide some sanity checks during development. This can be removed if
68 // it proves to be detrimental to performance.
69 if reflect.TypeOf(p) != mi.goType {
70 panic(fmt.Sprintf("type mismatch: got %T, want %v", p, mi.goType))
71 }
72}
73
Joe Tsaif0c01e42018-11-06 13:05:20 -080074// makeKnownFieldsFunc generates functions for operations that can be performed
75// on each protobuf message field. It takes in a reflect.Type representing the
76// Go struct and matches message fields with struct fields.
Joe Tsaifa02f4e2018-09-12 16:20:37 -070077//
78// This code assumes that the struct is well-formed and panics if there are
79// any discrepancies.
Joe Tsai95b02902018-10-31 18:23:42 -070080func (mi *MessageType) makeKnownFieldsFunc(t reflect.Type) {
Joe Tsaifa02f4e2018-09-12 16:20:37 -070081 // Generate a mapping of field numbers and names to Go struct field or type.
82 fields := map[pref.FieldNumber]reflect.StructField{}
83 oneofs := map[pref.Name]reflect.StructField{}
84 oneofFields := map[pref.FieldNumber]reflect.Type{}
85 special := map[string]reflect.StructField{}
86fieldLoop:
87 for i := 0; i < t.NumField(); i++ {
88 f := t.Field(i)
89 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
90 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
91 n, _ := strconv.ParseUint(s, 10, 64)
92 fields[pref.FieldNumber(n)] = f
93 continue fieldLoop
94 }
95 }
96 if s := f.Tag.Get("protobuf_oneof"); len(s) > 0 {
97 oneofs[pref.Name(s)] = f
98 continue fieldLoop
99 }
100 switch f.Name {
101 case "XXX_weak", "XXX_unrecognized", "XXX_sizecache", "XXX_extensions", "XXX_InternalExtensions":
102 special[f.Name] = f
103 continue fieldLoop
104 }
105 }
Joe Tsai2c870bb2018-10-17 11:46:52 -0700106 if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
Joe Tsai90fe9962018-10-18 11:06:29 -0700107 vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700108 oneofLoop:
109 for _, v := range vs.Interface().([]interface{}) {
110 tf := reflect.TypeOf(v).Elem()
111 f := tf.Field(0)
112 for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
113 if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
114 n, _ := strconv.ParseUint(s, 10, 64)
115 oneofFields[pref.FieldNumber(n)] = tf
116 continue oneofLoop
117 }
118 }
119 }
120 }
121
122 mi.fields = map[pref.FieldNumber]*fieldInfo{}
Joe Tsaif0c01e42018-11-06 13:05:20 -0800123 for i := 0; i < mi.Type.Fields().Len(); i++ {
124 fd := mi.Type.Fields().Get(i)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700125 fs := fields[fd.Number()]
126 var fi fieldInfo
127 switch {
128 case fd.IsWeak():
129 fi = fieldInfoForWeak(fd, special["XXX_weak"])
130 case fd.OneofType() != nil:
131 fi = fieldInfoForOneof(fd, oneofs[fd.OneofType().Name()], oneofFields[fd.Number()])
132 case fd.IsMap():
133 fi = fieldInfoForMap(fd, fs)
134 case fd.Cardinality() == pref.Repeated:
135 fi = fieldInfoForVector(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700136 case fd.Kind() == pref.MessageKind || fd.Kind() == pref.GroupKind:
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700137 fi = fieldInfoForMessage(fd, fs)
Joe Tsaic6b75612018-09-13 14:24:37 -0700138 default:
139 fi = fieldInfoForScalar(fd, fs)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700140 }
141 mi.fields[fd.Number()] = &fi
142 }
143}
Joe Tsaic6b75612018-09-13 14:24:37 -0700144
Joe Tsai95b02902018-10-31 18:23:42 -0700145func (mi *MessageType) makeUnknownFieldsFunc(t reflect.Type) {
146 if f := makeLegacyUnknownFieldsFunc(t); f != nil {
Joe Tsaie2afdc22018-10-25 14:06:56 -0700147 mi.unknownFields = f
148 return
149 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700150 mi.unknownFields = func(*messageDataType) pref.UnknownFields {
151 return emptyUnknownFields{}
152 }
153}
154
Joe Tsai95b02902018-10-31 18:23:42 -0700155func (mi *MessageType) makeExtensionFieldsFunc(t reflect.Type) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800156 if f := makeLegacyExtensionFieldsFunc(t); f != nil {
157 mi.extensionFields = f
158 return
159 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700160 mi.extensionFields = func(*messageDataType) pref.KnownFields {
161 return emptyExtensionFields{}
162 }
163}
164
Joe Tsaic6b75612018-09-13 14:24:37 -0700165func (mi *MessageType) KnownFieldsOf(p interface{}) pref.KnownFields {
Joe Tsaic6b75612018-09-13 14:24:37 -0700166 return (*knownFields)(mi.dataTypeOf(p))
167}
168
169func (mi *MessageType) UnknownFieldsOf(p interface{}) pref.UnknownFields {
Joe Tsaibe5348c2018-10-23 18:31:18 -0700170 return mi.unknownFields(mi.dataTypeOf(p))
Joe Tsaic6b75612018-09-13 14:24:37 -0700171}
172
173func (mi *MessageType) dataTypeOf(p interface{}) *messageDataType {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800174 mi.init(p)
Joe Tsaic6b75612018-09-13 14:24:37 -0700175 return &messageDataType{pointerOfIface(&p), mi}
176}
177
178// messageDataType is a tuple of a pointer to the message data and
179// a pointer to the message type.
180//
181// TODO: Unfortunately, we need to close over a pointer and MessageType,
182// which incurs an an allocation. This pair is similar to a Go interface,
183// which is essentially a tuple of the same thing. We can make this efficient
184// with reflect.NamedOf (see https://golang.org/issues/16522).
185//
186// With that hypothetical API, we could dynamically create a new named type
187// that has the same underlying type as MessageType.goType, and
188// dynamically create methods that close over MessageType.
189// Since the new type would have the same underlying type, we could directly
190// convert between pointers of those types, giving us an efficient way to swap
191// out the method set.
192//
193// Barring the ability to dynamically create named types, the workaround is
194// 1. either to accept the cost of an allocation for this wrapper struct or
195// 2. generate more types and methods, at the expense of binary size increase.
196type messageDataType struct {
197 p pointer
198 mi *MessageType
199}
200
Joe Tsaic6b75612018-09-13 14:24:37 -0700201type knownFields messageDataType
202
Joe Tsaic6b75612018-09-13 14:24:37 -0700203func (fs *knownFields) Len() (cnt int) {
204 for _, fi := range fs.mi.fields {
205 if fi.has(fs.p) {
206 cnt++
207 }
208 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700209 return cnt + fs.extensionFields().Len()
Joe Tsaic6b75612018-09-13 14:24:37 -0700210}
211func (fs *knownFields) Has(n pref.FieldNumber) bool {
212 if fi := fs.mi.fields[n]; fi != nil {
213 return fi.has(fs.p)
214 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700215 return fs.extensionFields().Has(n)
Joe Tsaic6b75612018-09-13 14:24:37 -0700216}
217func (fs *knownFields) Get(n pref.FieldNumber) pref.Value {
218 if fi := fs.mi.fields[n]; fi != nil {
219 return fi.get(fs.p)
220 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700221 return fs.extensionFields().Get(n)
Joe Tsaic6b75612018-09-13 14:24:37 -0700222}
223func (fs *knownFields) Set(n pref.FieldNumber, v pref.Value) {
224 if fi := fs.mi.fields[n]; fi != nil {
225 fi.set(fs.p, v)
226 return
227 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800228 if fs.mi.Type.ExtensionRanges().Has(n) {
229 fs.extensionFields().Set(n, v)
230 return
231 }
232 panic(fmt.Sprintf("invalid field: %d", n))
Joe Tsaic6b75612018-09-13 14:24:37 -0700233}
234func (fs *knownFields) Clear(n pref.FieldNumber) {
235 if fi := fs.mi.fields[n]; fi != nil {
236 fi.clear(fs.p)
237 return
238 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800239 if fs.mi.Type.ExtensionRanges().Has(n) {
240 fs.extensionFields().Clear(n)
241 return
242 }
Joe Tsaic6b75612018-09-13 14:24:37 -0700243}
244func (fs *knownFields) Mutable(n pref.FieldNumber) pref.Mutable {
245 if fi := fs.mi.fields[n]; fi != nil {
246 return fi.mutable(fs.p)
247 }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800248 if fs.mi.Type.ExtensionRanges().Has(n) {
249 return fs.extensionFields().Mutable(n)
250 }
251 panic(fmt.Sprintf("invalid field: %d", n))
Joe Tsaic6b75612018-09-13 14:24:37 -0700252}
253func (fs *knownFields) Range(f func(pref.FieldNumber, pref.Value) bool) {
254 for n, fi := range fs.mi.fields {
255 if fi.has(fs.p) {
256 if !f(n, fi.get(fs.p)) {
257 return
258 }
259 }
260 }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700261 fs.extensionFields().Range(f)
Joe Tsaic6b75612018-09-13 14:24:37 -0700262}
263func (fs *knownFields) ExtensionTypes() pref.ExtensionFieldTypes {
Joe Tsaibe5348c2018-10-23 18:31:18 -0700264 return fs.extensionFields().ExtensionTypes()
265}
266func (fs *knownFields) extensionFields() pref.KnownFields {
267 return fs.mi.extensionFields((*messageDataType)(fs))
Joe Tsaic6b75612018-09-13 14:24:37 -0700268}
269
Joe Tsaibe5348c2018-10-23 18:31:18 -0700270type emptyUnknownFields struct{}
Joe Tsaic6b75612018-09-13 14:24:37 -0700271
Joe Tsaibe5348c2018-10-23 18:31:18 -0700272func (emptyUnknownFields) Len() int { return 0 }
273func (emptyUnknownFields) Get(pref.FieldNumber) pref.RawFields { return nil }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800274func (emptyUnknownFields) Set(pref.FieldNumber, pref.RawFields) { return } // noop
275func (emptyUnknownFields) Range(func(pref.FieldNumber, pref.RawFields) bool) { return }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700276func (emptyUnknownFields) IsSupported() bool { return false }
Joe Tsaic6b75612018-09-13 14:24:37 -0700277
Joe Tsaibe5348c2018-10-23 18:31:18 -0700278type emptyExtensionFields struct{}
Joe Tsaic6b75612018-09-13 14:24:37 -0700279
Joe Tsaif0c01e42018-11-06 13:05:20 -0800280func (emptyExtensionFields) Len() int { return 0 }
281func (emptyExtensionFields) Has(pref.FieldNumber) bool { return false }
282func (emptyExtensionFields) Get(pref.FieldNumber) pref.Value { return pref.Value{} }
283func (emptyExtensionFields) Set(pref.FieldNumber, pref.Value) { panic("extensions not supported") }
284func (emptyExtensionFields) Clear(pref.FieldNumber) { return } // noop
285func (emptyExtensionFields) Mutable(pref.FieldNumber) pref.Mutable { panic("extensions not supported") }
286func (emptyExtensionFields) Range(func(pref.FieldNumber, pref.Value) bool) { return }
287func (emptyExtensionFields) ExtensionTypes() pref.ExtensionFieldTypes { return emptyExtensionTypes{} }
Joe Tsaibe5348c2018-10-23 18:31:18 -0700288
289type emptyExtensionTypes struct{}
290
291func (emptyExtensionTypes) Len() int { return 0 }
292func (emptyExtensionTypes) Register(pref.ExtensionType) { panic("extensions not supported") }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800293func (emptyExtensionTypes) Remove(pref.ExtensionType) { return } // noop
Joe Tsaibe5348c2018-10-23 18:31:18 -0700294func (emptyExtensionTypes) ByNumber(pref.FieldNumber) pref.ExtensionType { return nil }
295func (emptyExtensionTypes) ByName(pref.FullName) pref.ExtensionType { return nil }
Joe Tsai34eb7ef2018-11-07 16:39:49 -0800296func (emptyExtensionTypes) Range(func(pref.ExtensionType) bool) { return }