blob: 0fb8ae03e014dcb79ab2288490062c4f4ceb3533 [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 Tsaib2f66be2019-05-22 00:42:45 -040024 "google.golang.org/protobuf/reflect/prototype"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070025
Damien Neile89e6242019-05-13 23:55:40 -070026 proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
Joe Tsai82760ce2019-06-20 03:09:57 -070027 testpb "google.golang.org/protobuf/internal/testprotos/test"
Joe Tsaia95b29f2019-05-16 12:47:20 -070028 "google.golang.org/protobuf/types/descriptorpb"
Joe Tsaifa02f4e2018-09-12 16:20:37 -070029)
30
Joe Tsai4b7aff62018-11-14 14:05:19 -080031// List of test operations to perform on messages, lists, or maps.
Joe Tsai91e14662018-09-13 13:24:35 -070032type (
Joe Tsai87b955b2018-11-14 21:59:49 -080033 messageOp interface{ isMessageOp() }
Joe Tsai91e14662018-09-13 13:24:35 -070034 messageOps []messageOp
Joe Tsaifa02f4e2018-09-12 16:20:37 -070035
Joe Tsai87b955b2018-11-14 21:59:49 -080036 listOp interface{ isListOp() }
Joe Tsai4b7aff62018-11-14 14:05:19 -080037 listOps []listOp
Joe Tsai91e14662018-09-13 13:24:35 -070038
Joe Tsai87b955b2018-11-14 21:59:49 -080039 mapOp interface{ isMapOp() }
Joe Tsaibbfaeb72018-10-17 00:27:21 +000040 mapOps []mapOp
Joe Tsai91e14662018-09-13 13:24:35 -070041)
42
43// Test operations performed on a message.
44type (
Joe Tsai87b955b2018-11-14 21:59:49 -080045 // check that the message contents match
46 equalMessage struct{ pref.Message }
47 // check presence for specific fields in the message
48 hasFields map[pref.FieldNumber]bool
49 // check that specific message fields match
50 getFields map[pref.FieldNumber]pref.Value
51 // set specific message fields
52 setFields map[pref.FieldNumber]pref.Value
53 // clear specific fields in the message
54 clearFields []pref.FieldNumber
Joe Tsai4ec39c72019-04-03 13:40:53 -070055 // check for the presence of specific oneof member fields.
56 whichOneofs map[pref.Name]pref.FieldNumber
Joe Tsai87b955b2018-11-14 21:59:49 -080057 // apply messageOps on each specified message field
Joe Tsai378c1322019-04-25 23:48:08 -070058 messageFields map[pref.FieldNumber]messageOps
59 messageFieldsMutable map[pref.FieldNumber]messageOps
Joe Tsai87b955b2018-11-14 21:59:49 -080060 // apply listOps on each specified list field
Joe Tsai378c1322019-04-25 23:48:08 -070061 listFields map[pref.FieldNumber]listOps
62 listFieldsMutable map[pref.FieldNumber]listOps
Joe Tsai87b955b2018-11-14 21:59:49 -080063 // apply mapOps on each specified map fields
Joe Tsai378c1322019-04-25 23:48:08 -070064 mapFields map[pref.FieldNumber]mapOps
65 mapFieldsMutable map[pref.FieldNumber]mapOps
Joe Tsai87b955b2018-11-14 21:59:49 -080066 // range through all fields and check that they match
67 rangeFields map[pref.FieldNumber]pref.Value
Joe Tsai91e14662018-09-13 13:24:35 -070068)
69
Joe Tsai378c1322019-04-25 23:48:08 -070070func (equalMessage) isMessageOp() {}
71func (hasFields) isMessageOp() {}
72func (getFields) isMessageOp() {}
73func (setFields) isMessageOp() {}
74func (clearFields) isMessageOp() {}
75func (whichOneofs) isMessageOp() {}
76func (messageFields) isMessageOp() {}
77func (messageFieldsMutable) isMessageOp() {}
78func (listFields) isMessageOp() {}
79func (listFieldsMutable) isMessageOp() {}
80func (mapFields) isMessageOp() {}
81func (mapFieldsMutable) isMessageOp() {}
82func (rangeFields) isMessageOp() {}
Joe Tsai87b955b2018-11-14 21:59:49 -080083
Joe Tsai4b7aff62018-11-14 14:05:19 -080084// Test operations performed on a list.
Joe Tsai91e14662018-09-13 13:24:35 -070085type (
Joe Tsai87b955b2018-11-14 21:59:49 -080086 // check that the list contents match
87 equalList struct{ pref.List }
88 // check that list length matches
89 lenList int
90 // check that specific list entries match
91 getList map[int]pref.Value
92 // set specific list entries
93 setList map[int]pref.Value
94 // append entries to the list
Joe Tsai4b7aff62018-11-14 14:05:19 -080095 appendList []pref.Value
Joe Tsai87b955b2018-11-14 21:59:49 -080096 // apply messageOps on a newly appended message
97 appendMessageList messageOps
98 // truncate the list to the specified length
99 truncList int
Joe Tsai91e14662018-09-13 13:24:35 -0700100)
101
Joe Tsai87b955b2018-11-14 21:59:49 -0800102func (equalList) isListOp() {}
103func (lenList) isListOp() {}
104func (getList) isListOp() {}
105func (setList) isListOp() {}
106func (appendList) isListOp() {}
107func (appendMessageList) isListOp() {}
108func (truncList) isListOp() {}
109
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000110// Test operations performed on a map.
111type (
Joe Tsai87b955b2018-11-14 21:59:49 -0800112 // check that the map contents match
113 equalMap struct{ pref.Map }
114 // check that map length matches
115 lenMap int
116 // check presence for specific entries in the map
117 hasMap map[interface{}]bool
118 // check that specific map entries match
119 getMap map[interface{}]pref.Value
120 // set specific map entries
121 setMap map[interface{}]pref.Value
122 // clear specific entries in the map
123 clearMap []interface{}
124 // apply messageOps on each specified message entry
125 messageMap map[interface{}]messageOps
126 // range through all entries and check that they match
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000127 rangeMap map[interface{}]pref.Value
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000128)
129
Joe Tsai87b955b2018-11-14 21:59:49 -0800130func (equalMap) isMapOp() {}
131func (lenMap) isMapOp() {}
132func (hasMap) isMapOp() {}
133func (getMap) isMapOp() {}
134func (setMap) isMapOp() {}
135func (clearMap) isMapOp() {}
136func (messageMap) isMapOp() {}
137func (rangeMap) isMapOp() {}
138
Joe Tsaice6edd32018-10-19 16:27:46 -0700139type ScalarProto2 struct {
140 Bool *bool `protobuf:"1"`
141 Int32 *int32 `protobuf:"2"`
142 Int64 *int64 `protobuf:"3"`
143 Uint32 *uint32 `protobuf:"4"`
144 Uint64 *uint64 `protobuf:"5"`
145 Float32 *float32 `protobuf:"6"`
146 Float64 *float64 `protobuf:"7"`
147 String *string `protobuf:"8"`
148 StringA []byte `protobuf:"9"`
149 Bytes []byte `protobuf:"10"`
150 BytesA *string `protobuf:"11"`
151
152 MyBool *MyBool `protobuf:"12"`
153 MyInt32 *MyInt32 `protobuf:"13"`
154 MyInt64 *MyInt64 `protobuf:"14"`
155 MyUint32 *MyUint32 `protobuf:"15"`
156 MyUint64 *MyUint64 `protobuf:"16"`
157 MyFloat32 *MyFloat32 `protobuf:"17"`
158 MyFloat64 *MyFloat64 `protobuf:"18"`
159 MyString *MyString `protobuf:"19"`
160 MyStringA MyBytes `protobuf:"20"`
161 MyBytes MyBytes `protobuf:"21"`
162 MyBytesA *MyString `protobuf:"22"`
163}
164
Joe Tsaid8881392019-06-06 13:01:53 -0700165func mustMakeEnumDesc(path string, syntax pref.Syntax, enumDesc string) pref.EnumDescriptor {
166 s := fmt.Sprintf(`name:%q syntax:%q enum_type:[{%s}]`, path, syntax, enumDesc)
167 pb := new(descriptorpb.FileDescriptorProto)
168 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
169 panic(err)
170 }
171 fd, err := pdesc.NewFile(pb, nil)
Joe Tsai87b955b2018-11-14 21:59:49 -0800172 if err != nil {
173 panic(err)
174 }
Joe Tsaid8881392019-06-06 13:01:53 -0700175 return fd.Enums().Get(0)
Joe Tsai87b955b2018-11-14 21:59:49 -0800176}
177
Joe Tsaid8881392019-06-06 13:01:53 -0700178func mustMakeMessageDesc(path string, syntax pref.Syntax, fileDesc, msgDesc string, r pdesc.Resolver) pref.MessageDescriptor {
179 s := fmt.Sprintf(`name:%q syntax:%q %s message_type:[{%s}]`, path, syntax, fileDesc, msgDesc)
180 pb := new(descriptorpb.FileDescriptorProto)
181 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
182 panic(err)
183 }
184 fd, err := pdesc.NewFile(pb, r)
Joe Tsai87b955b2018-11-14 21:59:49 -0800185 if err != nil {
186 panic(err)
187 }
Joe Tsaid8881392019-06-06 13:01:53 -0700188 return fd.Messages().Get(0)
Joe Tsai87b955b2018-11-14 21:59:49 -0800189}
190
191var V = pref.ValueOf
192var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
193
194type (
195 MyBool bool
196 MyInt32 int32
197 MyInt64 int64
198 MyUint32 uint32
199 MyUint64 uint64
200 MyFloat32 float32
201 MyFloat64 float64
202 MyString string
203 MyBytes []byte
204
205 ListStrings []MyString
206 ListBytes []MyBytes
207
208 MapStrings map[MyString]MyString
209 MapBytes map[MyString]MyBytes
210)
211
Joe Tsaib2f66be2019-05-22 00:42:45 -0400212var scalarProto2Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto2)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700213 MessageDescriptor: mustMakeMessageDesc("scalar2.proto", pref.Proto2, "", `
214 name: "ScalarProto2"
215 field: [
216 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
217 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2"},
218 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3"},
219 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4"},
220 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5"},
221 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6"},
222 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7"},
223 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8"},
224 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9"},
225 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"10"},
226 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11"},
Joe Tsai91e14662018-09-13 13:24:35 -0700227
Joe Tsaid8881392019-06-06 13:01:53 -0700228 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
229 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"13"},
230 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"14"},
231 {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"15"},
232 {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"16"},
233 {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"17"},
234 {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"18"},
235 {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"19"},
236 {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"20"},
237 {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"21"},
238 {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"22"}
239 ]
240 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400241 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700242 return pref.ProtoMessage(new(ScalarProto2)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800243 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400244}}
Joe Tsai91e14662018-09-13 13:24:35 -0700245
Joe Tsai378c1322019-04-25 23:48:08 -0700246func (m *ScalarProto2) ProtoReflect() pref.Message { return scalarProto2Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800247
248func TestScalarProto2(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700249 testMessage(t, nil, new(ScalarProto2).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700250 hasFields{
251 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
252 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
253 },
254 getFields{
255 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")),
256 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")),
257 },
258 setFields{
259 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)),
260 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)),
261 },
262 hasFields{
263 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
264 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
265 },
Joe Tsai378c1322019-04-25 23:48:08 -0700266 equalMessage{(&ScalarProto2{
Joe Tsai91e14662018-09-13 13:24:35 -0700267 new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
268 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 -0700269 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800270 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 -0700271 equalMessage{new(ScalarProto2).ProtoReflect()},
Joe Tsai3e6a39b2019-07-10 10:21:12 -0700272
273 // Setting a bytes field nil empty bytes should preserve presence.
274 setFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
275 getFields{10: V([]byte{}), 11: V([]byte(nil)), 21: V([]byte{}), 22: V([]byte(nil))},
276 hasFields{10: true, 11: true, 21: true, 22: true},
Joe Tsai91e14662018-09-13 13:24:35 -0700277 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800278
279 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700280 testMessage(t, nil, (*ScalarProto2)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800281 hasFields{
282 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
283 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
284 },
285 getFields{
286 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")),
287 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")),
288 },
289 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700290}
291
Joe Tsaice6edd32018-10-19 16:27:46 -0700292type ScalarProto3 struct {
293 Bool bool `protobuf:"1"`
294 Int32 int32 `protobuf:"2"`
295 Int64 int64 `protobuf:"3"`
296 Uint32 uint32 `protobuf:"4"`
297 Uint64 uint64 `protobuf:"5"`
298 Float32 float32 `protobuf:"6"`
299 Float64 float64 `protobuf:"7"`
300 String string `protobuf:"8"`
301 StringA []byte `protobuf:"9"`
302 Bytes []byte `protobuf:"10"`
303 BytesA string `protobuf:"11"`
304
305 MyBool MyBool `protobuf:"12"`
306 MyInt32 MyInt32 `protobuf:"13"`
307 MyInt64 MyInt64 `protobuf:"14"`
308 MyUint32 MyUint32 `protobuf:"15"`
309 MyUint64 MyUint64 `protobuf:"16"`
310 MyFloat32 MyFloat32 `protobuf:"17"`
311 MyFloat64 MyFloat64 `protobuf:"18"`
312 MyString MyString `protobuf:"19"`
313 MyStringA MyBytes `protobuf:"20"`
314 MyBytes MyBytes `protobuf:"21"`
315 MyBytesA MyString `protobuf:"22"`
316}
317
Joe Tsaib2f66be2019-05-22 00:42:45 -0400318var scalarProto3Type = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ScalarProto3)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700319 MessageDescriptor: mustMakeMessageDesc("scalar3.proto", pref.Proto3, "", `
320 name: "ScalarProto3"
321 field: [
322 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL},
323 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32},
324 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64},
325 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32},
326 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64},
327 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT},
328 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
329 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING},
330 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING},
331 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES},
332 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700333
Joe Tsaid8881392019-06-06 13:01:53 -0700334 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL},
335 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32},
336 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64},
337 {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32},
338 {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64},
339 {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT},
340 {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
341 {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING},
342 {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING},
343 {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES},
344 {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES}
345 ]
346 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400347 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700348 return pref.ProtoMessage(new(ScalarProto3)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800349 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400350}}
Joe Tsai91e14662018-09-13 13:24:35 -0700351
Joe Tsai378c1322019-04-25 23:48:08 -0700352func (m *ScalarProto3) ProtoReflect() pref.Message { return scalarProto3Type.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800353
354func TestScalarProto3(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700355 testMessage(t, nil, new(ScalarProto3).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700356 hasFields{
357 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
358 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
359 },
360 getFields{
361 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
362 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
363 },
364 setFields{
365 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)),
366 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)),
367 },
368 hasFields{
369 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
370 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
371 },
Joe Tsai378c1322019-04-25 23:48:08 -0700372 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai91e14662018-09-13 13:24:35 -0700373 setFields{
374 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")),
375 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")),
376 },
377 hasFields{
378 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
379 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
380 },
Joe Tsai378c1322019-04-25 23:48:08 -0700381 equalMessage{(&ScalarProto3{
Joe Tsai91e14662018-09-13 13:24:35 -0700382 true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
383 true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
Joe Tsai378c1322019-04-25 23:48:08 -0700384 }).ProtoReflect()},
Joe Tsai44e389c2018-11-19 15:27:09 -0800385 setFields{
386 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
387 },
388 hasFields{
389 2: true, 3: true, 6: true, 7: true,
390 },
Joe Tsai87b955b2018-11-14 21:59:49 -0800391 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 -0700392 equalMessage{new(ScalarProto3).ProtoReflect()},
Joe Tsai060cdac2019-04-22 11:44:49 -0700393
394 // Verify that -0 triggers proper Has behavior.
395 hasFields{6: false, 7: false},
396 setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
397 hasFields{6: true, 7: true},
Joe Tsai3e6a39b2019-07-10 10:21:12 -0700398
399 // Setting a bytes field to non-nil empty bytes should not preserve presence.
400 setFields{10: V([]byte{}), 11: V([]byte{}), 21: V([]byte{}), 22: V([]byte{})},
401 getFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
402 hasFields{10: false, 11: false, 21: false, 22: false},
Joe Tsai91e14662018-09-13 13:24:35 -0700403 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800404
405 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700406 testMessage(t, nil, (*ScalarProto3)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800407 hasFields{
408 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
409 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
410 },
411 getFields{
412 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)),
413 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)),
414 },
415 })
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700416}
417
Joe Tsaif0c01e42018-11-06 13:05:20 -0800418type ListScalars struct {
Joe Tsaice6edd32018-10-19 16:27:46 -0700419 Bools []bool `protobuf:"1"`
420 Int32s []int32 `protobuf:"2"`
421 Int64s []int64 `protobuf:"3"`
422 Uint32s []uint32 `protobuf:"4"`
423 Uint64s []uint64 `protobuf:"5"`
424 Float32s []float32 `protobuf:"6"`
425 Float64s []float64 `protobuf:"7"`
426 Strings []string `protobuf:"8"`
427 StringsA [][]byte `protobuf:"9"`
428 Bytes [][]byte `protobuf:"10"`
429 BytesA []string `protobuf:"11"`
430
431 MyStrings1 []MyString `protobuf:"12"`
432 MyStrings2 []MyBytes `protobuf:"13"`
433 MyBytes1 []MyBytes `protobuf:"14"`
434 MyBytes2 []MyString `protobuf:"15"`
435
Joe Tsai4b7aff62018-11-14 14:05:19 -0800436 MyStrings3 ListStrings `protobuf:"16"`
437 MyStrings4 ListBytes `protobuf:"17"`
438 MyBytes3 ListBytes `protobuf:"18"`
439 MyBytes4 ListStrings `protobuf:"19"`
Joe Tsaice6edd32018-10-19 16:27:46 -0700440}
441
Joe Tsaib2f66be2019-05-22 00:42:45 -0400442var listScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(ListScalars)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700443 MessageDescriptor: mustMakeMessageDesc("list-scalars.proto", pref.Proto2, "", `
444 name: "ListScalars"
445 field: [
446 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_BOOL},
447 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_INT32},
448 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_INT64},
449 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_UINT32},
450 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_UINT64},
451 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_FLOAT},
452 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_DOUBLE},
453 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_STRING},
454 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_STRING},
455 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_BYTES},
456 {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700457
Joe Tsaid8881392019-06-06 13:01:53 -0700458 {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_STRING},
459 {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_STRING},
460 {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_BYTES},
461 {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_BYTES},
Joe Tsai91e14662018-09-13 13:24:35 -0700462
Joe Tsaid8881392019-06-06 13:01:53 -0700463 {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_STRING},
464 {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_STRING},
465 {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_BYTES},
466 {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_BYTES}
467 ]
468 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400469 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700470 return pref.ProtoMessage(new(ListScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800471 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400472}}
Joe Tsai91e14662018-09-13 13:24:35 -0700473
Joe Tsai378c1322019-04-25 23:48:08 -0700474func (m *ListScalars) ProtoReflect() pref.Message { return listScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800475
476func TestListScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700477 empty := new(ListScalars).ProtoReflect()
478 want := (&ListScalars{
Joe Tsai91e14662018-09-13 13:24:35 -0700479 Bools: []bool{true, false, true},
480 Int32s: []int32{2, math.MinInt32, math.MaxInt32},
481 Int64s: []int64{3, math.MinInt64, math.MaxInt64},
482 Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
483 Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
484 Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
485 Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
486 Strings: []string{"8", "", "eight"},
487 StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
488 Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
489 BytesA: []string{"11", "", "eleven"},
490
491 MyStrings1: []MyString{"12", "", "twelve"},
492 MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
493 MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
494 MyBytes2: []MyString{"15", "", "fifteen"},
495
Joe Tsai4b7aff62018-11-14 14:05:19 -0800496 MyStrings3: ListStrings{"16", "", "sixteen"},
497 MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
498 MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
499 MyBytes4: ListStrings{"19", "", "nineteen"},
Joe Tsai378c1322019-04-25 23:48:08 -0700500 }).ProtoReflect()
Joe Tsai91e14662018-09-13 13:24:35 -0700501
Joe Tsai378c1322019-04-25 23:48:08 -0700502 testMessage(t, nil, new(ListScalars).ProtoReflect(), messageOps{
Joe Tsai91e14662018-09-13 13:24:35 -0700503 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 -0700504 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)},
505 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)},
506 listFieldsMutable{
Joe Tsai91e14662018-09-13 13:24:35 -0700507 2: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800508 lenList(0),
509 appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
510 getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700511 equalList{getField(want, 2).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700512 },
513 4: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800514 appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
515 setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
516 lenList(3),
Joe Tsai91e14662018-09-13 13:24:35 -0700517 },
518 6: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800519 appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
Joe Tsai378c1322019-04-25 23:48:08 -0700520 equalList{getField(want, 6).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700521 },
522 8: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800523 appendList{V(""), V(""), V(""), V(""), V(""), V("")},
524 lenList(6),
525 setList{0: V("8"), 2: V("eight")},
526 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700527 equalList{getField(want, 8).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700528 },
529 10: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800530 appendList{V([]byte(nil)), V([]byte(nil))},
531 setList{0: V([]byte("10"))},
532 appendList{V([]byte("wrong"))},
533 setList{2: V([]byte("ten"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700534 equalList{getField(want, 10).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700535 },
536 12: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800537 appendList{V("12"), V("wrong"), V("twelve")},
538 setList{1: V("")},
Joe Tsai378c1322019-04-25 23:48:08 -0700539 equalList{getField(want, 12).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700540 },
541 14: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800542 appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700543 equalList{getField(want, 14).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700544 },
545 16: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800546 appendList{V("16"), V(""), V("sixteen"), V("extra")},
547 truncList(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700548 equalList{getField(want, 16).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700549 },
550 18: {
Joe Tsai4b7aff62018-11-14 14:05:19 -0800551 appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700552 equalList{getField(want, 18).List()},
Joe Tsai91e14662018-09-13 13:24:35 -0700553 },
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700554 },
Joe Tsai91e14662018-09-13 13:24:35 -0700555 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 -0800556 equalMessage{want},
557 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
558 equalMessage{empty},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000559 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800560
561 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700562 testMessage(t, nil, (*ListScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800563 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},
564 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)}},
565 })
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000566}
567
Joe Tsaice6edd32018-10-19 16:27:46 -0700568type MapScalars struct {
569 KeyBools map[bool]string `protobuf:"1"`
570 KeyInt32s map[int32]string `protobuf:"2"`
571 KeyInt64s map[int64]string `protobuf:"3"`
572 KeyUint32s map[uint32]string `protobuf:"4"`
573 KeyUint64s map[uint64]string `protobuf:"5"`
574 KeyStrings map[string]string `protobuf:"6"`
575
576 ValBools map[string]bool `protobuf:"7"`
577 ValInt32s map[string]int32 `protobuf:"8"`
578 ValInt64s map[string]int64 `protobuf:"9"`
579 ValUint32s map[string]uint32 `protobuf:"10"`
580 ValUint64s map[string]uint64 `protobuf:"11"`
581 ValFloat32s map[string]float32 `protobuf:"12"`
582 ValFloat64s map[string]float64 `protobuf:"13"`
583 ValStrings map[string]string `protobuf:"14"`
584 ValStringsA map[string][]byte `protobuf:"15"`
585 ValBytes map[string][]byte `protobuf:"16"`
586 ValBytesA map[string]string `protobuf:"17"`
587
588 MyStrings1 map[MyString]MyString `protobuf:"18"`
589 MyStrings2 map[MyString]MyBytes `protobuf:"19"`
590 MyBytes1 map[MyString]MyBytes `protobuf:"20"`
591 MyBytes2 map[MyString]MyString `protobuf:"21"`
592
593 MyStrings3 MapStrings `protobuf:"22"`
594 MyStrings4 MapBytes `protobuf:"23"`
595 MyBytes3 MapBytes `protobuf:"24"`
596 MyBytes4 MapStrings `protobuf:"25"`
597}
598
Joe Tsaib2f66be2019-05-22 00:42:45 -0400599var mapScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(MapScalars)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700600 MessageDescriptor: mustMakeMessageDesc("map-scalars.proto", pref.Proto2, "", `
601 name: "MapScalars"
602 field: [
603 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F1Entry"},
604 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F2Entry"},
605 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F3Entry"},
606 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F4Entry"},
607 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F5Entry"},
608 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F6Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000609
Joe Tsaid8881392019-06-06 13:01:53 -0700610 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F7Entry"},
611 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F8Entry"},
612 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F9Entry"},
613 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F10Entry"},
614 {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F11Entry"},
615 {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F12Entry"},
616 {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F13Entry"},
617 {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F14Entry"},
618 {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F15Entry"},
619 {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F16Entry"},
620 {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F17Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000621
Joe Tsaid8881392019-06-06 13:01:53 -0700622 {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F18Entry"},
623 {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F19Entry"},
624 {name:"f20" number:20 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F20Entry"},
625 {name:"f21" number:21 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F21Entry"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000626
Joe Tsaid8881392019-06-06 13:01:53 -0700627 {name:"f22" number:22 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F22Entry"},
628 {name:"f23" number:23 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F23Entry"},
629 {name:"f24" number:24 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F24Entry"},
630 {name:"f25" number:25 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F25Entry"}
631 ]
632 nested_type: [
633 {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}},
634 {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}},
635 {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}},
636 {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}},
637 {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}},
638 {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}},
639
640 {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}},
641 {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}},
642 {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}},
643 {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}},
644 {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}},
645 {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}},
646 {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}},
647 {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}},
648 {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}},
649 {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}},
650 {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}},
651
652 {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}},
653 {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}},
654 {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}},
655 {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}},
656
657 {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}},
658 {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}},
659 {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}},
660 {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}}
661 ]
662 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400663 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700664 return pref.ProtoMessage(new(MapScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800665 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400666}}
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000667
Joe Tsai378c1322019-04-25 23:48:08 -0700668func (m *MapScalars) ProtoReflect() pref.Message { return mapScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800669
670func TestMapScalars(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -0700671 empty := new(MapScalars).ProtoReflect()
672 want := (&MapScalars{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000673 KeyBools: map[bool]string{true: "true", false: "false"},
674 KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
675 KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
676 KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
677 KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
678 KeyStrings: map[string]string{"": "", "foo": "bar"},
679
680 ValBools: map[string]bool{"true": true, "false": false},
681 ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
682 ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
683 ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
684 ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
685 ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
686 ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
687 ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
688 ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
689 ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
690 ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
691
692 MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
693 MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
694 MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
695 MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
696
697 MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
698 MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
699 MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
700 MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
Joe Tsai378c1322019-04-25 23:48:08 -0700701 }).ProtoReflect()
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000702
Joe Tsai378c1322019-04-25 23:48:08 -0700703 testMessage(t, nil, new(MapScalars).ProtoReflect(), messageOps{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000704 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 -0700705 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)},
706 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)},
707 mapFieldsMutable{
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000708 2: {
709 lenMap(0),
710 hasMap{int32(0): false, int32(-1): false, int32(2): false},
711 setMap{int32(0): V("zero")},
712 lenMap(1),
713 hasMap{int32(0): true, int32(-1): false, int32(2): false},
714 setMap{int32(-1): V("one")},
715 lenMap(2),
716 hasMap{int32(0): true, int32(-1): true, int32(2): false},
717 setMap{int32(2): V("two")},
718 lenMap(3),
719 hasMap{int32(0): true, int32(-1): true, int32(2): true},
720 },
721 4: {
722 setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
Joe Tsai378c1322019-04-25 23:48:08 -0700723 equalMap{getField(want, 4).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000724 },
725 6: {
Joe Tsai87b955b2018-11-14 21:59:49 -0800726 clearMap{"noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000727 setMap{"foo": V("bar")},
728 setMap{"": V("empty")},
729 getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
730 setMap{"": V(""), "extra": V("extra")},
Joe Tsai87b955b2018-11-14 21:59:49 -0800731 clearMap{"extra", "noexist"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000732 },
733 8: {
Joe Tsai378c1322019-04-25 23:48:08 -0700734 equalMap{getField(empty, 8).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000735 setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
736 },
737 10: {
738 setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
739 lenMap(3),
Joe Tsai378c1322019-04-25 23:48:08 -0700740 equalMap{getField(want, 10).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000741 getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
742 },
743 12: {
744 setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800745 clearMap{"e", "phi"},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000746 rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
747 },
748 14: {
Joe Tsai378c1322019-04-25 23:48:08 -0700749 equalMap{getField(empty, 14).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000750 setMap{"s1": V("s1"), "s2": V("s2")},
751 },
752 16: {
753 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700754 equalMap{getField(want, 16).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000755 },
756 18: {
757 hasMap{"s1": false, "s2": false, "s3": false},
758 setMap{"s1": V("s1"), "s2": V("s2")},
759 hasMap{"s1": true, "s2": true, "s3": false},
760 },
761 20: {
Joe Tsai378c1322019-04-25 23:48:08 -0700762 equalMap{getField(empty, 20).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000763 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
764 },
765 22: {
766 rangeMap{},
767 setMap{"s1": V("s1"), "s2": V("s2")},
768 rangeMap{"s1": V("s1"), "s2": V("s2")},
769 lenMap(2),
770 },
771 24: {
772 setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
Joe Tsai378c1322019-04-25 23:48:08 -0700773 equalMap{getField(want, 24).Map()},
Joe Tsaibbfaeb72018-10-17 00:27:21 +0000774 },
775 },
776 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 -0800777 equalMessage{want},
778 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},
779 equalMessage{empty},
Joe Tsai91e14662018-09-13 13:24:35 -0700780 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800781
782 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700783 testMessage(t, nil, (*MapScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800784 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},
785 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)}},
786 })
Joe Tsai91e14662018-09-13 13:24:35 -0700787}
Joe Tsaifa02f4e2018-09-12 16:20:37 -0700788
Joe Tsai87b955b2018-11-14 21:59:49 -0800789type OneofScalars struct {
790 Union isOneofScalars_Union `protobuf_oneof:"union"`
791}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700792
Joe Tsaib2f66be2019-05-22 00:42:45 -0400793var oneofScalarsType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(OneofScalars)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700794 MessageDescriptor: mustMakeMessageDesc("oneof-scalars.proto", pref.Proto2, "", `
795 name: "OneofScalars"
796 field: [
797 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true" oneof_index:0},
798 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2" oneof_index:0},
799 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3" oneof_index:0},
800 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4" oneof_index:0},
801 {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5" oneof_index:0},
802 {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6" oneof_index:0},
803 {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7" oneof_index:0},
804 {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8" oneof_index:0},
805 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9" oneof_index:0},
806 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"10" oneof_index:0},
807 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11" oneof_index:0},
808 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"12" oneof_index:0},
809 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"13" oneof_index:0}
810 ]
811 oneof_decl: [{name:"union"}]
812 `, nil),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400813 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -0700814 return pref.ProtoMessage(new(OneofScalars)).ProtoReflect()
Joe Tsaif0c01e42018-11-06 13:05:20 -0800815 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400816}}
Joe Tsaif0c01e42018-11-06 13:05:20 -0800817
Joe Tsai378c1322019-04-25 23:48:08 -0700818func (m *OneofScalars) ProtoReflect() pref.Message { return oneofScalarsType.MessageOf(m) }
Joe Tsaif0c01e42018-11-06 13:05:20 -0800819
Joe Tsaif18ab532018-11-27 17:25:04 -0800820func (*OneofScalars) XXX_OneofWrappers() []interface{} {
821 return []interface{}{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700822 (*OneofScalars_Bool)(nil),
823 (*OneofScalars_Int32)(nil),
824 (*OneofScalars_Int64)(nil),
825 (*OneofScalars_Uint32)(nil),
826 (*OneofScalars_Uint64)(nil),
827 (*OneofScalars_Float32)(nil),
828 (*OneofScalars_Float64)(nil),
829 (*OneofScalars_String)(nil),
830 (*OneofScalars_StringA)(nil),
831 (*OneofScalars_StringB)(nil),
832 (*OneofScalars_Bytes)(nil),
833 (*OneofScalars_BytesA)(nil),
834 (*OneofScalars_BytesB)(nil),
835 }
836}
837
Joe Tsai87b955b2018-11-14 21:59:49 -0800838type (
839 isOneofScalars_Union interface {
840 isOneofScalars_Union()
841 }
842 OneofScalars_Bool struct {
843 Bool bool `protobuf:"1"`
844 }
845 OneofScalars_Int32 struct {
846 Int32 MyInt32 `protobuf:"2"`
847 }
848 OneofScalars_Int64 struct {
849 Int64 int64 `protobuf:"3"`
850 }
851 OneofScalars_Uint32 struct {
852 Uint32 MyUint32 `protobuf:"4"`
853 }
854 OneofScalars_Uint64 struct {
855 Uint64 uint64 `protobuf:"5"`
856 }
857 OneofScalars_Float32 struct {
858 Float32 MyFloat32 `protobuf:"6"`
859 }
860 OneofScalars_Float64 struct {
861 Float64 float64 `protobuf:"7"`
862 }
863 OneofScalars_String struct {
864 String string `protobuf:"8"`
865 }
866 OneofScalars_StringA struct {
867 StringA []byte `protobuf:"9"`
868 }
869 OneofScalars_StringB struct {
870 StringB MyString `protobuf:"10"`
871 }
872 OneofScalars_Bytes struct {
873 Bytes []byte `protobuf:"11"`
874 }
875 OneofScalars_BytesA struct {
876 BytesA string `protobuf:"12"`
877 }
878 OneofScalars_BytesB struct {
879 BytesB MyBytes `protobuf:"13"`
880 }
881)
882
Joe Tsai2c870bb2018-10-17 11:46:52 -0700883func (*OneofScalars_Bool) isOneofScalars_Union() {}
884func (*OneofScalars_Int32) isOneofScalars_Union() {}
885func (*OneofScalars_Int64) isOneofScalars_Union() {}
886func (*OneofScalars_Uint32) isOneofScalars_Union() {}
887func (*OneofScalars_Uint64) isOneofScalars_Union() {}
888func (*OneofScalars_Float32) isOneofScalars_Union() {}
889func (*OneofScalars_Float64) isOneofScalars_Union() {}
890func (*OneofScalars_String) isOneofScalars_Union() {}
891func (*OneofScalars_StringA) isOneofScalars_Union() {}
892func (*OneofScalars_StringB) isOneofScalars_Union() {}
893func (*OneofScalars_Bytes) isOneofScalars_Union() {}
894func (*OneofScalars_BytesA) isOneofScalars_Union() {}
895func (*OneofScalars_BytesB) isOneofScalars_Union() {}
896
897func TestOneofs(t *testing.T) {
Joe Tsaif0c01e42018-11-06 13:05:20 -0800898 empty := &OneofScalars{}
899 want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
900 want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
901 want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
902 want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
903 want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
904 want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
905 want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
906 want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
907 want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
908 want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
909 want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
910 want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
911 want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
Joe Tsai2c870bb2018-10-17 11:46:52 -0700912
Joe Tsai378c1322019-04-25 23:48:08 -0700913 testMessage(t, nil, new(OneofScalars).ProtoReflect(), messageOps{
Joe Tsai2c870bb2018-10-17 11:46:52 -0700914 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},
915 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 -0700916 whichOneofs{"union": 0},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700917
Joe Tsai378c1322019-04-25 23:48:08 -0700918 setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1.ProtoReflect()},
919 setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2.ProtoReflect()},
920 setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3.ProtoReflect()},
921 setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4.ProtoReflect()},
922 setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5.ProtoReflect()},
923 setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6.ProtoReflect()},
924 setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7.ProtoReflect()},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700925
926 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 -0700927 whichOneofs{"union": 7},
Joe Tsai4ec39c72019-04-03 13:40:53 -0700928
Joe Tsai378c1322019-04-25 23:48:08 -0700929 setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8.ProtoReflect()},
930 setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9.ProtoReflect()},
931 setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10.ProtoReflect()},
932 setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11.ProtoReflect()},
933 setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12.ProtoReflect()},
934 setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700935
936 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},
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("130"))},
Joe Tsai87b955b2018-11-14 21:59:49 -0800938 clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Joe Tsai378c1322019-04-25 23:48:08 -0700939 whichOneofs{"union": 13},
940 equalMessage{want13.ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -0800941 clearFields{13},
Joe Tsai378c1322019-04-25 23:48:08 -0700942 whichOneofs{"union": 0},
943 equalMessage{empty.ProtoReflect()},
Joe Tsai2c870bb2018-10-17 11:46:52 -0700944 })
Joe Tsai6cf80c42018-12-01 04:57:09 -0800945
946 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -0700947 testMessage(t, nil, (*OneofScalars)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -0800948 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},
949 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"))},
950 })
Joe Tsai2c870bb2018-10-17 11:46:52 -0700951}
952
Joe Tsai87b955b2018-11-14 21:59:49 -0800953type EnumProto2 int32
954
Joe Tsaib2f66be2019-05-22 00:42:45 -0400955var enumProto2Type = &prototype.Enum{
Joe Tsaid8881392019-06-06 13:01:53 -0700956 EnumDescriptor: mustMakeEnumDesc("enum2.proto", pref.Proto2, `
957 name: "EnumProto2"
958 value: [{name:"DEAD" number:0xdead}, {name:"BEEF" number:0xbeef}]
959 `),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400960 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800961 return EnumProto2(n)
962 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400963}
Joe Tsai87b955b2018-11-14 21:59:49 -0800964
Joe Tsai0fc49f82019-05-01 12:29:25 -0700965func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Type.Descriptor() }
Joe Tsaid4211502019-07-02 14:58:02 -0700966func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700967func (e EnumProto2) Enum() *EnumProto2 { return &e }
968func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800969
970type EnumProto3 int32
971
Joe Tsaib2f66be2019-05-22 00:42:45 -0400972var enumProto3Type = &prototype.Enum{
Joe Tsaid8881392019-06-06 13:01:53 -0700973 EnumDescriptor: mustMakeEnumDesc("enum3.proto", pref.Proto3, `
974 name: "EnumProto3",
975 value: [{name:"ALPHA" number:0}, {name:"BRAVO" number:1}]
976 `),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400977 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800978 return EnumProto3(n)
979 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400980}
Joe Tsai87b955b2018-11-14 21:59:49 -0800981
Joe Tsai0fc49f82019-05-01 12:29:25 -0700982func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Type.Descriptor() }
Joe Tsaid4211502019-07-02 14:58:02 -0700983func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700984func (e EnumProto3) Enum() *EnumProto3 { return &e }
985func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800986
987type EnumMessages struct {
988 EnumP2 *EnumProto2 `protobuf:"1"`
989 EnumP3 *EnumProto3 `protobuf:"2"`
990 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
991 MessageCycle *EnumMessages `protobuf:"4"`
992 EnumList []EnumProto2 `protobuf:"5"`
993 MessageList []*ScalarProto2 `protobuf:"6"`
994 EnumMap map[string]EnumProto3 `protobuf:"7"`
995 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
996 Union isEnumMessages_Union `protobuf_oneof:"union"`
997}
998
Joe Tsaib2f66be2019-05-22 00:42:45 -0400999var enumMessagesType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(EnumMessages)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -07001000 MessageDescriptor: mustMakeMessageDesc("enum-messages.proto", pref.Proto2, `
1001 dependency: ["enum2.proto", "enum3.proto", "scalar2.proto", "scalar3.proto", "proto2.v1.0.0-20180125-92554152/test.proto"]
1002 `, `
1003 name: "EnumMessages"
1004 field: [
1005 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" default_value:"BEEF"},
1006 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" default_value:"BRAVO"},
Herbie Ong20aefe92019-06-24 19:21:46 -07001007 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".google.golang.org.proto2_20180125.Message"},
1008 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".EnumMessages"},
Joe Tsaid8881392019-06-06 13:01:53 -07001009 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_ENUM type_name:".EnumProto2"},
1010 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".ScalarProto2"},
1011 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F7Entry"},
1012 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F8Entry"},
1013 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" oneof_index:0 default_value:"BEEF"},
1014 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" oneof_index:0 default_value:"BRAVO"},
1015 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto2" oneof_index:0},
1016 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto3" oneof_index:0}
1017 ]
1018 oneof_decl: [{name:"union"}]
1019 nested_type: [
1020 {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}},
1021 {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}}
1022 ]
1023 `, protoregistry.NewFiles(
1024 EnumProto2(0).Descriptor().ParentFile(),
1025 EnumProto3(0).Descriptor().ParentFile(),
1026 ((*ScalarProto2)(nil)).ProtoReflect().Descriptor().ParentFile(),
1027 ((*ScalarProto3)(nil)).ProtoReflect().Descriptor().ParentFile(),
1028 pimpl.Export{}.MessageDescriptorOf((*proto2_20180125.Message)(nil)).ParentFile(),
1029 )),
Joe Tsaib2f66be2019-05-22 00:42:45 -04001030 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -07001031 return pref.ProtoMessage(new(EnumMessages)).ProtoReflect()
Joe Tsai87b955b2018-11-14 21:59:49 -08001032 },
Joe Tsaib2f66be2019-05-22 00:42:45 -04001033}}
Joe Tsai87b955b2018-11-14 21:59:49 -08001034
Joe Tsai378c1322019-04-25 23:48:08 -07001035func (m *EnumMessages) ProtoReflect() pref.Message { return enumMessagesType.MessageOf(m) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001036
Joe Tsaif18ab532018-11-27 17:25:04 -08001037func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1038 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001039 (*EnumMessages_OneofE2)(nil),
1040 (*EnumMessages_OneofE3)(nil),
1041 (*EnumMessages_OneofM2)(nil),
1042 (*EnumMessages_OneofM3)(nil),
1043 }
1044}
1045
1046type (
1047 isEnumMessages_Union interface {
1048 isEnumMessages_Union()
1049 }
1050 EnumMessages_OneofE2 struct {
1051 OneofE2 EnumProto2 `protobuf:"9"`
1052 }
1053 EnumMessages_OneofE3 struct {
1054 OneofE3 EnumProto3 `protobuf:"10"`
1055 }
1056 EnumMessages_OneofM2 struct {
1057 OneofM2 *ScalarProto2 `protobuf:"11"`
1058 }
1059 EnumMessages_OneofM3 struct {
1060 OneofM3 *ScalarProto3 `protobuf:"12"`
1061 }
1062)
1063
1064func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1065func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1066func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1067func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1068
1069func TestEnumMessages(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -07001070 emptyL := pimpl.Export{}.MessageOf(new(proto2_20180125.Message))
1071 emptyM := new(EnumMessages).ProtoReflect()
1072 emptyM2 := new(ScalarProto2).ProtoReflect()
1073 emptyM3 := new(ScalarProto3).ProtoReflect()
1074
Damien Neila8a2cea2019-07-10 16:17:16 -07001075 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)})
Joe Tsai378c1322019-04-25 23:48:08 -07001076 wantM := (&EnumMessages{EnumP2: EnumProto2(1234).Enum()}).ProtoReflect()
Damien Neila8a2cea2019-07-10 16:17:16 -07001077 wantM2a := &ScalarProto2{Float32: proto.Float32(math.Pi)}
1078 wantM2b := &ScalarProto2{Float32: proto.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001079 wantM3a := &ScalarProto3{Float32: math.Pi}
1080 wantM3b := &ScalarProto3{Float32: math.Ln2}
1081
Joe Tsai378c1322019-04-25 23:48:08 -07001082 wantList5 := getField((&EnumMessages{EnumList: []EnumProto2{333, 222}}).ProtoReflect(), 5)
1083 wantList6 := getField((&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).ProtoReflect(), 6)
Joe Tsai87b955b2018-11-14 21:59:49 -08001084
Joe Tsai378c1322019-04-25 23:48:08 -07001085 wantMap7 := getField((&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).ProtoReflect(), 7)
1086 wantMap8 := getField((&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).ProtoReflect(), 8)
Joe Tsai87b955b2018-11-14 21:59:49 -08001087
Joe Tsai378c1322019-04-25 23:48:08 -07001088 testMessage(t, nil, new(EnumMessages).ProtoReflect(), messageOps{
Joe Tsai87b955b2018-11-14 21:59:49 -08001089 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 -07001090 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 -08001091
1092 // Test singular enums.
1093 setFields{1: VE(0xdead), 2: VE(0)},
1094 getFields{1: VE(0xdead), 2: VE(0)},
1095 hasFields{1: true, 2: true},
1096
1097 // Test singular messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001098 messageFieldsMutable{3: messageOps{setFields{109: V(float32(math.E))}}},
1099 messageFieldsMutable{4: messageOps{setFields{1: VE(1234)}}},
Joe Tsai87b955b2018-11-14 21:59:49 -08001100 getFields{3: V(wantL), 4: V(wantM)},
1101 clearFields{3, 4},
1102 hasFields{3: false, 4: false},
1103 setFields{3: V(wantL), 4: V(wantM)},
1104 hasFields{3: true, 4: true},
1105
1106 // Test list of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001107 listFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001108 5: listOps{
1109 appendList{VE(111), VE(222)},
1110 setList{0: VE(333)},
1111 getList{0: VE(333), 1: VE(222)},
1112 lenList(2),
1113 },
1114 6: listOps{
1115 appendMessageList{setFields{4: V(uint32(1e6))}},
1116 appendMessageList{setFields{6: V(float32(math.Phi))}},
Joe Tsai378c1322019-04-25 23:48:08 -07001117 setList{0: V(wantM2a.ProtoReflect())},
1118 getList{0: V(wantM2a.ProtoReflect()), 1: V(wantM2b.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001119 },
1120 },
1121 getFields{5: wantList5, 6: wantList6},
1122 hasFields{5: true, 6: true},
1123 listFields{5: listOps{truncList(0)}},
1124 hasFields{5: false, 6: true},
1125
1126 // Test maps of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001127 mapFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001128 7: mapOps{
1129 setMap{"one": VE(1), "two": VE(2)},
1130 hasMap{"one": true, "two": true, "three": false},
1131 lenMap(2),
1132 },
1133 8: mapOps{
1134 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
Joe Tsai378c1322019-04-25 23:48:08 -07001135 setMap{"ln2": V(wantM3b.ProtoReflect())},
1136 getMap{"pi": V(wantM3a.ProtoReflect()), "ln2": V(wantM3b.ProtoReflect()), "none": V(nil)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001137 lenMap(2),
1138 },
1139 },
1140 getFields{7: wantMap7, 8: wantMap8},
1141 hasFields{7: true, 8: true},
1142 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1143 hasFields{7: true, 8: false},
1144
1145 // Test oneofs of enums and messages.
1146 setFields{9: VE(0xdead)},
1147 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1148 setFields{10: VE(0)},
1149 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001150 messageFieldsMutable{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1151 getFields{11: V(wantM2a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001152 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001153 messageFieldsMutable{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1154 getFields{12: V(wantM3a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001155 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1156
1157 // Check entire message.
Joe Tsai378c1322019-04-25 23:48:08 -07001158 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a.ProtoReflect())},
1159 equalMessage{(&EnumMessages{
Joe Tsai87b955b2018-11-14 21:59:49 -08001160 EnumP2: EnumProto2(0xdead).Enum(),
1161 EnumP3: EnumProto3(0).Enum(),
Damien Neila8a2cea2019-07-10 16:17:16 -07001162 MessageLegacy: &proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)},
Joe Tsai378c1322019-04-25 23:48:08 -07001163 MessageCycle: wantM.Interface().(*EnumMessages),
Joe Tsai87b955b2018-11-14 21:59:49 -08001164 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1165 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1166 Union: &EnumMessages_OneofM3{wantM3a},
Joe Tsai378c1322019-04-25 23:48:08 -07001167 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001168 clearFields{1, 2, 3, 4, 6, 7, 12},
Joe Tsai378c1322019-04-25 23:48:08 -07001169 equalMessage{new(EnumMessages).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001170 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001171
1172 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -07001173 testMessage(t, nil, (*EnumMessages)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -08001174 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 -07001175 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 -08001176 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1177 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1178 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001179}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001180
Joe Tsai91e14662018-09-13 13:24:35 -07001181var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001182 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
Joe Tsaid8881392019-06-06 13:01:53 -07001183 mx := pimpl.Export{}.MessageOf(x).Interface()
1184 my := pimpl.Export{}.MessageOf(y).Interface()
1185 return proto.Equal(mx, my)
Joe Tsai91e14662018-09-13 13:24:35 -07001186 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001187 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
Joe Tsai378c1322019-04-25 23:48:08 -07001188 switch v := pv.Interface().(type) {
1189 case pref.Message:
1190 out := make(map[pref.FieldNumber]pref.Value)
1191 v.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1192 out[fd.Number()] = v
1193 return true
1194 })
1195 return out
1196 case pref.List:
1197 var out []pref.Value
1198 for i := 0; i < v.Len(); i++ {
1199 out = append(out, v.Get(i))
1200 }
1201 return out
1202 case pref.Map:
1203 out := make(map[interface{}]pref.Value)
1204 v.Range(func(k pref.MapKey, v pref.Value) bool {
1205 out[k.Interface()] = v
1206 return true
1207 })
1208 return out
1209 default:
1210 return v
1211 }
Joe Tsai91e14662018-09-13 13:24:35 -07001212 }),
1213 cmpopts.EquateNaNs(),
1214}
1215
1216func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
Joe Tsai378c1322019-04-25 23:48:08 -07001217 fieldDescs := m.Descriptor().Fields()
1218 oneofDescs := m.Descriptor().Oneofs()
Joe Tsai91e14662018-09-13 13:24:35 -07001219 for i, op := range tt {
1220 p.Push(i)
1221 switch op := op.(type) {
1222 case equalMessage:
Joe Tsai378c1322019-04-25 23:48:08 -07001223 if diff := cmp.Diff(V(op.Message), V(m), cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001224 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1225 }
1226 case hasFields:
1227 got := map[pref.FieldNumber]bool{}
1228 want := map[pref.FieldNumber]bool(op)
1229 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001230 fd := fieldDescs.ByNumber(n)
1231 got[n] = m.Has(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001232 }
1233 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001234 t.Errorf("operation %v, Message.Has mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001235 }
1236 case getFields:
1237 got := map[pref.FieldNumber]pref.Value{}
1238 want := map[pref.FieldNumber]pref.Value(op)
1239 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001240 fd := fieldDescs.ByNumber(n)
1241 got[n] = m.Get(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001242 }
1243 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001244 t.Errorf("operation %v, Message.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001245 }
1246 case setFields:
1247 for n, v := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001248 fd := fieldDescs.ByNumber(n)
1249 m.Set(fd, v)
Joe Tsai91e14662018-09-13 13:24:35 -07001250 }
1251 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001252 for _, n := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001253 fd := fieldDescs.ByNumber(n)
1254 m.Clear(fd)
Joe Tsai87b955b2018-11-14 21:59:49 -08001255 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001256 case whichOneofs:
1257 got := map[pref.Name]pref.FieldNumber{}
1258 want := map[pref.Name]pref.FieldNumber(op)
1259 for s := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001260 od := oneofDescs.ByName(s)
1261 fd := m.WhichOneof(od)
1262 if fd == nil {
1263 got[s] = 0
1264 } else {
1265 got[s] = fd.Number()
1266 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001267 }
1268 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001269 t.Errorf("operation %v, Message.WhichOneof mismatch (-want, +got):\n%s", p, diff)
Joe Tsai4ec39c72019-04-03 13:40:53 -07001270 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001271 case messageFields:
1272 for n, tt := range op {
1273 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001274 fd := fieldDescs.ByNumber(n)
1275 testMessage(t, p, m.Get(fd).Message(), tt)
1276 p.Pop()
1277 }
1278 case messageFieldsMutable:
1279 for n, tt := range op {
1280 p.Push(int(n))
1281 fd := fieldDescs.ByNumber(n)
1282 testMessage(t, p, m.Mutable(fd).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001283 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001284 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001285 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001286 for n, tt := range op {
1287 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001288 fd := fieldDescs.ByNumber(n)
1289 testLists(t, p, m.Get(fd).List(), tt)
1290 p.Pop()
1291 }
1292 case listFieldsMutable:
1293 for n, tt := range op {
1294 p.Push(int(n))
1295 fd := fieldDescs.ByNumber(n)
1296 testLists(t, p, m.Mutable(fd).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001297 p.Pop()
1298 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001299 case mapFields:
1300 for n, tt := range op {
1301 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001302 fd := fieldDescs.ByNumber(n)
1303 testMaps(t, p, m.Get(fd).Map(), tt)
1304 p.Pop()
1305 }
1306 case mapFieldsMutable:
1307 for n, tt := range op {
1308 p.Push(int(n))
1309 fd := fieldDescs.ByNumber(n)
1310 testMaps(t, p, m.Mutable(fd).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001311 p.Pop()
1312 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001313 case rangeFields:
1314 got := map[pref.FieldNumber]pref.Value{}
1315 want := map[pref.FieldNumber]pref.Value(op)
Joe Tsai378c1322019-04-25 23:48:08 -07001316 m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1317 got[fd.Number()] = v
Joe Tsai87b955b2018-11-14 21:59:49 -08001318 return true
1319 })
1320 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001321 t.Errorf("operation %v, Message.Range mismatch (-want, +got):\n%s", p, diff)
Joe Tsai87b955b2018-11-14 21:59:49 -08001322 }
Joe Tsai91e14662018-09-13 13:24:35 -07001323 default:
1324 t.Fatalf("operation %v, invalid operation: %T", p, op)
1325 }
1326 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001327 }
1328}
Joe Tsai91e14662018-09-13 13:24:35 -07001329
Joe Tsai4b7aff62018-11-14 14:05:19 -08001330func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001331 for i, op := range tt {
1332 p.Push(i)
1333 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001334 case equalList:
Joe Tsai378c1322019-04-25 23:48:08 -07001335 if diff := cmp.Diff(V(op.List), V(v), cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001336 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001337 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001338 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001339 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001340 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001341 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001342 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001343 got := map[int]pref.Value{}
1344 want := map[int]pref.Value(op)
1345 for n := range want {
1346 got[n] = v.Get(n)
1347 }
1348 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001349 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001350 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001351 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001352 for n, e := range op {
1353 v.Set(n, e)
1354 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001355 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001356 for _, e := range op {
1357 v.Append(e)
1358 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001359 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001360 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001361 v.Append(V(m))
1362 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001363 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001364 v.Truncate(int(op))
1365 default:
1366 t.Fatalf("operation %v, invalid operation: %T", p, op)
1367 }
1368 p.Pop()
1369 }
1370}
1371
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001372func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1373 for i, op := range tt {
1374 p.Push(i)
1375 switch op := op.(type) {
1376 case equalMap:
Joe Tsai378c1322019-04-25 23:48:08 -07001377 if diff := cmp.Diff(V(op.Map), V(m), cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001378 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1379 }
1380 case lenMap:
1381 if got, want := m.Len(), int(op); got != want {
1382 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1383 }
1384 case hasMap:
1385 got := map[interface{}]bool{}
1386 want := map[interface{}]bool(op)
1387 for k := range want {
1388 got[k] = m.Has(V(k).MapKey())
1389 }
1390 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1391 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1392 }
1393 case getMap:
1394 got := map[interface{}]pref.Value{}
1395 want := map[interface{}]pref.Value(op)
1396 for k := range want {
1397 got[k] = m.Get(V(k).MapKey())
1398 }
1399 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1400 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1401 }
1402 case setMap:
1403 for k, v := range op {
1404 m.Set(V(k).MapKey(), v)
1405 }
1406 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001407 for _, k := range op {
1408 m.Clear(V(k).MapKey())
1409 }
1410 case messageMap:
1411 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001412 mk := V(k).MapKey()
1413 if !m.Has(mk) {
1414 m.Set(mk, V(m.NewMessage()))
1415 }
1416 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001417 }
1418 case rangeMap:
1419 got := map[interface{}]pref.Value{}
1420 want := map[interface{}]pref.Value(op)
1421 m.Range(func(k pref.MapKey, v pref.Value) bool {
1422 got[k.Interface()] = v
1423 return true
1424 })
1425 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1426 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1427 }
1428 default:
1429 t.Fatalf("operation %v, invalid operation: %T", p, op)
1430 }
1431 p.Pop()
1432 }
1433}
1434
Joe Tsai378c1322019-04-25 23:48:08 -07001435func getField(m pref.Message, n pref.FieldNumber) pref.Value {
1436 fd := m.Descriptor().Fields().ByNumber(n)
1437 return m.Get(fd)
1438}
1439
Joe Tsai91e14662018-09-13 13:24:35 -07001440type path []int
1441
1442func (p *path) Push(i int) { *p = append(*p, i) }
1443func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1444func (p path) String() string {
1445 var ss []string
1446 for _, i := range p {
1447 ss = append(ss, fmt.Sprint(i))
1448 }
1449 return strings.Join(ss, ".")
1450}
Joe Tsai82760ce2019-06-20 03:09:57 -07001451
1452// The MessageState implementation makes the assumption that when a
1453// concrete message is unsafe casted as a *MessageState, the Go GC does
1454// not reclaim the memory for the remainder of the concrete message.
1455func TestUnsafeAssumptions(t *testing.T) {
1456 if !pimpl.UnsafeEnabled {
1457 t.Skip()
1458 }
1459
1460 var wg sync.WaitGroup
1461 for i := 0; i < 10; i++ {
1462 wg.Add(1)
1463 go func() {
1464 var ms [10]pref.Message
1465
1466 // Store the message only in its reflective form.
1467 // Trigger the GC after each iteration.
1468 for j := 0; j < 10; j++ {
1469 ms[j] = (&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -07001470 OptionalInt32: proto.Int32(int32(j)),
1471 OptionalFloat: proto.Float32(float32(j)),
Joe Tsai82760ce2019-06-20 03:09:57 -07001472 RepeatedInt32: []int32{int32(j)},
1473 RepeatedFloat: []float32{float32(j)},
Damien Neila8a2cea2019-07-10 16:17:16 -07001474 DefaultInt32: proto.Int32(int32(j)),
1475 DefaultFloat: proto.Float32(float32(j)),
Joe Tsai82760ce2019-06-20 03:09:57 -07001476 }).ProtoReflect()
1477 runtime.GC()
1478 }
1479
1480 // Convert the reflective form back into a concrete form.
1481 // Verify that the values written previously are still the same.
1482 for j := 0; j < 10; j++ {
1483 switch m := ms[j].Interface().(*testpb.TestAllTypes); {
1484 case m.GetOptionalInt32() != int32(j):
1485 case m.GetOptionalFloat() != float32(j):
1486 case m.GetRepeatedInt32()[0] != int32(j):
1487 case m.GetRepeatedFloat()[0] != float32(j):
1488 case m.GetDefaultInt32() != int32(j):
1489 case m.GetDefaultFloat() != float32(j):
1490 default:
1491 continue
1492 }
1493 t.Error("memory corrupted detected")
1494 }
1495 defer wg.Done()
1496 }()
1497 }
1498 wg.Wait()
1499}
1500
1501func BenchmarkName(b *testing.B) {
1502 var sink pref.FullName
1503 b.Run("Value", func(b *testing.B) {
1504 b.ReportAllocs()
1505 m := new(descriptorpb.FileDescriptorProto)
1506 for i := 0; i < b.N; i++ {
1507 sink = m.ProtoReflect().Descriptor().FullName()
1508 }
1509 })
1510 b.Run("Nil", func(b *testing.B) {
1511 b.ReportAllocs()
1512 m := (*descriptorpb.FileDescriptorProto)(nil)
1513 for i := 0; i < b.N; i++ {
1514 sink = m.ProtoReflect().Descriptor().FullName()
1515 }
1516 })
1517 runtime.KeepAlive(sink)
1518}