message_reflect_test.go 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. // 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. package impl_test
  5. import (
  6. "fmt"
  7. "math"
  8. "reflect"
  9. "runtime"
  10. "strings"
  11. "sync"
  12. "testing"
  13. cmp "github.com/google/go-cmp/cmp"
  14. cmpopts "github.com/google/go-cmp/cmp/cmpopts"
  15. "google.golang.org/protobuf/encoding/prototext"
  16. pimpl "google.golang.org/protobuf/internal/impl"
  17. "google.golang.org/protobuf/proto"
  18. pdesc "google.golang.org/protobuf/reflect/protodesc"
  19. pref "google.golang.org/protobuf/reflect/protoreflect"
  20. "google.golang.org/protobuf/reflect/protoregistry"
  21. proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
  22. testpb "google.golang.org/protobuf/internal/testprotos/test"
  23. "google.golang.org/protobuf/types/descriptorpb"
  24. )
  25. // List of test operations to perform on messages, lists, or maps.
  26. type (
  27. messageOp interface{ isMessageOp() }
  28. messageOps []messageOp
  29. listOp interface{ isListOp() }
  30. listOps []listOp
  31. mapOp interface{ isMapOp() }
  32. mapOps []mapOp
  33. )
  34. // Test operations performed on a message.
  35. type (
  36. // check that the message contents match
  37. equalMessage struct{ pref.Message }
  38. // check presence for specific fields in the message
  39. hasFields map[pref.FieldNumber]bool
  40. // check that specific message fields match
  41. getFields map[pref.FieldNumber]pref.Value
  42. // set specific message fields
  43. setFields map[pref.FieldNumber]pref.Value
  44. // clear specific fields in the message
  45. clearFields []pref.FieldNumber
  46. // check for the presence of specific oneof member fields.
  47. whichOneofs map[pref.Name]pref.FieldNumber
  48. // apply messageOps on each specified message field
  49. messageFields map[pref.FieldNumber]messageOps
  50. messageFieldsMutable map[pref.FieldNumber]messageOps
  51. // apply listOps on each specified list field
  52. listFields map[pref.FieldNumber]listOps
  53. listFieldsMutable map[pref.FieldNumber]listOps
  54. // apply mapOps on each specified map fields
  55. mapFields map[pref.FieldNumber]mapOps
  56. mapFieldsMutable map[pref.FieldNumber]mapOps
  57. // range through all fields and check that they match
  58. rangeFields map[pref.FieldNumber]pref.Value
  59. )
  60. func (equalMessage) isMessageOp() {}
  61. func (hasFields) isMessageOp() {}
  62. func (getFields) isMessageOp() {}
  63. func (setFields) isMessageOp() {}
  64. func (clearFields) isMessageOp() {}
  65. func (whichOneofs) isMessageOp() {}
  66. func (messageFields) isMessageOp() {}
  67. func (messageFieldsMutable) isMessageOp() {}
  68. func (listFields) isMessageOp() {}
  69. func (listFieldsMutable) isMessageOp() {}
  70. func (mapFields) isMessageOp() {}
  71. func (mapFieldsMutable) isMessageOp() {}
  72. func (rangeFields) isMessageOp() {}
  73. // Test operations performed on a list.
  74. type (
  75. // check that the list contents match
  76. equalList struct{ pref.List }
  77. // check that list length matches
  78. lenList int
  79. // check that specific list entries match
  80. getList map[int]pref.Value
  81. // set specific list entries
  82. setList map[int]pref.Value
  83. // append entries to the list
  84. appendList []pref.Value
  85. // apply messageOps on a newly appended message
  86. appendMessageList messageOps
  87. // truncate the list to the specified length
  88. truncList int
  89. )
  90. func (equalList) isListOp() {}
  91. func (lenList) isListOp() {}
  92. func (getList) isListOp() {}
  93. func (setList) isListOp() {}
  94. func (appendList) isListOp() {}
  95. func (appendMessageList) isListOp() {}
  96. func (truncList) isListOp() {}
  97. // Test operations performed on a map.
  98. type (
  99. // check that the map contents match
  100. equalMap struct{ pref.Map }
  101. // check that map length matches
  102. lenMap int
  103. // check presence for specific entries in the map
  104. hasMap map[interface{}]bool
  105. // check that specific map entries match
  106. getMap map[interface{}]pref.Value
  107. // set specific map entries
  108. setMap map[interface{}]pref.Value
  109. // clear specific entries in the map
  110. clearMap []interface{}
  111. // apply messageOps on each specified message entry
  112. messageMap map[interface{}]messageOps
  113. // range through all entries and check that they match
  114. rangeMap map[interface{}]pref.Value
  115. )
  116. func (equalMap) isMapOp() {}
  117. func (lenMap) isMapOp() {}
  118. func (hasMap) isMapOp() {}
  119. func (getMap) isMapOp() {}
  120. func (setMap) isMapOp() {}
  121. func (clearMap) isMapOp() {}
  122. func (messageMap) isMapOp() {}
  123. func (rangeMap) isMapOp() {}
  124. type ScalarProto2 struct {
  125. Bool *bool `protobuf:"1"`
  126. Int32 *int32 `protobuf:"2"`
  127. Int64 *int64 `protobuf:"3"`
  128. Uint32 *uint32 `protobuf:"4"`
  129. Uint64 *uint64 `protobuf:"5"`
  130. Float32 *float32 `protobuf:"6"`
  131. Float64 *float64 `protobuf:"7"`
  132. String *string `protobuf:"8"`
  133. StringA []byte `protobuf:"9"`
  134. Bytes []byte `protobuf:"10"`
  135. BytesA *string `protobuf:"11"`
  136. MyBool *MyBool `protobuf:"12"`
  137. MyInt32 *MyInt32 `protobuf:"13"`
  138. MyInt64 *MyInt64 `protobuf:"14"`
  139. MyUint32 *MyUint32 `protobuf:"15"`
  140. MyUint64 *MyUint64 `protobuf:"16"`
  141. MyFloat32 *MyFloat32 `protobuf:"17"`
  142. MyFloat64 *MyFloat64 `protobuf:"18"`
  143. MyString *MyString `protobuf:"19"`
  144. MyStringA MyBytes `protobuf:"20"`
  145. MyBytes MyBytes `protobuf:"21"`
  146. MyBytesA *MyString `protobuf:"22"`
  147. }
  148. func mustMakeEnumDesc(path string, syntax pref.Syntax, enumDesc string) pref.EnumDescriptor {
  149. s := fmt.Sprintf(`name:%q syntax:%q enum_type:[{%s}]`, path, syntax, enumDesc)
  150. pb := new(descriptorpb.FileDescriptorProto)
  151. if err := prototext.Unmarshal([]byte(s), pb); err != nil {
  152. panic(err)
  153. }
  154. fd, err := pdesc.NewFile(pb, nil)
  155. if err != nil {
  156. panic(err)
  157. }
  158. return fd.Enums().Get(0)
  159. }
  160. func mustMakeMessageDesc(path string, syntax pref.Syntax, fileDesc, msgDesc string, r pdesc.Resolver) pref.MessageDescriptor {
  161. s := fmt.Sprintf(`name:%q syntax:%q %s message_type:[{%s}]`, path, syntax, fileDesc, msgDesc)
  162. pb := new(descriptorpb.FileDescriptorProto)
  163. if err := prototext.Unmarshal([]byte(s), pb); err != nil {
  164. panic(err)
  165. }
  166. fd, err := pdesc.NewFile(pb, r)
  167. if err != nil {
  168. panic(err)
  169. }
  170. return fd.Messages().Get(0)
  171. }
  172. var V = pref.ValueOf
  173. var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
  174. type (
  175. MyBool bool
  176. MyInt32 int32
  177. MyInt64 int64
  178. MyUint32 uint32
  179. MyUint64 uint64
  180. MyFloat32 float32
  181. MyFloat64 float64
  182. MyString string
  183. MyBytes []byte
  184. ListStrings []MyString
  185. ListBytes []MyBytes
  186. MapStrings map[MyString]MyString
  187. MapBytes map[MyString]MyBytes
  188. )
  189. var scalarProto2Type = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(ScalarProto2)), Desc: mustMakeMessageDesc("scalar2.proto", pref.Proto2, "", `
  190. name: "ScalarProto2"
  191. field: [
  192. {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
  193. {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2"},
  194. {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3"},
  195. {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4"},
  196. {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5"},
  197. {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6"},
  198. {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7"},
  199. {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8"},
  200. {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9"},
  201. {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"10"},
  202. {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11"},
  203. {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true"},
  204. {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"13"},
  205. {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"14"},
  206. {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"15"},
  207. {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"16"},
  208. {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"17"},
  209. {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"18"},
  210. {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"19"},
  211. {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"20"},
  212. {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"21"},
  213. {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"22"}
  214. ]
  215. `, nil),
  216. }
  217. func (m *ScalarProto2) ProtoReflect() pref.Message { return scalarProto2Type.MessageOf(m) }
  218. func TestScalarProto2(t *testing.T) {
  219. testMessage(t, nil, new(ScalarProto2).ProtoReflect(), messageOps{
  220. hasFields{
  221. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  222. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  223. },
  224. getFields{
  225. 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")),
  226. 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")),
  227. },
  228. setFields{
  229. 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)),
  230. 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)),
  231. },
  232. hasFields{
  233. 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
  234. 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
  235. },
  236. equalMessage{(&ScalarProto2{
  237. new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
  238. new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString),
  239. }).ProtoReflect()},
  240. clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
  241. equalMessage{new(ScalarProto2).ProtoReflect()},
  242. // Setting a bytes field nil empty bytes should preserve presence.
  243. setFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
  244. getFields{10: V([]byte{}), 11: V([]byte(nil)), 21: V([]byte{}), 22: V([]byte(nil))},
  245. hasFields{10: true, 11: true, 21: true, 22: true},
  246. })
  247. // Test read-only operations on nil message.
  248. testMessage(t, nil, (*ScalarProto2)(nil).ProtoReflect(), messageOps{
  249. hasFields{
  250. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  251. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  252. },
  253. getFields{
  254. 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")),
  255. 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")),
  256. },
  257. })
  258. }
  259. type ScalarProto3 struct {
  260. Bool bool `protobuf:"1"`
  261. Int32 int32 `protobuf:"2"`
  262. Int64 int64 `protobuf:"3"`
  263. Uint32 uint32 `protobuf:"4"`
  264. Uint64 uint64 `protobuf:"5"`
  265. Float32 float32 `protobuf:"6"`
  266. Float64 float64 `protobuf:"7"`
  267. String string `protobuf:"8"`
  268. StringA []byte `protobuf:"9"`
  269. Bytes []byte `protobuf:"10"`
  270. BytesA string `protobuf:"11"`
  271. MyBool MyBool `protobuf:"12"`
  272. MyInt32 MyInt32 `protobuf:"13"`
  273. MyInt64 MyInt64 `protobuf:"14"`
  274. MyUint32 MyUint32 `protobuf:"15"`
  275. MyUint64 MyUint64 `protobuf:"16"`
  276. MyFloat32 MyFloat32 `protobuf:"17"`
  277. MyFloat64 MyFloat64 `protobuf:"18"`
  278. MyString MyString `protobuf:"19"`
  279. MyStringA MyBytes `protobuf:"20"`
  280. MyBytes MyBytes `protobuf:"21"`
  281. MyBytesA MyString `protobuf:"22"`
  282. }
  283. var scalarProto3Type = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(ScalarProto3)), Desc: mustMakeMessageDesc("scalar3.proto", pref.Proto3, "", `
  284. name: "ScalarProto3"
  285. field: [
  286. {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL},
  287. {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32},
  288. {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64},
  289. {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32},
  290. {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64},
  291. {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT},
  292. {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
  293. {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING},
  294. {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING},
  295. {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_BYTES},
  296. {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES},
  297. {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BOOL},
  298. {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_INT32},
  299. {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_INT64},
  300. {name:"f15" number:15 label:LABEL_OPTIONAL type:TYPE_UINT32},
  301. {name:"f16" number:16 label:LABEL_OPTIONAL type:TYPE_UINT64},
  302. {name:"f17" number:17 label:LABEL_OPTIONAL type:TYPE_FLOAT},
  303. {name:"f18" number:18 label:LABEL_OPTIONAL type:TYPE_DOUBLE},
  304. {name:"f19" number:19 label:LABEL_OPTIONAL type:TYPE_STRING},
  305. {name:"f20" number:20 label:LABEL_OPTIONAL type:TYPE_STRING},
  306. {name:"f21" number:21 label:LABEL_OPTIONAL type:TYPE_BYTES},
  307. {name:"f22" number:22 label:LABEL_OPTIONAL type:TYPE_BYTES}
  308. ]
  309. `, nil),
  310. }
  311. func (m *ScalarProto3) ProtoReflect() pref.Message { return scalarProto3Type.MessageOf(m) }
  312. func TestScalarProto3(t *testing.T) {
  313. testMessage(t, nil, new(ScalarProto3).ProtoReflect(), messageOps{
  314. hasFields{
  315. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  316. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  317. },
  318. getFields{
  319. 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)),
  320. 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)),
  321. },
  322. setFields{
  323. 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)),
  324. 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)),
  325. },
  326. hasFields{
  327. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  328. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  329. },
  330. equalMessage{new(ScalarProto3).ProtoReflect()},
  331. setFields{
  332. 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")),
  333. 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")),
  334. },
  335. hasFields{
  336. 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
  337. 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
  338. },
  339. equalMessage{(&ScalarProto3{
  340. true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
  341. true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
  342. }).ProtoReflect()},
  343. setFields{
  344. 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
  345. },
  346. hasFields{
  347. 2: true, 3: true, 6: true, 7: true,
  348. },
  349. clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
  350. equalMessage{new(ScalarProto3).ProtoReflect()},
  351. // Verify that -0 triggers proper Has behavior.
  352. hasFields{6: false, 7: false},
  353. setFields{6: V(float32(math.Copysign(0, -1))), 7: V(float64(math.Copysign(0, -1)))},
  354. hasFields{6: true, 7: true},
  355. // Setting a bytes field to non-nil empty bytes should not preserve presence.
  356. setFields{10: V([]byte{}), 11: V([]byte{}), 21: V([]byte{}), 22: V([]byte{})},
  357. getFields{10: V([]byte(nil)), 11: V([]byte(nil)), 21: V([]byte(nil)), 22: V([]byte(nil))},
  358. hasFields{10: false, 11: false, 21: false, 22: false},
  359. })
  360. // Test read-only operations on nil message.
  361. testMessage(t, nil, (*ScalarProto3)(nil).ProtoReflect(), messageOps{
  362. hasFields{
  363. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  364. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  365. },
  366. getFields{
  367. 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)),
  368. 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)),
  369. },
  370. })
  371. }
  372. type ListScalars struct {
  373. Bools []bool `protobuf:"1"`
  374. Int32s []int32 `protobuf:"2"`
  375. Int64s []int64 `protobuf:"3"`
  376. Uint32s []uint32 `protobuf:"4"`
  377. Uint64s []uint64 `protobuf:"5"`
  378. Float32s []float32 `protobuf:"6"`
  379. Float64s []float64 `protobuf:"7"`
  380. Strings []string `protobuf:"8"`
  381. StringsA [][]byte `protobuf:"9"`
  382. Bytes [][]byte `protobuf:"10"`
  383. BytesA []string `protobuf:"11"`
  384. MyStrings1 []MyString `protobuf:"12"`
  385. MyStrings2 []MyBytes `protobuf:"13"`
  386. MyBytes1 []MyBytes `protobuf:"14"`
  387. MyBytes2 []MyString `protobuf:"15"`
  388. MyStrings3 ListStrings `protobuf:"16"`
  389. MyStrings4 ListBytes `protobuf:"17"`
  390. MyBytes3 ListBytes `protobuf:"18"`
  391. MyBytes4 ListStrings `protobuf:"19"`
  392. }
  393. var listScalarsType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(ListScalars)), Desc: mustMakeMessageDesc("list-scalars.proto", pref.Proto2, "", `
  394. name: "ListScalars"
  395. field: [
  396. {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_BOOL},
  397. {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_INT32},
  398. {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_INT64},
  399. {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_UINT32},
  400. {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_UINT64},
  401. {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_FLOAT},
  402. {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_DOUBLE},
  403. {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_STRING},
  404. {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_STRING},
  405. {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_BYTES},
  406. {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_BYTES},
  407. {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_STRING},
  408. {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_STRING},
  409. {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_BYTES},
  410. {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_BYTES},
  411. {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_STRING},
  412. {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_STRING},
  413. {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_BYTES},
  414. {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_BYTES}
  415. ]
  416. `, nil),
  417. }
  418. func (m *ListScalars) ProtoReflect() pref.Message { return listScalarsType.MessageOf(m) }
  419. func TestListScalars(t *testing.T) {
  420. empty := new(ListScalars).ProtoReflect()
  421. want := (&ListScalars{
  422. Bools: []bool{true, false, true},
  423. Int32s: []int32{2, math.MinInt32, math.MaxInt32},
  424. Int64s: []int64{3, math.MinInt64, math.MaxInt64},
  425. Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
  426. Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
  427. Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
  428. Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
  429. Strings: []string{"8", "", "eight"},
  430. StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
  431. Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
  432. BytesA: []string{"11", "", "eleven"},
  433. MyStrings1: []MyString{"12", "", "twelve"},
  434. MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
  435. MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
  436. MyBytes2: []MyString{"15", "", "fifteen"},
  437. MyStrings3: ListStrings{"16", "", "sixteen"},
  438. MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
  439. MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
  440. MyBytes4: ListStrings{"19", "", "nineteen"},
  441. }).ProtoReflect()
  442. testMessage(t, nil, new(ListScalars).ProtoReflect(), messageOps{
  443. 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},
  444. 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)},
  445. 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)},
  446. listFieldsMutable{
  447. 2: {
  448. lenList(0),
  449. appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
  450. getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
  451. equalList{getField(want, 2).List()},
  452. },
  453. 4: {
  454. appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
  455. setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
  456. lenList(3),
  457. },
  458. 6: {
  459. appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
  460. equalList{getField(want, 6).List()},
  461. },
  462. 8: {
  463. appendList{V(""), V(""), V(""), V(""), V(""), V("")},
  464. lenList(6),
  465. setList{0: V("8"), 2: V("eight")},
  466. truncList(3),
  467. equalList{getField(want, 8).List()},
  468. },
  469. 10: {
  470. appendList{V([]byte(nil)), V([]byte(nil))},
  471. setList{0: V([]byte("10"))},
  472. appendList{V([]byte("wrong"))},
  473. setList{2: V([]byte("ten"))},
  474. equalList{getField(want, 10).List()},
  475. },
  476. 12: {
  477. appendList{V("12"), V("wrong"), V("twelve")},
  478. setList{1: V("")},
  479. equalList{getField(want, 12).List()},
  480. },
  481. 14: {
  482. appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
  483. equalList{getField(want, 14).List()},
  484. },
  485. 16: {
  486. appendList{V("16"), V(""), V("sixteen"), V("extra")},
  487. truncList(3),
  488. equalList{getField(want, 16).List()},
  489. },
  490. 18: {
  491. appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
  492. equalList{getField(want, 18).List()},
  493. },
  494. },
  495. 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},
  496. equalMessage{want},
  497. clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
  498. equalMessage{empty},
  499. })
  500. // Test read-only operations on nil message.
  501. testMessage(t, nil, (*ListScalars)(nil).ProtoReflect(), messageOps{
  502. 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},
  503. 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)}},
  504. })
  505. }
  506. type MapScalars struct {
  507. KeyBools map[bool]string `protobuf:"1"`
  508. KeyInt32s map[int32]string `protobuf:"2"`
  509. KeyInt64s map[int64]string `protobuf:"3"`
  510. KeyUint32s map[uint32]string `protobuf:"4"`
  511. KeyUint64s map[uint64]string `protobuf:"5"`
  512. KeyStrings map[string]string `protobuf:"6"`
  513. ValBools map[string]bool `protobuf:"7"`
  514. ValInt32s map[string]int32 `protobuf:"8"`
  515. ValInt64s map[string]int64 `protobuf:"9"`
  516. ValUint32s map[string]uint32 `protobuf:"10"`
  517. ValUint64s map[string]uint64 `protobuf:"11"`
  518. ValFloat32s map[string]float32 `protobuf:"12"`
  519. ValFloat64s map[string]float64 `protobuf:"13"`
  520. ValStrings map[string]string `protobuf:"14"`
  521. ValStringsA map[string][]byte `protobuf:"15"`
  522. ValBytes map[string][]byte `protobuf:"16"`
  523. ValBytesA map[string]string `protobuf:"17"`
  524. MyStrings1 map[MyString]MyString `protobuf:"18"`
  525. MyStrings2 map[MyString]MyBytes `protobuf:"19"`
  526. MyBytes1 map[MyString]MyBytes `protobuf:"20"`
  527. MyBytes2 map[MyString]MyString `protobuf:"21"`
  528. MyStrings3 MapStrings `protobuf:"22"`
  529. MyStrings4 MapBytes `protobuf:"23"`
  530. MyBytes3 MapBytes `protobuf:"24"`
  531. MyBytes4 MapStrings `protobuf:"25"`
  532. }
  533. var mapScalarsType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(MapScalars)), Desc: mustMakeMessageDesc("map-scalars.proto", pref.Proto2, "", `
  534. name: "MapScalars"
  535. field: [
  536. {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F1Entry"},
  537. {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F2Entry"},
  538. {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F3Entry"},
  539. {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F4Entry"},
  540. {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F5Entry"},
  541. {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F6Entry"},
  542. {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F7Entry"},
  543. {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F8Entry"},
  544. {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F9Entry"},
  545. {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F10Entry"},
  546. {name:"f11" number:11 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F11Entry"},
  547. {name:"f12" number:12 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F12Entry"},
  548. {name:"f13" number:13 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F13Entry"},
  549. {name:"f14" number:14 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F14Entry"},
  550. {name:"f15" number:15 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F15Entry"},
  551. {name:"f16" number:16 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F16Entry"},
  552. {name:"f17" number:17 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F17Entry"},
  553. {name:"f18" number:18 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F18Entry"},
  554. {name:"f19" number:19 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F19Entry"},
  555. {name:"f20" number:20 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F20Entry"},
  556. {name:"f21" number:21 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F21Entry"},
  557. {name:"f22" number:22 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F22Entry"},
  558. {name:"f23" number:23 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F23Entry"},
  559. {name:"f24" number:24 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F24Entry"},
  560. {name:"f25" number:25 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".MapScalars.F25Entry"}
  561. ]
  562. nested_type: [
  563. {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}},
  564. {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}},
  565. {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}},
  566. {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}},
  567. {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}},
  568. {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}},
  569. {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}},
  570. {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}},
  571. {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}},
  572. {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}},
  573. {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}},
  574. {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}},
  575. {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}},
  576. {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}},
  577. {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}},
  578. {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}},
  579. {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}},
  580. {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}},
  581. {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}},
  582. {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}},
  583. {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}},
  584. {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}},
  585. {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}},
  586. {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}},
  587. {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}}
  588. ]
  589. `, nil),
  590. }
  591. func (m *MapScalars) ProtoReflect() pref.Message { return mapScalarsType.MessageOf(m) }
  592. func TestMapScalars(t *testing.T) {
  593. empty := new(MapScalars).ProtoReflect()
  594. want := (&MapScalars{
  595. KeyBools: map[bool]string{true: "true", false: "false"},
  596. KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
  597. KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
  598. KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
  599. KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
  600. KeyStrings: map[string]string{"": "", "foo": "bar"},
  601. ValBools: map[string]bool{"true": true, "false": false},
  602. ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
  603. ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
  604. ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
  605. ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
  606. ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
  607. ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
  608. ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
  609. ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
  610. ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
  611. ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
  612. MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
  613. MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  614. MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  615. MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
  616. MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
  617. MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  618. MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  619. MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
  620. }).ProtoReflect()
  621. testMessage(t, nil, new(MapScalars).ProtoReflect(), messageOps{
  622. 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},
  623. 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)},
  624. 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)},
  625. mapFieldsMutable{
  626. 2: {
  627. lenMap(0),
  628. hasMap{int32(0): false, int32(-1): false, int32(2): false},
  629. setMap{int32(0): V("zero")},
  630. lenMap(1),
  631. hasMap{int32(0): true, int32(-1): false, int32(2): false},
  632. setMap{int32(-1): V("one")},
  633. lenMap(2),
  634. hasMap{int32(0): true, int32(-1): true, int32(2): false},
  635. setMap{int32(2): V("two")},
  636. lenMap(3),
  637. hasMap{int32(0): true, int32(-1): true, int32(2): true},
  638. },
  639. 4: {
  640. setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
  641. equalMap{getField(want, 4).Map()},
  642. },
  643. 6: {
  644. clearMap{"noexist"},
  645. setMap{"foo": V("bar")},
  646. setMap{"": V("empty")},
  647. getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
  648. setMap{"": V(""), "extra": V("extra")},
  649. clearMap{"extra", "noexist"},
  650. },
  651. 8: {
  652. equalMap{getField(empty, 8).Map()},
  653. setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
  654. },
  655. 10: {
  656. setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
  657. lenMap(3),
  658. equalMap{getField(want, 10).Map()},
  659. getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
  660. },
  661. 12: {
  662. setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
  663. clearMap{"e", "phi"},
  664. rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
  665. },
  666. 14: {
  667. equalMap{getField(empty, 14).Map()},
  668. setMap{"s1": V("s1"), "s2": V("s2")},
  669. },
  670. 16: {
  671. setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
  672. equalMap{getField(want, 16).Map()},
  673. },
  674. 18: {
  675. hasMap{"s1": false, "s2": false, "s3": false},
  676. setMap{"s1": V("s1"), "s2": V("s2")},
  677. hasMap{"s1": true, "s2": true, "s3": false},
  678. },
  679. 20: {
  680. equalMap{getField(empty, 20).Map()},
  681. setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
  682. },
  683. 22: {
  684. rangeMap{},
  685. setMap{"s1": V("s1"), "s2": V("s2")},
  686. rangeMap{"s1": V("s1"), "s2": V("s2")},
  687. lenMap(2),
  688. },
  689. 24: {
  690. setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
  691. equalMap{getField(want, 24).Map()},
  692. },
  693. },
  694. 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},
  695. equalMessage{want},
  696. 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},
  697. equalMessage{empty},
  698. })
  699. // Test read-only operations on nil message.
  700. testMessage(t, nil, (*MapScalars)(nil).ProtoReflect(), messageOps{
  701. 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},
  702. 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)}},
  703. })
  704. }
  705. type OneofScalars struct {
  706. Union isOneofScalars_Union `protobuf_oneof:"union"`
  707. }
  708. var oneofScalarsType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(OneofScalars)), Desc: mustMakeMessageDesc("oneof-scalars.proto", pref.Proto2, "", `
  709. name: "OneofScalars"
  710. field: [
  711. {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true" oneof_index:0},
  712. {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"2" oneof_index:0},
  713. {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_INT64 default_value:"3" oneof_index:0},
  714. {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"4" oneof_index:0},
  715. {name:"f5" number:5 label:LABEL_OPTIONAL type:TYPE_UINT64 default_value:"5" oneof_index:0},
  716. {name:"f6" number:6 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"6" oneof_index:0},
  717. {name:"f7" number:7 label:LABEL_OPTIONAL type:TYPE_DOUBLE default_value:"7" oneof_index:0},
  718. {name:"f8" number:8 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"8" oneof_index:0},
  719. {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"9" oneof_index:0},
  720. {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"10" oneof_index:0},
  721. {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"11" oneof_index:0},
  722. {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"12" oneof_index:0},
  723. {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"13" oneof_index:0}
  724. ]
  725. oneof_decl: [{name:"union"}]
  726. `, nil),
  727. }
  728. func (m *OneofScalars) ProtoReflect() pref.Message { return oneofScalarsType.MessageOf(m) }
  729. func (*OneofScalars) XXX_OneofWrappers() []interface{} {
  730. return []interface{}{
  731. (*OneofScalars_Bool)(nil),
  732. (*OneofScalars_Int32)(nil),
  733. (*OneofScalars_Int64)(nil),
  734. (*OneofScalars_Uint32)(nil),
  735. (*OneofScalars_Uint64)(nil),
  736. (*OneofScalars_Float32)(nil),
  737. (*OneofScalars_Float64)(nil),
  738. (*OneofScalars_String)(nil),
  739. (*OneofScalars_StringA)(nil),
  740. (*OneofScalars_StringB)(nil),
  741. (*OneofScalars_Bytes)(nil),
  742. (*OneofScalars_BytesA)(nil),
  743. (*OneofScalars_BytesB)(nil),
  744. }
  745. }
  746. type (
  747. isOneofScalars_Union interface {
  748. isOneofScalars_Union()
  749. }
  750. OneofScalars_Bool struct {
  751. Bool bool `protobuf:"1"`
  752. }
  753. OneofScalars_Int32 struct {
  754. Int32 MyInt32 `protobuf:"2"`
  755. }
  756. OneofScalars_Int64 struct {
  757. Int64 int64 `protobuf:"3"`
  758. }
  759. OneofScalars_Uint32 struct {
  760. Uint32 MyUint32 `protobuf:"4"`
  761. }
  762. OneofScalars_Uint64 struct {
  763. Uint64 uint64 `protobuf:"5"`
  764. }
  765. OneofScalars_Float32 struct {
  766. Float32 MyFloat32 `protobuf:"6"`
  767. }
  768. OneofScalars_Float64 struct {
  769. Float64 float64 `protobuf:"7"`
  770. }
  771. OneofScalars_String struct {
  772. String string `protobuf:"8"`
  773. }
  774. OneofScalars_StringA struct {
  775. StringA []byte `protobuf:"9"`
  776. }
  777. OneofScalars_StringB struct {
  778. StringB MyString `protobuf:"10"`
  779. }
  780. OneofScalars_Bytes struct {
  781. Bytes []byte `protobuf:"11"`
  782. }
  783. OneofScalars_BytesA struct {
  784. BytesA string `protobuf:"12"`
  785. }
  786. OneofScalars_BytesB struct {
  787. BytesB MyBytes `protobuf:"13"`
  788. }
  789. )
  790. func (*OneofScalars_Bool) isOneofScalars_Union() {}
  791. func (*OneofScalars_Int32) isOneofScalars_Union() {}
  792. func (*OneofScalars_Int64) isOneofScalars_Union() {}
  793. func (*OneofScalars_Uint32) isOneofScalars_Union() {}
  794. func (*OneofScalars_Uint64) isOneofScalars_Union() {}
  795. func (*OneofScalars_Float32) isOneofScalars_Union() {}
  796. func (*OneofScalars_Float64) isOneofScalars_Union() {}
  797. func (*OneofScalars_String) isOneofScalars_Union() {}
  798. func (*OneofScalars_StringA) isOneofScalars_Union() {}
  799. func (*OneofScalars_StringB) isOneofScalars_Union() {}
  800. func (*OneofScalars_Bytes) isOneofScalars_Union() {}
  801. func (*OneofScalars_BytesA) isOneofScalars_Union() {}
  802. func (*OneofScalars_BytesB) isOneofScalars_Union() {}
  803. func TestOneofs(t *testing.T) {
  804. empty := &OneofScalars{}
  805. want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
  806. want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
  807. want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
  808. want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
  809. want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
  810. want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
  811. want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
  812. want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
  813. want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
  814. want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
  815. want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
  816. want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
  817. want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
  818. testMessage(t, nil, new(OneofScalars).ProtoReflect(), messageOps{
  819. 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},
  820. 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"))},
  821. whichOneofs{"union": 0},
  822. setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1.ProtoReflect()},
  823. setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2.ProtoReflect()},
  824. setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3.ProtoReflect()},
  825. setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4.ProtoReflect()},
  826. setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5.ProtoReflect()},
  827. setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6.ProtoReflect()},
  828. setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7.ProtoReflect()},
  829. 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},
  830. whichOneofs{"union": 7},
  831. setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8.ProtoReflect()},
  832. setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9.ProtoReflect()},
  833. setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10.ProtoReflect()},
  834. setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11.ProtoReflect()},
  835. setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12.ProtoReflect()},
  836. setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13.ProtoReflect()},
  837. 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},
  838. 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"))},
  839. clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
  840. whichOneofs{"union": 13},
  841. equalMessage{want13.ProtoReflect()},
  842. clearFields{13},
  843. whichOneofs{"union": 0},
  844. equalMessage{empty.ProtoReflect()},
  845. })
  846. // Test read-only operations on nil message.
  847. testMessage(t, nil, (*OneofScalars)(nil).ProtoReflect(), messageOps{
  848. 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},
  849. 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"))},
  850. })
  851. }
  852. type EnumProto2 int32
  853. var enumProto2Desc = mustMakeEnumDesc("enum2.proto", pref.Proto2, `
  854. name: "EnumProto2"
  855. value: [{name:"DEAD" number:0xdead}, {name:"BEEF" number:0xbeef}]
  856. `)
  857. func (e EnumProto2) Descriptor() pref.EnumDescriptor { return enumProto2Desc }
  858. func (e EnumProto2) Type() pref.EnumType { return e }
  859. func (e EnumProto2) Enum() *EnumProto2 { return &e }
  860. func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
  861. func (t EnumProto2) New(n pref.EnumNumber) pref.Enum { return EnumProto2(n) }
  862. type EnumProto3 int32
  863. var enumProto3Desc = mustMakeEnumDesc("enum3.proto", pref.Proto3, `
  864. name: "EnumProto3",
  865. value: [{name:"ALPHA" number:0}, {name:"BRAVO" number:1}]
  866. `)
  867. func (e EnumProto3) Descriptor() pref.EnumDescriptor { return enumProto3Desc }
  868. func (e EnumProto3) Type() pref.EnumType { return e }
  869. func (e EnumProto3) Enum() *EnumProto3 { return &e }
  870. func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
  871. func (t EnumProto3) New(n pref.EnumNumber) pref.Enum { return EnumProto3(n) }
  872. type EnumMessages struct {
  873. EnumP2 *EnumProto2 `protobuf:"1"`
  874. EnumP3 *EnumProto3 `protobuf:"2"`
  875. MessageLegacy *proto2_20180125.Message `protobuf:"3"`
  876. MessageCycle *EnumMessages `protobuf:"4"`
  877. EnumList []EnumProto2 `protobuf:"5"`
  878. MessageList []*ScalarProto2 `protobuf:"6"`
  879. EnumMap map[string]EnumProto3 `protobuf:"7"`
  880. MessageMap map[string]*ScalarProto3 `protobuf:"8"`
  881. Union isEnumMessages_Union `protobuf_oneof:"union"`
  882. }
  883. var enumMessagesType = pimpl.MessageInfo{GoReflectType: reflect.TypeOf(new(EnumMessages)), Desc: mustMakeMessageDesc("enum-messages.proto", pref.Proto2, `
  884. dependency: ["enum2.proto", "enum3.proto", "scalar2.proto", "scalar3.proto", "proto2.v1.0.0-20180125-92554152/test.proto"]
  885. `, `
  886. name: "EnumMessages"
  887. field: [
  888. {name:"f1" number:1 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" default_value:"BEEF"},
  889. {name:"f2" number:2 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" default_value:"BRAVO"},
  890. {name:"f3" number:3 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".google.golang.org.proto2_20180125.Message"},
  891. {name:"f4" number:4 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".EnumMessages"},
  892. {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_ENUM type_name:".EnumProto2"},
  893. {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".ScalarProto2"},
  894. {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F7Entry"},
  895. {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages.F8Entry"},
  896. {name:"f9" number:9 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" oneof_index:0 default_value:"BEEF"},
  897. {name:"f10" number:10 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto3" oneof_index:0 default_value:"BRAVO"},
  898. {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto2" oneof_index:0},
  899. {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".ScalarProto3" oneof_index:0}
  900. ]
  901. oneof_decl: [{name:"union"}]
  902. nested_type: [
  903. {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}},
  904. {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}}
  905. ]
  906. `, protoregistry.NewFiles(
  907. EnumProto2(0).Descriptor().ParentFile(),
  908. EnumProto3(0).Descriptor().ParentFile(),
  909. ((*ScalarProto2)(nil)).ProtoReflect().Descriptor().ParentFile(),
  910. ((*ScalarProto3)(nil)).ProtoReflect().Descriptor().ParentFile(),
  911. pimpl.Export{}.MessageDescriptorOf((*proto2_20180125.Message)(nil)).ParentFile(),
  912. )),
  913. }
  914. func (m *EnumMessages) ProtoReflect() pref.Message { return enumMessagesType.MessageOf(m) }
  915. func (*EnumMessages) XXX_OneofWrappers() []interface{} {
  916. return []interface{}{
  917. (*EnumMessages_OneofE2)(nil),
  918. (*EnumMessages_OneofE3)(nil),
  919. (*EnumMessages_OneofM2)(nil),
  920. (*EnumMessages_OneofM3)(nil),
  921. }
  922. }
  923. type (
  924. isEnumMessages_Union interface {
  925. isEnumMessages_Union()
  926. }
  927. EnumMessages_OneofE2 struct {
  928. OneofE2 EnumProto2 `protobuf:"9"`
  929. }
  930. EnumMessages_OneofE3 struct {
  931. OneofE3 EnumProto3 `protobuf:"10"`
  932. }
  933. EnumMessages_OneofM2 struct {
  934. OneofM2 *ScalarProto2 `protobuf:"11"`
  935. }
  936. EnumMessages_OneofM3 struct {
  937. OneofM3 *ScalarProto3 `protobuf:"12"`
  938. }
  939. )
  940. func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
  941. func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
  942. func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
  943. func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
  944. func TestEnumMessages(t *testing.T) {
  945. emptyL := pimpl.Export{}.MessageOf(new(proto2_20180125.Message))
  946. emptyM := new(EnumMessages).ProtoReflect()
  947. emptyM2 := new(ScalarProto2).ProtoReflect()
  948. emptyM3 := new(ScalarProto3).ProtoReflect()
  949. wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)})
  950. wantM := (&EnumMessages{EnumP2: EnumProto2(1234).Enum()}).ProtoReflect()
  951. wantM2a := &ScalarProto2{Float32: proto.Float32(math.Pi)}
  952. wantM2b := &ScalarProto2{Float32: proto.Float32(math.Phi)}
  953. wantM3a := &ScalarProto3{Float32: math.Pi}
  954. wantM3b := &ScalarProto3{Float32: math.Ln2}
  955. wantList5 := getField((&EnumMessages{EnumList: []EnumProto2{333, 222}}).ProtoReflect(), 5)
  956. wantList6 := getField((&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).ProtoReflect(), 6)
  957. wantMap7 := getField((&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).ProtoReflect(), 7)
  958. wantMap8 := getField((&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).ProtoReflect(), 8)
  959. testMessage(t, nil, new(EnumMessages).ProtoReflect(), messageOps{
  960. 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},
  961. getFields{1: VE(0xbeef), 2: VE(1), 3: V(emptyL), 4: V(emptyM), 9: VE(0xbeef), 10: VE(1)},
  962. // Test singular enums.
  963. setFields{1: VE(0xdead), 2: VE(0)},
  964. getFields{1: VE(0xdead), 2: VE(0)},
  965. hasFields{1: true, 2: true},
  966. // Test singular messages.
  967. messageFieldsMutable{3: messageOps{setFields{109: V(float32(math.E))}}},
  968. messageFieldsMutable{4: messageOps{setFields{1: VE(1234)}}},
  969. getFields{3: V(wantL), 4: V(wantM)},
  970. clearFields{3, 4},
  971. hasFields{3: false, 4: false},
  972. setFields{3: V(wantL), 4: V(wantM)},
  973. hasFields{3: true, 4: true},
  974. // Test list of enums and messages.
  975. listFieldsMutable{
  976. 5: listOps{
  977. appendList{VE(111), VE(222)},
  978. setList{0: VE(333)},
  979. getList{0: VE(333), 1: VE(222)},
  980. lenList(2),
  981. },
  982. 6: listOps{
  983. appendMessageList{setFields{4: V(uint32(1e6))}},
  984. appendMessageList{setFields{6: V(float32(math.Phi))}},
  985. setList{0: V(wantM2a.ProtoReflect())},
  986. getList{0: V(wantM2a.ProtoReflect()), 1: V(wantM2b.ProtoReflect())},
  987. },
  988. },
  989. getFields{5: wantList5, 6: wantList6},
  990. hasFields{5: true, 6: true},
  991. listFields{5: listOps{truncList(0)}},
  992. hasFields{5: false, 6: true},
  993. // Test maps of enums and messages.
  994. mapFieldsMutable{
  995. 7: mapOps{
  996. setMap{"one": VE(1), "two": VE(2)},
  997. hasMap{"one": true, "two": true, "three": false},
  998. lenMap(2),
  999. },
  1000. 8: mapOps{
  1001. messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
  1002. setMap{"ln2": V(wantM3b.ProtoReflect())},
  1003. getMap{"pi": V(wantM3a.ProtoReflect()), "ln2": V(wantM3b.ProtoReflect()), "none": V(nil)},
  1004. lenMap(2),
  1005. },
  1006. },
  1007. getFields{7: wantMap7, 8: wantMap8},
  1008. hasFields{7: true, 8: true},
  1009. mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
  1010. hasFields{7: true, 8: false},
  1011. // Test oneofs of enums and messages.
  1012. setFields{9: VE(0xdead)},
  1013. hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
  1014. setFields{10: VE(0)},
  1015. hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
  1016. messageFieldsMutable{11: messageOps{setFields{6: V(float32(math.Pi))}}},
  1017. getFields{11: V(wantM2a.ProtoReflect())},
  1018. hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
  1019. messageFieldsMutable{12: messageOps{setFields{6: V(float32(math.Pi))}}},
  1020. getFields{12: V(wantM3a.ProtoReflect())},
  1021. hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
  1022. // Check entire message.
  1023. rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a.ProtoReflect())},
  1024. equalMessage{(&EnumMessages{
  1025. EnumP2: EnumProto2(0xdead).Enum(),
  1026. EnumP3: EnumProto3(0).Enum(),
  1027. MessageLegacy: &proto2_20180125.Message{OptionalFloat: proto.Float32(math.E)},
  1028. MessageCycle: wantM.Interface().(*EnumMessages),
  1029. MessageList: []*ScalarProto2{wantM2a, wantM2b},
  1030. EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
  1031. Union: &EnumMessages_OneofM3{wantM3a},
  1032. }).ProtoReflect()},
  1033. clearFields{1, 2, 3, 4, 6, 7, 12},
  1034. equalMessage{new(EnumMessages).ProtoReflect()},
  1035. })
  1036. // Test read-only operations on nil message.
  1037. testMessage(t, nil, (*EnumMessages)(nil).ProtoReflect(), messageOps{
  1038. 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},
  1039. 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)},
  1040. listFields{5: {lenList(0)}, 6: {lenList(0)}},
  1041. mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
  1042. })
  1043. }
  1044. var cmpOpts = cmp.Options{
  1045. cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
  1046. mx := pimpl.Export{}.MessageOf(x).Interface()
  1047. my := pimpl.Export{}.MessageOf(y).Interface()
  1048. return proto.Equal(mx, my)
  1049. }),
  1050. cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
  1051. switch v := pv.Interface().(type) {
  1052. case pref.Message:
  1053. out := make(map[pref.FieldNumber]pref.Value)
  1054. v.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
  1055. out[fd.Number()] = v
  1056. return true
  1057. })
  1058. return out
  1059. case pref.List:
  1060. var out []pref.Value
  1061. for i := 0; i < v.Len(); i++ {
  1062. out = append(out, v.Get(i))
  1063. }
  1064. return out
  1065. case pref.Map:
  1066. out := make(map[interface{}]pref.Value)
  1067. v.Range(func(k pref.MapKey, v pref.Value) bool {
  1068. out[k.Interface()] = v
  1069. return true
  1070. })
  1071. return out
  1072. default:
  1073. return v
  1074. }
  1075. }),
  1076. cmpopts.EquateNaNs(),
  1077. }
  1078. func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
  1079. fieldDescs := m.Descriptor().Fields()
  1080. oneofDescs := m.Descriptor().Oneofs()
  1081. for i, op := range tt {
  1082. p.Push(i)
  1083. switch op := op.(type) {
  1084. case equalMessage:
  1085. if diff := cmp.Diff(V(op.Message), V(m), cmpOpts); diff != "" {
  1086. t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
  1087. }
  1088. case hasFields:
  1089. got := map[pref.FieldNumber]bool{}
  1090. want := map[pref.FieldNumber]bool(op)
  1091. for n := range want {
  1092. fd := fieldDescs.ByNumber(n)
  1093. got[n] = m.Has(fd)
  1094. }
  1095. if diff := cmp.Diff(want, got); diff != "" {
  1096. t.Errorf("operation %v, Message.Has mismatch (-want, +got):\n%s", p, diff)
  1097. }
  1098. case getFields:
  1099. got := map[pref.FieldNumber]pref.Value{}
  1100. want := map[pref.FieldNumber]pref.Value(op)
  1101. for n := range want {
  1102. fd := fieldDescs.ByNumber(n)
  1103. got[n] = m.Get(fd)
  1104. }
  1105. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1106. t.Errorf("operation %v, Message.Get mismatch (-want, +got):\n%s", p, diff)
  1107. }
  1108. case setFields:
  1109. for n, v := range op {
  1110. fd := fieldDescs.ByNumber(n)
  1111. m.Set(fd, v)
  1112. }
  1113. case clearFields:
  1114. for _, n := range op {
  1115. fd := fieldDescs.ByNumber(n)
  1116. m.Clear(fd)
  1117. }
  1118. case whichOneofs:
  1119. got := map[pref.Name]pref.FieldNumber{}
  1120. want := map[pref.Name]pref.FieldNumber(op)
  1121. for s := range want {
  1122. od := oneofDescs.ByName(s)
  1123. fd := m.WhichOneof(od)
  1124. if fd == nil {
  1125. got[s] = 0
  1126. } else {
  1127. got[s] = fd.Number()
  1128. }
  1129. }
  1130. if diff := cmp.Diff(want, got); diff != "" {
  1131. t.Errorf("operation %v, Message.WhichOneof mismatch (-want, +got):\n%s", p, diff)
  1132. }
  1133. case messageFields:
  1134. for n, tt := range op {
  1135. p.Push(int(n))
  1136. fd := fieldDescs.ByNumber(n)
  1137. testMessage(t, p, m.Get(fd).Message(), tt)
  1138. p.Pop()
  1139. }
  1140. case messageFieldsMutable:
  1141. for n, tt := range op {
  1142. p.Push(int(n))
  1143. fd := fieldDescs.ByNumber(n)
  1144. testMessage(t, p, m.Mutable(fd).Message(), tt)
  1145. p.Pop()
  1146. }
  1147. case listFields:
  1148. for n, tt := range op {
  1149. p.Push(int(n))
  1150. fd := fieldDescs.ByNumber(n)
  1151. testLists(t, p, m.Get(fd).List(), tt)
  1152. p.Pop()
  1153. }
  1154. case listFieldsMutable:
  1155. for n, tt := range op {
  1156. p.Push(int(n))
  1157. fd := fieldDescs.ByNumber(n)
  1158. testLists(t, p, m.Mutable(fd).List(), tt)
  1159. p.Pop()
  1160. }
  1161. case mapFields:
  1162. for n, tt := range op {
  1163. p.Push(int(n))
  1164. fd := fieldDescs.ByNumber(n)
  1165. testMaps(t, p, m.Get(fd).Map(), tt)
  1166. p.Pop()
  1167. }
  1168. case mapFieldsMutable:
  1169. for n, tt := range op {
  1170. p.Push(int(n))
  1171. fd := fieldDescs.ByNumber(n)
  1172. testMaps(t, p, m.Mutable(fd).Map(), tt)
  1173. p.Pop()
  1174. }
  1175. case rangeFields:
  1176. got := map[pref.FieldNumber]pref.Value{}
  1177. want := map[pref.FieldNumber]pref.Value(op)
  1178. m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
  1179. got[fd.Number()] = v
  1180. return true
  1181. })
  1182. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1183. t.Errorf("operation %v, Message.Range mismatch (-want, +got):\n%s", p, diff)
  1184. }
  1185. default:
  1186. t.Fatalf("operation %v, invalid operation: %T", p, op)
  1187. }
  1188. p.Pop()
  1189. }
  1190. }
  1191. func testLists(t *testing.T, p path, v pref.List, tt listOps) {
  1192. for i, op := range tt {
  1193. p.Push(i)
  1194. switch op := op.(type) {
  1195. case equalList:
  1196. if diff := cmp.Diff(V(op.List), V(v), cmpOpts); diff != "" {
  1197. t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
  1198. }
  1199. case lenList:
  1200. if got, want := v.Len(), int(op); got != want {
  1201. t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
  1202. }
  1203. case getList:
  1204. got := map[int]pref.Value{}
  1205. want := map[int]pref.Value(op)
  1206. for n := range want {
  1207. got[n] = v.Get(n)
  1208. }
  1209. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1210. t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
  1211. }
  1212. case setList:
  1213. for n, e := range op {
  1214. v.Set(n, e)
  1215. }
  1216. case appendList:
  1217. for _, e := range op {
  1218. v.Append(e)
  1219. }
  1220. case appendMessageList:
  1221. e := v.NewElement()
  1222. v.Append(e)
  1223. testMessage(t, p, e.Message(), messageOps(op))
  1224. case truncList:
  1225. v.Truncate(int(op))
  1226. default:
  1227. t.Fatalf("operation %v, invalid operation: %T", p, op)
  1228. }
  1229. p.Pop()
  1230. }
  1231. }
  1232. func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
  1233. for i, op := range tt {
  1234. p.Push(i)
  1235. switch op := op.(type) {
  1236. case equalMap:
  1237. if diff := cmp.Diff(V(op.Map), V(m), cmpOpts); diff != "" {
  1238. t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
  1239. }
  1240. case lenMap:
  1241. if got, want := m.Len(), int(op); got != want {
  1242. t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
  1243. }
  1244. case hasMap:
  1245. got := map[interface{}]bool{}
  1246. want := map[interface{}]bool(op)
  1247. for k := range want {
  1248. got[k] = m.Has(V(k).MapKey())
  1249. }
  1250. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1251. t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
  1252. }
  1253. case getMap:
  1254. got := map[interface{}]pref.Value{}
  1255. want := map[interface{}]pref.Value(op)
  1256. for k := range want {
  1257. got[k] = m.Get(V(k).MapKey())
  1258. }
  1259. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1260. t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
  1261. }
  1262. case setMap:
  1263. for k, v := range op {
  1264. m.Set(V(k).MapKey(), v)
  1265. }
  1266. case clearMap:
  1267. for _, k := range op {
  1268. m.Clear(V(k).MapKey())
  1269. }
  1270. case messageMap:
  1271. for k, tt := range op {
  1272. mk := V(k).MapKey()
  1273. if !m.Has(mk) {
  1274. m.Set(mk, m.NewValue())
  1275. }
  1276. testMessage(t, p, m.Get(mk).Message(), tt)
  1277. }
  1278. case rangeMap:
  1279. got := map[interface{}]pref.Value{}
  1280. want := map[interface{}]pref.Value(op)
  1281. m.Range(func(k pref.MapKey, v pref.Value) bool {
  1282. got[k.Interface()] = v
  1283. return true
  1284. })
  1285. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1286. t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
  1287. }
  1288. default:
  1289. t.Fatalf("operation %v, invalid operation: %T", p, op)
  1290. }
  1291. p.Pop()
  1292. }
  1293. }
  1294. func getField(m pref.Message, n pref.FieldNumber) pref.Value {
  1295. fd := m.Descriptor().Fields().ByNumber(n)
  1296. return m.Get(fd)
  1297. }
  1298. type path []int
  1299. func (p *path) Push(i int) { *p = append(*p, i) }
  1300. func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
  1301. func (p path) String() string {
  1302. var ss []string
  1303. for _, i := range p {
  1304. ss = append(ss, fmt.Sprint(i))
  1305. }
  1306. return strings.Join(ss, ".")
  1307. }
  1308. // The MessageState implementation makes the assumption that when a
  1309. // concrete message is unsafe casted as a *MessageState, the Go GC does
  1310. // not reclaim the memory for the remainder of the concrete message.
  1311. func TestUnsafeAssumptions(t *testing.T) {
  1312. if !pimpl.UnsafeEnabled {
  1313. t.Skip()
  1314. }
  1315. var wg sync.WaitGroup
  1316. for i := 0; i < 10; i++ {
  1317. wg.Add(1)
  1318. go func() {
  1319. var ms [10]pref.Message
  1320. // Store the message only in its reflective form.
  1321. // Trigger the GC after each iteration.
  1322. for j := 0; j < 10; j++ {
  1323. ms[j] = (&testpb.TestAllTypes{
  1324. OptionalInt32: proto.Int32(int32(j)),
  1325. OptionalFloat: proto.Float32(float32(j)),
  1326. RepeatedInt32: []int32{int32(j)},
  1327. RepeatedFloat: []float32{float32(j)},
  1328. DefaultInt32: proto.Int32(int32(j)),
  1329. DefaultFloat: proto.Float32(float32(j)),
  1330. }).ProtoReflect()
  1331. runtime.GC()
  1332. }
  1333. // Convert the reflective form back into a concrete form.
  1334. // Verify that the values written previously are still the same.
  1335. for j := 0; j < 10; j++ {
  1336. switch m := ms[j].Interface().(*testpb.TestAllTypes); {
  1337. case m.GetOptionalInt32() != int32(j):
  1338. case m.GetOptionalFloat() != float32(j):
  1339. case m.GetRepeatedInt32()[0] != int32(j):
  1340. case m.GetRepeatedFloat()[0] != float32(j):
  1341. case m.GetDefaultInt32() != int32(j):
  1342. case m.GetDefaultFloat() != float32(j):
  1343. default:
  1344. continue
  1345. }
  1346. t.Error("memory corrupted detected")
  1347. }
  1348. defer wg.Done()
  1349. }()
  1350. }
  1351. wg.Wait()
  1352. }
  1353. func BenchmarkName(b *testing.B) {
  1354. var sink pref.FullName
  1355. b.Run("Value", func(b *testing.B) {
  1356. b.ReportAllocs()
  1357. m := new(descriptorpb.FileDescriptorProto)
  1358. for i := 0; i < b.N; i++ {
  1359. sink = m.ProtoReflect().Descriptor().FullName()
  1360. }
  1361. })
  1362. b.Run("Nil", func(b *testing.B) {
  1363. b.ReportAllocs()
  1364. m := (*descriptorpb.FileDescriptorProto)(nil)
  1365. for i := 0; i < b.N; i++ {
  1366. sink = m.ProtoReflect().Descriptor().FullName()
  1367. }
  1368. })
  1369. runtime.KeepAlive(sink)
  1370. }
  1371. func BenchmarkReflect(b *testing.B) {
  1372. m := new(testpb.TestAllTypes).ProtoReflect()
  1373. fds := m.Descriptor().Fields()
  1374. vs := make([]pref.Value, fds.Len())
  1375. for i := range vs {
  1376. vs[i] = m.NewField(fds.Get(i))
  1377. }
  1378. b.Run("Has", func(b *testing.B) {
  1379. b.ReportAllocs()
  1380. for i := 0; i < b.N; i++ {
  1381. for j := 0; j < fds.Len(); j++ {
  1382. m.Has(fds.Get(j))
  1383. }
  1384. }
  1385. })
  1386. b.Run("Get", func(b *testing.B) {
  1387. b.ReportAllocs()
  1388. for i := 0; i < b.N; i++ {
  1389. for j := 0; j < fds.Len(); j++ {
  1390. m.Get(fds.Get(j))
  1391. }
  1392. }
  1393. })
  1394. b.Run("Set", func(b *testing.B) {
  1395. b.ReportAllocs()
  1396. for i := 0; i < b.N; i++ {
  1397. for j := 0; j < fds.Len(); j++ {
  1398. m.Set(fds.Get(j), vs[j])
  1399. }
  1400. }
  1401. })
  1402. b.Run("Clear", func(b *testing.B) {
  1403. b.ReportAllocs()
  1404. for i := 0; i < b.N; i++ {
  1405. for j := 0; j < fds.Len(); j++ {
  1406. m.Clear(fds.Get(j))
  1407. }
  1408. }
  1409. })
  1410. b.Run("Range", func(b *testing.B) {
  1411. b.ReportAllocs()
  1412. for i := 0; i < b.N; i++ {
  1413. m.Range(func(pref.FieldDescriptor, pref.Value) bool {
  1414. return true
  1415. })
  1416. }
  1417. })
  1418. }