blob: f2fd2900240ce1af024d5b5f0a4afe443e0e010e [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"
Damien Neile89e6242019-05-13 23:55:40 -070020 scalar "google.golang.org/protobuf/internal/scalar"
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 Tsaib2f66be2019-05-22 00:42:45 -040025 "google.golang.org/protobuf/reflect/prototype"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070026
Damien Neile89e6242019-05-13 23:55:40 -070027 proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.0.0-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
Joe Tsaib2f66be2019-05-22 00:42:45 -0400213var scalarProto2Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto2)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700214 MessageDescriptor: mustMakeMessageDesc("scalar2.proto", pref.Proto2, "", `
215 name: "ScalarProto2"
216 field: [
217 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
218 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2"},
219 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3"},
220 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4"},
221 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5"},
222 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6"},
223 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7"},
224 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8"},
225 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9"},
226 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"10"},
227 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11"},
Joe Tsai91e14662018-09-13 13:24:35 -0700228
Joe Tsaid8881392019-06-06 13:01:53 -0700229 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
230 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"13"},
231 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"14"},
232 {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"15"},
233 {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"16"},
234 {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"17"},
235 {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"18"},
236 {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"19"},
237 {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"20"},
238 {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"21"},
239 {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"22"}
240 ]
241 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400242 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700243 return pref.ProtoMessage(new(ScalarProto2)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800244 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400245}}
Joe Tsai91e14662018-09-13 13:24:35 -0700246
Joe Tsai378c1322019-04-25 23:48:08 -0700247func (m *ScalarProto2) ProtoReflect() pref.Message { return scalarProto2Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800248
249func TestScalarProto2(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700250 testMessage(t, nil, new(ScalarProto2).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700251 hasFields{
252 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
253 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
254 },
255 getFields{
256 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")),
257 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")),
258 },
259 setFields{
260 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)),
261 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)),
262 },
263 hasFields{
264 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
265 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
266 },
Joe Tsai378c1322019-04-25 23:48:08 -0700267 equalMessage{(&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700268 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
269 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 -0700270 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800271 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 -0700272 equalMessage{new(ScalarProto2).ProtoReflect()},
Joe Tsai91e14662018-09-13 13:24:35 -0700273 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800274
275 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700276 testMessage(t, nil, (*ScalarProto2)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800277 hasFields{
278 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
279 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
280 },
281 getFields{
282 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")),
283 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")),
284 },
285 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700286}
287
Joe Tsaice6edd32018-10-19 16:27:46 -0700288type ScalarProto3 struct {
289 Bool bool `protobuf:"1"`
290 Int32 int32 `protobuf:"2"`
291 Int64 int64 `protobuf:"3"`
292 Uint32 uint32 `protobuf:"4"`
293 Uint64 uint64 `protobuf:"5"`
294 Float32 float32 `protobuf:"6"`
295 Float64 float64 `protobuf:"7"`
296 String string `protobuf:"8"`
297 StringA []byte `protobuf:"9"`
298 Bytes []byte `protobuf:"10"`
299 BytesA string `protobuf:"11"`
300
301 MyBool MyBool `protobuf:"12"`
302 MyInt32 MyInt32 `protobuf:"13"`
303 MyInt64 MyInt64 `protobuf:"14"`
304 MyUint32 MyUint32 `protobuf:"15"`
305 MyUint64 MyUint64 `protobuf:"16"`
306 MyFloat32 MyFloat32 `protobuf:"17"`
307 MyFloat64 MyFloat64 `protobuf:"18"`
308 MyString MyString `protobuf:"19"`
309 MyStringA MyBytes `protobuf:"20"`
310 MyBytes MyBytes `protobuf:"21"`
311 MyBytesA MyString `protobuf:"22"`
312}
313
Joe Tsaib2f66be2019-05-22 00:42:45 -0400314var scalarProto3Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto3)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700315 MessageDescriptor: mustMakeMessageDesc("scalar3.proto", pref.Proto3, "", `
316 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),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400343 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700344 return pref.ProtoMessage(new(ScalarProto3)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800345 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400346}}
Joe Tsai91e14662018-09-13 13:24:35 -0700347
Joe Tsai378c1322019-04-25 23:48:08 -0700348func (m *ScalarProto3) ProtoReflect() pref.Message { return scalarProto3Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800349
350func TestScalarProto3(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700351 testMessage(t, nil, new(ScalarProto3).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700352 hasFields{
353 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
354 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
355 },
356 getFields{
357 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)),
358 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)),
359 },
360 setFields{
361 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)),
362 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)),
363 },
364 hasFields{
365 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
366 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
367 },
Joe Tsai378c1322019-04-25 23:48:08 -0700368 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai91e14662018-09-13 13:24:35 -0700369 setFields{
370 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")),
371 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")),
372 },
373 hasFields{
374 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
375 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
376 },
Joe Tsai378c1322019-04-25 23:48:08 -0700377 equalMessage{(&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700378 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
379 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai378c1322019-04-25 23:48:08 -0700380 }).ProtoReflect()},
Joe Tsai44e389c2018-11-19 15:27:09 -0800381 setFields{
382 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
383 },
384 hasFields{
385 2: true, 3: true, 6: true, 7: true,
386 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800387 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 -0700388 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai060cdac2019-04-22 11:44:49 -0700389
390 // Verify that -0 triggers proper Has behavior.
391 hasFields{6: false, 7: false},
392 setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
393 hasFields{6: true, 7: true},
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
Joe Tsaib2f66be2019-05-22 00:42:45 -0400433var listScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ListScalars)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700434 MessageDescriptor: mustMakeMessageDesc("list-scalars.proto", pref.Proto2, "", `
435 name: "ListScalars"
436 field: [
437 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_BOOL},
438 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_INT32},
439 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_INT64},
440 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_UINT32},
441 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_UINT64},
442 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_FLOAT},
443 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_DOUBLE},
444 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_STRING},
445 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_STRING},
446 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_BYTES},
447 {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700448
Joe Tsaid8881392019-06-06 13:01:53 -0700449 {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_STRING},
450 {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_STRING},
451 {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_BYTES},
452 {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsai91e14662018-09-13 13:24:35 -0700453
Joe Tsaid8881392019-06-06 13:01:53 -0700454 {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_STRING},
455 {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_STRING},
456 {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_BYTES},
457 {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_BYTES}
458 ]
459 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400460 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700461 return pref.ProtoMessage(new(ListScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800462 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400463}}
Joe Tsai91e14662018-09-13 13:24:35 -0700464
Joe Tsai378c1322019-04-25 23:48:08 -0700465func (m *ListScalars) ProtoReflect() pref.Message { return listScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800466
467func TestListScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700468 empty := new(ListScalars).ProtoReflect()
469 want := (&ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700470 Bools: []bool{true, false, true},
471 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
472 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
473 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
474 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
475 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
476 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
477 Strings: []string{"8", "", "eight"},
478 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
479 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
480 BytesA: []string{"11", "", "eleven"},
481
482 MyStrings1: []MyString{"12", "", "twelve"},
483 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
484 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
485 MyBytes2: []MyString{"15", "", "fifteen"},
486
Joe Tsai4b7aff62018-11-14 14:05:19 -0800487 MyStrings3: ListStrings{"16", "", "sixteen"},
488 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
489 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
490 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsai378c1322019-04-25 23:48:08 -0700491 }).ProtoReflect()
Joe Tsai91e14662018-09-13 13:24:35 -0700492
Joe Tsai378c1322019-04-25 23:48:08 -0700493 testMessage(t, nil, new(ListScalars).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700494 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 -0700495 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)},
496 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)},
497 listFieldsMutable{
Joe Tsai91e14662018-09-13 13:24:35 -0700498 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800499 lenList(0),
500 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
501 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700502 equalList{getField(want, 2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700503 },
504 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800505 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
506 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
507 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700508 },
509 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800510 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700511 equalList{getField(want, 6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700512 },
513 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800514 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
515 lenList(6),
516 setList{0: V("8"), 2: V("eight")},
517 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700518 equalList{getField(want, 8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700519 },
520 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800521 appendList{V([]byte(nil)), V([]byte(nil))},
522 setList{0: V([]byte("10"))},
523 appendList{V([]byte("wrong"))},
524 setList{2: V([]byte("ten"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700525 equalList{getField(want, 10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700526 },
527 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800528 appendList{V("12"), V("wrong"), V("twelve")},
529 setList{1: V("")},
Joe Tsai378c1322019-04-25 23:48:08 -0700530 equalList{getField(want, 12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700531 },
532 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800533 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700534 equalList{getField(want, 14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700535 },
536 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800537 appendList{V("16"), V(""), V("sixteen"), V("extra")},
538 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700539 equalList{getField(want, 16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700540 },
541 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800542 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700543 equalList{getField(want, 18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700544 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700545 },
Joe Tsai91e14662018-09-13 13:24:35 -0700546 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 -0800547 equalMessage{want},
548 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
549 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000550 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800551
552 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700553 testMessage(t, nil, (*ListScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800554 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},
555 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)}},
556 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000557}
558
Joe Tsaice6edd32018-10-19 16:27:46 -0700559type MapScalars struct {
560 KeyBools map[bool]string `protobuf:"1"`
561 KeyInt32s map[int32]string `protobuf:"2"`
562 KeyInt64s map[int64]string `protobuf:"3"`
563 KeyUint32s map[uint32]string `protobuf:"4"`
564 KeyUint64s map[uint64]string `protobuf:"5"`
565 KeyStrings map[string]string `protobuf:"6"`
566
567 ValBools map[string]bool `protobuf:"7"`
568 ValInt32s map[string]int32 `protobuf:"8"`
569 ValInt64s map[string]int64 `protobuf:"9"`
570 ValUint32s map[string]uint32 `protobuf:"10"`
571 ValUint64s map[string]uint64 `protobuf:"11"`
572 ValFloat32s map[string]float32 `protobuf:"12"`
573 ValFloat64s map[string]float64 `protobuf:"13"`
574 ValStrings map[string]string `protobuf:"14"`
575 ValStringsA map[string][]byte `protobuf:"15"`
576 ValBytes map[string][]byte `protobuf:"16"`
577 ValBytesA map[string]string `protobuf:"17"`
578
579 MyStrings1 map[MyString]MyString `protobuf:"18"`
580 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
581 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
582 MyBytes2 map[MyString]MyString `protobuf:"21"`
583
584 MyStrings3 MapStrings `protobuf:"22"`
585 MyStrings4 MapBytes `protobuf:"23"`
586 MyBytes3 MapBytes `protobuf:"24"`
587 MyBytes4 MapStrings `protobuf:"25"`
588}
589
Joe Tsaib2f66be2019-05-22 00:42:45 -0400590var mapScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(MapScalars)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700591 MessageDescriptor: mustMakeMessageDesc("map-scalars.proto", pref.Proto2, "", `
592 name: "MapScalars"
593 field: [
594 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F1Entry"},
595 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F2Entry"},
596 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F3Entry"},
597 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F4Entry"},
598 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F5Entry"},
599 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F6Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000600
Joe Tsaid8881392019-06-06 13:01:53 -0700601 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F7Entry"},
602 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F8Entry"},
603 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F9Entry"},
604 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F10Entry"},
605 {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F11Entry"},
606 {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F12Entry"},
607 {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F13Entry"},
608 {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F14Entry"},
609 {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F15Entry"},
610 {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F16Entry"},
611 {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F17Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000612
Joe Tsaid8881392019-06-06 13:01:53 -0700613 {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F18Entry"},
614 {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F19Entry"},
615 {name:"f20" number:20 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F20Entry"},
616 {name:"f21" number:21 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F21Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000617
Joe Tsaid8881392019-06-06 13:01:53 -0700618 {name:"f22" number:22 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F22Entry"},
619 {name:"f23" number:23 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F23Entry"},
620 {name:"f24" number:24 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F24Entry"},
621 {name:"f25" number:25 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F25Entry"}
622 ]
623 nested_type: [
624 {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}},
625 {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}},
626 {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}},
627 {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}},
628 {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}},
629 {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}},
630
631 {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}},
632 {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}},
633 {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}},
634 {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}},
635 {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}},
636 {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}},
637 {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}},
638 {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}},
639 {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}},
640 {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}},
641 {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}},
642
643 {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}},
644 {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}},
645 {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}},
646 {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}},
647
648 {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}},
649 {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}},
650 {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}},
651 {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}}
652 ]
653 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400654 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700655 return pref.ProtoMessage(new(MapScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800656 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400657}}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000658
Joe Tsai378c1322019-04-25 23:48:08 -0700659func (m *MapScalars) ProtoReflect() pref.Message { return mapScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800660
661func TestMapScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700662 empty := new(MapScalars).ProtoReflect()
663 want := (&MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000664 KeyBools: map[bool]string{true: "true", false: "false"},
665 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
666 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
667 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
668 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
669 KeyStrings: map[string]string{"": "", "foo": "bar"},
670
671 ValBools: map[string]bool{"true": true, "false": false},
672 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
673 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
674 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
675 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
676 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
677 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
678 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
679 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
680 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
681 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
682
683 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
684 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
685 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
686 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
687
688 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
689 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
690 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
691 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsai378c1322019-04-25 23:48:08 -0700692 }).ProtoReflect()
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000693
Joe Tsai378c1322019-04-25 23:48:08 -0700694 testMessage(t, nil, new(MapScalars).ProtoReflect(), messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000695 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 -0700696 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)},
697 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)},
698 mapFieldsMutable{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000699 2: {
700 lenMap(0),
701 hasMap{int32(0): false, int32(-1): false, int32(2): false},
702 setMap{int32(0): V("zero")},
703 lenMap(1),
704 hasMap{int32(0): true, int32(-1): false, int32(2): false},
705 setMap{int32(-1): V("one")},
706 lenMap(2),
707 hasMap{int32(0): true, int32(-1): true, int32(2): false},
708 setMap{int32(2): V("two")},
709 lenMap(3),
710 hasMap{int32(0): true, int32(-1): true, int32(2): true},
711 },
712 4: {
713 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai378c1322019-04-25 23:48:08 -0700714 equalMap{getField(want, 4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000715 },
716 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800717 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000718 setMap{"foo": V("bar")},
719 setMap{"": V("empty")},
720 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
721 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800722 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000723 },
724 8: {
Joe Tsai378c1322019-04-25 23:48:08 -0700725 equalMap{getField(empty, 8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000726 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
727 },
728 10: {
729 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
730 lenMap(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700731 equalMap{getField(want, 10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000732 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
733 },
734 12: {
735 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800736 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000737 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
738 },
739 14: {
Joe Tsai378c1322019-04-25 23:48:08 -0700740 equalMap{getField(empty, 14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000741 setMap{"s1": V("s1"), "s2": V("s2")},
742 },
743 16: {
744 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700745 equalMap{getField(want, 16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000746 },
747 18: {
748 hasMap{"s1": false, "s2": false, "s3": false},
749 setMap{"s1": V("s1"), "s2": V("s2")},
750 hasMap{"s1": true, "s2": true, "s3": false},
751 },
752 20: {
Joe Tsai378c1322019-04-25 23:48:08 -0700753 equalMap{getField(empty, 20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000754 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
755 },
756 22: {
757 rangeMap{},
758 setMap{"s1": V("s1"), "s2": V("s2")},
759 rangeMap{"s1": V("s1"), "s2": V("s2")},
760 lenMap(2),
761 },
762 24: {
763 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700764 equalMap{getField(want, 24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000765 },
766 },
767 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 -0800768 equalMessage{want},
769 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},
770 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700771 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800772
773 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700774 testMessage(t, nil, (*MapScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800775 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},
776 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)}},
777 })
Joe Tsai91e14662018-09-13 13:24:35 -0700778}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700779
Joe Tsai87b955b2018-11-14 21:59:49 -0800780type OneofScalars struct {
781 Union isOneofScalars_Union `protobuf_oneof:"union"`
782}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700783
Joe Tsaib2f66be2019-05-22 00:42:45 -0400784var oneofScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(OneofScalars)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700785 MessageDescriptor: mustMakeMessageDesc("oneof-scalars.proto", pref.Proto2, "", `
786 name: "OneofScalars"
787 field: [
788 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true" oneof_index:0},
789 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2" oneof_index:0},
790 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3" oneof_index:0},
791 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4" oneof_index:0},
792 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5" oneof_index:0},
793 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6" oneof_index:0},
794 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7" oneof_index:0},
795 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8" oneof_index:0},
796 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9" oneof_index:0},
797 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"10" oneof_index:0},
798 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11" oneof_index:0},
799 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"12" oneof_index:0},
800 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"13" oneof_index:0}
801 ]
802 oneof_decl: [{name:"union"}]
803 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400804 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700805 return pref.ProtoMessage(new(OneofScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800806 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400807}}
Joe Tsaif0c01e42018-11-06 13:05:20 -0800808
Joe Tsai378c1322019-04-25 23:48:08 -0700809func (m *OneofScalars) ProtoReflect() pref.Message { return oneofScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800810
Joe Tsaif18ab532018-11-27 17:25:04 -0800811func (*OneofScalars) XXX_OneofWrappers() []interface{} {
812 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700813 (*OneofScalars_Bool)(nil),
814 (*OneofScalars_Int32)(nil),
815 (*OneofScalars_Int64)(nil),
816 (*OneofScalars_Uint32)(nil),
817 (*OneofScalars_Uint64)(nil),
818 (*OneofScalars_Float32)(nil),
819 (*OneofScalars_Float64)(nil),
820 (*OneofScalars_String)(nil),
821 (*OneofScalars_StringA)(nil),
822 (*OneofScalars_StringB)(nil),
823 (*OneofScalars_Bytes)(nil),
824 (*OneofScalars_BytesA)(nil),
825 (*OneofScalars_BytesB)(nil),
826 }
827}
828
Joe Tsai87b955b2018-11-14 21:59:49 -0800829type (
830 isOneofScalars_Union interface {
831 isOneofScalars_Union()
832 }
833 OneofScalars_Bool struct {
834 Bool bool `protobuf:"1"`
835 }
836 OneofScalars_Int32 struct {
837 Int32 MyInt32 `protobuf:"2"`
838 }
839 OneofScalars_Int64 struct {
840 Int64 int64 `protobuf:"3"`
841 }
842 OneofScalars_Uint32 struct {
843 Uint32 MyUint32 `protobuf:"4"`
844 }
845 OneofScalars_Uint64 struct {
846 Uint64 uint64 `protobuf:"5"`
847 }
848 OneofScalars_Float32 struct {
849 Float32 MyFloat32 `protobuf:"6"`
850 }
851 OneofScalars_Float64 struct {
852 Float64 float64 `protobuf:"7"`
853 }
854 OneofScalars_String struct {
855 String string `protobuf:"8"`
856 }
857 OneofScalars_StringA struct {
858 StringA []byte `protobuf:"9"`
859 }
860 OneofScalars_StringB struct {
861 StringB MyString `protobuf:"10"`
862 }
863 OneofScalars_Bytes struct {
864 Bytes []byte `protobuf:"11"`
865 }
866 OneofScalars_BytesA struct {
867 BytesA string `protobuf:"12"`
868 }
869 OneofScalars_BytesB struct {
870 BytesB MyBytes `protobuf:"13"`
871 }
872)
873
Joe Tsai2c870bb2018-10-17 11:46:52 -0700874func (*OneofScalars_Bool) isOneofScalars_Union() {}
875func (*OneofScalars_Int32) isOneofScalars_Union() {}
876func (*OneofScalars_Int64) isOneofScalars_Union() {}
877func (*OneofScalars_Uint32) isOneofScalars_Union() {}
878func (*OneofScalars_Uint64) isOneofScalars_Union() {}
879func (*OneofScalars_Float32) isOneofScalars_Union() {}
880func (*OneofScalars_Float64) isOneofScalars_Union() {}
881func (*OneofScalars_String) isOneofScalars_Union() {}
882func (*OneofScalars_StringA) isOneofScalars_Union() {}
883func (*OneofScalars_StringB) isOneofScalars_Union() {}
884func (*OneofScalars_Bytes) isOneofScalars_Union() {}
885func (*OneofScalars_BytesA) isOneofScalars_Union() {}
886func (*OneofScalars_BytesB) isOneofScalars_Union() {}
887
888func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800889 empty := &OneofScalars{}
890 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
891 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
892 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
893 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
894 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
895 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
896 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
897 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
898 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
899 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
900 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
901 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
902 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700903
Joe Tsai378c1322019-04-25 23:48:08 -0700904 testMessage(t, nil, new(OneofScalars).ProtoReflect(), messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700905 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},
906 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 -0700907 whichOneofs{"union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700908
Joe Tsai378c1322019-04-25 23:48:08 -0700909 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1.ProtoReflect()},
910 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2.ProtoReflect()},
911 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3.ProtoReflect()},
912 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4.ProtoReflect()},
913 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5.ProtoReflect()},
914 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6.ProtoReflect()},
915 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7.ProtoReflect()},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700916
917 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 -0700918 whichOneofs{"union": 7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700919
Joe Tsai378c1322019-04-25 23:48:08 -0700920 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8.ProtoReflect()},
921 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9.ProtoReflect()},
922 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10.ProtoReflect()},
923 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11.ProtoReflect()},
924 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12.ProtoReflect()},
925 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700926
927 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},
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("130"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800929 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai378c1322019-04-25 23:48:08 -0700930 whichOneofs{"union": 13},
931 equalMessage{want13.ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800932 clearFields{13},
Joe Tsai378c1322019-04-25 23:48:08 -0700933 whichOneofs{"union": 0},
934 equalMessage{empty.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700935 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800936
937 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700938 testMessage(t, nil, (*OneofScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800939 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},
940 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"))},
941 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700942}
943
Joe Tsai87b955b2018-11-14 21:59:49 -0800944type EnumProto2 int32
945
Joe Tsaib2f66be2019-05-22 00:42:45 -0400946var enumProto2Type = &prototype.Enum{
Joe Tsaid8881392019-06-06 13:01:53 -0700947 EnumDescriptor: mustMakeEnumDesc("enum2.proto", pref.Proto2, `
948 name: "EnumProto2"
949 value: [{name:"DEAD" number:0xdead}, {name:"BEEF" number:0xbeef}]
950 `),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400951 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800952 return EnumProto2(n)
953 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400954}
Joe Tsai87b955b2018-11-14 21:59:49 -0800955
Joe Tsai0fc49f82019-05-01 12:29:25 -0700956func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Type.Descriptor() }
957func (e EnumProto2) Enum() *EnumProto2 { return &e }
958func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800959
960type EnumProto3 int32
961
Joe Tsaib2f66be2019-05-22 00:42:45 -0400962var enumProto3Type = &prototype.Enum{
Joe Tsaid8881392019-06-06 13:01:53 -0700963 EnumDescriptor: mustMakeEnumDesc("enum3.proto", pref.Proto3, `
964 name: "EnumProto3",
965 value: [{name:"ALPHA" number:0}, {name:"BRAVO" number:1}]
966 `),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400967 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800968 return EnumProto3(n)
969 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400970}
Joe Tsai87b955b2018-11-14 21:59:49 -0800971
Joe Tsai0fc49f82019-05-01 12:29:25 -0700972func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Type.Descriptor() }
973func (e EnumProto3) Enum() *EnumProto3 { return &e }
974func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800975
976type EnumMessages struct {
977 EnumP2 *EnumProto2 `protobuf:"1"`
978 EnumP3 *EnumProto3 `protobuf:"2"`
979 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
980 MessageCycle *EnumMessages `protobuf:"4"`
981 EnumList []EnumProto2 `protobuf:"5"`
982 MessageList []*ScalarProto2 `protobuf:"6"`
983 EnumMap map[string]EnumProto3 `protobuf:"7"`
984 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
985 Union isEnumMessages_Union `protobuf_oneof:"union"`
986}
987
Joe Tsaib2f66be2019-05-22 00:42:45 -0400988var enumMessagesType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(EnumMessages)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700989 MessageDescriptor: mustMakeMessageDesc("enum-messages.proto", pref.Proto2, `
990 dependency: ["enum2.proto", "enum3.proto", "scalar2.proto", "scalar3.proto", "proto2.v1.0.0-20180125-92554152/test.proto"]
991 `, `
992 name: "EnumMessages"
993 field: [
994 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" default_value:"BEEF"},
995 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" default_value:"BRAVO"},
Herbie Ong20aefe92019-06-24 19:21:46 -0700996 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".google.golang.org.proto2_20180125.Message"},
997 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".EnumMessages"},
Joe Tsaid8881392019-06-06 13:01:53 -0700998 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_ENUM type_name:".EnumProto2"},
999 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".ScalarProto2"},
1000 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F7Entry"},
1001 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F8Entry"},
1002 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" oneof_index:0 default_value:"BEEF"},
1003 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" oneof_index:0 default_value:"BRAVO"},
1004 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto2" oneof_index:0},
1005 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto3" oneof_index:0}
1006 ]
1007 oneof_decl: [{name:"union"}]
1008 nested_type: [
1009 {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}},
1010 {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}}
1011 ]
1012 `, protoregistry.NewFiles(
1013 EnumProto2(0).Descriptor().ParentFile(),
1014 EnumProto3(0).Descriptor().ParentFile(),
1015 ((*ScalarProto2)(nil)).ProtoReflect().Descriptor().ParentFile(),
1016 ((*ScalarProto3)(nil)).ProtoReflect().Descriptor().ParentFile(),
1017 pimpl.Export{}.MessageDescriptorOf((*proto2_20180125.Message)(nil)).ParentFile(),
1018 )),
Joe Tsaib2f66be2019-05-22 00:42:45 -04001019 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -07001020 return pref.ProtoMessage(new(EnumMessages)).ProtoReflect()
Joe Tsai87b955b2018-11-14 21:59:49 -08001021 },
Joe Tsaib2f66be2019-05-22 00:42:45 -04001022}}
Joe Tsai87b955b2018-11-14 21:59:49 -08001023
Joe Tsai378c1322019-04-25 23:48:08 -07001024func (m *EnumMessages) ProtoReflect() pref.Message { return enumMessagesType.MessageOf(m) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001025
Joe Tsaif18ab532018-11-27 17:25:04 -08001026func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1027 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001028 (*EnumMessages_OneofE2)(nil),
1029 (*EnumMessages_OneofE3)(nil),
1030 (*EnumMessages_OneofM2)(nil),
1031 (*EnumMessages_OneofM3)(nil),
1032 }
1033}
1034
1035type (
1036 isEnumMessages_Union interface {
1037 isEnumMessages_Union()
1038 }
1039 EnumMessages_OneofE2 struct {
1040 OneofE2 EnumProto2 `protobuf:"9"`
1041 }
1042 EnumMessages_OneofE3 struct {
1043 OneofE3 EnumProto3 `protobuf:"10"`
1044 }
1045 EnumMessages_OneofM2 struct {
1046 OneofM2 *ScalarProto2 `protobuf:"11"`
1047 }
1048 EnumMessages_OneofM3 struct {
1049 OneofM3 *ScalarProto3 `protobuf:"12"`
1050 }
1051)
1052
1053func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1054func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1055func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1056func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1057
1058func TestEnumMessages(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -07001059 emptyL := pimpl.Export{}.MessageOf(new(proto2_20180125.Message))
1060 emptyM := new(EnumMessages).ProtoReflect()
1061 emptyM2 := new(ScalarProto2).ProtoReflect()
1062 emptyM3 := new(ScalarProto3).ProtoReflect()
1063
Joe Tsai08e00302018-11-26 22:32:06 -08001064 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai378c1322019-04-25 23:48:08 -07001065 wantM := (&EnumMessages{EnumP2: EnumProto2(1234).Enum()}).ProtoReflect()
Joe Tsai009e0672018-11-27 18:45:07 -08001066 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1067 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001068 wantM3a := &ScalarProto3{Float32: math.Pi}
1069 wantM3b := &ScalarProto3{Float32: math.Ln2}
1070
Joe Tsai378c1322019-04-25 23:48:08 -07001071 wantList5 := getField((&EnumMessages{EnumList: []EnumProto2{333, 222}}).ProtoReflect(), 5)
1072 wantList6 := getField((&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).ProtoReflect(), 6)
Joe Tsai87b955b2018-11-14 21:59:49 -08001073
Joe Tsai378c1322019-04-25 23:48:08 -07001074 wantMap7 := getField((&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).ProtoReflect(), 7)
1075 wantMap8 := getField((&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).ProtoReflect(), 8)
Joe Tsai87b955b2018-11-14 21:59:49 -08001076
Joe Tsai378c1322019-04-25 23:48:08 -07001077 testMessage(t, nil, new(EnumMessages).ProtoReflect(), messageOps{
Joe Tsai87b955b2018-11-14 21:59:49 -08001078 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 -07001079 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 -08001080
1081 // Test singular enums.
1082 setFields{1: VE(0xdead), 2: VE(0)},
1083 getFields{1: VE(0xdead), 2: VE(0)},
1084 hasFields{1: true, 2: true},
1085
1086 // Test singular messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001087 messageFieldsMutable{3: messageOps{setFields{109: V(float32(math.E))}}},
1088 messageFieldsMutable{4: messageOps{setFields{1: VE(1234)}}},
Joe Tsai87b955b2018-11-14 21:59:49 -08001089 getFields{3: V(wantL), 4: V(wantM)},
1090 clearFields{3, 4},
1091 hasFields{3: false, 4: false},
1092 setFields{3: V(wantL), 4: V(wantM)},
1093 hasFields{3: true, 4: true},
1094
1095 // Test list of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001096 listFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001097 5: listOps{
1098 appendList{VE(111), VE(222)},
1099 setList{0: VE(333)},
1100 getList{0: VE(333), 1: VE(222)},
1101 lenList(2),
1102 },
1103 6: listOps{
1104 appendMessageList{setFields{4: V(uint32(1e6))}},
1105 appendMessageList{setFields{6: V(float32(math.Phi))}},
Joe Tsai378c1322019-04-25 23:48:08 -07001106 setList{0: V(wantM2a.ProtoReflect())},
1107 getList{0: V(wantM2a.ProtoReflect()), 1: V(wantM2b.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001108 },
1109 },
1110 getFields{5: wantList5, 6: wantList6},
1111 hasFields{5: true, 6: true},
1112 listFields{5: listOps{truncList(0)}},
1113 hasFields{5: false, 6: true},
1114
1115 // Test maps of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001116 mapFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001117 7: mapOps{
1118 setMap{"one": VE(1), "two": VE(2)},
1119 hasMap{"one": true, "two": true, "three": false},
1120 lenMap(2),
1121 },
1122 8: mapOps{
1123 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
Joe Tsai378c1322019-04-25 23:48:08 -07001124 setMap{"ln2": V(wantM3b.ProtoReflect())},
1125 getMap{"pi": V(wantM3a.ProtoReflect()), "ln2": V(wantM3b.ProtoReflect()), "none": V(nil)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001126 lenMap(2),
1127 },
1128 },
1129 getFields{7: wantMap7, 8: wantMap8},
1130 hasFields{7: true, 8: true},
1131 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1132 hasFields{7: true, 8: false},
1133
1134 // Test oneofs of enums and messages.
1135 setFields{9: VE(0xdead)},
1136 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1137 setFields{10: VE(0)},
1138 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001139 messageFieldsMutable{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1140 getFields{11: V(wantM2a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001141 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001142 messageFieldsMutable{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1143 getFields{12: V(wantM3a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001144 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1145
1146 // Check entire message.
Joe Tsai378c1322019-04-25 23:48:08 -07001147 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a.ProtoReflect())},
1148 equalMessage{(&EnumMessages{
Joe Tsai87b955b2018-11-14 21:59:49 -08001149 EnumP2: EnumProto2(0xdead).Enum(),
1150 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001151 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai378c1322019-04-25 23:48:08 -07001152 MessageCycle: wantM.Interface().(*EnumMessages),
Joe Tsai87b955b2018-11-14 21:59:49 -08001153 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1154 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1155 Union: &EnumMessages_OneofM3{wantM3a},
Joe Tsai378c1322019-04-25 23:48:08 -07001156 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001157 clearFields{1, 2, 3, 4, 6, 7, 12},
Joe Tsai378c1322019-04-25 23:48:08 -07001158 equalMessage{new(EnumMessages).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001159 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001160
1161 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -07001162 testMessage(t, nil, (*EnumMessages)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -08001163 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 -07001164 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 -08001165 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1166 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1167 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001168}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001169
Joe Tsai91e14662018-09-13 13:24:35 -07001170var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001171 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
Joe Tsaid8881392019-06-06 13:01:53 -07001172 mx := pimpl.Export{}.MessageOf(x).Interface()
1173 my := pimpl.Export{}.MessageOf(y).Interface()
1174 return proto.Equal(mx, my)
Joe Tsai91e14662018-09-13 13:24:35 -07001175 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001176 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
Joe Tsai378c1322019-04-25 23:48:08 -07001177 switch v := pv.Interface().(type) {
1178 case pref.Message:
1179 out := make(map[pref.FieldNumber]pref.Value)
1180 v.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1181 out[fd.Number()] = v
1182 return true
1183 })
1184 return out
1185 case pref.List:
1186 var out []pref.Value
1187 for i := 0; i < v.Len(); i++ {
1188 out = append(out, v.Get(i))
1189 }
1190 return out
1191 case pref.Map:
1192 out := make(map[interface{}]pref.Value)
1193 v.Range(func(k pref.MapKey, v pref.Value) bool {
1194 out[k.Interface()] = v
1195 return true
1196 })
1197 return out
1198 default:
1199 return v
1200 }
Joe Tsai91e14662018-09-13 13:24:35 -07001201 }),
1202 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001203 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001204}
1205
1206func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
Joe Tsai378c1322019-04-25 23:48:08 -07001207 fieldDescs := m.Descriptor().Fields()
1208 oneofDescs := m.Descriptor().Oneofs()
Joe Tsai91e14662018-09-13 13:24:35 -07001209 for i, op := range tt {
1210 p.Push(i)
1211 switch op := op.(type) {
1212 case equalMessage:
Joe Tsai378c1322019-04-25 23:48:08 -07001213 if diff := cmp.Diff(V(op.Message), V(m), cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001214 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1215 }
1216 case hasFields:
1217 got := map[pref.FieldNumber]bool{}
1218 want := map[pref.FieldNumber]bool(op)
1219 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001220 fd := fieldDescs.ByNumber(n)
1221 got[n] = m.Has(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001222 }
1223 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001224 t.Errorf("operation %v, Message.Has mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001225 }
1226 case getFields:
1227 got := map[pref.FieldNumber]pref.Value{}
1228 want := map[pref.FieldNumber]pref.Value(op)
1229 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001230 fd := fieldDescs.ByNumber(n)
1231 got[n] = m.Get(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001232 }
1233 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001234 t.Errorf("operation %v, Message.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001235 }
1236 case setFields:
1237 for n, v := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001238 fd := fieldDescs.ByNumber(n)
1239 m.Set(fd, v)
Joe Tsai91e14662018-09-13 13:24:35 -07001240 }
1241 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001242 for _, n := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001243 fd := fieldDescs.ByNumber(n)
1244 m.Clear(fd)
Joe Tsai87b955b2018-11-14 21:59:49 -08001245 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001246 case whichOneofs:
1247 got := map[pref.Name]pref.FieldNumber{}
1248 want := map[pref.Name]pref.FieldNumber(op)
1249 for s := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001250 od := oneofDescs.ByName(s)
1251 fd := m.WhichOneof(od)
1252 if fd == nil {
1253 got[s] = 0
1254 } else {
1255 got[s] = fd.Number()
1256 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001257 }
1258 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001259 t.Errorf("operation %v, Message.WhichOneof mismatch (-want, +got):\n%s", p, diff)
Joe Tsai4ec39c72019-04-03 13:40:53 -07001260 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001261 case messageFields:
1262 for n, tt := range op {
1263 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001264 fd := fieldDescs.ByNumber(n)
1265 testMessage(t, p, m.Get(fd).Message(), tt)
1266 p.Pop()
1267 }
1268 case messageFieldsMutable:
1269 for n, tt := range op {
1270 p.Push(int(n))
1271 fd := fieldDescs.ByNumber(n)
1272 testMessage(t, p, m.Mutable(fd).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001273 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001274 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001275 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001276 for n, tt := range op {
1277 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001278 fd := fieldDescs.ByNumber(n)
1279 testLists(t, p, m.Get(fd).List(), tt)
1280 p.Pop()
1281 }
1282 case listFieldsMutable:
1283 for n, tt := range op {
1284 p.Push(int(n))
1285 fd := fieldDescs.ByNumber(n)
1286 testLists(t, p, m.Mutable(fd).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001287 p.Pop()
1288 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001289 case mapFields:
1290 for n, tt := range op {
1291 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001292 fd := fieldDescs.ByNumber(n)
1293 testMaps(t, p, m.Get(fd).Map(), tt)
1294 p.Pop()
1295 }
1296 case mapFieldsMutable:
1297 for n, tt := range op {
1298 p.Push(int(n))
1299 fd := fieldDescs.ByNumber(n)
1300 testMaps(t, p, m.Mutable(fd).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001301 p.Pop()
1302 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001303 case rangeFields:
1304 got := map[pref.FieldNumber]pref.Value{}
1305 want := map[pref.FieldNumber]pref.Value(op)
Joe Tsai378c1322019-04-25 23:48:08 -07001306 m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1307 got[fd.Number()] = v
Joe Tsai87b955b2018-11-14 21:59:49 -08001308 return true
1309 })
1310 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001311 t.Errorf("operation %v, Message.Range mismatch (-want, +got):\n%s", p, diff)
Joe Tsai87b955b2018-11-14 21:59:49 -08001312 }
Joe Tsai91e14662018-09-13 13:24:35 -07001313 default:
1314 t.Fatalf("operation %v, invalid operation: %T", p, op)
1315 }
1316 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001317 }
1318}
Joe Tsai91e14662018-09-13 13:24:35 -07001319
Joe Tsai4b7aff62018-11-14 14:05:19 -08001320func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001321 for i, op := range tt {
1322 p.Push(i)
1323 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001324 case equalList:
Joe Tsai378c1322019-04-25 23:48:08 -07001325 if diff := cmp.Diff(V(op.List), V(v), cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001326 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001327 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001328 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001329 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001330 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001331 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001332 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001333 got := map[int]pref.Value{}
1334 want := map[int]pref.Value(op)
1335 for n := range want {
1336 got[n] = v.Get(n)
1337 }
1338 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001339 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001340 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001341 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001342 for n, e := range op {
1343 v.Set(n, e)
1344 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001345 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001346 for _, e := range op {
1347 v.Append(e)
1348 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001349 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001350 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001351 v.Append(V(m))
1352 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001353 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001354 v.Truncate(int(op))
1355 default:
1356 t.Fatalf("operation %v, invalid operation: %T", p, op)
1357 }
1358 p.Pop()
1359 }
1360}
1361
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001362func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1363 for i, op := range tt {
1364 p.Push(i)
1365 switch op := op.(type) {
1366 case equalMap:
Joe Tsai378c1322019-04-25 23:48:08 -07001367 if diff := cmp.Diff(V(op.Map), V(m), cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001368 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1369 }
1370 case lenMap:
1371 if got, want := m.Len(), int(op); got != want {
1372 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1373 }
1374 case hasMap:
1375 got := map[interface{}]bool{}
1376 want := map[interface{}]bool(op)
1377 for k := range want {
1378 got[k] = m.Has(V(k).MapKey())
1379 }
1380 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1381 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1382 }
1383 case getMap:
1384 got := map[interface{}]pref.Value{}
1385 want := map[interface{}]pref.Value(op)
1386 for k := range want {
1387 got[k] = m.Get(V(k).MapKey())
1388 }
1389 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1390 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1391 }
1392 case setMap:
1393 for k, v := range op {
1394 m.Set(V(k).MapKey(), v)
1395 }
1396 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001397 for _, k := range op {
1398 m.Clear(V(k).MapKey())
1399 }
1400 case messageMap:
1401 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001402 mk := V(k).MapKey()
1403 if !m.Has(mk) {
1404 m.Set(mk, V(m.NewMessage()))
1405 }
1406 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001407 }
1408 case rangeMap:
1409 got := map[interface{}]pref.Value{}
1410 want := map[interface{}]pref.Value(op)
1411 m.Range(func(k pref.MapKey, v pref.Value) bool {
1412 got[k.Interface()] = v
1413 return true
1414 })
1415 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1416 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1417 }
1418 default:
1419 t.Fatalf("operation %v, invalid operation: %T", p, op)
1420 }
1421 p.Pop()
1422 }
1423}
1424
Joe Tsai378c1322019-04-25 23:48:08 -07001425func getField(m pref.Message, n pref.FieldNumber) pref.Value {
1426 fd := m.Descriptor().Fields().ByNumber(n)
1427 return m.Get(fd)
1428}
1429
Joe Tsai91e14662018-09-13 13:24:35 -07001430type path []int
1431
1432func (p *path) Push(i int) { *p = append(*p, i) }
1433func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1434func (p path) String() string {
1435 var ss []string
1436 for _, i := range p {
1437 ss = append(ss, fmt.Sprint(i))
1438 }
1439 return strings.Join(ss, ".")
1440}
Joe Tsai82760ce2019-06-20 03:09:57 -07001441
1442// The MessageState implementation makes the assumption that when a
1443// concrete message is unsafe casted as a *MessageState, the Go GC does
1444// not reclaim the memory for the remainder of the concrete message.
1445func TestUnsafeAssumptions(t *testing.T) {
1446 if !pimpl.UnsafeEnabled {
1447 t.Skip()
1448 }
1449
1450 var wg sync.WaitGroup
1451 for i := 0; i < 10; i++ {
1452 wg.Add(1)
1453 go func() {
1454 var ms [10]pref.Message
1455
1456 // Store the message only in its reflective form.
1457 // Trigger the GC after each iteration.
1458 for j := 0; j < 10; j++ {
1459 ms[j] = (&testpb.TestAllTypes{
1460 OptionalInt32: scalar.Int32(int32(j)),
1461 OptionalFloat: scalar.Float32(float32(j)),
1462 RepeatedInt32: []int32{int32(j)},
1463 RepeatedFloat: []float32{float32(j)},
1464 DefaultInt32: scalar.Int32(int32(j)),
1465 DefaultFloat: scalar.Float32(float32(j)),
1466 }).ProtoReflect()
1467 runtime.GC()
1468 }
1469
1470 // Convert the reflective form back into a concrete form.
1471 // Verify that the values written previously are still the same.
1472 for j := 0; j < 10; j++ {
1473 switch m := ms[j].Interface().(*testpb.TestAllTypes); {
1474 case m.GetOptionalInt32() != int32(j):
1475 case m.GetOptionalFloat() != float32(j):
1476 case m.GetRepeatedInt32()[0] != int32(j):
1477 case m.GetRepeatedFloat()[0] != float32(j):
1478 case m.GetDefaultInt32() != int32(j):
1479 case m.GetDefaultFloat() != float32(j):
1480 default:
1481 continue
1482 }
1483 t.Error("memory corrupted detected")
1484 }
1485 defer wg.Done()
1486 }()
1487 }
1488 wg.Wait()
1489}
1490
1491func BenchmarkName(b *testing.B) {
1492 var sink pref.FullName
1493 b.Run("Value", func(b *testing.B) {
1494 b.ReportAllocs()
1495 m := new(descriptorpb.FileDescriptorProto)
1496 for i := 0; i < b.N; i++ {
1497 sink = m.ProtoReflect().Descriptor().FullName()
1498 }
1499 })
1500 b.Run("Nil", func(b *testing.B) {
1501 b.ReportAllocs()
1502 m := (*descriptorpb.FileDescriptorProto)(nil)
1503 for i := 0; i < b.N; i++ {
1504 sink = m.ProtoReflect().Descriptor().FullName()
1505 }
1506 })
1507 runtime.KeepAlive(sink)
1508}