message_test.go 60 KB

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