message_test.go 58 KB

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