message_test.go 57 KB

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