blob: 96c7e1b6cfc0de11ad0bcfb6523f6d45725edffe [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 Tsaie0daf312020-02-25 12:51:10 -080018
Joe Tsaid8881392019-06-06 13:01:53 -070019 "google.golang.org/protobuf/encoding/prototext"
Damien Neile89e6242019-05-13 23:55:40 -070020 pimpl "google.golang.org/protobuf/internal/impl"
Joe Tsaid8881392019-06-06 13:01:53 -070021 "google.golang.org/protobuf/proto"
22 pdesc "google.golang.org/protobuf/reflect/protodesc"
Damien Neile89e6242019-05-13 23:55:40 -070023 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaid8881392019-06-06 13:01:53 -070024 "google.golang.org/protobuf/reflect/protoregistry"
Joe Tsai5d634732020-07-26 22:37:47 -070025 "google.golang.org/protobuf/testing/protopack"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070026
Joe Tsai55f18252020-01-11 00:25:01 -080027 proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2_20180125_92554152"
Joe Tsai82760ce2019-06-20 03:09:57 -070028 testpb "google.golang.org/protobuf/internal/testprotos/test"
Joe Tsaia95b29f2019-05-16 12:47:20 -070029 "google.golang.org/protobuf/types/descriptorpb"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070030)
31
Joe Tsai4b7aff62018-11-14 14:05:19 -080032// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070033type (
Joe Tsai87b955b2018-11-14 21:59:49 -080034 messageOp interface{ isMessageOp() }
Joe Tsai91e14662018-09-13 13:24:35 -070035 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070036
Joe Tsai87b955b2018-11-14 21:59:49 -080037 listOp interface{ isListOp() }
Joe Tsai4b7aff62018-11-14 14:05:19 -080038 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070039
Joe Tsai87b955b2018-11-14 21:59:49 -080040 mapOp interface{ isMapOp() }
Joe Tsaibbfaeb72018-10-17 00:27:21 +000041 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070042)
43
44// Test operations performed on a message.
45type (
Joe Tsai87b955b2018-11-14 21:59:49 -080046 // check that the message contents match
47 equalMessage struct{ pref.Message }
48 // check presence for specific fields in the message
49 hasFields map[pref.FieldNumber]bool
50 // check that specific message fields match
51 getFields map[pref.FieldNumber]pref.Value
52 // set specific message fields
53 setFields map[pref.FieldNumber]pref.Value
54 // clear specific fields in the message
55 clearFields []pref.FieldNumber
Joe Tsai4ec39c72019-04-03 13:40:53 -070056 // check for the presence of specific oneof member fields.
57 whichOneofs map[pref.Name]pref.FieldNumber
Joe Tsai87b955b2018-11-14 21:59:49 -080058 // apply messageOps on each specified message field
Joe Tsai378c1322019-04-25 23:48:08 -070059 messageFields map[pref.FieldNumber]messageOps
60 messageFieldsMutable map[pref.FieldNumber]messageOps
Joe Tsai87b955b2018-11-14 21:59:49 -080061 // apply listOps on each specified list field
Joe Tsai378c1322019-04-25 23:48:08 -070062 listFields map[pref.FieldNumber]listOps
63 listFieldsMutable map[pref.FieldNumber]listOps
Joe Tsai87b955b2018-11-14 21:59:49 -080064 // apply mapOps on each specified map fields
Joe Tsai378c1322019-04-25 23:48:08 -070065 mapFields map[pref.FieldNumber]mapOps
66 mapFieldsMutable map[pref.FieldNumber]mapOps
Joe Tsai87b955b2018-11-14 21:59:49 -080067 // range through all fields and check that they match
68 rangeFields map[pref.FieldNumber]pref.Value
Joe Tsai91e14662018-09-13 13:24:35 -070069)
70
Joe Tsai378c1322019-04-25 23:48:08 -070071func (equalMessage) isMessageOp() {}
72func (hasFields) isMessageOp() {}
73func (getFields) isMessageOp() {}
74func (setFields) isMessageOp() {}
75func (clearFields) isMessageOp() {}
76func (whichOneofs) isMessageOp() {}
77func (messageFields) isMessageOp() {}
78func (messageFieldsMutable) isMessageOp() {}
79func (listFields) isMessageOp() {}
80func (listFieldsMutable) isMessageOp() {}
81func (mapFields) isMessageOp() {}
82func (mapFieldsMutable) isMessageOp() {}
83func (rangeFields) isMessageOp() {}
Joe Tsai87b955b2018-11-14 21:59:49 -080084
Joe Tsai4b7aff62018-11-14 14:05:19 -080085// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070086type (
Joe Tsai87b955b2018-11-14 21:59:49 -080087 // check that the list contents match
88 equalList struct{ pref.List }
89 // check that list length matches
90 lenList int
91 // check that specific list entries match
92 getList map[int]pref.Value
93 // set specific list entries
94 setList map[int]pref.Value
95 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080096 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080097 // apply messageOps on a newly appended message
98 appendMessageList messageOps
99 // truncate the list to the specified length
100 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -0700101)
102
Joe Tsai87b955b2018-11-14 21:59:49 -0800103func (equalList) isListOp() {}
104func (lenList) isListOp() {}
105func (getList) isListOp() {}
106func (setList) isListOp() {}
107func (appendList) isListOp() {}
108func (appendMessageList) isListOp() {}
109func (truncList) isListOp() {}
110
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000111// Test operations performed on a map.
112type (
Joe Tsai87b955b2018-11-14 21:59:49 -0800113 // check that the map contents match
114 equalMap struct{ pref.Map }
115 // check that map length matches
116 lenMap int
117 // check presence for specific entries in the map
118 hasMap map[interface{}]bool
119 // check that specific map entries match
120 getMap map[interface{}]pref.Value
121 // set specific map entries
122 setMap map[interface{}]pref.Value
123 // clear specific entries in the map
124 clearMap []interface{}
125 // apply messageOps on each specified message entry
126 messageMap map[interface{}]messageOps
127 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000128 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000129)
130
Joe Tsai87b955b2018-11-14 21:59:49 -0800131func (equalMap) isMapOp() {}
132func (lenMap) isMapOp() {}
133func (hasMap) isMapOp() {}
134func (getMap) isMapOp() {}
135func (setMap) isMapOp() {}
136func (clearMap) isMapOp() {}
137func (messageMap) isMapOp() {}
138func (rangeMap) isMapOp() {}
139
Joe Tsaice6edd32018-10-19 16:27:46 -0700140type ScalarProto2 struct {
141 Bool *bool `protobuf:"1"`
142 Int32 *int32 `protobuf:"2"`
143 Int64 *int64 `protobuf:"3"`
144 Uint32 *uint32 `protobuf:"4"`
145 Uint64 *uint64 `protobuf:"5"`
146 Float32 *float32 `protobuf:"6"`
147 Float64 *float64 `protobuf:"7"`
148 String *string `protobuf:"8"`
149 StringA []byte `protobuf:"9"`
150 Bytes []byte `protobuf:"10"`
151 BytesA *string `protobuf:"11"`
152
153 MyBool *MyBool `protobuf:"12"`
154 MyInt32 *MyInt32 `protobuf:"13"`
155 MyInt64 *MyInt64 `protobuf:"14"`
156 MyUint32 *MyUint32 `protobuf:"15"`
157 MyUint64 *MyUint64 `protobuf:"16"`
158 MyFloat32 *MyFloat32 `protobuf:"17"`
159 MyFloat64 *MyFloat64 `protobuf:"18"`
160 MyString *MyString `protobuf:"19"`
161 MyStringA MyBytes `protobuf:"20"`
162 MyBytes MyBytes `protobuf:"21"`
163 MyBytesA *MyString `protobuf:"22"`
164}
165
Joe Tsaid8881392019-06-06 13:01:53 -0700166func mustMakeEnumDesc(path string, syntax pref.Syntax, enumDesc string) pref.EnumDescriptor {
167 s := fmt.Sprintf(`name:%q syntax:%q enum_type:[{%s}]`, path, syntax, enumDesc)
168 pb := new(descriptorpb.FileDescriptorProto)
169 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
170 panic(err)
171 }
172 fd, err := pdesc.NewFile(pb, nil)
Joe Tsai87b955b2018-11-14 21:59:49 -0800173 if err != nil {
174 panic(err)
175 }
Joe Tsaid8881392019-06-06 13:01:53 -0700176 return fd.Enums().Get(0)
Joe Tsai87b955b2018-11-14 21:59:49 -0800177}
178
Joe Tsaid8881392019-06-06 13:01:53 -0700179func mustMakeMessageDesc(path string, syntax pref.Syntax, fileDesc, msgDesc string, r pdesc.Resolver) pref.MessageDescriptor {
180 s := fmt.Sprintf(`name:%q syntax:%q %s message_type:[{%s}]`, path, syntax, fileDesc, msgDesc)
181 pb := new(descriptorpb.FileDescriptorProto)
182 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
183 panic(err)
184 }
185 fd, err := pdesc.NewFile(pb, r)
Joe Tsai87b955b2018-11-14 21:59:49 -0800186 if err != nil {
187 panic(err)
188 }
Joe Tsaid8881392019-06-06 13:01:53 -0700189 return fd.Messages().Get(0)
Joe Tsai87b955b2018-11-14 21:59:49 -0800190}
191
192var V = pref.ValueOf
193var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
194
195type (
196 MyBool bool
197 MyInt32 int32
198 MyInt64 int64
199 MyUint32 uint32
200 MyUint64 uint64
201 MyFloat32 float32
202 MyFloat64 float64
203 MyString string
204 MyBytes []byte
205
206 ListStrings []MyString
207 ListBytes []MyBytes
208
209 MapStrings map[MyString]MyString
210 MapBytes map[MyString]MyBytes
211)
212
Damien Neil16163b42019-08-06 15:43:25 -0700213var scalarProto2Type = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(ScalarProto2)), Desc: mustMakeMessageDesc("scalar2.proto", pref.Proto2, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700214 name: "ScalarProto2"
215 field: [
216 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
217 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2"},
218 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3"},
219 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4"},
220 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5"},
221 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6"},
222 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7"},
223 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8"},
224 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9"},
225 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"10"},
226 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11"},
Joe Tsai91e14662018-09-13 13:24:35 -0700227
Joe Tsaid8881392019-06-06 13:01:53 -0700228 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
229 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"13"},
230 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"14"},
231 {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"15"},
232 {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"16"},
233 {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"17"},
234 {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"18"},
235 {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"19"},
236 {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"20"},
237 {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"21"},
238 {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"22"}
239 ]
240 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700241}
Joe Tsai91e14662018-09-13 13:24:35 -0700242
Joe Tsai378c1322019-04-25 23:48:08 -0700243func (m *ScalarProto2) ProtoReflect() pref.Message { return scalarProto2Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800244
245func TestScalarProto2(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700246 testMessage(t, nil, new(ScalarProto2).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700247 hasFields{
248 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
249 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
250 },
251 getFields{
252 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")),
253 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")),
254 },
255 setFields{
256 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)),
257 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)),
258 },
259 hasFields{
260 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
261 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
262 },
Joe Tsai378c1322019-04-25 23:48:08 -0700263 equalMessage{(&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700264 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
265 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 -0700266 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800267 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 -0700268 equalMessage{new(ScalarProto2).ProtoReflect()},
Joe Tsai3e6a39b2019-07-10 10:21:12 -0700269
270 // Setting a bytes field nil empty bytes should preserve presence.
271 setFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
272 getFields{10: V([]byte{}), 11: V([]byte(nil)), 21: V([]byte{}), 22: V([]byte(nil))},
273 hasFields{10: true, 11: true, 21: true, 22: true},
Joe Tsai91e14662018-09-13 13:24:35 -0700274 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800275
276 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700277 testMessage(t, nil, (*ScalarProto2)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800278 hasFields{
279 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
280 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
281 },
282 getFields{
283 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")),
284 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")),
285 },
286 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700287}
288
Joe Tsaice6edd32018-10-19 16:27:46 -0700289type ScalarProto3 struct {
290 Bool bool `protobuf:"1"`
291 Int32 int32 `protobuf:"2"`
292 Int64 int64 `protobuf:"3"`
293 Uint32 uint32 `protobuf:"4"`
294 Uint64 uint64 `protobuf:"5"`
295 Float32 float32 `protobuf:"6"`
296 Float64 float64 `protobuf:"7"`
297 String string `protobuf:"8"`
298 StringA []byte `protobuf:"9"`
299 Bytes []byte `protobuf:"10"`
300 BytesA string `protobuf:"11"`
301
302 MyBool MyBool `protobuf:"12"`
303 MyInt32 MyInt32 `protobuf:"13"`
304 MyInt64 MyInt64 `protobuf:"14"`
305 MyUint32 MyUint32 `protobuf:"15"`
306 MyUint64 MyUint64 `protobuf:"16"`
307 MyFloat32 MyFloat32 `protobuf:"17"`
308 MyFloat64 MyFloat64 `protobuf:"18"`
309 MyString MyString `protobuf:"19"`
310 MyStringA MyBytes `protobuf:"20"`
311 MyBytes MyBytes `protobuf:"21"`
312 MyBytesA MyString `protobuf:"22"`
313}
314
Damien Neil16163b42019-08-06 15:43:25 -0700315var scalarProto3Type = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(ScalarProto3)), Desc: mustMakeMessageDesc("scalar3.proto", pref.Proto3, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700316 name: "ScalarProto3"
317 field: [
318 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL},
319 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32},
320 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64},
321 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32},
322 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64},
323 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT},
324 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
325 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING},
326 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING},
327 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES},
328 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700329
Joe Tsaid8881392019-06-06 13:01:53 -0700330 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL},
331 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32},
332 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64},
333 {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32},
334 {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64},
335 {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT},
336 {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
337 {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING},
338 {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING},
339 {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES},
340 {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES}
341 ]
342 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700343}
Joe Tsai91e14662018-09-13 13:24:35 -0700344
Joe Tsai378c1322019-04-25 23:48:08 -0700345func (m *ScalarProto3) ProtoReflect() pref.Message { return scalarProto3Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800346
347func TestScalarProto3(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700348 testMessage(t, nil, new(ScalarProto3).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700349 hasFields{
350 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
351 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
352 },
353 getFields{
354 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)),
355 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)),
356 },
357 setFields{
358 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)),
359 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)),
360 },
361 hasFields{
362 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
363 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
364 },
Joe Tsai378c1322019-04-25 23:48:08 -0700365 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai91e14662018-09-13 13:24:35 -0700366 setFields{
367 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")),
368 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")),
369 },
370 hasFields{
371 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
372 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
373 },
Joe Tsai378c1322019-04-25 23:48:08 -0700374 equalMessage{(&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700375 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
376 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai378c1322019-04-25 23:48:08 -0700377 }).ProtoReflect()},
Joe Tsai44e389c2018-11-19 15:27:09 -0800378 setFields{
379 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
380 },
381 hasFields{
382 2: true, 3: true, 6: true, 7: true,
383 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800384 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 -0700385 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai060cdac2019-04-22 11:44:49 -0700386
387 // Verify that -0 triggers proper Has behavior.
388 hasFields{6: false, 7: false},
389 setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
390 hasFields{6: true, 7: true},
Joe Tsai3e6a39b2019-07-10 10:21:12 -0700391
392 // Setting a bytes field to non-nil empty bytes should not preserve presence.
393 setFields{10: V([]byte{}), 11: V([]byte{}), 21: V([]byte{}), 22: V([]byte{})},
394 getFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
395 hasFields{10: false, 11: false, 21: false, 22: false},
Joe Tsai91e14662018-09-13 13:24:35 -0700396 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800397
398 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700399 testMessage(t, nil, (*ScalarProto3)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800400 hasFields{
401 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
402 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
403 },
404 getFields{
405 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)),
406 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)),
407 },
408 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700409}
410
Joe Tsaif0c01e42018-11-06 13:05:20 -0800411type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700412 Bools []bool `protobuf:"1"`
413 Int32s []int32 `protobuf:"2"`
414 Int64s []int64 `protobuf:"3"`
415 Uint32s []uint32 `protobuf:"4"`
416 Uint64s []uint64 `protobuf:"5"`
417 Float32s []float32 `protobuf:"6"`
418 Float64s []float64 `protobuf:"7"`
419 Strings []string `protobuf:"8"`
420 StringsA [][]byte `protobuf:"9"`
421 Bytes [][]byte `protobuf:"10"`
422 BytesA []string `protobuf:"11"`
423
424 MyStrings1 []MyString `protobuf:"12"`
425 MyStrings2 []MyBytes `protobuf:"13"`
426 MyBytes1 []MyBytes `protobuf:"14"`
427 MyBytes2 []MyString `protobuf:"15"`
428
Joe Tsai4b7aff62018-11-14 14:05:19 -0800429 MyStrings3 ListStrings `protobuf:"16"`
430 MyStrings4 ListBytes `protobuf:"17"`
431 MyBytes3 ListBytes `protobuf:"18"`
432 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700433}
434
Damien Neil16163b42019-08-06 15:43:25 -0700435var listScalarsType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(ListScalars)), Desc: mustMakeMessageDesc("list-scalars.proto", pref.Proto2, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700436 name: "ListScalars"
437 field: [
438 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_BOOL},
439 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_INT32},
440 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_INT64},
441 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_UINT32},
442 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_UINT64},
443 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_FLOAT},
444 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_DOUBLE},
445 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_STRING},
446 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_STRING},
447 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_BYTES},
448 {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700449
Joe Tsaid8881392019-06-06 13:01:53 -0700450 {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_STRING},
451 {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_STRING},
452 {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_BYTES},
453 {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsai91e14662018-09-13 13:24:35 -0700454
Joe Tsaid8881392019-06-06 13:01:53 -0700455 {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_STRING},
456 {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_STRING},
457 {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_BYTES},
458 {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_BYTES}
459 ]
460 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700461}
Joe Tsai91e14662018-09-13 13:24:35 -0700462
Joe Tsai378c1322019-04-25 23:48:08 -0700463func (m *ListScalars) ProtoReflect() pref.Message { return listScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800464
465func TestListScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700466 empty := new(ListScalars).ProtoReflect()
467 want := (&ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700468 Bools: []bool{true, false, true},
469 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
470 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
471 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
472 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
473 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
474 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
475 Strings: []string{"8", "", "eight"},
476 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
477 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
478 BytesA: []string{"11", "", "eleven"},
479
480 MyStrings1: []MyString{"12", "", "twelve"},
481 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
482 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
483 MyBytes2: []MyString{"15", "", "fifteen"},
484
Joe Tsai4b7aff62018-11-14 14:05:19 -0800485 MyStrings3: ListStrings{"16", "", "sixteen"},
486 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
487 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
488 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsai378c1322019-04-25 23:48:08 -0700489 }).ProtoReflect()
Joe Tsai91e14662018-09-13 13:24:35 -0700490
Joe Tsai378c1322019-04-25 23:48:08 -0700491 testMessage(t, nil, new(ListScalars).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700492 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 -0700493 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)},
494 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)},
495 listFieldsMutable{
Joe Tsai91e14662018-09-13 13:24:35 -0700496 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800497 lenList(0),
498 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
499 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700500 equalList{getField(want, 2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700501 },
502 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800503 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
504 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
505 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700506 },
507 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800508 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700509 equalList{getField(want, 6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700510 },
511 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800512 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
513 lenList(6),
514 setList{0: V("8"), 2: V("eight")},
515 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700516 equalList{getField(want, 8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700517 },
518 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800519 appendList{V([]byte(nil)), V([]byte(nil))},
520 setList{0: V([]byte("10"))},
521 appendList{V([]byte("wrong"))},
522 setList{2: V([]byte("ten"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700523 equalList{getField(want, 10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700524 },
525 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800526 appendList{V("12"), V("wrong"), V("twelve")},
527 setList{1: V("")},
Joe Tsai378c1322019-04-25 23:48:08 -0700528 equalList{getField(want, 12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700529 },
530 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800531 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700532 equalList{getField(want, 14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700533 },
534 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800535 appendList{V("16"), V(""), V("sixteen"), V("extra")},
536 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700537 equalList{getField(want, 16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700538 },
539 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800540 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700541 equalList{getField(want, 18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700542 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700543 },
Joe Tsai91e14662018-09-13 13:24:35 -0700544 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 -0800545 equalMessage{want},
546 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
547 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000548 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800549
550 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700551 testMessage(t, nil, (*ListScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800552 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},
553 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)}},
554 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000555}
556
Joe Tsaice6edd32018-10-19 16:27:46 -0700557type MapScalars struct {
558 KeyBools map[bool]string `protobuf:"1"`
559 KeyInt32s map[int32]string `protobuf:"2"`
560 KeyInt64s map[int64]string `protobuf:"3"`
561 KeyUint32s map[uint32]string `protobuf:"4"`
562 KeyUint64s map[uint64]string `protobuf:"5"`
563 KeyStrings map[string]string `protobuf:"6"`
564
565 ValBools map[string]bool `protobuf:"7"`
566 ValInt32s map[string]int32 `protobuf:"8"`
567 ValInt64s map[string]int64 `protobuf:"9"`
568 ValUint32s map[string]uint32 `protobuf:"10"`
569 ValUint64s map[string]uint64 `protobuf:"11"`
570 ValFloat32s map[string]float32 `protobuf:"12"`
571 ValFloat64s map[string]float64 `protobuf:"13"`
572 ValStrings map[string]string `protobuf:"14"`
573 ValStringsA map[string][]byte `protobuf:"15"`
574 ValBytes map[string][]byte `protobuf:"16"`
575 ValBytesA map[string]string `protobuf:"17"`
576
577 MyStrings1 map[MyString]MyString `protobuf:"18"`
578 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
579 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
580 MyBytes2 map[MyString]MyString `protobuf:"21"`
581
582 MyStrings3 MapStrings `protobuf:"22"`
583 MyStrings4 MapBytes `protobuf:"23"`
584 MyBytes3 MapBytes `protobuf:"24"`
585 MyBytes4 MapStrings `protobuf:"25"`
586}
587
Damien Neil16163b42019-08-06 15:43:25 -0700588var mapScalarsType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(MapScalars)), Desc: mustMakeMessageDesc("map-scalars.proto", pref.Proto2, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700589 name: "MapScalars"
590 field: [
591 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F1Entry"},
592 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F2Entry"},
593 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F3Entry"},
594 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F4Entry"},
595 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F5Entry"},
596 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F6Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000597
Joe Tsaid8881392019-06-06 13:01:53 -0700598 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F7Entry"},
599 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F8Entry"},
600 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F9Entry"},
601 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F10Entry"},
602 {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F11Entry"},
603 {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F12Entry"},
604 {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F13Entry"},
605 {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F14Entry"},
606 {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F15Entry"},
607 {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F16Entry"},
608 {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F17Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000609
Joe Tsaid8881392019-06-06 13:01:53 -0700610 {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F18Entry"},
611 {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F19Entry"},
612 {name:"f20" number:20 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F20Entry"},
613 {name:"f21" number:21 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F21Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000614
Joe Tsaid8881392019-06-06 13:01:53 -0700615 {name:"f22" number:22 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F22Entry"},
616 {name:"f23" number:23 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F23Entry"},
617 {name:"f24" number:24 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F24Entry"},
618 {name:"f25" number:25 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F25Entry"}
619 ]
620 nested_type: [
621 {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}},
622 {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}},
623 {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}},
624 {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}},
625 {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}},
626 {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}},
627
628 {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}},
629 {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}},
630 {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}},
631 {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}},
632 {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}},
633 {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}},
634 {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}},
635 {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}},
636 {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}},
637 {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}},
638 {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}},
639
640 {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}},
641 {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}},
642 {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}},
643 {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}},
644
645 {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}},
646 {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}},
647 {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}},
648 {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}}
649 ]
650 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700651}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000652
Joe Tsai378c1322019-04-25 23:48:08 -0700653func (m *MapScalars) ProtoReflect() pref.Message { return mapScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800654
655func TestMapScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700656 empty := new(MapScalars).ProtoReflect()
657 want := (&MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000658 KeyBools: map[bool]string{true: "true", false: "false"},
659 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
660 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
661 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
662 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
663 KeyStrings: map[string]string{"": "", "foo": "bar"},
664
665 ValBools: map[string]bool{"true": true, "false": false},
666 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
667 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
668 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
669 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
670 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
671 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
672 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
673 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
674 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
675 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
676
677 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
678 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
679 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
680 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
681
682 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
683 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
684 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
685 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsai378c1322019-04-25 23:48:08 -0700686 }).ProtoReflect()
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000687
Joe Tsai378c1322019-04-25 23:48:08 -0700688 testMessage(t, nil, new(MapScalars).ProtoReflect(), messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000689 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 -0700690 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)},
691 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)},
692 mapFieldsMutable{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000693 2: {
694 lenMap(0),
695 hasMap{int32(0): false, int32(-1): false, int32(2): false},
696 setMap{int32(0): V("zero")},
697 lenMap(1),
698 hasMap{int32(0): true, int32(-1): false, int32(2): false},
699 setMap{int32(-1): V("one")},
700 lenMap(2),
701 hasMap{int32(0): true, int32(-1): true, int32(2): false},
702 setMap{int32(2): V("two")},
703 lenMap(3),
704 hasMap{int32(0): true, int32(-1): true, int32(2): true},
705 },
706 4: {
707 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai378c1322019-04-25 23:48:08 -0700708 equalMap{getField(want, 4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000709 },
710 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800711 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000712 setMap{"foo": V("bar")},
713 setMap{"": V("empty")},
714 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
715 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800716 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000717 },
718 8: {
Joe Tsai378c1322019-04-25 23:48:08 -0700719 equalMap{getField(empty, 8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000720 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
721 },
722 10: {
723 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
724 lenMap(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700725 equalMap{getField(want, 10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000726 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
727 },
728 12: {
729 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800730 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000731 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
732 },
733 14: {
Joe Tsai378c1322019-04-25 23:48:08 -0700734 equalMap{getField(empty, 14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000735 setMap{"s1": V("s1"), "s2": V("s2")},
736 },
737 16: {
738 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700739 equalMap{getField(want, 16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000740 },
741 18: {
742 hasMap{"s1": false, "s2": false, "s3": false},
743 setMap{"s1": V("s1"), "s2": V("s2")},
744 hasMap{"s1": true, "s2": true, "s3": false},
745 },
746 20: {
Joe Tsai378c1322019-04-25 23:48:08 -0700747 equalMap{getField(empty, 20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000748 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
749 },
750 22: {
751 rangeMap{},
752 setMap{"s1": V("s1"), "s2": V("s2")},
753 rangeMap{"s1": V("s1"), "s2": V("s2")},
754 lenMap(2),
755 },
756 24: {
757 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700758 equalMap{getField(want, 24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000759 },
760 },
761 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 -0800762 equalMessage{want},
763 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},
764 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700765 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800766
767 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700768 testMessage(t, nil, (*MapScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800769 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},
770 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)}},
771 })
Joe Tsai91e14662018-09-13 13:24:35 -0700772}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700773
Joe Tsai87b955b2018-11-14 21:59:49 -0800774type OneofScalars struct {
775 Union isOneofScalars_Union `protobuf_oneof:"union"`
776}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700777
Damien Neil16163b42019-08-06 15:43:25 -0700778var oneofScalarsType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(OneofScalars)), Desc: mustMakeMessageDesc("oneof-scalars.proto", pref.Proto2, "", `
Joe Tsaid8881392019-06-06 13:01:53 -0700779 name: "OneofScalars"
780 field: [
781 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true" oneof_index:0},
782 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2" oneof_index:0},
783 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3" oneof_index:0},
784 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4" oneof_index:0},
785 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5" oneof_index:0},
786 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6" oneof_index:0},
787 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7" oneof_index:0},
788 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8" oneof_index:0},
789 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9" oneof_index:0},
790 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"10" oneof_index:0},
791 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11" oneof_index:0},
792 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"12" oneof_index:0},
793 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"13" oneof_index:0}
794 ]
795 oneof_decl: [{name:"union"}]
796 `, nil),
Damien Neil16163b42019-08-06 15:43:25 -0700797}
Joe Tsaif0c01e42018-11-06 13:05:20 -0800798
Joe Tsai378c1322019-04-25 23:48:08 -0700799func (m *OneofScalars) ProtoReflect() pref.Message { return oneofScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800800
Joe Tsaif18ab532018-11-27 17:25:04 -0800801func (*OneofScalars) XXX_OneofWrappers() []interface{} {
802 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700803 (*OneofScalars_Bool)(nil),
804 (*OneofScalars_Int32)(nil),
805 (*OneofScalars_Int64)(nil),
806 (*OneofScalars_Uint32)(nil),
807 (*OneofScalars_Uint64)(nil),
808 (*OneofScalars_Float32)(nil),
809 (*OneofScalars_Float64)(nil),
810 (*OneofScalars_String)(nil),
811 (*OneofScalars_StringA)(nil),
812 (*OneofScalars_StringB)(nil),
813 (*OneofScalars_Bytes)(nil),
814 (*OneofScalars_BytesA)(nil),
815 (*OneofScalars_BytesB)(nil),
816 }
817}
818
Joe Tsai87b955b2018-11-14 21:59:49 -0800819type (
820 isOneofScalars_Union interface {
821 isOneofScalars_Union()
822 }
823 OneofScalars_Bool struct {
824 Bool bool `protobuf:"1"`
825 }
826 OneofScalars_Int32 struct {
827 Int32 MyInt32 `protobuf:"2"`
828 }
829 OneofScalars_Int64 struct {
830 Int64 int64 `protobuf:"3"`
831 }
832 OneofScalars_Uint32 struct {
833 Uint32 MyUint32 `protobuf:"4"`
834 }
835 OneofScalars_Uint64 struct {
836 Uint64 uint64 `protobuf:"5"`
837 }
838 OneofScalars_Float32 struct {
839 Float32 MyFloat32 `protobuf:"6"`
840 }
841 OneofScalars_Float64 struct {
842 Float64 float64 `protobuf:"7"`
843 }
844 OneofScalars_String struct {
845 String string `protobuf:"8"`
846 }
847 OneofScalars_StringA struct {
848 StringA []byte `protobuf:"9"`
849 }
850 OneofScalars_StringB struct {
851 StringB MyString `protobuf:"10"`
852 }
853 OneofScalars_Bytes struct {
854 Bytes []byte `protobuf:"11"`
855 }
856 OneofScalars_BytesA struct {
857 BytesA string `protobuf:"12"`
858 }
859 OneofScalars_BytesB struct {
860 BytesB MyBytes `protobuf:"13"`
861 }
862)
863
Joe Tsai2c870bb2018-10-17 11:46:52 -0700864func (*OneofScalars_Bool) isOneofScalars_Union() {}
865func (*OneofScalars_Int32) isOneofScalars_Union() {}
866func (*OneofScalars_Int64) isOneofScalars_Union() {}
867func (*OneofScalars_Uint32) isOneofScalars_Union() {}
868func (*OneofScalars_Uint64) isOneofScalars_Union() {}
869func (*OneofScalars_Float32) isOneofScalars_Union() {}
870func (*OneofScalars_Float64) isOneofScalars_Union() {}
871func (*OneofScalars_String) isOneofScalars_Union() {}
872func (*OneofScalars_StringA) isOneofScalars_Union() {}
873func (*OneofScalars_StringB) isOneofScalars_Union() {}
874func (*OneofScalars_Bytes) isOneofScalars_Union() {}
875func (*OneofScalars_BytesA) isOneofScalars_Union() {}
876func (*OneofScalars_BytesB) isOneofScalars_Union() {}
877
878func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800879 empty := &OneofScalars{}
880 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
881 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
882 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
883 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
884 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
885 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
886 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
887 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
888 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
889 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
890 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
891 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
892 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700893
Joe Tsai378c1322019-04-25 23:48:08 -0700894 testMessage(t, nil, new(OneofScalars).ProtoReflect(), messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700895 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},
896 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 -0700897 whichOneofs{"union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700898
Joe Tsai378c1322019-04-25 23:48:08 -0700899 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1.ProtoReflect()},
900 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2.ProtoReflect()},
901 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3.ProtoReflect()},
902 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4.ProtoReflect()},
903 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5.ProtoReflect()},
904 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6.ProtoReflect()},
905 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7.ProtoReflect()},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700906
907 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 -0700908 whichOneofs{"union": 7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700909
Joe Tsai378c1322019-04-25 23:48:08 -0700910 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8.ProtoReflect()},
911 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9.ProtoReflect()},
912 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10.ProtoReflect()},
913 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11.ProtoReflect()},
914 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12.ProtoReflect()},
915 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700916
917 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},
918 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 -0800919 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai378c1322019-04-25 23:48:08 -0700920 whichOneofs{"union": 13},
921 equalMessage{want13.ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800922 clearFields{13},
Joe Tsai378c1322019-04-25 23:48:08 -0700923 whichOneofs{"union": 0},
924 equalMessage{empty.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700925 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800926
927 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700928 testMessage(t, nil, (*OneofScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800929 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},
930 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"))},
931 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700932}
933
Joe Tsai87b955b2018-11-14 21:59:49 -0800934type EnumProto2 int32
935
Damien Neil16163b42019-08-06 15:43:25 -0700936var enumProto2Desc = mustMakeEnumDesc("enum2.proto", pref.Proto2, `
937 name: "EnumProto2"
938 value: [{name:"DEAD" number:0xdead}, {name:"BEEF" number:0xbeef}]
939`)
Joe Tsai87b955b2018-11-14 21:59:49 -0800940
Damien Neil16163b42019-08-06 15:43:25 -0700941func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Desc }
942func (e EnumProto2) Type() pref.EnumType { return e }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700943func (e EnumProto2) Enum() *EnumProto2 { return &e }
944func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Damien Neil16163b42019-08-06 15:43:25 -0700945func (t EnumProto2) New(n pref.EnumNumber) pref.Enum { return EnumProto2(n) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800946
947type EnumProto3 int32
948
Damien Neil16163b42019-08-06 15:43:25 -0700949var enumProto3Desc = mustMakeEnumDesc("enum3.proto", pref.Proto3, `
950 name: "EnumProto3",
951 value: [{name:"ALPHA" number:0}, {name:"BRAVO" number:1}]
952`)
Joe Tsai87b955b2018-11-14 21:59:49 -0800953
Damien Neil16163b42019-08-06 15:43:25 -0700954func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Desc }
955func (e EnumProto3) Type() pref.EnumType { return e }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700956func (e EnumProto3) Enum() *EnumProto3 { return &e }
957func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Damien Neil16163b42019-08-06 15:43:25 -0700958func (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 Tsai55f18252020-01-11 00:25:01 -0800973 dependency: ["enum2.proto", "enum3.proto", "scalar2.proto", "scalar3.proto", "proto2_20180125_92554152/test.proto"]
Joe Tsaid8881392019-06-06 13:01:53 -0700974 `, `
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 ]
Damien Neilc8268852019-10-08 13:28:53 -0700995 `, newFileRegistry(
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
Damien Neilc8268852019-10-08 13:28:53 -07001004func newFileRegistry(files ...pref.FileDescriptor) *protoregistry.Files {
1005 r := new(protoregistry.Files)
1006 for _, file := range files {
1007 r.RegisterFile(file)
1008 }
1009 return r
1010}
1011
Joe Tsai378c1322019-04-25 23:48:08 -07001012func (m *EnumMessages) ProtoReflect() pref.Message { return enumMessagesType.MessageOf(m) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001013
Joe Tsaif18ab532018-11-27 17:25:04 -08001014func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1015 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001016 (*EnumMessages_OneofE2)(nil),
1017 (*EnumMessages_OneofE3)(nil),
1018 (*EnumMessages_OneofM2)(nil),
1019 (*EnumMessages_OneofM3)(nil),
1020 }
1021}
1022
1023type (
1024 isEnumMessages_Union interface {
1025 isEnumMessages_Union()
1026 }
1027 EnumMessages_OneofE2 struct {
1028 OneofE2 EnumProto2 `protobuf:"9"`
1029 }
1030 EnumMessages_OneofE3 struct {
1031 OneofE3 EnumProto3 `protobuf:"10"`
1032 }
1033 EnumMessages_OneofM2 struct {
1034 OneofM2 *ScalarProto2 `protobuf:"11"`
1035 }
1036 EnumMessages_OneofM3 struct {
1037 OneofM3 *ScalarProto3 `protobuf:"12"`
1038 }
1039)
1040
1041func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1042func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1043func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1044func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1045
1046func TestEnumMessages(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -07001047 emptyL := pimpl.Export{}.MessageOf(new(proto2_20180125.Message))
1048 emptyM := new(EnumMessages).ProtoReflect()
1049 emptyM2 := new(ScalarProto2).ProtoReflect()
1050 emptyM3 := new(ScalarProto3).ProtoReflect()
1051
Damien Neila8a2cea2019-07-10 16:17:16 -07001052 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)})
Joe Tsai378c1322019-04-25 23:48:08 -07001053 wantM := (&EnumMessages{EnumP2: EnumProto2(1234).Enum()}).ProtoReflect()
Damien Neila8a2cea2019-07-10 16:17:16 -07001054 wantM2a := &ScalarProto2{Float32: proto.Float32(math.Pi)}
1055 wantM2b := &ScalarProto2{Float32: proto.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001056 wantM3a := &ScalarProto3{Float32: math.Pi}
1057 wantM3b := &ScalarProto3{Float32: math.Ln2}
1058
Joe Tsai378c1322019-04-25 23:48:08 -07001059 wantList5 := getField((&EnumMessages{EnumList: []EnumProto2{333, 222}}).ProtoReflect(), 5)
1060 wantList6 := getField((&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).ProtoReflect(), 6)
Joe Tsai87b955b2018-11-14 21:59:49 -08001061
Joe Tsai378c1322019-04-25 23:48:08 -07001062 wantMap7 := getField((&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).ProtoReflect(), 7)
1063 wantMap8 := getField((&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).ProtoReflect(), 8)
Joe Tsai87b955b2018-11-14 21:59:49 -08001064
Joe Tsai378c1322019-04-25 23:48:08 -07001065 testMessage(t, nil, new(EnumMessages).ProtoReflect(), messageOps{
Joe Tsai87b955b2018-11-14 21:59:49 -08001066 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 -07001067 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 -08001068
1069 // Test singular enums.
1070 setFields{1: VE(0xdead), 2: VE(0)},
1071 getFields{1: VE(0xdead), 2: VE(0)},
1072 hasFields{1: true, 2: true},
1073
1074 // Test singular messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001075 messageFieldsMutable{3: messageOps{setFields{109: V(float32(math.E))}}},
1076 messageFieldsMutable{4: messageOps{setFields{1: VE(1234)}}},
Joe Tsai87b955b2018-11-14 21:59:49 -08001077 getFields{3: V(wantL), 4: V(wantM)},
1078 clearFields{3, 4},
1079 hasFields{3: false, 4: false},
1080 setFields{3: V(wantL), 4: V(wantM)},
1081 hasFields{3: true, 4: true},
1082
1083 // Test list of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001084 listFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001085 5: listOps{
1086 appendList{VE(111), VE(222)},
1087 setList{0: VE(333)},
1088 getList{0: VE(333), 1: VE(222)},
1089 lenList(2),
1090 },
1091 6: listOps{
1092 appendMessageList{setFields{4: V(uint32(1e6))}},
1093 appendMessageList{setFields{6: V(float32(math.Phi))}},
Joe Tsai378c1322019-04-25 23:48:08 -07001094 setList{0: V(wantM2a.ProtoReflect())},
1095 getList{0: V(wantM2a.ProtoReflect()), 1: V(wantM2b.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001096 },
1097 },
1098 getFields{5: wantList5, 6: wantList6},
1099 hasFields{5: true, 6: true},
1100 listFields{5: listOps{truncList(0)}},
1101 hasFields{5: false, 6: true},
1102
1103 // Test maps of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001104 mapFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001105 7: mapOps{
1106 setMap{"one": VE(1), "two": VE(2)},
1107 hasMap{"one": true, "two": true, "three": false},
1108 lenMap(2),
1109 },
1110 8: mapOps{
1111 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
Joe Tsai378c1322019-04-25 23:48:08 -07001112 setMap{"ln2": V(wantM3b.ProtoReflect())},
1113 getMap{"pi": V(wantM3a.ProtoReflect()), "ln2": V(wantM3b.ProtoReflect()), "none": V(nil)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001114 lenMap(2),
1115 },
1116 },
1117 getFields{7: wantMap7, 8: wantMap8},
1118 hasFields{7: true, 8: true},
1119 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1120 hasFields{7: true, 8: false},
1121
1122 // Test oneofs of enums and messages.
1123 setFields{9: VE(0xdead)},
1124 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1125 setFields{10: VE(0)},
1126 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001127 messageFieldsMutable{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1128 getFields{11: V(wantM2a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001129 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001130 messageFieldsMutable{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1131 getFields{12: V(wantM3a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001132 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1133
1134 // Check entire message.
Joe Tsai378c1322019-04-25 23:48:08 -07001135 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a.ProtoReflect())},
1136 equalMessage{(&EnumMessages{
Joe Tsai87b955b2018-11-14 21:59:49 -08001137 EnumP2: EnumProto2(0xdead).Enum(),
1138 EnumP3: EnumProto3(0).Enum(),
Damien Neila8a2cea2019-07-10 16:17:16 -07001139 MessageLegacy: &proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)},
Joe Tsai378c1322019-04-25 23:48:08 -07001140 MessageCycle: wantM.Interface().(*EnumMessages),
Joe Tsai87b955b2018-11-14 21:59:49 -08001141 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1142 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1143 Union: &EnumMessages_OneofM3{wantM3a},
Joe Tsai378c1322019-04-25 23:48:08 -07001144 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001145 clearFields{1, 2, 3, 4, 6, 7, 12},
Joe Tsai378c1322019-04-25 23:48:08 -07001146 equalMessage{new(EnumMessages).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001147 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001148
1149 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -07001150 testMessage(t, nil, (*EnumMessages)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -08001151 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 -07001152 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 -08001153 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1154 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1155 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001156}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001157
Joe Tsai91e14662018-09-13 13:24:35 -07001158var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001159 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
Joe Tsaid8881392019-06-06 13:01:53 -07001160 mx := pimpl.Export{}.MessageOf(x).Interface()
1161 my := pimpl.Export{}.MessageOf(y).Interface()
1162 return proto.Equal(mx, my)
Joe Tsai91e14662018-09-13 13:24:35 -07001163 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001164 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
Joe Tsai378c1322019-04-25 23:48:08 -07001165 switch v := pv.Interface().(type) {
1166 case pref.Message:
1167 out := make(map[pref.FieldNumber]pref.Value)
1168 v.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1169 out[fd.Number()] = v
1170 return true
1171 })
1172 return out
1173 case pref.List:
1174 var out []pref.Value
1175 for i := 0; i < v.Len(); i++ {
1176 out = append(out, v.Get(i))
1177 }
1178 return out
1179 case pref.Map:
1180 out := make(map[interface{}]pref.Value)
1181 v.Range(func(k pref.MapKey, v pref.Value) bool {
1182 out[k.Interface()] = v
1183 return true
1184 })
1185 return out
1186 default:
1187 return v
1188 }
Joe Tsai91e14662018-09-13 13:24:35 -07001189 }),
1190 cmpopts.EquateNaNs(),
1191}
1192
1193func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
Joe Tsai378c1322019-04-25 23:48:08 -07001194 fieldDescs := m.Descriptor().Fields()
1195 oneofDescs := m.Descriptor().Oneofs()
Joe Tsai91e14662018-09-13 13:24:35 -07001196 for i, op := range tt {
1197 p.Push(i)
1198 switch op := op.(type) {
1199 case equalMessage:
Joe Tsai378c1322019-04-25 23:48:08 -07001200 if diff := cmp.Diff(V(op.Message), V(m), cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001201 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1202 }
1203 case hasFields:
1204 got := map[pref.FieldNumber]bool{}
1205 want := map[pref.FieldNumber]bool(op)
1206 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001207 fd := fieldDescs.ByNumber(n)
1208 got[n] = m.Has(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001209 }
1210 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001211 t.Errorf("operation %v, Message.Has mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001212 }
1213 case getFields:
1214 got := map[pref.FieldNumber]pref.Value{}
1215 want := map[pref.FieldNumber]pref.Value(op)
1216 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001217 fd := fieldDescs.ByNumber(n)
1218 got[n] = m.Get(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001219 }
1220 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001221 t.Errorf("operation %v, Message.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001222 }
1223 case setFields:
1224 for n, v := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001225 fd := fieldDescs.ByNumber(n)
1226 m.Set(fd, v)
Joe Tsai91e14662018-09-13 13:24:35 -07001227 }
1228 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001229 for _, n := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001230 fd := fieldDescs.ByNumber(n)
1231 m.Clear(fd)
Joe Tsai87b955b2018-11-14 21:59:49 -08001232 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001233 case whichOneofs:
1234 got := map[pref.Name]pref.FieldNumber{}
1235 want := map[pref.Name]pref.FieldNumber(op)
1236 for s := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001237 od := oneofDescs.ByName(s)
1238 fd := m.WhichOneof(od)
1239 if fd == nil {
1240 got[s] = 0
1241 } else {
1242 got[s] = fd.Number()
1243 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001244 }
1245 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001246 t.Errorf("operation %v, Message.WhichOneof mismatch (-want, +got):\n%s", p, diff)
Joe Tsai4ec39c72019-04-03 13:40:53 -07001247 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001248 case messageFields:
1249 for n, tt := range op {
1250 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001251 fd := fieldDescs.ByNumber(n)
1252 testMessage(t, p, m.Get(fd).Message(), tt)
1253 p.Pop()
1254 }
1255 case messageFieldsMutable:
1256 for n, tt := range op {
1257 p.Push(int(n))
1258 fd := fieldDescs.ByNumber(n)
1259 testMessage(t, p, m.Mutable(fd).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001260 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001261 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001262 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001263 for n, tt := range op {
1264 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001265 fd := fieldDescs.ByNumber(n)
1266 testLists(t, p, m.Get(fd).List(), tt)
1267 p.Pop()
1268 }
1269 case listFieldsMutable:
1270 for n, tt := range op {
1271 p.Push(int(n))
1272 fd := fieldDescs.ByNumber(n)
1273 testLists(t, p, m.Mutable(fd).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001274 p.Pop()
1275 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001276 case mapFields:
1277 for n, tt := range op {
1278 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001279 fd := fieldDescs.ByNumber(n)
1280 testMaps(t, p, m.Get(fd).Map(), tt)
1281 p.Pop()
1282 }
1283 case mapFieldsMutable:
1284 for n, tt := range op {
1285 p.Push(int(n))
1286 fd := fieldDescs.ByNumber(n)
1287 testMaps(t, p, m.Mutable(fd).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001288 p.Pop()
1289 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001290 case rangeFields:
1291 got := map[pref.FieldNumber]pref.Value{}
1292 want := map[pref.FieldNumber]pref.Value(op)
Joe Tsai378c1322019-04-25 23:48:08 -07001293 m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1294 got[fd.Number()] = v
Joe Tsai87b955b2018-11-14 21:59:49 -08001295 return true
1296 })
1297 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001298 t.Errorf("operation %v, Message.Range mismatch (-want, +got):\n%s", p, diff)
Joe Tsai87b955b2018-11-14 21:59:49 -08001299 }
Joe Tsai91e14662018-09-13 13:24:35 -07001300 default:
1301 t.Fatalf("operation %v, invalid operation: %T", p, op)
1302 }
1303 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001304 }
1305}
Joe Tsai91e14662018-09-13 13:24:35 -07001306
Joe Tsai4b7aff62018-11-14 14:05:19 -08001307func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001308 for i, op := range tt {
1309 p.Push(i)
1310 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001311 case equalList:
Joe Tsai378c1322019-04-25 23:48:08 -07001312 if diff := cmp.Diff(V(op.List), V(v), cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001313 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001314 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001315 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001316 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001317 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001318 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001319 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001320 got := map[int]pref.Value{}
1321 want := map[int]pref.Value(op)
1322 for n := range want {
1323 got[n] = v.Get(n)
1324 }
1325 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001326 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001327 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001328 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001329 for n, e := range op {
1330 v.Set(n, e)
1331 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001332 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001333 for _, e := range op {
1334 v.Append(e)
1335 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001336 case appendMessageList:
Damien Neild91c4222019-09-04 10:46:00 -07001337 e := v.NewElement()
1338 v.Append(e)
1339 testMessage(t, p, e.Message(), messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001340 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001341 v.Truncate(int(op))
1342 default:
1343 t.Fatalf("operation %v, invalid operation: %T", p, op)
1344 }
1345 p.Pop()
1346 }
1347}
1348
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001349func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1350 for i, op := range tt {
1351 p.Push(i)
1352 switch op := op.(type) {
1353 case equalMap:
Joe Tsai378c1322019-04-25 23:48:08 -07001354 if diff := cmp.Diff(V(op.Map), V(m), cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001355 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1356 }
1357 case lenMap:
1358 if got, want := m.Len(), int(op); got != want {
1359 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1360 }
1361 case hasMap:
1362 got := map[interface{}]bool{}
1363 want := map[interface{}]bool(op)
1364 for k := range want {
1365 got[k] = m.Has(V(k).MapKey())
1366 }
1367 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1368 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1369 }
1370 case getMap:
1371 got := map[interface{}]pref.Value{}
1372 want := map[interface{}]pref.Value(op)
1373 for k := range want {
1374 got[k] = m.Get(V(k).MapKey())
1375 }
1376 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1377 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1378 }
1379 case setMap:
1380 for k, v := range op {
1381 m.Set(V(k).MapKey(), v)
1382 }
1383 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001384 for _, k := range op {
1385 m.Clear(V(k).MapKey())
1386 }
1387 case messageMap:
1388 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001389 mk := V(k).MapKey()
1390 if !m.Has(mk) {
Damien Neild91c4222019-09-04 10:46:00 -07001391 m.Set(mk, m.NewValue())
Damien Neil97e7f572018-12-07 14:28:33 -08001392 }
1393 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001394 }
1395 case rangeMap:
1396 got := map[interface{}]pref.Value{}
1397 want := map[interface{}]pref.Value(op)
1398 m.Range(func(k pref.MapKey, v pref.Value) bool {
1399 got[k.Interface()] = v
1400 return true
1401 })
1402 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1403 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1404 }
1405 default:
1406 t.Fatalf("operation %v, invalid operation: %T", p, op)
1407 }
1408 p.Pop()
1409 }
1410}
1411
Joe Tsai378c1322019-04-25 23:48:08 -07001412func getField(m pref.Message, n pref.FieldNumber) pref.Value {
1413 fd := m.Descriptor().Fields().ByNumber(n)
1414 return m.Get(fd)
1415}
1416
Joe Tsai91e14662018-09-13 13:24:35 -07001417type path []int
1418
1419func (p *path) Push(i int) { *p = append(*p, i) }
1420func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1421func (p path) String() string {
1422 var ss []string
1423 for _, i := range p {
1424 ss = append(ss, fmt.Sprint(i))
1425 }
1426 return strings.Join(ss, ".")
1427}
Joe Tsai82760ce2019-06-20 03:09:57 -07001428
Joe Tsai5d634732020-07-26 22:37:47 -07001429type UnknownFieldsA struct {
1430 XXX_unrecognized []byte
1431}
1432
1433var unknownFieldsAType = pimpl.MessageInfo{
1434 GoReflectType: reflect.TypeOf(new(UnknownFieldsA)),
1435 Desc: mustMakeMessageDesc("unknown.proto", pref.Proto2, "", `name: "UnknownFieldsA"`, nil),
1436}
1437
1438func (m *UnknownFieldsA) ProtoReflect() pref.Message { return unknownFieldsAType.MessageOf(m) }
1439
1440type UnknownFieldsB struct {
1441 XXX_unrecognized *[]byte
1442}
1443
1444var unknownFieldsBType = pimpl.MessageInfo{
1445 GoReflectType: reflect.TypeOf(new(UnknownFieldsB)),
1446 Desc: mustMakeMessageDesc("unknown.proto", pref.Proto2, "", `name: "UnknownFieldsB"`, nil),
1447}
1448
1449func (m *UnknownFieldsB) ProtoReflect() pref.Message { return unknownFieldsBType.MessageOf(m) }
1450
1451func TestUnknownFields(t *testing.T) {
1452 for _, m := range []proto.Message{new(UnknownFieldsA), new(UnknownFieldsB)} {
1453 t.Run(reflect.TypeOf(m).Elem().Name(), func(t *testing.T) {
1454 want := protopack.Message{
1455 protopack.Tag{1, protopack.BytesType}, protopack.String("Hello, world!"),
1456 }.Marshal()
1457 m.ProtoReflect().SetUnknown(want)
1458 got := []byte(m.ProtoReflect().GetUnknown())
1459 if diff := cmp.Diff(want, got); diff != "" {
1460 t.Errorf("UnknownFields mismatch (-want +got):\n%s", diff)
1461 }
1462 })
1463 }
1464}
1465
Joe Tsaiae313d42019-10-16 10:25:54 -07001466func TestReset(t *testing.T) {
1467 mi := new(testpb.TestAllTypes)
1468
1469 // ProtoReflect is implemented using a messageState cache.
1470 m := mi.ProtoReflect()
1471
1472 // Reset must not clear the messageState cache.
1473 mi.Reset()
1474
1475 // If Reset accidentally cleared the messageState cache, this panics.
1476 m.Descriptor()
1477}
1478
Damien Neilb120a232019-12-16 13:23:25 -08001479func TestIsValid(t *testing.T) {
1480 var m *testpb.TestAllTypes
1481 if got, want := m.ProtoReflect().IsValid(), false; got != want {
1482 t.Errorf("((*M)(nil)).ProtoReflect().IsValid() = %v, want %v", got, want)
1483 }
1484 m = &testpb.TestAllTypes{}
1485 if got, want := m.ProtoReflect().IsValid(), true; got != want {
1486 t.Errorf("(&M{}).ProtoReflect().IsValid() = %v, want %v", got, want)
1487 }
1488}
1489
Joe Tsai82760ce2019-06-20 03:09:57 -07001490// The MessageState implementation makes the assumption that when a
1491// concrete message is unsafe casted as a *MessageState, the Go GC does
1492// not reclaim the memory for the remainder of the concrete message.
1493func TestUnsafeAssumptions(t *testing.T) {
1494 if !pimpl.UnsafeEnabled {
1495 t.Skip()
1496 }
1497
1498 var wg sync.WaitGroup
1499 for i := 0; i < 10; i++ {
1500 wg.Add(1)
1501 go func() {
1502 var ms [10]pref.Message
1503
1504 // Store the message only in its reflective form.
1505 // Trigger the GC after each iteration.
1506 for j := 0; j < 10; j++ {
1507 ms[j] = (&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -07001508 OptionalInt32: proto.Int32(int32(j)),
1509 OptionalFloat: proto.Float32(float32(j)),
Joe Tsai82760ce2019-06-20 03:09:57 -07001510 RepeatedInt32: []int32{int32(j)},
1511 RepeatedFloat: []float32{float32(j)},
Damien Neila8a2cea2019-07-10 16:17:16 -07001512 DefaultInt32: proto.Int32(int32(j)),
1513 DefaultFloat: proto.Float32(float32(j)),
Joe Tsai82760ce2019-06-20 03:09:57 -07001514 }).ProtoReflect()
1515 runtime.GC()
1516 }
1517
1518 // Convert the reflective form back into a concrete form.
1519 // Verify that the values written previously are still the same.
1520 for j := 0; j < 10; j++ {
1521 switch m := ms[j].Interface().(*testpb.TestAllTypes); {
1522 case m.GetOptionalInt32() != int32(j):
1523 case m.GetOptionalFloat() != float32(j):
1524 case m.GetRepeatedInt32()[0] != int32(j):
1525 case m.GetRepeatedFloat()[0] != float32(j):
1526 case m.GetDefaultInt32() != int32(j):
1527 case m.GetDefaultFloat() != float32(j):
1528 default:
1529 continue
1530 }
1531 t.Error("memory corrupted detected")
1532 }
1533 defer wg.Done()
1534 }()
1535 }
1536 wg.Wait()
1537}
1538
1539func BenchmarkName(b *testing.B) {
1540 var sink pref.FullName
1541 b.Run("Value", func(b *testing.B) {
1542 b.ReportAllocs()
1543 m := new(descriptorpb.FileDescriptorProto)
1544 for i := 0; i < b.N; i++ {
1545 sink = m.ProtoReflect().Descriptor().FullName()
1546 }
1547 })
1548 b.Run("Nil", func(b *testing.B) {
1549 b.ReportAllocs()
1550 m := (*descriptorpb.FileDescriptorProto)(nil)
1551 for i := 0; i < b.N; i++ {
1552 sink = m.ProtoReflect().Descriptor().FullName()
1553 }
1554 })
1555 runtime.KeepAlive(sink)
1556}
Joe Tsai9d637ca2019-09-18 18:08:52 -07001557
1558func BenchmarkReflect(b *testing.B) {
1559 m := new(testpb.TestAllTypes).ProtoReflect()
1560 fds := m.Descriptor().Fields()
1561 vs := make([]pref.Value, fds.Len())
1562 for i := range vs {
1563 vs[i] = m.NewField(fds.Get(i))
1564 }
1565
1566 b.Run("Has", func(b *testing.B) {
1567 b.ReportAllocs()
1568 for i := 0; i < b.N; i++ {
1569 for j := 0; j < fds.Len(); j++ {
1570 m.Has(fds.Get(j))
1571 }
1572 }
1573 })
1574 b.Run("Get", func(b *testing.B) {
1575 b.ReportAllocs()
1576 for i := 0; i < b.N; i++ {
1577 for j := 0; j < fds.Len(); j++ {
1578 m.Get(fds.Get(j))
1579 }
1580 }
1581 })
1582 b.Run("Set", func(b *testing.B) {
1583 b.ReportAllocs()
1584 for i := 0; i < b.N; i++ {
1585 for j := 0; j < fds.Len(); j++ {
1586 m.Set(fds.Get(j), vs[j])
1587 }
1588 }
1589 })
1590 b.Run("Clear", func(b *testing.B) {
1591 b.ReportAllocs()
1592 for i := 0; i < b.N; i++ {
1593 for j := 0; j < fds.Len(); j++ {
1594 m.Clear(fds.Get(j))
1595 }
1596 }
1597 })
1598 b.Run("Range", func(b *testing.B) {
1599 b.ReportAllocs()
1600 for i := 0; i < b.N; i++ {
1601 m.Range(func(pref.FieldDescriptor, pref.Value) bool {
1602 return true
1603 })
1604 }
1605 })
1606}