message_test.go 58 KB

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