blob: a836c893939ed5bffaae3d11fa76a895f4cf41e4 [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
Joe Tsai08e00302018-11-26 22:32:06 -08005package impl_test
Joe Tsaifa02f4e2018-09-12 16:20:37 -07006
7import (
Joe Tsai91e14662018-09-13 13:24:35 -07008 "fmt"
9 "math"
Damien Neil8012b442019-01-18 09:32:24 -080010 "reflect"
Joe Tsai82760ce2019-06-20 03:09:57 -070011 "runtime"
Joe Tsai91e14662018-09-13 13:24:35 -070012 "strings"
Joe Tsai82760ce2019-06-20 03:09:57 -070013 "sync"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070014 "testing"
15
Joe Tsai87b955b2018-11-14 21:59:49 -080016 cmp "github.com/google/go-cmp/cmp"
17 cmpopts "github.com/google/go-cmp/cmp/cmpopts"
Joe Tsaid8881392019-06-06 13:01:53 -070018 "google.golang.org/protobuf/encoding/prototext"
Damien Neile89e6242019-05-13 23:55:40 -070019 pimpl "google.golang.org/protobuf/internal/impl"
Joe Tsaid8881392019-06-06 13:01:53 -070020 "google.golang.org/protobuf/proto"
21 pdesc "google.golang.org/protobuf/reflect/protodesc"
Damien Neile89e6242019-05-13 23:55:40 -070022 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaid8881392019-06-06 13:01:53 -070023 "google.golang.org/protobuf/reflect/protoregistry"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070024
Damien Neile89e6242019-05-13 23:55:40 -070025 proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
Joe Tsai82760ce2019-06-20 03:09:57 -070026 testpb "google.golang.org/protobuf/internal/testprotos/test"
Joe Tsaia95b29f2019-05-16 12:47:20 -070027 "google.golang.org/protobuf/types/descriptorpb"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070028)
29
Joe Tsai4b7aff62018-11-14 14:05:19 -080030// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070031type (
Joe Tsai87b955b2018-11-14 21:59:49 -080032 messageOp interface{ isMessageOp() }
Joe Tsai91e14662018-09-13 13:24:35 -070033 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070034
Joe Tsai87b955b2018-11-14 21:59:49 -080035 listOp interface{ isListOp() }
Joe Tsai4b7aff62018-11-14 14:05:19 -080036 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070037
Joe Tsai87b955b2018-11-14 21:59:49 -080038 mapOp interface{ isMapOp() }
Joe Tsaibbfaeb72018-10-17 00:27:21 +000039 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070040)
41
42// Test operations performed on a message.
43type (
Joe Tsai87b955b2018-11-14 21:59:49 -080044 // check that the message contents match
45 equalMessage struct{ pref.Message }
46 // check presence for specific fields in the message
47 hasFields map[pref.FieldNumber]bool
48 // check that specific message fields match
49 getFields map[pref.FieldNumber]pref.Value
50 // set specific message fields
51 setFields map[pref.FieldNumber]pref.Value
52 // clear specific fields in the message
53 clearFields []pref.FieldNumber
Joe Tsai4ec39c72019-04-03 13:40:53 -070054 // check for the presence of specific oneof member fields.
55 whichOneofs map[pref.Name]pref.FieldNumber
Joe Tsai87b955b2018-11-14 21:59:49 -080056 // apply messageOps on each specified message field
Joe Tsai378c1322019-04-25 23:48:08 -070057 messageFields map[pref.FieldNumber]messageOps
58 messageFieldsMutable map[pref.FieldNumber]messageOps
Joe Tsai87b955b2018-11-14 21:59:49 -080059 // apply listOps on each specified list field
Joe Tsai378c1322019-04-25 23:48:08 -070060 listFields map[pref.FieldNumber]listOps
61 listFieldsMutable map[pref.FieldNumber]listOps
Joe Tsai87b955b2018-11-14 21:59:49 -080062 // apply mapOps on each specified map fields
Joe Tsai378c1322019-04-25 23:48:08 -070063 mapFields map[pref.FieldNumber]mapOps
64 mapFieldsMutable map[pref.FieldNumber]mapOps
Joe Tsai87b955b2018-11-14 21:59:49 -080065 // range through all fields and check that they match
66 rangeFields map[pref.FieldNumber]pref.Value
Joe Tsai91e14662018-09-13 13:24:35 -070067)
68
Joe Tsai378c1322019-04-25 23:48:08 -070069func (equalMessage) isMessageOp() {}
70func (hasFields) isMessageOp() {}
71func (getFields) isMessageOp() {}
72func (setFields) isMessageOp() {}
73func (clearFields) isMessageOp() {}
74func (whichOneofs) isMessageOp() {}
75func (messageFields) isMessageOp() {}
76func (messageFieldsMutable) isMessageOp() {}
77func (listFields) isMessageOp() {}
78func (listFieldsMutable) isMessageOp() {}
79func (mapFields) isMessageOp() {}
80func (mapFieldsMutable) isMessageOp() {}
81func (rangeFields) isMessageOp() {}
Joe Tsai87b955b2018-11-14 21:59:49 -080082
Joe Tsai4b7aff62018-11-14 14:05:19 -080083// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070084type (
Joe Tsai87b955b2018-11-14 21:59:49 -080085 // check that the list contents match
86 equalList struct{ pref.List }
87 // check that list length matches
88 lenList int
89 // check that specific list entries match
90 getList map[int]pref.Value
91 // set specific list entries
92 setList map[int]pref.Value
93 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080094 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080095 // apply messageOps on a newly appended message
96 appendMessageList messageOps
97 // truncate the list to the specified length
98 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -070099)
100
Joe Tsai87b955b2018-11-14 21:59:49 -0800101func (equalList) isListOp() {}
102func (lenList) isListOp() {}
103func (getList) isListOp() {}
104func (setList) isListOp() {}
105func (appendList) isListOp() {}
106func (appendMessageList) isListOp() {}
107func (truncList) isListOp() {}
108
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000109// Test operations performed on a map.
110type (
Joe Tsai87b955b2018-11-14 21:59:49 -0800111 // check that the map contents match
112 equalMap struct{ pref.Map }
113 // check that map length matches
114 lenMap int
115 // check presence for specific entries in the map
116 hasMap map[interface{}]bool
117 // check that specific map entries match
118 getMap map[interface{}]pref.Value
119 // set specific map entries
120 setMap map[interface{}]pref.Value
121 // clear specific entries in the map
122 clearMap []interface{}
123 // apply messageOps on each specified message entry
124 messageMap map[interface{}]messageOps
125 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000126 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000127)
128
Joe Tsai87b955b2018-11-14 21:59:49 -0800129func (equalMap) isMapOp() {}
130func (lenMap) isMapOp() {}
131func (hasMap) isMapOp() {}
132func (getMap) isMapOp() {}
133func (setMap) isMapOp() {}
134func (clearMap) isMapOp() {}
135func (messageMap) isMapOp() {}
136func (rangeMap) isMapOp() {}
137
Joe Tsaice6edd32018-10-19 16:27:46 -0700138type ScalarProto2 struct {
139 Bool *bool `protobuf:"1"`
140 Int32 *int32 `protobuf:"2"`
141 Int64 *int64 `protobuf:"3"`
142 Uint32 *uint32 `protobuf:"4"`
143 Uint64 *uint64 `protobuf:"5"`
144 Float32 *float32 `protobuf:"6"`
145 Float64 *float64 `protobuf:"7"`
146 String *string `protobuf:"8"`
147 StringA []byte `protobuf:"9"`
148 Bytes []byte `protobuf:"10"`
149 BytesA *string `protobuf:"11"`
150
151 MyBool *MyBool `protobuf:"12"`
152 MyInt32 *MyInt32 `protobuf:"13"`
153 MyInt64 *MyInt64 `protobuf:"14"`
154 MyUint32 *MyUint32 `protobuf:"15"`
155 MyUint64 *MyUint64 `protobuf:"16"`
156 MyFloat32 *MyFloat32 `protobuf:"17"`
157 MyFloat64 *MyFloat64 `protobuf:"18"`
158 MyString *MyString `protobuf:"19"`
159 MyStringA MyBytes `protobuf:"20"`
160 MyBytes MyBytes `protobuf:"21"`
161 MyBytesA *MyString `protobuf:"22"`
162}
163
Joe Tsaid8881392019-06-06 13:01:53 -0700164func mustMakeEnumDesc(path string, syntax pref.Syntax, enumDesc string) pref.EnumDescriptor {
165 s := fmt.Sprintf(`name:%q syntax:%q enum_type:[{%s}]`, path, syntax, enumDesc)
166 pb := new(descriptorpb.FileDescriptorProto)
167 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
168 panic(err)
169 }
170 fd, err := pdesc.NewFile(pb, nil)
Joe Tsai87b955b2018-11-14 21:59:49 -0800171 if err != nil {
172 panic(err)
173 }
Joe Tsaid8881392019-06-06 13:01:53 -0700174 return fd.Enums().Get(0)
Joe Tsai87b955b2018-11-14 21:59:49 -0800175}
176
Joe Tsaid8881392019-06-06 13:01:53 -0700177func mustMakeMessageDesc(path string, syntax pref.Syntax, fileDesc, msgDesc string, r pdesc.Resolver) pref.MessageDescriptor {
178 s := fmt.Sprintf(`name:%q syntax:%q %s message_type:[{%s}]`, path, syntax, fileDesc, msgDesc)
179 pb := new(descriptorpb.FileDescriptorProto)
180 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
181 panic(err)
182 }
183 fd, err := pdesc.NewFile(pb, r)
Joe Tsai87b955b2018-11-14 21:59:49 -0800184 if err != nil {
185 panic(err)
186 }
Joe Tsaid8881392019-06-06 13:01:53 -0700187 return fd.Messages().Get(0)
Joe Tsai87b955b2018-11-14 21:59:49 -0800188}
189
190var V = pref.ValueOf
191var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
192
193type (
194 MyBool bool
195 MyInt32 int32
196 MyInt64 int64
197 MyUint32 uint32
198 MyUint64 uint64
199 MyFloat32 float32
200 MyFloat64 float64
201 MyString string
202 MyBytes []byte
203
204 ListStrings []MyString
205 ListBytes []MyBytes
206
207 MapStrings map[MyString]MyString
208 MapBytes map[MyString]MyBytes
209)
210
Damien Neil16163b42019-08-06 15:43:25 -0700211var scalarProto2Type = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(ScalarProto2)), Desc: mustMakeMessageDesc("scalar2.proto", pref.Proto2, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700212 name: "ScalarProto2"
213 field: [
214 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
215 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2"},
216 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3"},
217 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4"},
218 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5"},
219 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6"},
220 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7"},
221 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8"},
222 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9"},
223 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"10"},
224 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11"},
Joe Tsai91e14662018-09-13 13:24:35 -0700225
Joe Tsaid8881392019-06-06 13:01:53 -0700226 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
227 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"13"},
228 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"14"},
229 {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"15"},
230 {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"16"},
231 {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"17"},
232 {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"18"},
233 {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"19"},
234 {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"20"},
235 {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"21"},
236 {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"22"}
237 ]
238 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700239}
Joe Tsai91e14662018-09-13 13:24:35 -0700240
Joe Tsai378c1322019-04-25 23:48:08 -0700241func (m *ScalarProto2) ProtoReflect() pref.Message { return scalarProto2Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800242
243func TestScalarProto2(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700244 testMessage(t, nil, new(ScalarProto2).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700245 hasFields{
246 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
247 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
248 },
249 getFields{
250 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")),
251 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")),
252 },
253 setFields{
254 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
255 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
256 },
257 hasFields{
258 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
259 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
260 },
Joe Tsai378c1322019-04-25 23:48:08 -0700261 equalMessage{(&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700262 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
263 new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString),
Joe Tsai378c1322019-04-25 23:48:08 -0700264 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800265 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
Joe Tsai378c1322019-04-25 23:48:08 -0700266 equalMessage{new(ScalarProto2).ProtoReflect()},
Joe Tsai3e6a39b2019-07-10 10:21:12 -0700267
268 // Setting a bytes field nil empty bytes should preserve presence.
269 setFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
270 getFields{10: V([]byte{}), 11: V([]byte(nil)), 21: V([]byte{}), 22: V([]byte(nil))},
271 hasFields{10: true, 11: true, 21: true, 22: true},
Joe Tsai91e14662018-09-13 13:24:35 -0700272 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800273
274 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700275 testMessage(t, nil, (*ScalarProto2)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800276 hasFields{
277 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
278 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
279 },
280 getFields{
281 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")),
282 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")),
283 },
284 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700285}
286
Joe Tsaice6edd32018-10-19 16:27:46 -0700287type ScalarProto3 struct {
288 Bool bool `protobuf:"1"`
289 Int32 int32 `protobuf:"2"`
290 Int64 int64 `protobuf:"3"`
291 Uint32 uint32 `protobuf:"4"`
292 Uint64 uint64 `protobuf:"5"`
293 Float32 float32 `protobuf:"6"`
294 Float64 float64 `protobuf:"7"`
295 String string `protobuf:"8"`
296 StringA []byte `protobuf:"9"`
297 Bytes []byte `protobuf:"10"`
298 BytesA string `protobuf:"11"`
299
300 MyBool MyBool `protobuf:"12"`
301 MyInt32 MyInt32 `protobuf:"13"`
302 MyInt64 MyInt64 `protobuf:"14"`
303 MyUint32 MyUint32 `protobuf:"15"`
304 MyUint64 MyUint64 `protobuf:"16"`
305 MyFloat32 MyFloat32 `protobuf:"17"`
306 MyFloat64 MyFloat64 `protobuf:"18"`
307 MyString MyString `protobuf:"19"`
308 MyStringA MyBytes `protobuf:"20"`
309 MyBytes MyBytes `protobuf:"21"`
310 MyBytesA MyString `protobuf:"22"`
311}
312
Damien Neil16163b42019-08-06 15:43:25 -0700313var scalarProto3Type = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(ScalarProto3)), Desc: mustMakeMessageDesc("scalar3.proto", pref.Proto3, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700314 name: "ScalarProto3"
315 field: [
316 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL},
317 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32},
318 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64},
319 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32},
320 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64},
321 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT},
322 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
323 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING},
324 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING},
325 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES},
326 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700327
Joe Tsaid8881392019-06-06 13:01:53 -0700328 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL},
329 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32},
330 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64},
331 {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32},
332 {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64},
333 {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT},
334 {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
335 {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING},
336 {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING},
337 {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES},
338 {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES}
339 ]
340 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700341}
Joe Tsai91e14662018-09-13 13:24:35 -0700342
Joe Tsai378c1322019-04-25 23:48:08 -0700343func (m *ScalarProto3) ProtoReflect() pref.Message { return scalarProto3Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800344
345func TestScalarProto3(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700346 testMessage(t, nil, new(ScalarProto3).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700347 hasFields{
348 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
349 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
350 },
351 getFields{
352 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
353 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
354 },
355 setFields{
356 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
357 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
358 },
359 hasFields{
360 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
361 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
362 },
Joe Tsai378c1322019-04-25 23:48:08 -0700363 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai91e14662018-09-13 13:24:35 -0700364 setFields{
365 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")),
366 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")),
367 },
368 hasFields{
369 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
370 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
371 },
Joe Tsai378c1322019-04-25 23:48:08 -0700372 equalMessage{(&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700373 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
374 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai378c1322019-04-25 23:48:08 -0700375 }).ProtoReflect()},
Joe Tsai44e389c2018-11-19 15:27:09 -0800376 setFields{
377 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
378 },
379 hasFields{
380 2: true, 3: true, 6: true, 7: true,
381 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800382 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
Joe Tsai378c1322019-04-25 23:48:08 -0700383 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai060cdac2019-04-22 11:44:49 -0700384
385 // Verify that -0 triggers proper Has behavior.
386 hasFields{6: false, 7: false},
387 setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
388 hasFields{6: true, 7: true},
Joe Tsai3e6a39b2019-07-10 10:21:12 -0700389
390 // Setting a bytes field to non-nil empty bytes should not preserve presence.
391 setFields{10: V([]byte{}), 11: V([]byte{}), 21: V([]byte{}), 22: V([]byte{})},
392 getFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
393 hasFields{10: false, 11: false, 21: false, 22: false},
Joe Tsai91e14662018-09-13 13:24:35 -0700394 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800395
396 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700397 testMessage(t, nil, (*ScalarProto3)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800398 hasFields{
399 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
400 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
401 },
402 getFields{
403 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
404 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
405 },
406 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700407}
408
Joe Tsaif0c01e42018-11-06 13:05:20 -0800409type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700410 Bools []bool `protobuf:"1"`
411 Int32s []int32 `protobuf:"2"`
412 Int64s []int64 `protobuf:"3"`
413 Uint32s []uint32 `protobuf:"4"`
414 Uint64s []uint64 `protobuf:"5"`
415 Float32s []float32 `protobuf:"6"`
416 Float64s []float64 `protobuf:"7"`
417 Strings []string `protobuf:"8"`
418 StringsA [][]byte `protobuf:"9"`
419 Bytes [][]byte `protobuf:"10"`
420 BytesA []string `protobuf:"11"`
421
422 MyStrings1 []MyString `protobuf:"12"`
423 MyStrings2 []MyBytes `protobuf:"13"`
424 MyBytes1 []MyBytes `protobuf:"14"`
425 MyBytes2 []MyString `protobuf:"15"`
426
Joe Tsai4b7aff62018-11-14 14:05:19 -0800427 MyStrings3 ListStrings `protobuf:"16"`
428 MyStrings4 ListBytes `protobuf:"17"`
429 MyBytes3 ListBytes `protobuf:"18"`
430 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700431}
432
Damien Neil16163b42019-08-06 15:43:25 -0700433var listScalarsType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(ListScalars)), Desc: mustMakeMessageDesc("list-scalars.proto", pref.Proto2, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700434 name: "ListScalars"
435 field: [
436 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_BOOL},
437 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_INT32},
438 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_INT64},
439 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_UINT32},
440 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_UINT64},
441 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_FLOAT},
442 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_DOUBLE},
443 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_STRING},
444 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_STRING},
445 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_BYTES},
446 {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700447
Joe Tsaid8881392019-06-06 13:01:53 -0700448 {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_STRING},
449 {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_STRING},
450 {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_BYTES},
451 {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsai91e14662018-09-13 13:24:35 -0700452
Joe Tsaid8881392019-06-06 13:01:53 -0700453 {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_STRING},
454 {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_STRING},
455 {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_BYTES},
456 {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_BYTES}
457 ]
458 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700459}
Joe Tsai91e14662018-09-13 13:24:35 -0700460
Joe Tsai378c1322019-04-25 23:48:08 -0700461func (m *ListScalars) ProtoReflect() pref.Message { return listScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800462
463func TestListScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700464 empty := new(ListScalars).ProtoReflect()
465 want := (&ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700466 Bools: []bool{true, false, true},
467 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
468 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
469 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
470 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
471 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
472 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
473 Strings: []string{"8", "", "eight"},
474 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
475 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
476 BytesA: []string{"11", "", "eleven"},
477
478 MyStrings1: []MyString{"12", "", "twelve"},
479 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
480 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
481 MyBytes2: []MyString{"15", "", "fifteen"},
482
Joe Tsai4b7aff62018-11-14 14:05:19 -0800483 MyStrings3: ListStrings{"16", "", "sixteen"},
484 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
485 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
486 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsai378c1322019-04-25 23:48:08 -0700487 }).ProtoReflect()
Joe Tsai91e14662018-09-13 13:24:35 -0700488
Joe Tsai378c1322019-04-25 23:48:08 -0700489 testMessage(t, nil, new(ListScalars).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700490 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false},
Joe Tsai378c1322019-04-25 23:48:08 -0700491 getFields{1: getField(empty, 1), 3: getField(empty, 3), 5: getField(empty, 5), 7: getField(empty, 7), 9: getField(empty, 9), 11: getField(empty, 11), 13: getField(empty, 13), 15: getField(empty, 15), 17: getField(empty, 17), 19: getField(empty, 19)},
492 setFields{1: getField(want, 1), 3: getField(want, 3), 5: getField(want, 5), 7: getField(want, 7), 9: getField(want, 9), 11: getField(want, 11), 13: getField(want, 13), 15: getField(want, 15), 17: getField(want, 17), 19: getField(want, 19)},
493 listFieldsMutable{
Joe Tsai91e14662018-09-13 13:24:35 -0700494 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800495 lenList(0),
496 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
497 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700498 equalList{getField(want, 2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700499 },
500 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800501 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
502 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
503 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700504 },
505 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800506 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700507 equalList{getField(want, 6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700508 },
509 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800510 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
511 lenList(6),
512 setList{0: V("8"), 2: V("eight")},
513 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700514 equalList{getField(want, 8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700515 },
516 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800517 appendList{V([]byte(nil)), V([]byte(nil))},
518 setList{0: V([]byte("10"))},
519 appendList{V([]byte("wrong"))},
520 setList{2: V([]byte("ten"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700521 equalList{getField(want, 10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700522 },
523 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800524 appendList{V("12"), V("wrong"), V("twelve")},
525 setList{1: V("")},
Joe Tsai378c1322019-04-25 23:48:08 -0700526 equalList{getField(want, 12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700527 },
528 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800529 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700530 equalList{getField(want, 14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700531 },
532 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800533 appendList{V("16"), V(""), V("sixteen"), V("extra")},
534 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700535 equalList{getField(want, 16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700536 },
537 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800538 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700539 equalList{getField(want, 18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700540 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700541 },
Joe Tsai91e14662018-09-13 13:24:35 -0700542 hasFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true},
Joe Tsai87b955b2018-11-14 21:59:49 -0800543 equalMessage{want},
544 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
545 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000546 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800547
548 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700549 testMessage(t, nil, (*ListScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800550 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false},
551 listFields{2: {lenList(0)}, 4: {lenList(0)}, 6: {lenList(0)}, 8: {lenList(0)}, 10: {lenList(0)}, 12: {lenList(0)}, 14: {lenList(0)}, 16: {lenList(0)}, 18: {lenList(0)}},
552 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000553}
554
Joe Tsaice6edd32018-10-19 16:27:46 -0700555type MapScalars struct {
556 KeyBools map[bool]string `protobuf:"1"`
557 KeyInt32s map[int32]string `protobuf:"2"`
558 KeyInt64s map[int64]string `protobuf:"3"`
559 KeyUint32s map[uint32]string `protobuf:"4"`
560 KeyUint64s map[uint64]string `protobuf:"5"`
561 KeyStrings map[string]string `protobuf:"6"`
562
563 ValBools map[string]bool `protobuf:"7"`
564 ValInt32s map[string]int32 `protobuf:"8"`
565 ValInt64s map[string]int64 `protobuf:"9"`
566 ValUint32s map[string]uint32 `protobuf:"10"`
567 ValUint64s map[string]uint64 `protobuf:"11"`
568 ValFloat32s map[string]float32 `protobuf:"12"`
569 ValFloat64s map[string]float64 `protobuf:"13"`
570 ValStrings map[string]string `protobuf:"14"`
571 ValStringsA map[string][]byte `protobuf:"15"`
572 ValBytes map[string][]byte `protobuf:"16"`
573 ValBytesA map[string]string `protobuf:"17"`
574
575 MyStrings1 map[MyString]MyString `protobuf:"18"`
576 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
577 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
578 MyBytes2 map[MyString]MyString `protobuf:"21"`
579
580 MyStrings3 MapStrings `protobuf:"22"`
581 MyStrings4 MapBytes `protobuf:"23"`
582 MyBytes3 MapBytes `protobuf:"24"`
583 MyBytes4 MapStrings `protobuf:"25"`
584}
585
Damien Neil16163b42019-08-06 15:43:25 -0700586var mapScalarsType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(MapScalars)), Desc: mustMakeMessageDesc("map-scalars.proto", pref.Proto2, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700587 name: "MapScalars"
588 field: [
589 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F1Entry"},
590 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F2Entry"},
591 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F3Entry"},
592 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F4Entry"},
593 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F5Entry"},
594 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F6Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000595
Joe Tsaid8881392019-06-06 13:01:53 -0700596 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F7Entry"},
597 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F8Entry"},
598 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F9Entry"},
599 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F10Entry"},
600 {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F11Entry"},
601 {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F12Entry"},
602 {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F13Entry"},
603 {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F14Entry"},
604 {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F15Entry"},
605 {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F16Entry"},
606 {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F17Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000607
Joe Tsaid8881392019-06-06 13:01:53 -0700608 {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F18Entry"},
609 {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F19Entry"},
610 {name:"f20" number:20 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F20Entry"},
611 {name:"f21" number:21 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F21Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000612
Joe Tsaid8881392019-06-06 13:01:53 -0700613 {name:"f22" number:22 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F22Entry"},
614 {name:"f23" number:23 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F23Entry"},
615 {name:"f24" number:24 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F24Entry"},
616 {name:"f25" number:25 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F25Entry"}
617 ]
618 nested_type: [
619 {name:"F1Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
620 {name:"F2Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_INT32}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
621 {name:"F3Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_INT64}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
622 {name:"F4Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_UINT32}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
623 {name:"F5Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_UINT64}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
624 {name:"F6Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
625
626 {name:"F7Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_BOOL}] options:{map_entry:true}},
627 {name:"F8Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_INT32}] options:{map_entry:true}},
628 {name:"F9Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_INT64}] options:{map_entry:true}},
629 {name:"F10Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_UINT32}] options:{map_entry:true}},
630 {name:"F11Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_UINT64}] options:{map_entry:true}},
631 {name:"F12Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_FLOAT}] options:{map_entry:true}},
632 {name:"F13Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_DOUBLE}] options:{map_entry:true}},
633 {name:"F14Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
634 {name:"F15Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
635 {name:"F16Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_BYTES}] options:{map_entry:true}},
636 {name:"F17Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_BYTES}] options:{map_entry:true}},
637
638 {name:"F18Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
639 {name:"F19Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
640 {name:"F20Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_BYTES}] options:{map_entry:true}},
641 {name:"F21Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_BYTES}] options:{map_entry:true}},
642
643 {name:"F22Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
644 {name:"F23Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_STRING}] options:{map_entry:true}},
645 {name:"F24Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_BYTES}] options:{map_entry:true}},
646 {name:"F25Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_BYTES}] options:{map_entry:true}}
647 ]
648 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700649}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000650
Joe Tsai378c1322019-04-25 23:48:08 -0700651func (m *MapScalars) ProtoReflect() pref.Message { return mapScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800652
653func TestMapScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700654 empty := new(MapScalars).ProtoReflect()
655 want := (&MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000656 KeyBools: map[bool]string{true: "true", false: "false"},
657 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
658 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
659 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
660 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
661 KeyStrings: map[string]string{"": "", "foo": "bar"},
662
663 ValBools: map[string]bool{"true": true, "false": false},
664 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
665 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
666 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
667 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
668 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
669 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
670 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
671 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
672 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
673 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
674
675 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
676 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
677 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
678 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
679
680 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
681 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
682 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
683 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsai378c1322019-04-25 23:48:08 -0700684 }).ProtoReflect()
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000685
Joe Tsai378c1322019-04-25 23:48:08 -0700686 testMessage(t, nil, new(MapScalars).ProtoReflect(), messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000687 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, 23: false, 24: false, 25: false},
Joe Tsai378c1322019-04-25 23:48:08 -0700688 getFields{1: getField(empty, 1), 3: getField(empty, 3), 5: getField(empty, 5), 7: getField(empty, 7), 9: getField(empty, 9), 11: getField(empty, 11), 13: getField(empty, 13), 15: getField(empty, 15), 17: getField(empty, 17), 19: getField(empty, 19), 21: getField(empty, 21), 23: getField(empty, 23), 25: getField(empty, 25)},
689 setFields{1: getField(want, 1), 3: getField(want, 3), 5: getField(want, 5), 7: getField(want, 7), 9: getField(want, 9), 11: getField(want, 11), 13: getField(want, 13), 15: getField(want, 15), 17: getField(want, 17), 19: getField(want, 19), 21: getField(want, 21), 23: getField(want, 23), 25: getField(want, 25)},
690 mapFieldsMutable{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000691 2: {
692 lenMap(0),
693 hasMap{int32(0): false, int32(-1): false, int32(2): false},
694 setMap{int32(0): V("zero")},
695 lenMap(1),
696 hasMap{int32(0): true, int32(-1): false, int32(2): false},
697 setMap{int32(-1): V("one")},
698 lenMap(2),
699 hasMap{int32(0): true, int32(-1): true, int32(2): false},
700 setMap{int32(2): V("two")},
701 lenMap(3),
702 hasMap{int32(0): true, int32(-1): true, int32(2): true},
703 },
704 4: {
705 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai378c1322019-04-25 23:48:08 -0700706 equalMap{getField(want, 4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000707 },
708 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800709 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000710 setMap{"foo": V("bar")},
711 setMap{"": V("empty")},
712 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
713 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800714 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000715 },
716 8: {
Joe Tsai378c1322019-04-25 23:48:08 -0700717 equalMap{getField(empty, 8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000718 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
719 },
720 10: {
721 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
722 lenMap(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700723 equalMap{getField(want, 10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000724 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
725 },
726 12: {
727 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800728 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000729 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
730 },
731 14: {
Joe Tsai378c1322019-04-25 23:48:08 -0700732 equalMap{getField(empty, 14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000733 setMap{"s1": V("s1"), "s2": V("s2")},
734 },
735 16: {
736 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700737 equalMap{getField(want, 16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000738 },
739 18: {
740 hasMap{"s1": false, "s2": false, "s3": false},
741 setMap{"s1": V("s1"), "s2": V("s2")},
742 hasMap{"s1": true, "s2": true, "s3": false},
743 },
744 20: {
Joe Tsai378c1322019-04-25 23:48:08 -0700745 equalMap{getField(empty, 20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000746 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
747 },
748 22: {
749 rangeMap{},
750 setMap{"s1": V("s1"), "s2": V("s2")},
751 rangeMap{"s1": V("s1"), "s2": V("s2")},
752 lenMap(2),
753 },
754 24: {
755 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700756 equalMap{getField(want, 24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000757 },
758 },
759 hasFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, 23: true, 24: true, 25: true},
Joe Tsai87b955b2018-11-14 21:59:49 -0800760 equalMessage{want},
761 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25},
762 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700763 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800764
765 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700766 testMessage(t, nil, (*MapScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800767 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, 23: false, 24: false, 25: false},
768 mapFields{2: {lenMap(0)}, 4: {lenMap(0)}, 6: {lenMap(0)}, 8: {lenMap(0)}, 10: {lenMap(0)}, 12: {lenMap(0)}, 14: {lenMap(0)}, 16: {lenMap(0)}, 18: {lenMap(0)}, 20: {lenMap(0)}, 22: {lenMap(0)}, 24: {lenMap(0)}},
769 })
Joe Tsai91e14662018-09-13 13:24:35 -0700770}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700771
Joe Tsai87b955b2018-11-14 21:59:49 -0800772type OneofScalars struct {
773 Union isOneofScalars_Union `protobuf_oneof:"union"`
774}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700775
Damien Neil16163b42019-08-06 15:43:25 -0700776var oneofScalarsType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(OneofScalars)), Desc: mustMakeMessageDesc("oneof-scalars.proto", pref.Proto2, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700777 name: "OneofScalars"
778 field: [
779 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true" oneof_index:0},
780 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2" oneof_index:0},
781 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3" oneof_index:0},
782 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4" oneof_index:0},
783 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5" oneof_index:0},
784 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6" oneof_index:0},
785 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7" oneof_index:0},
786 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8" oneof_index:0},
787 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9" oneof_index:0},
788 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"10" oneof_index:0},
789 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11" oneof_index:0},
790 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"12" oneof_index:0},
791 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"13" oneof_index:0}
792 ]
793 oneof_decl: [{name:"union"}]
794 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700795}
Joe Tsaif0c01e42018-11-06 13:05:20 -0800796
Joe Tsai378c1322019-04-25 23:48:08 -0700797func (m *OneofScalars) ProtoReflect() pref.Message { return oneofScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800798
Joe Tsaif18ab532018-11-27 17:25:04 -0800799func (*OneofScalars) XXX_OneofWrappers() []interface{} {
800 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700801 (*OneofScalars_Bool)(nil),
802 (*OneofScalars_Int32)(nil),
803 (*OneofScalars_Int64)(nil),
804 (*OneofScalars_Uint32)(nil),
805 (*OneofScalars_Uint64)(nil),
806 (*OneofScalars_Float32)(nil),
807 (*OneofScalars_Float64)(nil),
808 (*OneofScalars_String)(nil),
809 (*OneofScalars_StringA)(nil),
810 (*OneofScalars_StringB)(nil),
811 (*OneofScalars_Bytes)(nil),
812 (*OneofScalars_BytesA)(nil),
813 (*OneofScalars_BytesB)(nil),
814 }
815}
816
Joe Tsai87b955b2018-11-14 21:59:49 -0800817type (
818 isOneofScalars_Union interface {
819 isOneofScalars_Union()
820 }
821 OneofScalars_Bool struct {
822 Bool bool `protobuf:"1"`
823 }
824 OneofScalars_Int32 struct {
825 Int32 MyInt32 `protobuf:"2"`
826 }
827 OneofScalars_Int64 struct {
828 Int64 int64 `protobuf:"3"`
829 }
830 OneofScalars_Uint32 struct {
831 Uint32 MyUint32 `protobuf:"4"`
832 }
833 OneofScalars_Uint64 struct {
834 Uint64 uint64 `protobuf:"5"`
835 }
836 OneofScalars_Float32 struct {
837 Float32 MyFloat32 `protobuf:"6"`
838 }
839 OneofScalars_Float64 struct {
840 Float64 float64 `protobuf:"7"`
841 }
842 OneofScalars_String struct {
843 String string `protobuf:"8"`
844 }
845 OneofScalars_StringA struct {
846 StringA []byte `protobuf:"9"`
847 }
848 OneofScalars_StringB struct {
849 StringB MyString `protobuf:"10"`
850 }
851 OneofScalars_Bytes struct {
852 Bytes []byte `protobuf:"11"`
853 }
854 OneofScalars_BytesA struct {
855 BytesA string `protobuf:"12"`
856 }
857 OneofScalars_BytesB struct {
858 BytesB MyBytes `protobuf:"13"`
859 }
860)
861
Joe Tsai2c870bb2018-10-17 11:46:52 -0700862func (*OneofScalars_Bool) isOneofScalars_Union() {}
863func (*OneofScalars_Int32) isOneofScalars_Union() {}
864func (*OneofScalars_Int64) isOneofScalars_Union() {}
865func (*OneofScalars_Uint32) isOneofScalars_Union() {}
866func (*OneofScalars_Uint64) isOneofScalars_Union() {}
867func (*OneofScalars_Float32) isOneofScalars_Union() {}
868func (*OneofScalars_Float64) isOneofScalars_Union() {}
869func (*OneofScalars_String) isOneofScalars_Union() {}
870func (*OneofScalars_StringA) isOneofScalars_Union() {}
871func (*OneofScalars_StringB) isOneofScalars_Union() {}
872func (*OneofScalars_Bytes) isOneofScalars_Union() {}
873func (*OneofScalars_BytesA) isOneofScalars_Union() {}
874func (*OneofScalars_BytesB) isOneofScalars_Union() {}
875
876func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800877 empty := &OneofScalars{}
878 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
879 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
880 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
881 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
882 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
883 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
884 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
885 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
886 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
887 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
888 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
889 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
890 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700891
Joe Tsai378c1322019-04-25 23:48:08 -0700892 testMessage(t, nil, new(OneofScalars).ProtoReflect(), messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700893 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false},
894 getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("13"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700895 whichOneofs{"union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700896
Joe Tsai378c1322019-04-25 23:48:08 -0700897 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1.ProtoReflect()},
898 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2.ProtoReflect()},
899 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3.ProtoReflect()},
900 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4.ProtoReflect()},
901 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5.ProtoReflect()},
902 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6.ProtoReflect()},
903 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7.ProtoReflect()},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700904
905 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: true, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false},
Joe Tsai378c1322019-04-25 23:48:08 -0700906 whichOneofs{"union": 7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700907
Joe Tsai378c1322019-04-25 23:48:08 -0700908 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8.ProtoReflect()},
909 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9.ProtoReflect()},
910 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10.ProtoReflect()},
911 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11.ProtoReflect()},
912 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12.ProtoReflect()},
913 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700914
915 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: true},
916 getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("130"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800917 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai378c1322019-04-25 23:48:08 -0700918 whichOneofs{"union": 13},
919 equalMessage{want13.ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800920 clearFields{13},
Joe Tsai378c1322019-04-25 23:48:08 -0700921 whichOneofs{"union": 0},
922 equalMessage{empty.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700923 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800924
925 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700926 testMessage(t, nil, (*OneofScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800927 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false},
928 getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("13"))},
929 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700930}
931
Joe Tsai87b955b2018-11-14 21:59:49 -0800932type EnumProto2 int32
933
Damien Neil16163b42019-08-06 15:43:25 -0700934var enumProto2Desc = mustMakeEnumDesc("enum2.proto", pref.Proto2, `
935 name: "EnumProto2"
936 value: [{name:"DEAD" number:0xdead}, {name:"BEEF" number:0xbeef}]
937`)
Joe Tsai87b955b2018-11-14 21:59:49 -0800938
Damien Neil16163b42019-08-06 15:43:25 -0700939func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Desc }
940func (e EnumProto2) Type() pref.EnumType { return e }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700941func (e EnumProto2) Enum() *EnumProto2 { return &e }
942func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Damien Neil16163b42019-08-06 15:43:25 -0700943func (t EnumProto2) GoType() reflect.Type { return reflect.TypeOf(t) }
944func (t EnumProto2) New(n pref.EnumNumber) pref.Enum { return EnumProto2(n) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800945
946type EnumProto3 int32
947
Damien Neil16163b42019-08-06 15:43:25 -0700948var enumProto3Desc = mustMakeEnumDesc("enum3.proto", pref.Proto3, `
949 name: "EnumProto3",
950 value: [{name:"ALPHA" number:0}, {name:"BRAVO" number:1}]
951`)
Joe Tsai87b955b2018-11-14 21:59:49 -0800952
Damien Neil16163b42019-08-06 15:43:25 -0700953func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Desc }
954func (e EnumProto3) Type() pref.EnumType { return e }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700955func (e EnumProto3) Enum() *EnumProto3 { return &e }
956func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Damien Neil16163b42019-08-06 15:43:25 -0700957func (t EnumProto3) GoType() reflect.Type { return reflect.TypeOf(t) }
958func (t EnumProto3) New(n pref.EnumNumber) pref.Enum { return EnumProto3(n) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800959
960type EnumMessages struct {
961 EnumP2 *EnumProto2 `protobuf:"1"`
962 EnumP3 *EnumProto3 `protobuf:"2"`
963 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
964 MessageCycle *EnumMessages `protobuf:"4"`
965 EnumList []EnumProto2 `protobuf:"5"`
966 MessageList []*ScalarProto2 `protobuf:"6"`
967 EnumMap map[string]EnumProto3 `protobuf:"7"`
968 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
969 Union isEnumMessages_Union `protobuf_oneof:"union"`
970}
971
Damien Neil16163b42019-08-06 15:43:25 -0700972var enumMessagesType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(EnumMessages)), Desc: mustMakeMessageDesc("enum-messages.proto", pref.Proto2, `
Joe Tsaid8881392019-06-06 13:01:53 -0700973 dependency: ["enum2.proto", "enum3.proto", "scalar2.proto", "scalar3.proto", "proto2.v1.0.0-20180125-92554152/test.proto"]
974 `, `
975 name: "EnumMessages"
976 field: [
977 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" default_value:"BEEF"},
978 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" default_value:"BRAVO"},
Herbie Ong20aefe92019-06-24 19:21:46 -0700979 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".google.golang.org.proto2_20180125.Message"},
980 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".EnumMessages"},
Joe Tsaid8881392019-06-06 13:01:53 -0700981 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_ENUM type_name:".EnumProto2"},
982 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".ScalarProto2"},
983 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F7Entry"},
984 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F8Entry"},
985 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" oneof_index:0 default_value:"BEEF"},
986 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" oneof_index:0 default_value:"BRAVO"},
987 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto2" oneof_index:0},
988 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto3" oneof_index:0}
989 ]
990 oneof_decl: [{name:"union"}]
991 nested_type: [
992 {name:"F7Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3"}] options:{map_entry:true}},
993 {name:"F8Entry" field:[{name:"key" number:1 label:LABEL_OPTIONAL type:TYPE_STRING}, {name:"value" number:2 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto3"}] options:{map_entry:true}}
994 ]
995 `, protoregistry.NewFiles(
Damien Neil16163b42019-08-06 15:43:25 -0700996 EnumProto2(0).Descriptor().ParentFile(),
997 EnumProto3(0).Descriptor().ParentFile(),
998 ((*ScalarProto2)(nil)).ProtoReflect().Descriptor().ParentFile(),
999 ((*ScalarProto3)(nil)).ProtoReflect().Descriptor().ParentFile(),
1000 pimpl.Export{}.MessageDescriptorOf((*proto2_20180125.Message)(nil)).ParentFile(),
1001)),
1002}
Joe Tsai87b955b2018-11-14 21:59:49 -08001003
Joe Tsai378c1322019-04-25 23:48:08 -07001004func (m *EnumMessages) ProtoReflect() pref.Message { return enumMessagesType.MessageOf(m) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001005
Joe Tsaif18ab532018-11-27 17:25:04 -08001006func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1007 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001008 (*EnumMessages_OneofE2)(nil),
1009 (*EnumMessages_OneofE3)(nil),
1010 (*EnumMessages_OneofM2)(nil),
1011 (*EnumMessages_OneofM3)(nil),
1012 }
1013}
1014
1015type (
1016 isEnumMessages_Union interface {
1017 isEnumMessages_Union()
1018 }
1019 EnumMessages_OneofE2 struct {
1020 OneofE2 EnumProto2 `protobuf:"9"`
1021 }
1022 EnumMessages_OneofE3 struct {
1023 OneofE3 EnumProto3 `protobuf:"10"`
1024 }
1025 EnumMessages_OneofM2 struct {
1026 OneofM2 *ScalarProto2 `protobuf:"11"`
1027 }
1028 EnumMessages_OneofM3 struct {
1029 OneofM3 *ScalarProto3 `protobuf:"12"`
1030 }
1031)
1032
1033func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1034func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1035func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1036func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1037
1038func TestEnumMessages(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -07001039 emptyL := pimpl.Export{}.MessageOf(new(proto2_20180125.Message))
1040 emptyM := new(EnumMessages).ProtoReflect()
1041 emptyM2 := new(ScalarProto2).ProtoReflect()
1042 emptyM3 := new(ScalarProto3).ProtoReflect()
1043
Damien Neila8a2cea2019-07-10 16:17:16 -07001044 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)})
Joe Tsai378c1322019-04-25 23:48:08 -07001045 wantM := (&EnumMessages{EnumP2: EnumProto2(1234).Enum()}).ProtoReflect()
Damien Neila8a2cea2019-07-10 16:17:16 -07001046 wantM2a := &ScalarProto2{Float32: proto.Float32(math.Pi)}
1047 wantM2b := &ScalarProto2{Float32: proto.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001048 wantM3a := &ScalarProto3{Float32: math.Pi}
1049 wantM3b := &ScalarProto3{Float32: math.Ln2}
1050
Joe Tsai378c1322019-04-25 23:48:08 -07001051 wantList5 := getField((&EnumMessages{EnumList: []EnumProto2{333, 222}}).ProtoReflect(), 5)
1052 wantList6 := getField((&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).ProtoReflect(), 6)
Joe Tsai87b955b2018-11-14 21:59:49 -08001053
Joe Tsai378c1322019-04-25 23:48:08 -07001054 wantMap7 := getField((&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).ProtoReflect(), 7)
1055 wantMap8 := getField((&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).ProtoReflect(), 8)
Joe Tsai87b955b2018-11-14 21:59:49 -08001056
Joe Tsai378c1322019-04-25 23:48:08 -07001057 testMessage(t, nil, new(EnumMessages).ProtoReflect(), messageOps{
Joe Tsai87b955b2018-11-14 21:59:49 -08001058 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001059 getFields{1: VE(0xbeef), 2: VE(1), 3: V(emptyL), 4: V(emptyM), 9: VE(0xbeef), 10: VE(1)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001060
1061 // Test singular enums.
1062 setFields{1: VE(0xdead), 2: VE(0)},
1063 getFields{1: VE(0xdead), 2: VE(0)},
1064 hasFields{1: true, 2: true},
1065
1066 // Test singular messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001067 messageFieldsMutable{3: messageOps{setFields{109: V(float32(math.E))}}},
1068 messageFieldsMutable{4: messageOps{setFields{1: VE(1234)}}},
Joe Tsai87b955b2018-11-14 21:59:49 -08001069 getFields{3: V(wantL), 4: V(wantM)},
1070 clearFields{3, 4},
1071 hasFields{3: false, 4: false},
1072 setFields{3: V(wantL), 4: V(wantM)},
1073 hasFields{3: true, 4: true},
1074
1075 // Test list of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001076 listFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001077 5: listOps{
1078 appendList{VE(111), VE(222)},
1079 setList{0: VE(333)},
1080 getList{0: VE(333), 1: VE(222)},
1081 lenList(2),
1082 },
1083 6: listOps{
1084 appendMessageList{setFields{4: V(uint32(1e6))}},
1085 appendMessageList{setFields{6: V(float32(math.Phi))}},
Joe Tsai378c1322019-04-25 23:48:08 -07001086 setList{0: V(wantM2a.ProtoReflect())},
1087 getList{0: V(wantM2a.ProtoReflect()), 1: V(wantM2b.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001088 },
1089 },
1090 getFields{5: wantList5, 6: wantList6},
1091 hasFields{5: true, 6: true},
1092 listFields{5: listOps{truncList(0)}},
1093 hasFields{5: false, 6: true},
1094
1095 // Test maps of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001096 mapFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001097 7: mapOps{
1098 setMap{"one": VE(1), "two": VE(2)},
1099 hasMap{"one": true, "two": true, "three": false},
1100 lenMap(2),
1101 },
1102 8: mapOps{
1103 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
Joe Tsai378c1322019-04-25 23:48:08 -07001104 setMap{"ln2": V(wantM3b.ProtoReflect())},
1105 getMap{"pi": V(wantM3a.ProtoReflect()), "ln2": V(wantM3b.ProtoReflect()), "none": V(nil)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001106 lenMap(2),
1107 },
1108 },
1109 getFields{7: wantMap7, 8: wantMap8},
1110 hasFields{7: true, 8: true},
1111 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1112 hasFields{7: true, 8: false},
1113
1114 // Test oneofs of enums and messages.
1115 setFields{9: VE(0xdead)},
1116 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1117 setFields{10: VE(0)},
1118 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001119 messageFieldsMutable{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1120 getFields{11: V(wantM2a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001121 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001122 messageFieldsMutable{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1123 getFields{12: V(wantM3a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001124 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1125
1126 // Check entire message.
Joe Tsai378c1322019-04-25 23:48:08 -07001127 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a.ProtoReflect())},
1128 equalMessage{(&EnumMessages{
Joe Tsai87b955b2018-11-14 21:59:49 -08001129 EnumP2: EnumProto2(0xdead).Enum(),
1130 EnumP3: EnumProto3(0).Enum(),
Damien Neila8a2cea2019-07-10 16:17:16 -07001131 MessageLegacy: &proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)},
Joe Tsai378c1322019-04-25 23:48:08 -07001132 MessageCycle: wantM.Interface().(*EnumMessages),
Joe Tsai87b955b2018-11-14 21:59:49 -08001133 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1134 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1135 Union: &EnumMessages_OneofM3{wantM3a},
Joe Tsai378c1322019-04-25 23:48:08 -07001136 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001137 clearFields{1, 2, 3, 4, 6, 7, 12},
Joe Tsai378c1322019-04-25 23:48:08 -07001138 equalMessage{new(EnumMessages).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001139 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001140
1141 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -07001142 testMessage(t, nil, (*EnumMessages)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -08001143 hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001144 getFields{1: VE(0xbeef), 2: VE(1), 3: V(emptyL), 4: V(emptyM), 9: VE(0xbeef), 10: VE(1), 11: V(emptyM2), 12: V(emptyM3)},
Joe Tsai6cf80c42018-12-01 04:57:09 -08001145 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1146 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1147 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001148}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001149
Joe Tsai91e14662018-09-13 13:24:35 -07001150var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001151 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
Joe Tsaid8881392019-06-06 13:01:53 -07001152 mx := pimpl.Export{}.MessageOf(x).Interface()
1153 my := pimpl.Export{}.MessageOf(y).Interface()
1154 return proto.Equal(mx, my)
Joe Tsai91e14662018-09-13 13:24:35 -07001155 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001156 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
Joe Tsai378c1322019-04-25 23:48:08 -07001157 switch v := pv.Interface().(type) {
1158 case pref.Message:
1159 out := make(map[pref.FieldNumber]pref.Value)
1160 v.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1161 out[fd.Number()] = v
1162 return true
1163 })
1164 return out
1165 case pref.List:
1166 var out []pref.Value
1167 for i := 0; i < v.Len(); i++ {
1168 out = append(out, v.Get(i))
1169 }
1170 return out
1171 case pref.Map:
1172 out := make(map[interface{}]pref.Value)
1173 v.Range(func(k pref.MapKey, v pref.Value) bool {
1174 out[k.Interface()] = v
1175 return true
1176 })
1177 return out
1178 default:
1179 return v
1180 }
Joe Tsai91e14662018-09-13 13:24:35 -07001181 }),
1182 cmpopts.EquateNaNs(),
1183}
1184
1185func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
Joe Tsai378c1322019-04-25 23:48:08 -07001186 fieldDescs := m.Descriptor().Fields()
1187 oneofDescs := m.Descriptor().Oneofs()
Joe Tsai91e14662018-09-13 13:24:35 -07001188 for i, op := range tt {
1189 p.Push(i)
1190 switch op := op.(type) {
1191 case equalMessage:
Joe Tsai378c1322019-04-25 23:48:08 -07001192 if diff := cmp.Diff(V(op.Message), V(m), cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001193 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1194 }
1195 case hasFields:
1196 got := map[pref.FieldNumber]bool{}
1197 want := map[pref.FieldNumber]bool(op)
1198 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001199 fd := fieldDescs.ByNumber(n)
1200 got[n] = m.Has(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001201 }
1202 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001203 t.Errorf("operation %v, Message.Has mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001204 }
1205 case getFields:
1206 got := map[pref.FieldNumber]pref.Value{}
1207 want := map[pref.FieldNumber]pref.Value(op)
1208 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001209 fd := fieldDescs.ByNumber(n)
1210 got[n] = m.Get(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001211 }
1212 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001213 t.Errorf("operation %v, Message.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001214 }
1215 case setFields:
1216 for n, v := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001217 fd := fieldDescs.ByNumber(n)
1218 m.Set(fd, v)
Joe Tsai91e14662018-09-13 13:24:35 -07001219 }
1220 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001221 for _, n := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001222 fd := fieldDescs.ByNumber(n)
1223 m.Clear(fd)
Joe Tsai87b955b2018-11-14 21:59:49 -08001224 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001225 case whichOneofs:
1226 got := map[pref.Name]pref.FieldNumber{}
1227 want := map[pref.Name]pref.FieldNumber(op)
1228 for s := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001229 od := oneofDescs.ByName(s)
1230 fd := m.WhichOneof(od)
1231 if fd == nil {
1232 got[s] = 0
1233 } else {
1234 got[s] = fd.Number()
1235 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001236 }
1237 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001238 t.Errorf("operation %v, Message.WhichOneof mismatch (-want, +got):\n%s", p, diff)
Joe Tsai4ec39c72019-04-03 13:40:53 -07001239 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001240 case messageFields:
1241 for n, tt := range op {
1242 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001243 fd := fieldDescs.ByNumber(n)
1244 testMessage(t, p, m.Get(fd).Message(), tt)
1245 p.Pop()
1246 }
1247 case messageFieldsMutable:
1248 for n, tt := range op {
1249 p.Push(int(n))
1250 fd := fieldDescs.ByNumber(n)
1251 testMessage(t, p, m.Mutable(fd).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001252 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001253 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001254 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001255 for n, tt := range op {
1256 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001257 fd := fieldDescs.ByNumber(n)
1258 testLists(t, p, m.Get(fd).List(), tt)
1259 p.Pop()
1260 }
1261 case listFieldsMutable:
1262 for n, tt := range op {
1263 p.Push(int(n))
1264 fd := fieldDescs.ByNumber(n)
1265 testLists(t, p, m.Mutable(fd).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001266 p.Pop()
1267 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001268 case mapFields:
1269 for n, tt := range op {
1270 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001271 fd := fieldDescs.ByNumber(n)
1272 testMaps(t, p, m.Get(fd).Map(), tt)
1273 p.Pop()
1274 }
1275 case mapFieldsMutable:
1276 for n, tt := range op {
1277 p.Push(int(n))
1278 fd := fieldDescs.ByNumber(n)
1279 testMaps(t, p, m.Mutable(fd).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001280 p.Pop()
1281 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001282 case rangeFields:
1283 got := map[pref.FieldNumber]pref.Value{}
1284 want := map[pref.FieldNumber]pref.Value(op)
Joe Tsai378c1322019-04-25 23:48:08 -07001285 m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1286 got[fd.Number()] = v
Joe Tsai87b955b2018-11-14 21:59:49 -08001287 return true
1288 })
1289 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001290 t.Errorf("operation %v, Message.Range mismatch (-want, +got):\n%s", p, diff)
Joe Tsai87b955b2018-11-14 21:59:49 -08001291 }
Joe Tsai91e14662018-09-13 13:24:35 -07001292 default:
1293 t.Fatalf("operation %v, invalid operation: %T", p, op)
1294 }
1295 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001296 }
1297}
Joe Tsai91e14662018-09-13 13:24:35 -07001298
Joe Tsai4b7aff62018-11-14 14:05:19 -08001299func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001300 for i, op := range tt {
1301 p.Push(i)
1302 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001303 case equalList:
Joe Tsai378c1322019-04-25 23:48:08 -07001304 if diff := cmp.Diff(V(op.List), V(v), cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001305 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001306 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001307 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001308 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001309 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001310 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001311 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001312 got := map[int]pref.Value{}
1313 want := map[int]pref.Value(op)
1314 for n := range want {
1315 got[n] = v.Get(n)
1316 }
1317 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001318 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001319 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001320 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001321 for n, e := range op {
1322 v.Set(n, e)
1323 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001324 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001325 for _, e := range op {
1326 v.Append(e)
1327 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001328 case appendMessageList:
Damien Neild91c4222019-09-04 10:46:00 -07001329 e := v.NewElement()
1330 v.Append(e)
1331 testMessage(t, p, e.Message(), messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001332 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001333 v.Truncate(int(op))
1334 default:
1335 t.Fatalf("operation %v, invalid operation: %T", p, op)
1336 }
1337 p.Pop()
1338 }
1339}
1340
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001341func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1342 for i, op := range tt {
1343 p.Push(i)
1344 switch op := op.(type) {
1345 case equalMap:
Joe Tsai378c1322019-04-25 23:48:08 -07001346 if diff := cmp.Diff(V(op.Map), V(m), cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001347 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1348 }
1349 case lenMap:
1350 if got, want := m.Len(), int(op); got != want {
1351 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1352 }
1353 case hasMap:
1354 got := map[interface{}]bool{}
1355 want := map[interface{}]bool(op)
1356 for k := range want {
1357 got[k] = m.Has(V(k).MapKey())
1358 }
1359 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1360 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1361 }
1362 case getMap:
1363 got := map[interface{}]pref.Value{}
1364 want := map[interface{}]pref.Value(op)
1365 for k := range want {
1366 got[k] = m.Get(V(k).MapKey())
1367 }
1368 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1369 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1370 }
1371 case setMap:
1372 for k, v := range op {
1373 m.Set(V(k).MapKey(), v)
1374 }
1375 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001376 for _, k := range op {
1377 m.Clear(V(k).MapKey())
1378 }
1379 case messageMap:
1380 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001381 mk := V(k).MapKey()
1382 if !m.Has(mk) {
Damien Neild91c4222019-09-04 10:46:00 -07001383 m.Set(mk, m.NewValue())
Damien Neil97e7f572018-12-07 14:28:33 -08001384 }
1385 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001386 }
1387 case rangeMap:
1388 got := map[interface{}]pref.Value{}
1389 want := map[interface{}]pref.Value(op)
1390 m.Range(func(k pref.MapKey, v pref.Value) bool {
1391 got[k.Interface()] = v
1392 return true
1393 })
1394 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1395 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1396 }
1397 default:
1398 t.Fatalf("operation %v, invalid operation: %T", p, op)
1399 }
1400 p.Pop()
1401 }
1402}
1403
Joe Tsai378c1322019-04-25 23:48:08 -07001404func getField(m pref.Message, n pref.FieldNumber) pref.Value {
1405 fd := m.Descriptor().Fields().ByNumber(n)
1406 return m.Get(fd)
1407}
1408
Joe Tsai91e14662018-09-13 13:24:35 -07001409type path []int
1410
1411func (p *path) Push(i int) { *p = append(*p, i) }
1412func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1413func (p path) String() string {
1414 var ss []string
1415 for _, i := range p {
1416 ss = append(ss, fmt.Sprint(i))
1417 }
1418 return strings.Join(ss, ".")
1419}
Joe Tsai82760ce2019-06-20 03:09:57 -07001420
1421// The MessageState implementation makes the assumption that when a
1422// concrete message is unsafe casted as a *MessageState, the Go GC does
1423// not reclaim the memory for the remainder of the concrete message.
1424func TestUnsafeAssumptions(t *testing.T) {
1425 if !pimpl.UnsafeEnabled {
1426 t.Skip()
1427 }
1428
1429 var wg sync.WaitGroup
1430 for i := 0; i < 10; i++ {
1431 wg.Add(1)
1432 go func() {
1433 var ms [10]pref.Message
1434
1435 // Store the message only in its reflective form.
1436 // Trigger the GC after each iteration.
1437 for j := 0; j < 10; j++ {
1438 ms[j] = (&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -07001439 OptionalInt32: proto.Int32(int32(j)),
1440 OptionalFloat: proto.Float32(float32(j)),
Joe Tsai82760ce2019-06-20 03:09:57 -07001441 RepeatedInt32: []int32{int32(j)},
1442 RepeatedFloat: []float32{float32(j)},
Damien Neila8a2cea2019-07-10 16:17:16 -07001443 DefaultInt32: proto.Int32(int32(j)),
1444 DefaultFloat: proto.Float32(float32(j)),
Joe Tsai82760ce2019-06-20 03:09:57 -07001445 }).ProtoReflect()
1446 runtime.GC()
1447 }
1448
1449 // Convert the reflective form back into a concrete form.
1450 // Verify that the values written previously are still the same.
1451 for j := 0; j < 10; j++ {
1452 switch m := ms[j].Interface().(*testpb.TestAllTypes); {
1453 case m.GetOptionalInt32() != int32(j):
1454 case m.GetOptionalFloat() != float32(j):
1455 case m.GetRepeatedInt32()[0] != int32(j):
1456 case m.GetRepeatedFloat()[0] != float32(j):
1457 case m.GetDefaultInt32() != int32(j):
1458 case m.GetDefaultFloat() != float32(j):
1459 default:
1460 continue
1461 }
1462 t.Error("memory corrupted detected")
1463 }
1464 defer wg.Done()
1465 }()
1466 }
1467 wg.Wait()
1468}
1469
1470func BenchmarkName(b *testing.B) {
1471 var sink pref.FullName
1472 b.Run("Value", func(b *testing.B) {
1473 b.ReportAllocs()
1474 m := new(descriptorpb.FileDescriptorProto)
1475 for i := 0; i < b.N; i++ {
1476 sink = m.ProtoReflect().Descriptor().FullName()
1477 }
1478 })
1479 b.Run("Nil", func(b *testing.B) {
1480 b.ReportAllocs()
1481 m := (*descriptorpb.FileDescriptorProto)(nil)
1482 for i := 0; i < b.N; i++ {
1483 sink = m.ProtoReflect().Descriptor().FullName()
1484 }
1485 })
1486 runtime.KeepAlive(sink)
1487}