blob: c55cbc805524344922a6b786996f29a159d90555 [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 (
8 "fmt"
9 "reflect"
10
Joe Tsai01ab2962018-09-21 17:44:00 -070011 "github.com/golang/protobuf/v2/internal/flags"
Joe Tsaif0c01e42018-11-06 13:05:20 -080012 pvalue "github.com/golang/protobuf/v2/internal/value"
Joe Tsai01ab2962018-09-21 17:44:00 -070013 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070014)
15
16type fieldInfo struct {
17 // TODO: specialize marshal and unmarshal functions?
18
19 has func(pointer) bool
20 get func(pointer) pref.Value
21 set func(pointer, pref.Value)
22 clear func(pointer)
23 mutable func(pointer) pref.Mutable
24}
25
26func fieldInfoForWeak(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
27 if !flags.Proto1Legacy {
28 panic("weak fields not supported")
29 }
30 // TODO: support weak fields.
31 panic(fmt.Sprintf("invalid field: %v", fd))
32}
33
34func fieldInfoForOneof(fd pref.FieldDescriptor, fs reflect.StructField, ot reflect.Type) fieldInfo {
Joe Tsai2c870bb2018-10-17 11:46:52 -070035 ft := fs.Type
36 if ft.Kind() != reflect.Interface {
37 panic(fmt.Sprintf("invalid type: got %v, want interface kind", ft))
38 }
39 if ot.Kind() != reflect.Struct {
40 panic(fmt.Sprintf("invalid type: got %v, want struct kind", ot))
41 }
42 if !reflect.PtrTo(ot).Implements(ft) {
43 panic(fmt.Sprintf("invalid type: %v does not implement %v", ot, ft))
44 }
Joe Tsai08e00302018-11-26 22:32:06 -080045 conv := pvalue.NewLegacyConverter(ot.Field(0).Type, fd.Kind(), legacyWrapper)
Joe Tsai2c870bb2018-10-17 11:46:52 -070046 fieldOffset := offsetOf(fs)
47 // TODO: Implement unsafe fast path?
48 return fieldInfo{
49 // NOTE: The logic below intentionally assumes that oneof fields are
50 // well-formatted. That is, the oneof interface never contains a
51 // typed nil pointer to one of the wrapper structs.
52
53 has: func(p pointer) bool {
Joe Tsai6cf80c42018-12-01 04:57:09 -080054 if p.IsNil() {
55 return false
56 }
57 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsai2c870bb2018-10-17 11:46:52 -070058 if rv.IsNil() || rv.Elem().Type().Elem() != ot {
59 return false
60 }
61 return true
62 },
63 get: func(p pointer) pref.Value {
Joe Tsai6cf80c42018-12-01 04:57:09 -080064 if p.IsNil() {
65 return defaultValueOf(fd)
66 }
67 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsai2c870bb2018-10-17 11:46:52 -070068 if rv.IsNil() || rv.Elem().Type().Elem() != ot {
Joe Tsai6cf80c42018-12-01 04:57:09 -080069 return defaultValueOf(fd)
Joe Tsai2c870bb2018-10-17 11:46:52 -070070 }
71 rv = rv.Elem().Elem().Field(0)
Joe Tsai88bc5a72018-11-05 11:42:22 -080072 return conv.PBValueOf(rv)
Joe Tsai2c870bb2018-10-17 11:46:52 -070073 },
74 set: func(p pointer, v pref.Value) {
Joe Tsai6cf80c42018-12-01 04:57:09 -080075 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsai2c870bb2018-10-17 11:46:52 -070076 if rv.IsNil() || rv.Elem().Type().Elem() != ot {
77 rv.Set(reflect.New(ot))
78 }
79 rv = rv.Elem().Elem().Field(0)
Joe Tsai88bc5a72018-11-05 11:42:22 -080080 rv.Set(conv.GoValueOf(v))
Joe Tsai2c870bb2018-10-17 11:46:52 -070081 },
82 clear: func(p pointer) {
Joe Tsai6cf80c42018-12-01 04:57:09 -080083 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsai2c870bb2018-10-17 11:46:52 -070084 if rv.IsNil() || rv.Elem().Type().Elem() != ot {
85 return
86 }
87 rv.Set(reflect.Zero(rv.Type()))
88 },
89 mutable: func(p pointer) pref.Mutable {
90 // Mutable is only valid for messages and panics for other kinds.
Joe Tsai6cf80c42018-12-01 04:57:09 -080091 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsai2c870bb2018-10-17 11:46:52 -070092 if rv.IsNil() || rv.Elem().Type().Elem() != ot {
93 rv.Set(reflect.New(ot))
94 }
95 rv = rv.Elem().Elem().Field(0)
96 if rv.IsNil() {
Joe Tsai6f9095c2018-11-10 14:12:21 -080097 pv := pref.ValueOf(conv.MessageType.New().ProtoReflect())
Joe Tsai88bc5a72018-11-05 11:42:22 -080098 rv.Set(conv.GoValueOf(pv))
Joe Tsai2c870bb2018-10-17 11:46:52 -070099 }
Joe Tsaif6d4a422018-11-19 14:26:06 -0800100 return conv.PBValueOf(rv).Message()
Joe Tsai2c870bb2018-10-17 11:46:52 -0700101 },
102 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700103}
104
105func fieldInfoForMap(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000106 ft := fs.Type
107 if ft.Kind() != reflect.Map {
108 panic(fmt.Sprintf("invalid type: got %v, want map kind", ft))
109 }
Joe Tsai08e00302018-11-26 22:32:06 -0800110 keyConv := pvalue.NewLegacyConverter(ft.Key(), fd.MessageType().Fields().ByNumber(1).Kind(), legacyWrapper)
111 valConv := pvalue.NewLegacyConverter(ft.Elem(), fd.MessageType().Fields().ByNumber(2).Kind(), legacyWrapper)
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000112 fieldOffset := offsetOf(fs)
113 // TODO: Implement unsafe fast path?
114 return fieldInfo{
115 has: func(p pointer) bool {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800116 if p.IsNil() {
117 return false
118 }
119 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000120 return rv.Len() > 0
121 },
122 get: func(p pointer) pref.Value {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800123 if p.IsNil() {
124 v := reflect.Zero(reflect.PtrTo(fs.Type)).Interface()
125 return pref.ValueOf(pvalue.MapOf(v, keyConv, valConv))
126 }
127 v := p.Apply(fieldOffset).AsIfaceOf(fs.Type)
Joe Tsaif0c01e42018-11-06 13:05:20 -0800128 return pref.ValueOf(pvalue.MapOf(v, keyConv, valConv))
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000129 },
130 set: func(p pointer, v pref.Value) {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800131 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaiba0ef9a2018-11-29 14:54:05 -0800132 rv.Set(reflect.ValueOf(v.Map().(pvalue.Unwrapper).ProtoUnwrap()).Elem())
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000133 },
134 clear: func(p pointer) {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800135 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000136 rv.Set(reflect.Zero(rv.Type()))
137 },
138 mutable: func(p pointer) pref.Mutable {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800139 v := p.Apply(fieldOffset).AsIfaceOf(fs.Type)
Joe Tsaif0c01e42018-11-06 13:05:20 -0800140 return pvalue.MapOf(v, keyConv, valConv)
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000141 },
142 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700143}
144
Joe Tsai4b7aff62018-11-14 14:05:19 -0800145func fieldInfoForList(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
Joe Tsai91e14662018-09-13 13:24:35 -0700146 ft := fs.Type
147 if ft.Kind() != reflect.Slice {
148 panic(fmt.Sprintf("invalid type: got %v, want slice kind", ft))
149 }
Joe Tsai08e00302018-11-26 22:32:06 -0800150 conv := pvalue.NewLegacyConverter(ft.Elem(), fd.Kind(), legacyWrapper)
Joe Tsai91e14662018-09-13 13:24:35 -0700151 fieldOffset := offsetOf(fs)
152 // TODO: Implement unsafe fast path?
153 return fieldInfo{
154 has: func(p pointer) bool {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800155 if p.IsNil() {
156 return false
157 }
158 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsai91e14662018-09-13 13:24:35 -0700159 return rv.Len() > 0
160 },
161 get: func(p pointer) pref.Value {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800162 if p.IsNil() {
163 v := reflect.Zero(reflect.PtrTo(fs.Type)).Interface()
164 return pref.ValueOf(pvalue.ListOf(v, conv))
165 }
166 v := p.Apply(fieldOffset).AsIfaceOf(fs.Type)
Joe Tsai4b7aff62018-11-14 14:05:19 -0800167 return pref.ValueOf(pvalue.ListOf(v, conv))
Joe Tsai91e14662018-09-13 13:24:35 -0700168 },
169 set: func(p pointer, v pref.Value) {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800170 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaiba0ef9a2018-11-29 14:54:05 -0800171 rv.Set(reflect.ValueOf(v.List().(pvalue.Unwrapper).ProtoUnwrap()).Elem())
Joe Tsai91e14662018-09-13 13:24:35 -0700172 },
173 clear: func(p pointer) {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800174 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsai91e14662018-09-13 13:24:35 -0700175 rv.Set(reflect.Zero(rv.Type()))
176 },
177 mutable: func(p pointer) pref.Mutable {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800178 v := p.Apply(fieldOffset).AsIfaceOf(fs.Type)
Joe Tsai4b7aff62018-11-14 14:05:19 -0800179 return pvalue.ListOf(v, conv)
Joe Tsai91e14662018-09-13 13:24:35 -0700180 },
181 }
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700182}
183
184var emptyBytes = reflect.ValueOf([]byte{})
185
186func fieldInfoForScalar(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
187 ft := fs.Type
188 nullable := fd.Syntax() == pref.Proto2
189 if nullable {
190 if ft.Kind() != reflect.Ptr && ft.Kind() != reflect.Slice {
191 panic(fmt.Sprintf("invalid type: got %v, want pointer", ft))
192 }
193 if ft.Kind() == reflect.Ptr {
194 ft = ft.Elem()
195 }
196 }
Joe Tsai08e00302018-11-26 22:32:06 -0800197 conv := pvalue.NewLegacyConverter(ft, fd.Kind(), legacyWrapper)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700198 fieldOffset := offsetOf(fs)
199 // TODO: Implement unsafe fast path?
200 return fieldInfo{
201 has: func(p pointer) bool {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800202 if p.IsNil() {
203 return false
204 }
205 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700206 if nullable {
207 return !rv.IsNil()
208 }
209 switch rv.Kind() {
210 case reflect.Bool:
211 return rv.Bool()
212 case reflect.Int32, reflect.Int64:
Joe Tsai44e389c2018-11-19 15:27:09 -0800213 return rv.Int() != 0
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700214 case reflect.Uint32, reflect.Uint64:
Joe Tsai44e389c2018-11-19 15:27:09 -0800215 return rv.Uint() != 0
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700216 case reflect.Float32, reflect.Float64:
Joe Tsai44e389c2018-11-19 15:27:09 -0800217 return rv.Float() != 0
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700218 case reflect.String, reflect.Slice:
219 return rv.Len() > 0
220 default:
221 panic(fmt.Sprintf("invalid type: %v", rv.Type())) // should never happen
222 }
223 },
224 get: func(p pointer) pref.Value {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800225 if p.IsNil() {
226 return defaultValueOf(fd)
227 }
228 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700229 if nullable {
230 if rv.IsNil() {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800231 return defaultValueOf(fd)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700232 }
233 if rv.Kind() == reflect.Ptr {
234 rv = rv.Elem()
235 }
236 }
Joe Tsai88bc5a72018-11-05 11:42:22 -0800237 return conv.PBValueOf(rv)
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700238 },
239 set: func(p pointer, v pref.Value) {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800240 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700241 if nullable && rv.Kind() == reflect.Ptr {
242 if rv.IsNil() {
243 rv.Set(reflect.New(ft))
244 }
245 rv = rv.Elem()
246 }
Joe Tsai88bc5a72018-11-05 11:42:22 -0800247 rv.Set(conv.GoValueOf(v))
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700248 if nullable && rv.Kind() == reflect.Slice && rv.IsNil() {
249 rv.Set(emptyBytes)
250 }
251 },
252 clear: func(p pointer) {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800253 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700254 rv.Set(reflect.Zero(rv.Type()))
255 },
256 mutable: func(p pointer) pref.Mutable {
257 panic("invalid mutable call")
258 },
259 }
260}
261
262func fieldInfoForMessage(fd pref.FieldDescriptor, fs reflect.StructField) fieldInfo {
Joe Tsaice6edd32018-10-19 16:27:46 -0700263 ft := fs.Type
Joe Tsai08e00302018-11-26 22:32:06 -0800264 conv := pvalue.NewLegacyConverter(ft, fd.Kind(), legacyWrapper)
Joe Tsaice6edd32018-10-19 16:27:46 -0700265 fieldOffset := offsetOf(fs)
266 // TODO: Implement unsafe fast path?
267 return fieldInfo{
268 has: func(p pointer) bool {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800269 if p.IsNil() {
270 return false
271 }
272 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaice6edd32018-10-19 16:27:46 -0700273 return !rv.IsNil()
274 },
275 get: func(p pointer) pref.Value {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800276 if p.IsNil() {
277 return pref.Value{}
278 }
279 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaif6d4a422018-11-19 14:26:06 -0800280 if rv.IsNil() {
281 return pref.Value{}
282 }
Joe Tsai88bc5a72018-11-05 11:42:22 -0800283 return conv.PBValueOf(rv)
Joe Tsaice6edd32018-10-19 16:27:46 -0700284 },
285 set: func(p pointer, v pref.Value) {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800286 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsai88bc5a72018-11-05 11:42:22 -0800287 rv.Set(conv.GoValueOf(v))
Joe Tsaif6d4a422018-11-19 14:26:06 -0800288 if rv.IsNil() {
289 panic("invalid nil pointer")
290 }
Joe Tsaice6edd32018-10-19 16:27:46 -0700291 },
292 clear: func(p pointer) {
Joe Tsai6cf80c42018-12-01 04:57:09 -0800293 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaice6edd32018-10-19 16:27:46 -0700294 rv.Set(reflect.Zero(rv.Type()))
295 },
296 mutable: func(p pointer) pref.Mutable {
297 // Mutable is only valid for messages and panics for other kinds.
Joe Tsai6cf80c42018-12-01 04:57:09 -0800298 rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
Joe Tsaice6edd32018-10-19 16:27:46 -0700299 if rv.IsNil() {
Joe Tsai6f9095c2018-11-10 14:12:21 -0800300 pv := pref.ValueOf(conv.MessageType.New().ProtoReflect())
Joe Tsai88bc5a72018-11-05 11:42:22 -0800301 rv.Set(conv.GoValueOf(pv))
Joe Tsaice6edd32018-10-19 16:27:46 -0700302 }
Joe Tsai88bc5a72018-11-05 11:42:22 -0800303 return conv.PBValueOf(rv).Message()
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700304 },
305 }
306}
Joe Tsai6cf80c42018-12-01 04:57:09 -0800307
308// defaultValueOf returns the default value for the field.
309func defaultValueOf(fd pref.FieldDescriptor) pref.Value {
310 if fd == nil {
311 return pref.Value{}
312 }
313 pv := fd.Default() // invalid Value for messages and repeated fields
314 if fd.Kind() == pref.BytesKind && pv.IsValid() && len(pv.Bytes()) > 0 {
315 return pref.ValueOf(append([]byte(nil), pv.Bytes()...)) // copy default bytes for safety
316 }
317 return pv
318}