blob: fb89a2d64db390144378daf78e44b1c260da7c0b [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 Tsai91e14662018-09-13 13:24:35 -070011 "strings"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070012 "testing"
13
Joe Tsai87b955b2018-11-14 21:59:49 -080014 cmp "github.com/google/go-cmp/cmp"
15 cmpopts "github.com/google/go-cmp/cmp/cmpopts"
Joe Tsaid8881392019-06-06 13:01:53 -070016 "google.golang.org/protobuf/encoding/prototext"
Damien Neile89e6242019-05-13 23:55:40 -070017 pimpl "google.golang.org/protobuf/internal/impl"
Damien Neile89e6242019-05-13 23:55:40 -070018 scalar "google.golang.org/protobuf/internal/scalar"
Joe Tsaid8881392019-06-06 13:01:53 -070019 "google.golang.org/protobuf/proto"
20 pdesc "google.golang.org/protobuf/reflect/protodesc"
Damien Neile89e6242019-05-13 23:55:40 -070021 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaid8881392019-06-06 13:01:53 -070022 "google.golang.org/protobuf/reflect/protoregistry"
Joe Tsaib2f66be2019-05-22 00:42:45 -040023 "google.golang.org/protobuf/reflect/prototype"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070024
Damien Neile89e6242019-05-13 23:55:40 -070025 proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
Joe Tsaia95b29f2019-05-16 12:47:20 -070026 "google.golang.org/protobuf/types/descriptorpb"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070027)
28
Joe Tsai4b7aff62018-11-14 14:05:19 -080029// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070030type (
Joe Tsai87b955b2018-11-14 21:59:49 -080031 messageOp interface{ isMessageOp() }
Joe Tsai91e14662018-09-13 13:24:35 -070032 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070033
Joe Tsai87b955b2018-11-14 21:59:49 -080034 listOp interface{ isListOp() }
Joe Tsai4b7aff62018-11-14 14:05:19 -080035 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070036
Joe Tsai87b955b2018-11-14 21:59:49 -080037 mapOp interface{ isMapOp() }
Joe Tsaibbfaeb72018-10-17 00:27:21 +000038 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070039)
40
41// Test operations performed on a message.
42type (
Joe Tsai87b955b2018-11-14 21:59:49 -080043 // check that the message contents match
44 equalMessage struct{ pref.Message }
45 // check presence for specific fields in the message
46 hasFields map[pref.FieldNumber]bool
47 // check that specific message fields match
48 getFields map[pref.FieldNumber]pref.Value
49 // set specific message fields
50 setFields map[pref.FieldNumber]pref.Value
51 // clear specific fields in the message
52 clearFields []pref.FieldNumber
Joe Tsai4ec39c72019-04-03 13:40:53 -070053 // check for the presence of specific oneof member fields.
54 whichOneofs map[pref.Name]pref.FieldNumber
Joe Tsai87b955b2018-11-14 21:59:49 -080055 // apply messageOps on each specified message field
Joe Tsai378c1322019-04-25 23:48:08 -070056 messageFields map[pref.FieldNumber]messageOps
57 messageFieldsMutable map[pref.FieldNumber]messageOps
Joe Tsai87b955b2018-11-14 21:59:49 -080058 // apply listOps on each specified list field
Joe Tsai378c1322019-04-25 23:48:08 -070059 listFields map[pref.FieldNumber]listOps
60 listFieldsMutable map[pref.FieldNumber]listOps
Joe Tsai87b955b2018-11-14 21:59:49 -080061 // apply mapOps on each specified map fields
Joe Tsai378c1322019-04-25 23:48:08 -070062 mapFields map[pref.FieldNumber]mapOps
63 mapFieldsMutable map[pref.FieldNumber]mapOps
Joe Tsai87b955b2018-11-14 21:59:49 -080064 // range through all fields and check that they match
65 rangeFields map[pref.FieldNumber]pref.Value
Joe Tsai91e14662018-09-13 13:24:35 -070066)
67
Joe Tsai378c1322019-04-25 23:48:08 -070068func (equalMessage) isMessageOp() {}
69func (hasFields) isMessageOp() {}
70func (getFields) isMessageOp() {}
71func (setFields) isMessageOp() {}
72func (clearFields) isMessageOp() {}
73func (whichOneofs) isMessageOp() {}
74func (messageFields) isMessageOp() {}
75func (messageFieldsMutable) isMessageOp() {}
76func (listFields) isMessageOp() {}
77func (listFieldsMutable) isMessageOp() {}
78func (mapFields) isMessageOp() {}
79func (mapFieldsMutable) isMessageOp() {}
80func (rangeFields) isMessageOp() {}
Joe Tsai87b955b2018-11-14 21:59:49 -080081
Joe Tsai4b7aff62018-11-14 14:05:19 -080082// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070083type (
Joe Tsai87b955b2018-11-14 21:59:49 -080084 // check that the list contents match
85 equalList struct{ pref.List }
86 // check that list length matches
87 lenList int
88 // check that specific list entries match
89 getList map[int]pref.Value
90 // set specific list entries
91 setList map[int]pref.Value
92 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080093 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080094 // apply messageOps on a newly appended message
95 appendMessageList messageOps
96 // truncate the list to the specified length
97 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -070098)
99
Joe Tsai87b955b2018-11-14 21:59:49 -0800100func (equalList) isListOp() {}
101func (lenList) isListOp() {}
102func (getList) isListOp() {}
103func (setList) isListOp() {}
104func (appendList) isListOp() {}
105func (appendMessageList) isListOp() {}
106func (truncList) isListOp() {}
107
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000108// Test operations performed on a map.
109type (
Joe Tsai87b955b2018-11-14 21:59:49 -0800110 // check that the map contents match
111 equalMap struct{ pref.Map }
112 // check that map length matches
113 lenMap int
114 // check presence for specific entries in the map
115 hasMap map[interface{}]bool
116 // check that specific map entries match
117 getMap map[interface{}]pref.Value
118 // set specific map entries
119 setMap map[interface{}]pref.Value
120 // clear specific entries in the map
121 clearMap []interface{}
122 // apply messageOps on each specified message entry
123 messageMap map[interface{}]messageOps
124 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000125 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000126)
127
Joe Tsai87b955b2018-11-14 21:59:49 -0800128func (equalMap) isMapOp() {}
129func (lenMap) isMapOp() {}
130func (hasMap) isMapOp() {}
131func (getMap) isMapOp() {}
132func (setMap) isMapOp() {}
133func (clearMap) isMapOp() {}
134func (messageMap) isMapOp() {}
135func (rangeMap) isMapOp() {}
136
Joe Tsaice6edd32018-10-19 16:27:46 -0700137type ScalarProto2 struct {
138 Bool *bool `protobuf:"1"`
139 Int32 *int32 `protobuf:"2"`
140 Int64 *int64 `protobuf:"3"`
141 Uint32 *uint32 `protobuf:"4"`
142 Uint64 *uint64 `protobuf:"5"`
143 Float32 *float32 `protobuf:"6"`
144 Float64 *float64 `protobuf:"7"`
145 String *string `protobuf:"8"`
146 StringA []byte `protobuf:"9"`
147 Bytes []byte `protobuf:"10"`
148 BytesA *string `protobuf:"11"`
149
150 MyBool *MyBool `protobuf:"12"`
151 MyInt32 *MyInt32 `protobuf:"13"`
152 MyInt64 *MyInt64 `protobuf:"14"`
153 MyUint32 *MyUint32 `protobuf:"15"`
154 MyUint64 *MyUint64 `protobuf:"16"`
155 MyFloat32 *MyFloat32 `protobuf:"17"`
156 MyFloat64 *MyFloat64 `protobuf:"18"`
157 MyString *MyString `protobuf:"19"`
158 MyStringA MyBytes `protobuf:"20"`
159 MyBytes MyBytes `protobuf:"21"`
160 MyBytesA *MyString `protobuf:"22"`
161}
162
Joe Tsaid8881392019-06-06 13:01:53 -0700163func mustMakeEnumDesc(path string, syntax pref.Syntax, enumDesc string) pref.EnumDescriptor {
164 s := fmt.Sprintf(`name:%q syntax:%q enum_type:[{%s}]`, path, syntax, enumDesc)
165 pb := new(descriptorpb.FileDescriptorProto)
166 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
167 panic(err)
168 }
169 fd, err := pdesc.NewFile(pb, nil)
Joe Tsai87b955b2018-11-14 21:59:49 -0800170 if err != nil {
171 panic(err)
172 }
Joe Tsaid8881392019-06-06 13:01:53 -0700173 return fd.Enums().Get(0)
Joe Tsai87b955b2018-11-14 21:59:49 -0800174}
175
Joe Tsaid8881392019-06-06 13:01:53 -0700176func mustMakeMessageDesc(path string, syntax pref.Syntax, fileDesc, msgDesc string, r pdesc.Resolver) pref.MessageDescriptor {
177 s := fmt.Sprintf(`name:%q syntax:%q %s message_type:[{%s}]`, path, syntax, fileDesc, msgDesc)
178 pb := new(descriptorpb.FileDescriptorProto)
179 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
180 panic(err)
181 }
182 fd, err := pdesc.NewFile(pb, r)
Joe Tsai87b955b2018-11-14 21:59:49 -0800183 if err != nil {
184 panic(err)
185 }
Joe Tsaid8881392019-06-06 13:01:53 -0700186 return fd.Messages().Get(0)
Joe Tsai87b955b2018-11-14 21:59:49 -0800187}
188
189var V = pref.ValueOf
190var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
191
192type (
193 MyBool bool
194 MyInt32 int32
195 MyInt64 int64
196 MyUint32 uint32
197 MyUint64 uint64
198 MyFloat32 float32
199 MyFloat64 float64
200 MyString string
201 MyBytes []byte
202
203 ListStrings []MyString
204 ListBytes []MyBytes
205
206 MapStrings map[MyString]MyString
207 MapBytes map[MyString]MyBytes
208)
209
Joe Tsaib2f66be2019-05-22 00:42:45 -0400210var scalarProto2Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto2)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700211 MessageDescriptor: mustMakeMessageDesc("scalar2.proto", pref.Proto2, "", `
212 name: "ScalarProto2"
213 field: [
214 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
215 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2"},
216 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3"},
217 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4"},
218 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5"},
219 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6"},
220 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7"},
221 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8"},
222 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9"},
223 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"10"},
224 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11"},
Joe Tsai91e14662018-09-13 13:24:35 -0700225
Joe Tsaid8881392019-06-06 13:01:53 -0700226 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
227 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"13"},
228 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"14"},
229 {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"15"},
230 {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"16"},
231 {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"17"},
232 {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"18"},
233 {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"19"},
234 {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"20"},
235 {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"21"},
236 {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"22"}
237 ]
238 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400239 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700240 return pref.ProtoMessage(new(ScalarProto2)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800241 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400242}}
Joe Tsai91e14662018-09-13 13:24:35 -0700243
Joe Tsai378c1322019-04-25 23:48:08 -0700244func (m *ScalarProto2) ProtoReflect() pref.Message { return scalarProto2Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800245
246func TestScalarProto2(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700247 testMessage(t, nil, new(ScalarProto2).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700248 hasFields{
249 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
250 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
251 },
252 getFields{
253 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")),
254 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")),
255 },
256 setFields{
257 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)),
258 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)),
259 },
260 hasFields{
261 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
262 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
263 },
Joe Tsai378c1322019-04-25 23:48:08 -0700264 equalMessage{(&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700265 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
266 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 -0700267 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800268 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 -0700269 equalMessage{new(ScalarProto2).ProtoReflect()},
Joe Tsai91e14662018-09-13 13:24:35 -0700270 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800271
272 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700273 testMessage(t, nil, (*ScalarProto2)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800274 hasFields{
275 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
276 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
277 },
278 getFields{
279 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")),
280 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")),
281 },
282 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700283}
284
Joe Tsaice6edd32018-10-19 16:27:46 -0700285type ScalarProto3 struct {
286 Bool bool `protobuf:"1"`
287 Int32 int32 `protobuf:"2"`
288 Int64 int64 `protobuf:"3"`
289 Uint32 uint32 `protobuf:"4"`
290 Uint64 uint64 `protobuf:"5"`
291 Float32 float32 `protobuf:"6"`
292 Float64 float64 `protobuf:"7"`
293 String string `protobuf:"8"`
294 StringA []byte `protobuf:"9"`
295 Bytes []byte `protobuf:"10"`
296 BytesA string `protobuf:"11"`
297
298 MyBool MyBool `protobuf:"12"`
299 MyInt32 MyInt32 `protobuf:"13"`
300 MyInt64 MyInt64 `protobuf:"14"`
301 MyUint32 MyUint32 `protobuf:"15"`
302 MyUint64 MyUint64 `protobuf:"16"`
303 MyFloat32 MyFloat32 `protobuf:"17"`
304 MyFloat64 MyFloat64 `protobuf:"18"`
305 MyString MyString `protobuf:"19"`
306 MyStringA MyBytes `protobuf:"20"`
307 MyBytes MyBytes `protobuf:"21"`
308 MyBytesA MyString `protobuf:"22"`
309}
310
Joe Tsaib2f66be2019-05-22 00:42:45 -0400311var scalarProto3Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto3)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700312 MessageDescriptor: mustMakeMessageDesc("scalar3.proto", pref.Proto3, "", `
313 name: "ScalarProto3"
314 field: [
315 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL},
316 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32},
317 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64},
318 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32},
319 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64},
320 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT},
321 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
322 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING},
323 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING},
324 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES},
325 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700326
Joe Tsaid8881392019-06-06 13:01:53 -0700327 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL},
328 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32},
329 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64},
330 {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32},
331 {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64},
332 {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT},
333 {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
334 {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING},
335 {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING},
336 {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES},
337 {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES}
338 ]
339 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400340 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700341 return pref.ProtoMessage(new(ScalarProto3)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800342 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400343}}
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 Tsai91e14662018-09-13 13:24:35 -0700391 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800392
393 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700394 testMessage(t, nil, (*ScalarProto3)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800395 hasFields{
396 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
397 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
398 },
399 getFields{
400 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)),
401 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)),
402 },
403 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700404}
405
Joe Tsaif0c01e42018-11-06 13:05:20 -0800406type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700407 Bools []bool `protobuf:"1"`
408 Int32s []int32 `protobuf:"2"`
409 Int64s []int64 `protobuf:"3"`
410 Uint32s []uint32 `protobuf:"4"`
411 Uint64s []uint64 `protobuf:"5"`
412 Float32s []float32 `protobuf:"6"`
413 Float64s []float64 `protobuf:"7"`
414 Strings []string `protobuf:"8"`
415 StringsA [][]byte `protobuf:"9"`
416 Bytes [][]byte `protobuf:"10"`
417 BytesA []string `protobuf:"11"`
418
419 MyStrings1 []MyString `protobuf:"12"`
420 MyStrings2 []MyBytes `protobuf:"13"`
421 MyBytes1 []MyBytes `protobuf:"14"`
422 MyBytes2 []MyString `protobuf:"15"`
423
Joe Tsai4b7aff62018-11-14 14:05:19 -0800424 MyStrings3 ListStrings `protobuf:"16"`
425 MyStrings4 ListBytes `protobuf:"17"`
426 MyBytes3 ListBytes `protobuf:"18"`
427 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700428}
429
Joe Tsaib2f66be2019-05-22 00:42:45 -0400430var listScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ListScalars)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700431 MessageDescriptor: mustMakeMessageDesc("list-scalars.proto", pref.Proto2, "", `
432 name: "ListScalars"
433 field: [
434 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_BOOL},
435 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_INT32},
436 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_INT64},
437 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_UINT32},
438 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_UINT64},
439 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_FLOAT},
440 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_DOUBLE},
441 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_STRING},
442 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_STRING},
443 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_BYTES},
444 {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700445
Joe Tsaid8881392019-06-06 13:01:53 -0700446 {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_STRING},
447 {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_STRING},
448 {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_BYTES},
449 {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsai91e14662018-09-13 13:24:35 -0700450
Joe Tsaid8881392019-06-06 13:01:53 -0700451 {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_STRING},
452 {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_STRING},
453 {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_BYTES},
454 {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_BYTES}
455 ]
456 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400457 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700458 return pref.ProtoMessage(new(ListScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800459 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400460}}
Joe Tsai91e14662018-09-13 13:24:35 -0700461
Joe Tsai378c1322019-04-25 23:48:08 -0700462func (m *ListScalars) ProtoReflect() pref.Message { return listScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800463
464func TestListScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700465 empty := new(ListScalars).ProtoReflect()
466 want := (&ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700467 Bools: []bool{true, false, true},
468 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
469 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
470 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
471 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
472 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
473 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
474 Strings: []string{"8", "", "eight"},
475 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
476 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
477 BytesA: []string{"11", "", "eleven"},
478
479 MyStrings1: []MyString{"12", "", "twelve"},
480 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
481 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
482 MyBytes2: []MyString{"15", "", "fifteen"},
483
Joe Tsai4b7aff62018-11-14 14:05:19 -0800484 MyStrings3: ListStrings{"16", "", "sixteen"},
485 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
486 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
487 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsai378c1322019-04-25 23:48:08 -0700488 }).ProtoReflect()
Joe Tsai91e14662018-09-13 13:24:35 -0700489
Joe Tsai378c1322019-04-25 23:48:08 -0700490 testMessage(t, nil, new(ListScalars).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700491 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 -0700492 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)},
493 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)},
494 listFieldsMutable{
Joe Tsai91e14662018-09-13 13:24:35 -0700495 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800496 lenList(0),
497 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
498 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700499 equalList{getField(want, 2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700500 },
501 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800502 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
503 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
504 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700505 },
506 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800507 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700508 equalList{getField(want, 6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700509 },
510 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800511 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
512 lenList(6),
513 setList{0: V("8"), 2: V("eight")},
514 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700515 equalList{getField(want, 8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700516 },
517 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800518 appendList{V([]byte(nil)), V([]byte(nil))},
519 setList{0: V([]byte("10"))},
520 appendList{V([]byte("wrong"))},
521 setList{2: V([]byte("ten"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700522 equalList{getField(want, 10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700523 },
524 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800525 appendList{V("12"), V("wrong"), V("twelve")},
526 setList{1: V("")},
Joe Tsai378c1322019-04-25 23:48:08 -0700527 equalList{getField(want, 12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700528 },
529 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800530 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700531 equalList{getField(want, 14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700532 },
533 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800534 appendList{V("16"), V(""), V("sixteen"), V("extra")},
535 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700536 equalList{getField(want, 16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700537 },
538 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800539 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700540 equalList{getField(want, 18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700541 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700542 },
Joe Tsai91e14662018-09-13 13:24:35 -0700543 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 -0800544 equalMessage{want},
545 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
546 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000547 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800548
549 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700550 testMessage(t, nil, (*ListScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800551 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},
552 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)}},
553 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000554}
555
Joe Tsaice6edd32018-10-19 16:27:46 -0700556type MapScalars struct {
557 KeyBools map[bool]string `protobuf:"1"`
558 KeyInt32s map[int32]string `protobuf:"2"`
559 KeyInt64s map[int64]string `protobuf:"3"`
560 KeyUint32s map[uint32]string `protobuf:"4"`
561 KeyUint64s map[uint64]string `protobuf:"5"`
562 KeyStrings map[string]string `protobuf:"6"`
563
564 ValBools map[string]bool `protobuf:"7"`
565 ValInt32s map[string]int32 `protobuf:"8"`
566 ValInt64s map[string]int64 `protobuf:"9"`
567 ValUint32s map[string]uint32 `protobuf:"10"`
568 ValUint64s map[string]uint64 `protobuf:"11"`
569 ValFloat32s map[string]float32 `protobuf:"12"`
570 ValFloat64s map[string]float64 `protobuf:"13"`
571 ValStrings map[string]string `protobuf:"14"`
572 ValStringsA map[string][]byte `protobuf:"15"`
573 ValBytes map[string][]byte `protobuf:"16"`
574 ValBytesA map[string]string `protobuf:"17"`
575
576 MyStrings1 map[MyString]MyString `protobuf:"18"`
577 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
578 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
579 MyBytes2 map[MyString]MyString `protobuf:"21"`
580
581 MyStrings3 MapStrings `protobuf:"22"`
582 MyStrings4 MapBytes `protobuf:"23"`
583 MyBytes3 MapBytes `protobuf:"24"`
584 MyBytes4 MapStrings `protobuf:"25"`
585}
586
Joe Tsaib2f66be2019-05-22 00:42:45 -0400587var mapScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(MapScalars)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700588 MessageDescriptor: mustMakeMessageDesc("map-scalars.proto", pref.Proto2, "", `
589 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),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400651 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700652 return pref.ProtoMessage(new(MapScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800653 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400654}}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000655
Joe Tsai378c1322019-04-25 23:48:08 -0700656func (m *MapScalars) ProtoReflect() pref.Message { return mapScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800657
658func TestMapScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700659 empty := new(MapScalars).ProtoReflect()
660 want := (&MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000661 KeyBools: map[bool]string{true: "true", false: "false"},
662 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
663 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
664 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
665 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
666 KeyStrings: map[string]string{"": "", "foo": "bar"},
667
668 ValBools: map[string]bool{"true": true, "false": false},
669 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
670 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
671 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
672 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
673 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
674 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
675 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
676 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
677 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
678 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
679
680 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
681 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
682 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
683 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
684
685 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
686 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
687 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
688 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsai378c1322019-04-25 23:48:08 -0700689 }).ProtoReflect()
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000690
Joe Tsai378c1322019-04-25 23:48:08 -0700691 testMessage(t, nil, new(MapScalars).ProtoReflect(), messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000692 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 -0700693 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)},
694 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)},
695 mapFieldsMutable{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000696 2: {
697 lenMap(0),
698 hasMap{int32(0): false, int32(-1): false, int32(2): false},
699 setMap{int32(0): V("zero")},
700 lenMap(1),
701 hasMap{int32(0): true, int32(-1): false, int32(2): false},
702 setMap{int32(-1): V("one")},
703 lenMap(2),
704 hasMap{int32(0): true, int32(-1): true, int32(2): false},
705 setMap{int32(2): V("two")},
706 lenMap(3),
707 hasMap{int32(0): true, int32(-1): true, int32(2): true},
708 },
709 4: {
710 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai378c1322019-04-25 23:48:08 -0700711 equalMap{getField(want, 4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000712 },
713 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800714 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000715 setMap{"foo": V("bar")},
716 setMap{"": V("empty")},
717 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
718 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800719 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000720 },
721 8: {
Joe Tsai378c1322019-04-25 23:48:08 -0700722 equalMap{getField(empty, 8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000723 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
724 },
725 10: {
726 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
727 lenMap(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700728 equalMap{getField(want, 10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000729 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
730 },
731 12: {
732 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800733 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000734 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
735 },
736 14: {
Joe Tsai378c1322019-04-25 23:48:08 -0700737 equalMap{getField(empty, 14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000738 setMap{"s1": V("s1"), "s2": V("s2")},
739 },
740 16: {
741 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700742 equalMap{getField(want, 16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000743 },
744 18: {
745 hasMap{"s1": false, "s2": false, "s3": false},
746 setMap{"s1": V("s1"), "s2": V("s2")},
747 hasMap{"s1": true, "s2": true, "s3": false},
748 },
749 20: {
Joe Tsai378c1322019-04-25 23:48:08 -0700750 equalMap{getField(empty, 20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000751 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
752 },
753 22: {
754 rangeMap{},
755 setMap{"s1": V("s1"), "s2": V("s2")},
756 rangeMap{"s1": V("s1"), "s2": V("s2")},
757 lenMap(2),
758 },
759 24: {
760 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700761 equalMap{getField(want, 24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000762 },
763 },
764 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 -0800765 equalMessage{want},
766 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},
767 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700768 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800769
770 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700771 testMessage(t, nil, (*MapScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800772 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},
773 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)}},
774 })
Joe Tsai91e14662018-09-13 13:24:35 -0700775}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700776
Joe Tsai87b955b2018-11-14 21:59:49 -0800777type OneofScalars struct {
778 Union isOneofScalars_Union `protobuf_oneof:"union"`
779}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700780
Joe Tsaib2f66be2019-05-22 00:42:45 -0400781var oneofScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(OneofScalars)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700782 MessageDescriptor: mustMakeMessageDesc("oneof-scalars.proto", pref.Proto2, "", `
783 name: "OneofScalars"
784 field: [
785 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true" oneof_index:0},
786 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2" oneof_index:0},
787 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3" oneof_index:0},
788 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4" oneof_index:0},
789 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5" oneof_index:0},
790 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6" oneof_index:0},
791 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7" oneof_index:0},
792 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8" oneof_index:0},
793 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9" oneof_index:0},
794 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"10" oneof_index:0},
795 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11" oneof_index:0},
796 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"12" oneof_index:0},
797 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"13" oneof_index:0}
798 ]
799 oneof_decl: [{name:"union"}]
800 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400801 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700802 return pref.ProtoMessage(new(OneofScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800803 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400804}}
Joe Tsaif0c01e42018-11-06 13:05:20 -0800805
Joe Tsai378c1322019-04-25 23:48:08 -0700806func (m *OneofScalars) ProtoReflect() pref.Message { return oneofScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800807
Joe Tsaif18ab532018-11-27 17:25:04 -0800808func (*OneofScalars) XXX_OneofWrappers() []interface{} {
809 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700810 (*OneofScalars_Bool)(nil),
811 (*OneofScalars_Int32)(nil),
812 (*OneofScalars_Int64)(nil),
813 (*OneofScalars_Uint32)(nil),
814 (*OneofScalars_Uint64)(nil),
815 (*OneofScalars_Float32)(nil),
816 (*OneofScalars_Float64)(nil),
817 (*OneofScalars_String)(nil),
818 (*OneofScalars_StringA)(nil),
819 (*OneofScalars_StringB)(nil),
820 (*OneofScalars_Bytes)(nil),
821 (*OneofScalars_BytesA)(nil),
822 (*OneofScalars_BytesB)(nil),
823 }
824}
825
Joe Tsai87b955b2018-11-14 21:59:49 -0800826type (
827 isOneofScalars_Union interface {
828 isOneofScalars_Union()
829 }
830 OneofScalars_Bool struct {
831 Bool bool `protobuf:"1"`
832 }
833 OneofScalars_Int32 struct {
834 Int32 MyInt32 `protobuf:"2"`
835 }
836 OneofScalars_Int64 struct {
837 Int64 int64 `protobuf:"3"`
838 }
839 OneofScalars_Uint32 struct {
840 Uint32 MyUint32 `protobuf:"4"`
841 }
842 OneofScalars_Uint64 struct {
843 Uint64 uint64 `protobuf:"5"`
844 }
845 OneofScalars_Float32 struct {
846 Float32 MyFloat32 `protobuf:"6"`
847 }
848 OneofScalars_Float64 struct {
849 Float64 float64 `protobuf:"7"`
850 }
851 OneofScalars_String struct {
852 String string `protobuf:"8"`
853 }
854 OneofScalars_StringA struct {
855 StringA []byte `protobuf:"9"`
856 }
857 OneofScalars_StringB struct {
858 StringB MyString `protobuf:"10"`
859 }
860 OneofScalars_Bytes struct {
861 Bytes []byte `protobuf:"11"`
862 }
863 OneofScalars_BytesA struct {
864 BytesA string `protobuf:"12"`
865 }
866 OneofScalars_BytesB struct {
867 BytesB MyBytes `protobuf:"13"`
868 }
869)
870
Joe Tsai2c870bb2018-10-17 11:46:52 -0700871func (*OneofScalars_Bool) isOneofScalars_Union() {}
872func (*OneofScalars_Int32) isOneofScalars_Union() {}
873func (*OneofScalars_Int64) isOneofScalars_Union() {}
874func (*OneofScalars_Uint32) isOneofScalars_Union() {}
875func (*OneofScalars_Uint64) isOneofScalars_Union() {}
876func (*OneofScalars_Float32) isOneofScalars_Union() {}
877func (*OneofScalars_Float64) isOneofScalars_Union() {}
878func (*OneofScalars_String) isOneofScalars_Union() {}
879func (*OneofScalars_StringA) isOneofScalars_Union() {}
880func (*OneofScalars_StringB) isOneofScalars_Union() {}
881func (*OneofScalars_Bytes) isOneofScalars_Union() {}
882func (*OneofScalars_BytesA) isOneofScalars_Union() {}
883func (*OneofScalars_BytesB) isOneofScalars_Union() {}
884
885func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800886 empty := &OneofScalars{}
887 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
888 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
889 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
890 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
891 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
892 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
893 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
894 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
895 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
896 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
897 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
898 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
899 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700900
Joe Tsai378c1322019-04-25 23:48:08 -0700901 testMessage(t, nil, new(OneofScalars).ProtoReflect(), messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700902 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},
903 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 -0700904 whichOneofs{"union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700905
Joe Tsai378c1322019-04-25 23:48:08 -0700906 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1.ProtoReflect()},
907 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2.ProtoReflect()},
908 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3.ProtoReflect()},
909 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4.ProtoReflect()},
910 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5.ProtoReflect()},
911 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6.ProtoReflect()},
912 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7.ProtoReflect()},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700913
914 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 -0700915 whichOneofs{"union": 7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700916
Joe Tsai378c1322019-04-25 23:48:08 -0700917 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8.ProtoReflect()},
918 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9.ProtoReflect()},
919 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10.ProtoReflect()},
920 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11.ProtoReflect()},
921 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12.ProtoReflect()},
922 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700923
924 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},
925 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 -0800926 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai378c1322019-04-25 23:48:08 -0700927 whichOneofs{"union": 13},
928 equalMessage{want13.ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800929 clearFields{13},
Joe Tsai378c1322019-04-25 23:48:08 -0700930 whichOneofs{"union": 0},
931 equalMessage{empty.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700932 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800933
934 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700935 testMessage(t, nil, (*OneofScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800936 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},
937 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"))},
938 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700939}
940
Joe Tsai87b955b2018-11-14 21:59:49 -0800941type EnumProto2 int32
942
Joe Tsaib2f66be2019-05-22 00:42:45 -0400943var enumProto2Type = &prototype.Enum{
Joe Tsaid8881392019-06-06 13:01:53 -0700944 EnumDescriptor: mustMakeEnumDesc("enum2.proto", pref.Proto2, `
945 name: "EnumProto2"
946 value: [{name:"DEAD" number:0xdead}, {name:"BEEF" number:0xbeef}]
947 `),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400948 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800949 return EnumProto2(n)
950 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400951}
Joe Tsai87b955b2018-11-14 21:59:49 -0800952
Joe Tsai0fc49f82019-05-01 12:29:25 -0700953func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Type.Descriptor() }
954func (e EnumProto2) Enum() *EnumProto2 { return &e }
955func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800956
957type EnumProto3 int32
958
Joe Tsaib2f66be2019-05-22 00:42:45 -0400959var enumProto3Type = &prototype.Enum{
Joe Tsaid8881392019-06-06 13:01:53 -0700960 EnumDescriptor: mustMakeEnumDesc("enum3.proto", pref.Proto3, `
961 name: "EnumProto3",
962 value: [{name:"ALPHA" number:0}, {name:"BRAVO" number:1}]
963 `),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400964 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800965 return EnumProto3(n)
966 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400967}
Joe Tsai87b955b2018-11-14 21:59:49 -0800968
Joe Tsai0fc49f82019-05-01 12:29:25 -0700969func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Type.Descriptor() }
970func (e EnumProto3) Enum() *EnumProto3 { return &e }
971func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800972
973type EnumMessages struct {
974 EnumP2 *EnumProto2 `protobuf:"1"`
975 EnumP3 *EnumProto3 `protobuf:"2"`
976 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
977 MessageCycle *EnumMessages `protobuf:"4"`
978 EnumList []EnumProto2 `protobuf:"5"`
979 MessageList []*ScalarProto2 `protobuf:"6"`
980 EnumMap map[string]EnumProto3 `protobuf:"7"`
981 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
982 Union isEnumMessages_Union `protobuf_oneof:"union"`
983}
984
Joe Tsaib2f66be2019-05-22 00:42:45 -0400985var enumMessagesType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(EnumMessages)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700986 MessageDescriptor: mustMakeMessageDesc("enum-messages.proto", pref.Proto2, `
987 dependency: ["enum2.proto", "enum3.proto", "scalar2.proto", "scalar3.proto", "proto2.v1.0.0-20180125-92554152/test.proto"]
988 `, `
989 name: "EnumMessages"
990 field: [
991 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" default_value:"BEEF"},
992 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" default_value:"BRAVO"},
993 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_Name:".google.golang.org.proto2_20180125.Message"},
994 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_Name:".EnumMessages"},
995 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_ENUM type_name:".EnumProto2"},
996 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".ScalarProto2"},
997 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F7Entry"},
998 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F8Entry"},
999 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" oneof_index:0 default_value:"BEEF"},
1000 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" oneof_index:0 default_value:"BRAVO"},
1001 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto2" oneof_index:0},
1002 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto3" oneof_index:0}
1003 ]
1004 oneof_decl: [{name:"union"}]
1005 nested_type: [
1006 {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}},
1007 {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}}
1008 ]
1009 `, protoregistry.NewFiles(
1010 EnumProto2(0).Descriptor().ParentFile(),
1011 EnumProto3(0).Descriptor().ParentFile(),
1012 ((*ScalarProto2)(nil)).ProtoReflect().Descriptor().ParentFile(),
1013 ((*ScalarProto3)(nil)).ProtoReflect().Descriptor().ParentFile(),
1014 pimpl.Export{}.MessageDescriptorOf((*proto2_20180125.Message)(nil)).ParentFile(),
1015 )),
Joe Tsaib2f66be2019-05-22 00:42:45 -04001016 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -07001017 return pref.ProtoMessage(new(EnumMessages)).ProtoReflect()
Joe Tsai87b955b2018-11-14 21:59:49 -08001018 },
Joe Tsaib2f66be2019-05-22 00:42:45 -04001019}}
Joe Tsai87b955b2018-11-14 21:59:49 -08001020
Joe Tsai378c1322019-04-25 23:48:08 -07001021func (m *EnumMessages) ProtoReflect() pref.Message { return enumMessagesType.MessageOf(m) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001022
Joe Tsaif18ab532018-11-27 17:25:04 -08001023func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1024 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001025 (*EnumMessages_OneofE2)(nil),
1026 (*EnumMessages_OneofE3)(nil),
1027 (*EnumMessages_OneofM2)(nil),
1028 (*EnumMessages_OneofM3)(nil),
1029 }
1030}
1031
1032type (
1033 isEnumMessages_Union interface {
1034 isEnumMessages_Union()
1035 }
1036 EnumMessages_OneofE2 struct {
1037 OneofE2 EnumProto2 `protobuf:"9"`
1038 }
1039 EnumMessages_OneofE3 struct {
1040 OneofE3 EnumProto3 `protobuf:"10"`
1041 }
1042 EnumMessages_OneofM2 struct {
1043 OneofM2 *ScalarProto2 `protobuf:"11"`
1044 }
1045 EnumMessages_OneofM3 struct {
1046 OneofM3 *ScalarProto3 `protobuf:"12"`
1047 }
1048)
1049
1050func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1051func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1052func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1053func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1054
1055func TestEnumMessages(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -07001056 emptyL := pimpl.Export{}.MessageOf(new(proto2_20180125.Message))
1057 emptyM := new(EnumMessages).ProtoReflect()
1058 emptyM2 := new(ScalarProto2).ProtoReflect()
1059 emptyM3 := new(ScalarProto3).ProtoReflect()
1060
Joe Tsai08e00302018-11-26 22:32:06 -08001061 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
Joe Tsai378c1322019-04-25 23:48:08 -07001062 wantM := (&EnumMessages{EnumP2: EnumProto2(1234).Enum()}).ProtoReflect()
Joe Tsai009e0672018-11-27 18:45:07 -08001063 wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
1064 wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001065 wantM3a := &ScalarProto3{Float32: math.Pi}
1066 wantM3b := &ScalarProto3{Float32: math.Ln2}
1067
Joe Tsai378c1322019-04-25 23:48:08 -07001068 wantList5 := getField((&EnumMessages{EnumList: []EnumProto2{333, 222}}).ProtoReflect(), 5)
1069 wantList6 := getField((&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).ProtoReflect(), 6)
Joe Tsai87b955b2018-11-14 21:59:49 -08001070
Joe Tsai378c1322019-04-25 23:48:08 -07001071 wantMap7 := getField((&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).ProtoReflect(), 7)
1072 wantMap8 := getField((&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).ProtoReflect(), 8)
Joe Tsai87b955b2018-11-14 21:59:49 -08001073
Joe Tsai378c1322019-04-25 23:48:08 -07001074 testMessage(t, nil, new(EnumMessages).ProtoReflect(), messageOps{
Joe Tsai87b955b2018-11-14 21:59:49 -08001075 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 -07001076 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 -08001077
1078 // Test singular enums.
1079 setFields{1: VE(0xdead), 2: VE(0)},
1080 getFields{1: VE(0xdead), 2: VE(0)},
1081 hasFields{1: true, 2: true},
1082
1083 // Test singular messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001084 messageFieldsMutable{3: messageOps{setFields{109: V(float32(math.E))}}},
1085 messageFieldsMutable{4: messageOps{setFields{1: VE(1234)}}},
Joe Tsai87b955b2018-11-14 21:59:49 -08001086 getFields{3: V(wantL), 4: V(wantM)},
1087 clearFields{3, 4},
1088 hasFields{3: false, 4: false},
1089 setFields{3: V(wantL), 4: V(wantM)},
1090 hasFields{3: true, 4: true},
1091
1092 // Test list of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001093 listFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001094 5: listOps{
1095 appendList{VE(111), VE(222)},
1096 setList{0: VE(333)},
1097 getList{0: VE(333), 1: VE(222)},
1098 lenList(2),
1099 },
1100 6: listOps{
1101 appendMessageList{setFields{4: V(uint32(1e6))}},
1102 appendMessageList{setFields{6: V(float32(math.Phi))}},
Joe Tsai378c1322019-04-25 23:48:08 -07001103 setList{0: V(wantM2a.ProtoReflect())},
1104 getList{0: V(wantM2a.ProtoReflect()), 1: V(wantM2b.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001105 },
1106 },
1107 getFields{5: wantList5, 6: wantList6},
1108 hasFields{5: true, 6: true},
1109 listFields{5: listOps{truncList(0)}},
1110 hasFields{5: false, 6: true},
1111
1112 // Test maps of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001113 mapFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001114 7: mapOps{
1115 setMap{"one": VE(1), "two": VE(2)},
1116 hasMap{"one": true, "two": true, "three": false},
1117 lenMap(2),
1118 },
1119 8: mapOps{
1120 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
Joe Tsai378c1322019-04-25 23:48:08 -07001121 setMap{"ln2": V(wantM3b.ProtoReflect())},
1122 getMap{"pi": V(wantM3a.ProtoReflect()), "ln2": V(wantM3b.ProtoReflect()), "none": V(nil)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001123 lenMap(2),
1124 },
1125 },
1126 getFields{7: wantMap7, 8: wantMap8},
1127 hasFields{7: true, 8: true},
1128 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1129 hasFields{7: true, 8: false},
1130
1131 // Test oneofs of enums and messages.
1132 setFields{9: VE(0xdead)},
1133 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1134 setFields{10: VE(0)},
1135 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001136 messageFieldsMutable{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1137 getFields{11: V(wantM2a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001138 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001139 messageFieldsMutable{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1140 getFields{12: V(wantM3a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001141 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1142
1143 // Check entire message.
Joe Tsai378c1322019-04-25 23:48:08 -07001144 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a.ProtoReflect())},
1145 equalMessage{(&EnumMessages{
Joe Tsai87b955b2018-11-14 21:59:49 -08001146 EnumP2: EnumProto2(0xdead).Enum(),
1147 EnumP3: EnumProto3(0).Enum(),
Joe Tsai009e0672018-11-27 18:45:07 -08001148 MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
Joe Tsai378c1322019-04-25 23:48:08 -07001149 MessageCycle: wantM.Interface().(*EnumMessages),
Joe Tsai87b955b2018-11-14 21:59:49 -08001150 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1151 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1152 Union: &EnumMessages_OneofM3{wantM3a},
Joe Tsai378c1322019-04-25 23:48:08 -07001153 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001154 clearFields{1, 2, 3, 4, 6, 7, 12},
Joe Tsai378c1322019-04-25 23:48:08 -07001155 equalMessage{new(EnumMessages).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001156 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001157
1158 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -07001159 testMessage(t, nil, (*EnumMessages)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -08001160 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 -07001161 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 -08001162 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1163 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1164 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001165}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001166
Joe Tsai91e14662018-09-13 13:24:35 -07001167var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001168 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
Joe Tsaid8881392019-06-06 13:01:53 -07001169 mx := pimpl.Export{}.MessageOf(x).Interface()
1170 my := pimpl.Export{}.MessageOf(y).Interface()
1171 return proto.Equal(mx, my)
Joe Tsai91e14662018-09-13 13:24:35 -07001172 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001173 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
Joe Tsai378c1322019-04-25 23:48:08 -07001174 switch v := pv.Interface().(type) {
1175 case pref.Message:
1176 out := make(map[pref.FieldNumber]pref.Value)
1177 v.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1178 out[fd.Number()] = v
1179 return true
1180 })
1181 return out
1182 case pref.List:
1183 var out []pref.Value
1184 for i := 0; i < v.Len(); i++ {
1185 out = append(out, v.Get(i))
1186 }
1187 return out
1188 case pref.Map:
1189 out := make(map[interface{}]pref.Value)
1190 v.Range(func(k pref.MapKey, v pref.Value) bool {
1191 out[k.Interface()] = v
1192 return true
1193 })
1194 return out
1195 default:
1196 return v
1197 }
Joe Tsai91e14662018-09-13 13:24:35 -07001198 }),
1199 cmpopts.EquateNaNs(),
Joe Tsai87b955b2018-11-14 21:59:49 -08001200 cmpopts.EquateEmpty(),
Joe Tsai91e14662018-09-13 13:24:35 -07001201}
1202
1203func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
Joe Tsai378c1322019-04-25 23:48:08 -07001204 fieldDescs := m.Descriptor().Fields()
1205 oneofDescs := m.Descriptor().Oneofs()
Joe Tsai91e14662018-09-13 13:24:35 -07001206 for i, op := range tt {
1207 p.Push(i)
1208 switch op := op.(type) {
1209 case equalMessage:
Joe Tsai378c1322019-04-25 23:48:08 -07001210 if diff := cmp.Diff(V(op.Message), V(m), cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001211 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1212 }
1213 case hasFields:
1214 got := map[pref.FieldNumber]bool{}
1215 want := map[pref.FieldNumber]bool(op)
1216 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001217 fd := fieldDescs.ByNumber(n)
1218 got[n] = m.Has(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001219 }
1220 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001221 t.Errorf("operation %v, Message.Has mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001222 }
1223 case getFields:
1224 got := map[pref.FieldNumber]pref.Value{}
1225 want := map[pref.FieldNumber]pref.Value(op)
1226 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001227 fd := fieldDescs.ByNumber(n)
1228 got[n] = m.Get(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001229 }
1230 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001231 t.Errorf("operation %v, Message.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001232 }
1233 case setFields:
1234 for n, v := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001235 fd := fieldDescs.ByNumber(n)
1236 m.Set(fd, v)
Joe Tsai91e14662018-09-13 13:24:35 -07001237 }
1238 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001239 for _, n := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001240 fd := fieldDescs.ByNumber(n)
1241 m.Clear(fd)
Joe Tsai87b955b2018-11-14 21:59:49 -08001242 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001243 case whichOneofs:
1244 got := map[pref.Name]pref.FieldNumber{}
1245 want := map[pref.Name]pref.FieldNumber(op)
1246 for s := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001247 od := oneofDescs.ByName(s)
1248 fd := m.WhichOneof(od)
1249 if fd == nil {
1250 got[s] = 0
1251 } else {
1252 got[s] = fd.Number()
1253 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001254 }
1255 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001256 t.Errorf("operation %v, Message.WhichOneof mismatch (-want, +got):\n%s", p, diff)
Joe Tsai4ec39c72019-04-03 13:40:53 -07001257 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001258 case messageFields:
1259 for n, tt := range op {
1260 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001261 fd := fieldDescs.ByNumber(n)
1262 testMessage(t, p, m.Get(fd).Message(), tt)
1263 p.Pop()
1264 }
1265 case messageFieldsMutable:
1266 for n, tt := range op {
1267 p.Push(int(n))
1268 fd := fieldDescs.ByNumber(n)
1269 testMessage(t, p, m.Mutable(fd).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001270 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001271 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001272 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001273 for n, tt := range op {
1274 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001275 fd := fieldDescs.ByNumber(n)
1276 testLists(t, p, m.Get(fd).List(), tt)
1277 p.Pop()
1278 }
1279 case listFieldsMutable:
1280 for n, tt := range op {
1281 p.Push(int(n))
1282 fd := fieldDescs.ByNumber(n)
1283 testLists(t, p, m.Mutable(fd).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001284 p.Pop()
1285 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001286 case mapFields:
1287 for n, tt := range op {
1288 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001289 fd := fieldDescs.ByNumber(n)
1290 testMaps(t, p, m.Get(fd).Map(), tt)
1291 p.Pop()
1292 }
1293 case mapFieldsMutable:
1294 for n, tt := range op {
1295 p.Push(int(n))
1296 fd := fieldDescs.ByNumber(n)
1297 testMaps(t, p, m.Mutable(fd).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001298 p.Pop()
1299 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001300 case rangeFields:
1301 got := map[pref.FieldNumber]pref.Value{}
1302 want := map[pref.FieldNumber]pref.Value(op)
Joe Tsai378c1322019-04-25 23:48:08 -07001303 m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1304 got[fd.Number()] = v
Joe Tsai87b955b2018-11-14 21:59:49 -08001305 return true
1306 })
1307 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001308 t.Errorf("operation %v, Message.Range mismatch (-want, +got):\n%s", p, diff)
Joe Tsai87b955b2018-11-14 21:59:49 -08001309 }
Joe Tsai91e14662018-09-13 13:24:35 -07001310 default:
1311 t.Fatalf("operation %v, invalid operation: %T", p, op)
1312 }
1313 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001314 }
1315}
Joe Tsai91e14662018-09-13 13:24:35 -07001316
Joe Tsai4b7aff62018-11-14 14:05:19 -08001317func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001318 for i, op := range tt {
1319 p.Push(i)
1320 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001321 case equalList:
Joe Tsai378c1322019-04-25 23:48:08 -07001322 if diff := cmp.Diff(V(op.List), V(v), cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001323 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001324 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001325 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001326 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001327 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001328 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001329 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001330 got := map[int]pref.Value{}
1331 want := map[int]pref.Value(op)
1332 for n := range want {
1333 got[n] = v.Get(n)
1334 }
1335 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001336 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001337 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001338 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001339 for n, e := range op {
1340 v.Set(n, e)
1341 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001342 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001343 for _, e := range op {
1344 v.Append(e)
1345 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001346 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001347 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001348 v.Append(V(m))
1349 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001350 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001351 v.Truncate(int(op))
1352 default:
1353 t.Fatalf("operation %v, invalid operation: %T", p, op)
1354 }
1355 p.Pop()
1356 }
1357}
1358
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001359func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1360 for i, op := range tt {
1361 p.Push(i)
1362 switch op := op.(type) {
1363 case equalMap:
Joe Tsai378c1322019-04-25 23:48:08 -07001364 if diff := cmp.Diff(V(op.Map), V(m), cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001365 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1366 }
1367 case lenMap:
1368 if got, want := m.Len(), int(op); got != want {
1369 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1370 }
1371 case hasMap:
1372 got := map[interface{}]bool{}
1373 want := map[interface{}]bool(op)
1374 for k := range want {
1375 got[k] = m.Has(V(k).MapKey())
1376 }
1377 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1378 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1379 }
1380 case getMap:
1381 got := map[interface{}]pref.Value{}
1382 want := map[interface{}]pref.Value(op)
1383 for k := range want {
1384 got[k] = m.Get(V(k).MapKey())
1385 }
1386 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1387 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1388 }
1389 case setMap:
1390 for k, v := range op {
1391 m.Set(V(k).MapKey(), v)
1392 }
1393 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001394 for _, k := range op {
1395 m.Clear(V(k).MapKey())
1396 }
1397 case messageMap:
1398 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001399 mk := V(k).MapKey()
1400 if !m.Has(mk) {
1401 m.Set(mk, V(m.NewMessage()))
1402 }
1403 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001404 }
1405 case rangeMap:
1406 got := map[interface{}]pref.Value{}
1407 want := map[interface{}]pref.Value(op)
1408 m.Range(func(k pref.MapKey, v pref.Value) bool {
1409 got[k.Interface()] = v
1410 return true
1411 })
1412 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1413 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1414 }
1415 default:
1416 t.Fatalf("operation %v, invalid operation: %T", p, op)
1417 }
1418 p.Pop()
1419 }
1420}
1421
Joe Tsai378c1322019-04-25 23:48:08 -07001422func getField(m pref.Message, n pref.FieldNumber) pref.Value {
1423 fd := m.Descriptor().Fields().ByNumber(n)
1424 return m.Get(fd)
1425}
1426
Joe Tsai91e14662018-09-13 13:24:35 -07001427type path []int
1428
1429func (p *path) Push(i int) { *p = append(*p, i) }
1430func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1431func (p path) String() string {
1432 var ss []string
1433 for _, i := range p {
1434 ss = append(ss, fmt.Sprint(i))
1435 }
1436 return strings.Join(ss, ".")
1437}