message_test.go 65 KB

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