message_test.go 57 KB

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