blob: 75c44605ae64a5abe42922af2848d2396720b5b9 [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() }
966func (e EnumProto2) Enum() *EnumProto2 { return &e }
967func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800968
969type EnumProto3 int32
970
Joe Tsaib2f66be2019-05-22 00:42:45 -0400971var enumProto3Type = &prototype.Enum{
Joe Tsaid8881392019-06-06 13:01:53 -0700972 EnumDescriptor: mustMakeEnumDesc("enum3.proto", pref.Proto3, `
973 name: "EnumProto3",
974 value: [{name:"ALPHA" number:0}, {name:"BRAVO" number:1}]
975 `),
Joe Tsaib2f66be2019-05-22 00:42:45 -0400976 NewEnum: func(n pref.EnumNumber) pref.Enum {
Joe Tsai87b955b2018-11-14 21:59:49 -0800977 return EnumProto3(n)
978 },
Joe Tsaib2f66be2019-05-22 00:42:45 -0400979}
Joe Tsai87b955b2018-11-14 21:59:49 -0800980
Joe Tsai0fc49f82019-05-01 12:29:25 -0700981func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Type.Descriptor() }
982func (e EnumProto3) Enum() *EnumProto3 { return &e }
983func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
Joe Tsai87b955b2018-11-14 21:59:49 -0800984
985type EnumMessages struct {
986 EnumP2 *EnumProto2 `protobuf:"1"`
987 EnumP3 *EnumProto3 `protobuf:"2"`
988 MessageLegacy *proto2_20180125.Message `protobuf:"3"`
989 MessageCycle *EnumMessages `protobuf:"4"`
990 EnumList []EnumProto2 `protobuf:"5"`
991 MessageList []*ScalarProto2 `protobuf:"6"`
992 EnumMap map[string]EnumProto3 `protobuf:"7"`
993 MessageMap map[string]*ScalarProto3 `protobuf:"8"`
994 Union isEnumMessages_Union `protobuf_oneof:"union"`
995}
996
Joe Tsaib2f66be2019-05-22 00:42:45 -0400997var enumMessagesType = pimpl.MessageInfo{GoType: reflect.TypeOf(new(EnumMessages)), PBType: &prototype.Message{
Joe Tsaid8881392019-06-06 13:01:53 -0700998 MessageDescriptor: mustMakeMessageDesc("enum-messages.proto", pref.Proto2, `
999 dependency: ["enum2.proto", "enum3.proto", "scalar2.proto", "scalar3.proto", "proto2.v1.0.0-20180125-92554152/test.proto"]
1000 `, `
1001 name: "EnumMessages"
1002 field: [
1003 {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" default_value:"BEEF"},
1004 {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" default_value:"BRAVO"},
Herbie Ong20aefe92019-06-24 19:21:46 -07001005 {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".google.golang.org.proto2_20180125.Message"},
1006 {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".EnumMessages"},
Joe Tsaid8881392019-06-06 13:01:53 -07001007 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_ENUM type_name:".EnumProto2"},
1008 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".ScalarProto2"},
1009 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F7Entry"},
1010 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F8Entry"},
1011 {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" oneof_index:0 default_value:"BEEF"},
1012 {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" oneof_index:0 default_value:"BRAVO"},
1013 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto2" oneof_index:0},
1014 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto3" oneof_index:0}
1015 ]
1016 oneof_decl: [{name:"union"}]
1017 nested_type: [
1018 {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}},
1019 {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}}
1020 ]
1021 `, protoregistry.NewFiles(
1022 EnumProto2(0).Descriptor().ParentFile(),
1023 EnumProto3(0).Descriptor().ParentFile(),
1024 ((*ScalarProto2)(nil)).ProtoReflect().Descriptor().ParentFile(),
1025 ((*ScalarProto3)(nil)).ProtoReflect().Descriptor().ParentFile(),
1026 pimpl.Export{}.MessageDescriptorOf((*proto2_20180125.Message)(nil)).ParentFile(),
1027 )),
Joe Tsaib2f66be2019-05-22 00:42:45 -04001028 NewMessage: func() pref.Message {
Joe Tsai378c1322019-04-25 23:48:08 -07001029 return pref.ProtoMessage(new(EnumMessages)).ProtoReflect()
Joe Tsai87b955b2018-11-14 21:59:49 -08001030 },
Joe Tsaib2f66be2019-05-22 00:42:45 -04001031}}
Joe Tsai87b955b2018-11-14 21:59:49 -08001032
Joe Tsai378c1322019-04-25 23:48:08 -07001033func (m *EnumMessages) ProtoReflect() pref.Message { return enumMessagesType.MessageOf(m) }
Joe Tsai87b955b2018-11-14 21:59:49 -08001034
Joe Tsaif18ab532018-11-27 17:25:04 -08001035func (*EnumMessages) XXX_OneofWrappers() []interface{} {
1036 return []interface{}{
Joe Tsai87b955b2018-11-14 21:59:49 -08001037 (*EnumMessages_OneofE2)(nil),
1038 (*EnumMessages_OneofE3)(nil),
1039 (*EnumMessages_OneofM2)(nil),
1040 (*EnumMessages_OneofM3)(nil),
1041 }
1042}
1043
1044type (
1045 isEnumMessages_Union interface {
1046 isEnumMessages_Union()
1047 }
1048 EnumMessages_OneofE2 struct {
1049 OneofE2 EnumProto2 `protobuf:"9"`
1050 }
1051 EnumMessages_OneofE3 struct {
1052 OneofE3 EnumProto3 `protobuf:"10"`
1053 }
1054 EnumMessages_OneofM2 struct {
1055 OneofM2 *ScalarProto2 `protobuf:"11"`
1056 }
1057 EnumMessages_OneofM3 struct {
1058 OneofM3 *ScalarProto3 `protobuf:"12"`
1059 }
1060)
1061
1062func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
1063func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
1064func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
1065func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
1066
1067func TestEnumMessages(t *testing.T) {
Joe Tsai378c1322019-04-25 23:48:08 -07001068 emptyL := pimpl.Export{}.MessageOf(new(proto2_20180125.Message))
1069 emptyM := new(EnumMessages).ProtoReflect()
1070 emptyM2 := new(ScalarProto2).ProtoReflect()
1071 emptyM3 := new(ScalarProto3).ProtoReflect()
1072
Damien Neila8a2cea2019-07-10 16:17:16 -07001073 wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)})
Joe Tsai378c1322019-04-25 23:48:08 -07001074 wantM := (&EnumMessages{EnumP2: EnumProto2(1234).Enum()}).ProtoReflect()
Damien Neila8a2cea2019-07-10 16:17:16 -07001075 wantM2a := &ScalarProto2{Float32: proto.Float32(math.Pi)}
1076 wantM2b := &ScalarProto2{Float32: proto.Float32(math.Phi)}
Joe Tsai87b955b2018-11-14 21:59:49 -08001077 wantM3a := &ScalarProto3{Float32: math.Pi}
1078 wantM3b := &ScalarProto3{Float32: math.Ln2}
1079
Joe Tsai378c1322019-04-25 23:48:08 -07001080 wantList5 := getField((&EnumMessages{EnumList: []EnumProto2{333, 222}}).ProtoReflect(), 5)
1081 wantList6 := getField((&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).ProtoReflect(), 6)
Joe Tsai87b955b2018-11-14 21:59:49 -08001082
Joe Tsai378c1322019-04-25 23:48:08 -07001083 wantMap7 := getField((&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).ProtoReflect(), 7)
1084 wantMap8 := getField((&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).ProtoReflect(), 8)
Joe Tsai87b955b2018-11-14 21:59:49 -08001085
Joe Tsai378c1322019-04-25 23:48:08 -07001086 testMessage(t, nil, new(EnumMessages).ProtoReflect(), messageOps{
Joe Tsai87b955b2018-11-14 21:59:49 -08001087 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 -07001088 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 -08001089
1090 // Test singular enums.
1091 setFields{1: VE(0xdead), 2: VE(0)},
1092 getFields{1: VE(0xdead), 2: VE(0)},
1093 hasFields{1: true, 2: true},
1094
1095 // Test singular messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001096 messageFieldsMutable{3: messageOps{setFields{109: V(float32(math.E))}}},
1097 messageFieldsMutable{4: messageOps{setFields{1: VE(1234)}}},
Joe Tsai87b955b2018-11-14 21:59:49 -08001098 getFields{3: V(wantL), 4: V(wantM)},
1099 clearFields{3, 4},
1100 hasFields{3: false, 4: false},
1101 setFields{3: V(wantL), 4: V(wantM)},
1102 hasFields{3: true, 4: true},
1103
1104 // Test list of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001105 listFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001106 5: listOps{
1107 appendList{VE(111), VE(222)},
1108 setList{0: VE(333)},
1109 getList{0: VE(333), 1: VE(222)},
1110 lenList(2),
1111 },
1112 6: listOps{
1113 appendMessageList{setFields{4: V(uint32(1e6))}},
1114 appendMessageList{setFields{6: V(float32(math.Phi))}},
Joe Tsai378c1322019-04-25 23:48:08 -07001115 setList{0: V(wantM2a.ProtoReflect())},
1116 getList{0: V(wantM2a.ProtoReflect()), 1: V(wantM2b.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001117 },
1118 },
1119 getFields{5: wantList5, 6: wantList6},
1120 hasFields{5: true, 6: true},
1121 listFields{5: listOps{truncList(0)}},
1122 hasFields{5: false, 6: true},
1123
1124 // Test maps of enums and messages.
Joe Tsai378c1322019-04-25 23:48:08 -07001125 mapFieldsMutable{
Joe Tsai87b955b2018-11-14 21:59:49 -08001126 7: mapOps{
1127 setMap{"one": VE(1), "two": VE(2)},
1128 hasMap{"one": true, "two": true, "three": false},
1129 lenMap(2),
1130 },
1131 8: mapOps{
1132 messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
Joe Tsai378c1322019-04-25 23:48:08 -07001133 setMap{"ln2": V(wantM3b.ProtoReflect())},
1134 getMap{"pi": V(wantM3a.ProtoReflect()), "ln2": V(wantM3b.ProtoReflect()), "none": V(nil)},
Joe Tsai87b955b2018-11-14 21:59:49 -08001135 lenMap(2),
1136 },
1137 },
1138 getFields{7: wantMap7, 8: wantMap8},
1139 hasFields{7: true, 8: true},
1140 mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
1141 hasFields{7: true, 8: false},
1142
1143 // Test oneofs of enums and messages.
1144 setFields{9: VE(0xdead)},
1145 hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
1146 setFields{10: VE(0)},
1147 hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001148 messageFieldsMutable{11: messageOps{setFields{6: V(float32(math.Pi))}}},
1149 getFields{11: V(wantM2a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001150 hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
Joe Tsai378c1322019-04-25 23:48:08 -07001151 messageFieldsMutable{12: messageOps{setFields{6: V(float32(math.Pi))}}},
1152 getFields{12: V(wantM3a.ProtoReflect())},
Joe Tsai87b955b2018-11-14 21:59:49 -08001153 hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
1154
1155 // Check entire message.
Joe Tsai378c1322019-04-25 23:48:08 -07001156 rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a.ProtoReflect())},
1157 equalMessage{(&EnumMessages{
Joe Tsai87b955b2018-11-14 21:59:49 -08001158 EnumP2: EnumProto2(0xdead).Enum(),
1159 EnumP3: EnumProto3(0).Enum(),
Damien Neila8a2cea2019-07-10 16:17:16 -07001160 MessageLegacy: &proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)},
Joe Tsai378c1322019-04-25 23:48:08 -07001161 MessageCycle: wantM.Interface().(*EnumMessages),
Joe Tsai87b955b2018-11-14 21:59:49 -08001162 MessageList: []*ScalarProto2{wantM2a, wantM2b},
1163 EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
1164 Union: &EnumMessages_OneofM3{wantM3a},
Joe Tsai378c1322019-04-25 23:48:08 -07001165 }).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001166 clearFields{1, 2, 3, 4, 6, 7, 12},
Joe Tsai378c1322019-04-25 23:48:08 -07001167 equalMessage{new(EnumMessages).ProtoReflect()},
Joe Tsai87b955b2018-11-14 21:59:49 -08001168 })
Joe Tsai6cf80c42018-12-01 04:57:09 -08001169
1170 // Test read-only operations on nil message.
Joe Tsai378c1322019-04-25 23:48:08 -07001171 testMessage(t, nil, (*EnumMessages)(nil).ProtoReflect(), messageOps{
Joe Tsai6cf80c42018-12-01 04:57:09 -08001172 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 -07001173 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 -08001174 listFields{5: {lenList(0)}, 6: {lenList(0)}},
1175 mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
1176 })
Joe Tsai87b955b2018-11-14 21:59:49 -08001177}
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001178
Joe Tsai91e14662018-09-13 13:24:35 -07001179var cmpOpts = cmp.Options{
Joe Tsai87b955b2018-11-14 21:59:49 -08001180 cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
Joe Tsaid8881392019-06-06 13:01:53 -07001181 mx := pimpl.Export{}.MessageOf(x).Interface()
1182 my := pimpl.Export{}.MessageOf(y).Interface()
1183 return proto.Equal(mx, my)
Joe Tsai91e14662018-09-13 13:24:35 -07001184 }),
Joe Tsai87b955b2018-11-14 21:59:49 -08001185 cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
Joe Tsai378c1322019-04-25 23:48:08 -07001186 switch v := pv.Interface().(type) {
1187 case pref.Message:
1188 out := make(map[pref.FieldNumber]pref.Value)
1189 v.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1190 out[fd.Number()] = v
1191 return true
1192 })
1193 return out
1194 case pref.List:
1195 var out []pref.Value
1196 for i := 0; i < v.Len(); i++ {
1197 out = append(out, v.Get(i))
1198 }
1199 return out
1200 case pref.Map:
1201 out := make(map[interface{}]pref.Value)
1202 v.Range(func(k pref.MapKey, v pref.Value) bool {
1203 out[k.Interface()] = v
1204 return true
1205 })
1206 return out
1207 default:
1208 return v
1209 }
Joe Tsai91e14662018-09-13 13:24:35 -07001210 }),
1211 cmpopts.EquateNaNs(),
1212}
1213
1214func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
Joe Tsai378c1322019-04-25 23:48:08 -07001215 fieldDescs := m.Descriptor().Fields()
1216 oneofDescs := m.Descriptor().Oneofs()
Joe Tsai91e14662018-09-13 13:24:35 -07001217 for i, op := range tt {
1218 p.Push(i)
1219 switch op := op.(type) {
1220 case equalMessage:
Joe Tsai378c1322019-04-25 23:48:08 -07001221 if diff := cmp.Diff(V(op.Message), V(m), cmpOpts); diff != "" {
Joe Tsai91e14662018-09-13 13:24:35 -07001222 t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
1223 }
1224 case hasFields:
1225 got := map[pref.FieldNumber]bool{}
1226 want := map[pref.FieldNumber]bool(op)
1227 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001228 fd := fieldDescs.ByNumber(n)
1229 got[n] = m.Has(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001230 }
1231 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001232 t.Errorf("operation %v, Message.Has mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001233 }
1234 case getFields:
1235 got := map[pref.FieldNumber]pref.Value{}
1236 want := map[pref.FieldNumber]pref.Value(op)
1237 for n := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001238 fd := fieldDescs.ByNumber(n)
1239 got[n] = m.Get(fd)
Joe Tsai91e14662018-09-13 13:24:35 -07001240 }
1241 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001242 t.Errorf("operation %v, Message.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001243 }
1244 case setFields:
1245 for n, v := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001246 fd := fieldDescs.ByNumber(n)
1247 m.Set(fd, v)
Joe Tsai91e14662018-09-13 13:24:35 -07001248 }
1249 case clearFields:
Joe Tsai87b955b2018-11-14 21:59:49 -08001250 for _, n := range op {
Joe Tsai378c1322019-04-25 23:48:08 -07001251 fd := fieldDescs.ByNumber(n)
1252 m.Clear(fd)
Joe Tsai87b955b2018-11-14 21:59:49 -08001253 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001254 case whichOneofs:
1255 got := map[pref.Name]pref.FieldNumber{}
1256 want := map[pref.Name]pref.FieldNumber(op)
1257 for s := range want {
Joe Tsai378c1322019-04-25 23:48:08 -07001258 od := oneofDescs.ByName(s)
1259 fd := m.WhichOneof(od)
1260 if fd == nil {
1261 got[s] = 0
1262 } else {
1263 got[s] = fd.Number()
1264 }
Joe Tsai4ec39c72019-04-03 13:40:53 -07001265 }
1266 if diff := cmp.Diff(want, got); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001267 t.Errorf("operation %v, Message.WhichOneof mismatch (-want, +got):\n%s", p, diff)
Joe Tsai4ec39c72019-04-03 13:40:53 -07001268 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001269 case messageFields:
1270 for n, tt := range op {
1271 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001272 fd := fieldDescs.ByNumber(n)
1273 testMessage(t, p, m.Get(fd).Message(), tt)
1274 p.Pop()
1275 }
1276 case messageFieldsMutable:
1277 for n, tt := range op {
1278 p.Push(int(n))
1279 fd := fieldDescs.ByNumber(n)
1280 testMessage(t, p, m.Mutable(fd).Message(), tt)
Joe Tsai87b955b2018-11-14 21:59:49 -08001281 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001282 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001283 case listFields:
Joe Tsai91e14662018-09-13 13:24:35 -07001284 for n, tt := range op {
1285 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001286 fd := fieldDescs.ByNumber(n)
1287 testLists(t, p, m.Get(fd).List(), tt)
1288 p.Pop()
1289 }
1290 case listFieldsMutable:
1291 for n, tt := range op {
1292 p.Push(int(n))
1293 fd := fieldDescs.ByNumber(n)
1294 testLists(t, p, m.Mutable(fd).List(), tt)
Joe Tsai91e14662018-09-13 13:24:35 -07001295 p.Pop()
1296 }
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001297 case mapFields:
1298 for n, tt := range op {
1299 p.Push(int(n))
Joe Tsai378c1322019-04-25 23:48:08 -07001300 fd := fieldDescs.ByNumber(n)
1301 testMaps(t, p, m.Get(fd).Map(), tt)
1302 p.Pop()
1303 }
1304 case mapFieldsMutable:
1305 for n, tt := range op {
1306 p.Push(int(n))
1307 fd := fieldDescs.ByNumber(n)
1308 testMaps(t, p, m.Mutable(fd).Map(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001309 p.Pop()
1310 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001311 case rangeFields:
1312 got := map[pref.FieldNumber]pref.Value{}
1313 want := map[pref.FieldNumber]pref.Value(op)
Joe Tsai378c1322019-04-25 23:48:08 -07001314 m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
1315 got[fd.Number()] = v
Joe Tsai87b955b2018-11-14 21:59:49 -08001316 return true
1317 })
1318 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai378c1322019-04-25 23:48:08 -07001319 t.Errorf("operation %v, Message.Range mismatch (-want, +got):\n%s", p, diff)
Joe Tsai87b955b2018-11-14 21:59:49 -08001320 }
Joe Tsai91e14662018-09-13 13:24:35 -07001321 default:
1322 t.Fatalf("operation %v, invalid operation: %T", p, op)
1323 }
1324 p.Pop()
Joe Tsaifa02f4e2018-09-12 16:20:37 -07001325 }
1326}
Joe Tsai91e14662018-09-13 13:24:35 -07001327
Joe Tsai4b7aff62018-11-14 14:05:19 -08001328func testLists(t *testing.T, p path, v pref.List, tt listOps) {
Joe Tsai91e14662018-09-13 13:24:35 -07001329 for i, op := range tt {
1330 p.Push(i)
1331 switch op := op.(type) {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001332 case equalList:
Joe Tsai378c1322019-04-25 23:48:08 -07001333 if diff := cmp.Diff(V(op.List), V(v), cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001334 t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001335 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001336 case lenList:
Joe Tsai91e14662018-09-13 13:24:35 -07001337 if got, want := v.Len(), int(op); got != want {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001338 t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
Joe Tsai91e14662018-09-13 13:24:35 -07001339 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001340 case getList:
Joe Tsai91e14662018-09-13 13:24:35 -07001341 got := map[int]pref.Value{}
1342 want := map[int]pref.Value(op)
1343 for n := range want {
1344 got[n] = v.Get(n)
1345 }
1346 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
Joe Tsai4b7aff62018-11-14 14:05:19 -08001347 t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
Joe Tsai91e14662018-09-13 13:24:35 -07001348 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001349 case setList:
Joe Tsai91e14662018-09-13 13:24:35 -07001350 for n, e := range op {
1351 v.Set(n, e)
1352 }
Joe Tsai4b7aff62018-11-14 14:05:19 -08001353 case appendList:
Joe Tsai91e14662018-09-13 13:24:35 -07001354 for _, e := range op {
1355 v.Append(e)
1356 }
Joe Tsai87b955b2018-11-14 21:59:49 -08001357 case appendMessageList:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -08001358 m := v.NewMessage()
Damien Neil97e7f572018-12-07 14:28:33 -08001359 v.Append(V(m))
1360 testMessage(t, p, m, messageOps(op))
Joe Tsai4b7aff62018-11-14 14:05:19 -08001361 case truncList:
Joe Tsai91e14662018-09-13 13:24:35 -07001362 v.Truncate(int(op))
1363 default:
1364 t.Fatalf("operation %v, invalid operation: %T", p, op)
1365 }
1366 p.Pop()
1367 }
1368}
1369
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001370func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
1371 for i, op := range tt {
1372 p.Push(i)
1373 switch op := op.(type) {
1374 case equalMap:
Joe Tsai378c1322019-04-25 23:48:08 -07001375 if diff := cmp.Diff(V(op.Map), V(m), cmpOpts); diff != "" {
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001376 t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
1377 }
1378 case lenMap:
1379 if got, want := m.Len(), int(op); got != want {
1380 t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
1381 }
1382 case hasMap:
1383 got := map[interface{}]bool{}
1384 want := map[interface{}]bool(op)
1385 for k := range want {
1386 got[k] = m.Has(V(k).MapKey())
1387 }
1388 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1389 t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
1390 }
1391 case getMap:
1392 got := map[interface{}]pref.Value{}
1393 want := map[interface{}]pref.Value(op)
1394 for k := range want {
1395 got[k] = m.Get(V(k).MapKey())
1396 }
1397 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1398 t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
1399 }
1400 case setMap:
1401 for k, v := range op {
1402 m.Set(V(k).MapKey(), v)
1403 }
1404 case clearMap:
Joe Tsai87b955b2018-11-14 21:59:49 -08001405 for _, k := range op {
1406 m.Clear(V(k).MapKey())
1407 }
1408 case messageMap:
1409 for k, tt := range op {
Damien Neil97e7f572018-12-07 14:28:33 -08001410 mk := V(k).MapKey()
1411 if !m.Has(mk) {
1412 m.Set(mk, V(m.NewMessage()))
1413 }
1414 testMessage(t, p, m.Get(mk).Message(), tt)
Joe Tsaibbfaeb72018-10-17 00:27:21 +00001415 }
1416 case rangeMap:
1417 got := map[interface{}]pref.Value{}
1418 want := map[interface{}]pref.Value(op)
1419 m.Range(func(k pref.MapKey, v pref.Value) bool {
1420 got[k.Interface()] = v
1421 return true
1422 })
1423 if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
1424 t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
1425 }
1426 default:
1427 t.Fatalf("operation %v, invalid operation: %T", p, op)
1428 }
1429 p.Pop()
1430 }
1431}
1432
Joe Tsai378c1322019-04-25 23:48:08 -07001433func getField(m pref.Message, n pref.FieldNumber) pref.Value {
1434 fd := m.Descriptor().Fields().ByNumber(n)
1435 return m.Get(fd)
1436}
1437
Joe Tsai91e14662018-09-13 13:24:35 -07001438type path []int
1439
1440func (p *path) Push(i int) { *p = append(*p, i) }
1441func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
1442func (p path) String() string {
1443 var ss []string
1444 for _, i := range p {
1445 ss = append(ss, fmt.Sprint(i))
1446 }
1447 return strings.Join(ss, ".")
1448}
Joe Tsai82760ce2019-06-20 03:09:57 -07001449
1450// The MessageState implementation makes the assumption that when a
1451// concrete message is unsafe casted as a *MessageState, the Go GC does
1452// not reclaim the memory for the remainder of the concrete message.
1453func TestUnsafeAssumptions(t *testing.T) {
1454 if !pimpl.UnsafeEnabled {
1455 t.Skip()
1456 }
1457
1458 var wg sync.WaitGroup
1459 for i := 0; i < 10; i++ {
1460 wg.Add(1)
1461 go func() {
1462 var ms [10]pref.Message
1463
1464 // Store the message only in its reflective form.
1465 // Trigger the GC after each iteration.
1466 for j := 0; j < 10; j++ {
1467 ms[j] = (&testpb.TestAllTypes{
Damien Neila8a2cea2019-07-10 16:17:16 -07001468 OptionalInt32: proto.Int32(int32(j)),
1469 OptionalFloat: proto.Float32(float32(j)),
Joe Tsai82760ce2019-06-20 03:09:57 -07001470 RepeatedInt32: []int32{int32(j)},
1471 RepeatedFloat: []float32{float32(j)},
Damien Neila8a2cea2019-07-10 16:17:16 -07001472 DefaultInt32: proto.Int32(int32(j)),
1473 DefaultFloat: proto.Float32(float32(j)),
Joe Tsai82760ce2019-06-20 03:09:57 -07001474 }).ProtoReflect()
1475 runtime.GC()
1476 }
1477
1478 // Convert the reflective form back into a concrete form.
1479 // Verify that the values written previously are still the same.
1480 for j := 0; j < 10; j++ {
1481 switch m := ms[j].Interface().(*testpb.TestAllTypes); {
1482 case m.GetOptionalInt32() != int32(j):
1483 case m.GetOptionalFloat() != float32(j):
1484 case m.GetRepeatedInt32()[0] != int32(j):
1485 case m.GetRepeatedFloat()[0] != float32(j):
1486 case m.GetDefaultInt32() != int32(j):
1487 case m.GetDefaultFloat() != float32(j):
1488 default:
1489 continue
1490 }
1491 t.Error("memory corrupted detected")
1492 }
1493 defer wg.Done()
1494 }()
1495 }
1496 wg.Wait()
1497}
1498
1499func BenchmarkName(b *testing.B) {
1500 var sink pref.FullName
1501 b.Run("Value", func(b *testing.B) {
1502 b.ReportAllocs()
1503 m := new(descriptorpb.FileDescriptorProto)
1504 for i := 0; i < b.N; i++ {
1505 sink = m.ProtoReflect().Descriptor().FullName()
1506 }
1507 })
1508 b.Run("Nil", func(b *testing.B) {
1509 b.ReportAllocs()
1510 m := (*descriptorpb.FileDescriptorProto)(nil)
1511 for i := 0; i < b.N; i++ {
1512 sink = m.ProtoReflect().Descriptor().FullName()
1513 }
1514 })
1515 runtime.KeepAlive(sink)
1516}