message_test.go 57 KB

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