message_test.go 59 KB

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