message_test.go 62 KB

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