message_test.go 59 KB

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