message_test.go 65 KB

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