message_test.go 59 KB

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