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