Visitor.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // Sources/SwiftProtobuf/Visitor.swift - Basic serialization machinery
  2. //
  3. // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Protocol for traversing the object tree.
  12. ///
  13. /// This is used by:
  14. /// = Protobuf serialization
  15. /// = JSON serialization (with some twists to account for specialty JSON
  16. /// encodings)
  17. /// = Protobuf text serialization
  18. /// = Hashable computation
  19. ///
  20. /// Conceptually, serializers create visitor objects that are
  21. /// then passed recursively to every message and field via generated
  22. /// 'traverse' methods. The details get a little involved due to
  23. /// the need to allow particular messages to override particular
  24. /// behaviors for specific encodings, but the general idea is quite simple.
  25. ///
  26. // -----------------------------------------------------------------------------
  27. import Foundation
  28. /// This is the key interface used by the generated `traverse()` methods
  29. /// used for serialization. It is implemented by each serialization protocol:
  30. /// Protobuf Binary, Protobuf Text, JSON, and the Hash encoder.
  31. public protocol Visitor {
  32. /// Called for each non-repeated float field
  33. ///
  34. /// A default implementation is provided that just widens the value
  35. /// and calls `visitSingularDoubleField`
  36. mutating func visitSingularFloatField(value: Float, fieldNumber: Int) throws
  37. /// Called for each non-repeated double field
  38. ///
  39. /// There is no default implementation. This must be implemented.
  40. mutating func visitSingularDoubleField(value: Double, fieldNumber: Int) throws
  41. /// Called for each non-repeated int32 field
  42. ///
  43. /// A default implementation is provided that just widens the value
  44. /// and calls `visitSingularInt64Field`
  45. mutating func visitSingularInt32Field(value: Int32, fieldNumber: Int) throws
  46. /// Called for each non-repeated int64 field
  47. ///
  48. /// There is no default implementation. This must be implemented.
  49. mutating func visitSingularInt64Field(value: Int64, fieldNumber: Int) throws
  50. /// Called for each non-repeated uint32 field
  51. ///
  52. /// A default implementation is provided that just widens the value
  53. /// and calls `visitSingularUInt64Field`
  54. mutating func visitSingularUInt32Field(value: UInt32, fieldNumber: Int) throws
  55. /// Called for each non-repeated uint64 field
  56. ///
  57. /// There is no default implementation. This must be implemented.
  58. mutating func visitSingularUInt64Field(value: UInt64, fieldNumber: Int) throws
  59. /// Called for each non-repeated sint32 field
  60. ///
  61. /// A default implementation is provided that just forwards to
  62. /// `visitSingularInt32Field`
  63. mutating func visitSingularSInt32Field(value: Int32, fieldNumber: Int) throws
  64. /// Called for each non-repeated sint64 field
  65. ///
  66. /// A default implementation is provided that just forwards to
  67. /// `visitSingularInt64Field`
  68. mutating func visitSingularSInt64Field(value: Int64, fieldNumber: Int) throws
  69. /// Called for each non-repeated fixed32 field
  70. ///
  71. /// A default implementation is provided that just forwards to
  72. /// `visitSingularUInt32Field`
  73. mutating func visitSingularFixed32Field(value: UInt32, fieldNumber: Int) throws
  74. /// Called for each non-repeated fixed64 field
  75. ///
  76. /// A default implementation is provided that just forwards to
  77. /// `visitSingularUInt64Field`
  78. mutating func visitSingularFixed64Field(value: UInt64, fieldNumber: Int) throws
  79. /// Called for each non-repeated sfixed32 field
  80. ///
  81. /// A default implementation is provided that just forwards to
  82. /// `visitSingularInt32Field`
  83. mutating func visitSingularSFixed32Field(value: Int32, fieldNumber: Int) throws
  84. /// Called for each non-repeated sfixed64 field
  85. ///
  86. /// A default implementation is provided that just forwards to
  87. /// `visitSingularInt64Field`
  88. mutating func visitSingularSFixed64Field(value: Int64, fieldNumber: Int) throws
  89. /// Called for each non-repeated bool field
  90. ///
  91. /// There is no default implementation. This must be implemented.
  92. mutating func visitSingularBoolField(value: Bool, fieldNumber: Int) throws
  93. /// Called for each non-repeated string field
  94. ///
  95. /// There is no default implementation. This must be implemented.
  96. mutating func visitSingularStringField(value: String, fieldNumber: Int) throws
  97. /// Called for each non-repeated bytes field
  98. ///
  99. /// There is no default implementation. This must be implemented.
  100. mutating func visitSingularBytesField(value: Data, fieldNumber: Int) throws
  101. /// Called for each non-repeated enum field
  102. ///
  103. /// There is no default implementation. This must be implemented.
  104. mutating func visitSingularEnumField<E: Enum>(value: E, fieldNumber: Int) throws
  105. /// Called for each non-repeated nested message field.
  106. ///
  107. /// There is no default implementation. This must be implemented.
  108. mutating func visitSingularMessageField<M: Message>(value: M, fieldNumber: Int) throws
  109. /// Called for each non-repeated proto2 group field.
  110. ///
  111. /// A default implementation is provided that simply forwards to
  112. /// `visitSingularMessageField`. Implementors who need to handle groups
  113. /// differently than nested messages can override this and provide distinct
  114. /// implementations.
  115. mutating func visitSingularGroupField<G: Message>(value: G, fieldNumber: Int) throws
  116. // Called for each non-packed repeated float field.
  117. /// The method is called once with the complete array of values for
  118. /// the field.
  119. ///
  120. /// A default implementation is provided that simply calls
  121. /// `visitSingularFloatField` once for each item in the array.
  122. mutating func visitRepeatedFloatField(value: [Float], fieldNumber: Int) throws
  123. // Called for each non-packed repeated double field.
  124. /// The method is called once with the complete array of values for
  125. /// the field.
  126. ///
  127. /// A default implementation is provided that simply calls
  128. /// `visitSingularDoubleField` once for each item in the array.
  129. mutating func visitRepeatedDoubleField(value: [Double], fieldNumber: Int) throws
  130. // Called for each non-packed repeated int32 field.
  131. /// The method is called once with the complete array of values for
  132. /// the field.
  133. ///
  134. /// A default implementation is provided that simply calls
  135. /// `visitSingularInt32Field` once for each item in the array.
  136. mutating func visitRepeatedInt32Field(value: [Int32], fieldNumber: Int) throws
  137. // Called for each non-packed repeated int64 field.
  138. /// The method is called once with the complete array of values for
  139. /// the field.
  140. ///
  141. /// A default implementation is provided that simply calls
  142. /// `visitSingularInt64Field` once for each item in the array.
  143. mutating func visitRepeatedInt64Field(value: [Int64], fieldNumber: Int) throws
  144. // Called for each non-packed repeated uint32 field.
  145. /// The method is called once with the complete array of values for
  146. /// the field.
  147. ///
  148. /// A default implementation is provided that simply calls
  149. /// `visitSingularUInt32Field` once for each item in the array.
  150. mutating func visitRepeatedUInt32Field(value: [UInt32], fieldNumber: Int) throws
  151. // Called for each non-packed repeated uint64 field.
  152. /// The method is called once with the complete array of values for
  153. /// the field.
  154. ///
  155. /// A default implementation is provided that simply calls
  156. /// `visitSingularUInt64Field` once for each item in the array.
  157. mutating func visitRepeatedUInt64Field(value: [UInt64], fieldNumber: Int) throws
  158. // Called for each non-packed repeated sint32 field.
  159. /// The method is called once with the complete array of values for
  160. /// the field.
  161. ///
  162. /// A default implementation is provided that simply calls
  163. /// `visitSingularSInt32Field` once for each item in the array.
  164. mutating func visitRepeatedSInt32Field(value: [Int32], fieldNumber: Int) throws
  165. // Called for each non-packed repeated sint64 field.
  166. /// The method is called once with the complete array of values for
  167. /// the field.
  168. ///
  169. /// A default implementation is provided that simply calls
  170. /// `visitSingularSInt64Field` once for each item in the array.
  171. mutating func visitRepeatedSInt64Field(value: [Int64], fieldNumber: Int) throws
  172. // Called for each non-packed repeated fixed32 field.
  173. /// The method is called once with the complete array of values for
  174. /// the field.
  175. ///
  176. /// A default implementation is provided that simply calls
  177. /// `visitSingularFixed32Field` once for each item in the array.
  178. mutating func visitRepeatedFixed32Field(value: [UInt32], fieldNumber: Int) throws
  179. // Called for each non-packed repeated fixed64 field.
  180. /// The method is called once with the complete array of values for
  181. /// the field.
  182. ///
  183. /// A default implementation is provided that simply calls
  184. /// `visitSingularFixed64Field` once for each item in the array.
  185. mutating func visitRepeatedFixed64Field(value: [UInt64], fieldNumber: Int) throws
  186. // Called for each non-packed repeated sfixed32 field.
  187. /// The method is called once with the complete array of values for
  188. /// the field.
  189. ///
  190. /// A default implementation is provided that simply calls
  191. /// `visitSingularSFixed32Field` once for each item in the array.
  192. mutating func visitRepeatedSFixed32Field(value: [Int32], fieldNumber: Int) throws
  193. // Called for each non-packed repeated sfixed64 field.
  194. /// The method is called once with the complete array of values for
  195. /// the field.
  196. ///
  197. /// A default implementation is provided that simply calls
  198. /// `visitSingularSFixed64Field` once for each item in the array.
  199. mutating func visitRepeatedSFixed64Field(value: [Int64], fieldNumber: Int) throws
  200. // Called for each non-packed repeated bool field.
  201. /// The method is called once with the complete array of values for
  202. /// the field.
  203. ///
  204. /// A default implementation is provided that simply calls
  205. /// `visitSingularBoolField` once for each item in the array.
  206. mutating func visitRepeatedBoolField(value: [Bool], fieldNumber: Int) throws
  207. // Called for each non-packed repeated string field.
  208. /// The method is called once with the complete array of values for
  209. /// the field.
  210. ///
  211. /// A default implementation is provided that simply calls
  212. /// `visitSingularStringField` once for each item in the array.
  213. mutating func visitRepeatedStringField(value: [String], fieldNumber: Int) throws
  214. // Called for each non-packed repeated bytes field.
  215. /// The method is called once with the complete array of values for
  216. /// the field.
  217. ///
  218. /// A default implementation is provided that simply calls
  219. /// `visitSingularBytesField` once for each item in the array.
  220. mutating func visitRepeatedBytesField(value: [Data], fieldNumber: Int) throws
  221. /// Called for each repeated, unpacked enum field.
  222. /// The method is called once with the complete array of values for
  223. /// the field.
  224. ///
  225. /// A default implementation is provided that simply calls
  226. /// `visitSingularEnumField` once for each item in the array.
  227. mutating func visitRepeatedEnumField<E: Enum>(value: [E], fieldNumber: Int) throws
  228. /// Called for each repeated nested message field. The method is called once
  229. /// with the complete array of values for the field.
  230. ///
  231. /// A default implementation is provided that simply calls
  232. /// `visitSingularMessageField` once for each item in the array.
  233. mutating func visitRepeatedMessageField<M: Message>(
  234. value: [M],
  235. fieldNumber: Int
  236. ) throws
  237. /// Called for each repeated proto2 group field.
  238. ///
  239. /// A default implementation is provided that simply calls
  240. /// `visitSingularGroupField` once for each item in the array.
  241. mutating func visitRepeatedGroupField<G: Message>(value: [G], fieldNumber: Int) throws
  242. // Called for each packed, repeated float field.
  243. ///
  244. /// This is called once with the complete array of values for
  245. /// the field.
  246. ///
  247. /// There is a default implementation that forwards to the non-packed
  248. /// function.
  249. mutating func visitPackedFloatField(value: [Float], fieldNumber: Int) throws
  250. // Called for each packed, repeated double field.
  251. ///
  252. /// This is called once with the complete array of values for
  253. /// the field.
  254. ///
  255. /// There is a default implementation that forwards to the non-packed
  256. /// function.
  257. mutating func visitPackedDoubleField(value: [Double], fieldNumber: Int) throws
  258. // Called for each packed, repeated int32 field.
  259. ///
  260. /// This is called once with the complete array of values for
  261. /// the field.
  262. ///
  263. /// There is a default implementation that forwards to the non-packed
  264. /// function.
  265. mutating func visitPackedInt32Field(value: [Int32], fieldNumber: Int) throws
  266. // Called for each packed, repeated int64 field.
  267. ///
  268. /// This is called once with the complete array of values for
  269. /// the field.
  270. ///
  271. /// There is a default implementation that forwards to the non-packed
  272. /// function.
  273. mutating func visitPackedInt64Field(value: [Int64], fieldNumber: Int) throws
  274. // Called for each packed, repeated uint32 field.
  275. ///
  276. /// This is called once with the complete array of values for
  277. /// the field.
  278. ///
  279. /// There is a default implementation that forwards to the non-packed
  280. /// function.
  281. mutating func visitPackedUInt32Field(value: [UInt32], fieldNumber: Int) throws
  282. // Called for each packed, repeated uint64 field.
  283. ///
  284. /// This is called once with the complete array of values for
  285. /// the field.
  286. ///
  287. /// There is a default implementation that forwards to the non-packed
  288. /// function.
  289. mutating func visitPackedUInt64Field(value: [UInt64], fieldNumber: Int) throws
  290. // Called for each packed, repeated sint32 field.
  291. ///
  292. /// This is called once with the complete array of values for
  293. /// the field.
  294. ///
  295. /// There is a default implementation that forwards to the non-packed
  296. /// function.
  297. mutating func visitPackedSInt32Field(value: [Int32], fieldNumber: Int) throws
  298. // Called for each packed, repeated sint64 field.
  299. ///
  300. /// This is called once with the complete array of values for
  301. /// the field.
  302. ///
  303. /// There is a default implementation that forwards to the non-packed
  304. /// function.
  305. mutating func visitPackedSInt64Field(value: [Int64], fieldNumber: Int) throws
  306. // Called for each packed, repeated fixed32 field.
  307. ///
  308. /// This is called once with the complete array of values for
  309. /// the field.
  310. ///
  311. /// There is a default implementation that forwards to the non-packed
  312. /// function.
  313. mutating func visitPackedFixed32Field(value: [UInt32], fieldNumber: Int) throws
  314. // Called for each packed, repeated fixed64 field.
  315. ///
  316. /// This is called once with the complete array of values for
  317. /// the field.
  318. ///
  319. /// There is a default implementation that forwards to the non-packed
  320. /// function.
  321. mutating func visitPackedFixed64Field(value: [UInt64], fieldNumber: Int) throws
  322. // Called for each packed, repeated sfixed32 field.
  323. ///
  324. /// This is called once with the complete array of values for
  325. /// the field.
  326. ///
  327. /// There is a default implementation that forwards to the non-packed
  328. /// function.
  329. mutating func visitPackedSFixed32Field(value: [Int32], fieldNumber: Int) throws
  330. // Called for each packed, repeated sfixed64 field.
  331. ///
  332. /// This is called once with the complete array of values for
  333. /// the field.
  334. ///
  335. /// There is a default implementation that forwards to the non-packed
  336. /// function.
  337. mutating func visitPackedSFixed64Field(value: [Int64], fieldNumber: Int) throws
  338. // Called for each packed, repeated bool field.
  339. ///
  340. /// This is called once with the complete array of values for
  341. /// the field.
  342. ///
  343. /// There is a default implementation that forwards to the non-packed
  344. /// function.
  345. mutating func visitPackedBoolField(value: [Bool], fieldNumber: Int) throws
  346. /// Called for each repeated, packed enum field.
  347. /// The method is called once with the complete array of values for
  348. /// the field.
  349. ///
  350. /// A default implementation is provided that simply forwards to
  351. /// `visitRepeatedEnumField`. Implementors who need to handle packed fields
  352. /// differently than unpacked fields can override this and provide distinct
  353. /// implementations.
  354. mutating func visitPackedEnumField<E: Enum>(value: [E], fieldNumber: Int) throws
  355. /// Called for each map field with primitive values. The method is
  356. /// called once with the complete dictionary of keys/values for the
  357. /// field.
  358. ///
  359. /// There is no default implementation. This must be implemented.
  360. mutating func visitMapField<KeyType, ValueType: MapValueType>(
  361. fieldType: _ProtobufMap<KeyType, ValueType>.Type,
  362. value: _ProtobufMap<KeyType, ValueType>.BaseType,
  363. fieldNumber: Int
  364. ) throws
  365. /// Called for each map field with enum values. The method is called
  366. /// once with the complete dictionary of keys/values for the field.
  367. ///
  368. /// There is no default implementation. This must be implemented.
  369. mutating func visitMapField<KeyType, ValueType>(
  370. fieldType: _ProtobufEnumMap<KeyType, ValueType>.Type,
  371. value: _ProtobufEnumMap<KeyType, ValueType>.BaseType,
  372. fieldNumber: Int
  373. ) throws where ValueType.RawValue == Int
  374. /// Called for each map field with message values. The method is
  375. /// called once with the complete dictionary of keys/values for the
  376. /// field.
  377. ///
  378. /// There is no default implementation. This must be implemented.
  379. mutating func visitMapField<KeyType, ValueType>(
  380. fieldType: _ProtobufMessageMap<KeyType, ValueType>.Type,
  381. value: _ProtobufMessageMap<KeyType, ValueType>.BaseType,
  382. fieldNumber: Int
  383. ) throws
  384. /// Called for each extension range.
  385. mutating func visitExtensionFields(fields: ExtensionFieldValueSet, start: Int, end: Int) throws
  386. /// Called for each extension range.
  387. mutating func visitExtensionFieldsAsMessageSet(
  388. fields: ExtensionFieldValueSet,
  389. start: Int,
  390. end: Int
  391. ) throws
  392. /// Called with the raw bytes that represent any unknown fields.
  393. mutating func visitUnknown(bytes: Data) throws
  394. }
  395. /// Forwarding default implementations of some visitor methods, for convenience.
  396. extension Visitor {
  397. // Default definitions of numeric serializations.
  398. //
  399. // The 32-bit versions widen and delegate to 64-bit versions.
  400. // The specialized integer codings delegate to standard Int/UInt.
  401. //
  402. // These "just work" for Hash and Text formats. Most of these work
  403. // for JSON (32-bit integers are overridden to suppress quoting),
  404. // and a few even work for Protobuf Binary (thanks to varint coding
  405. // which erases the size difference between 32-bit and 64-bit ints).
  406. public mutating func visitSingularFloatField(value: Float, fieldNumber: Int) throws {
  407. try visitSingularDoubleField(value: Double(value), fieldNumber: fieldNumber)
  408. }
  409. public mutating func visitSingularInt32Field(value: Int32, fieldNumber: Int) throws {
  410. try visitSingularInt64Field(value: Int64(value), fieldNumber: fieldNumber)
  411. }
  412. public mutating func visitSingularUInt32Field(value: UInt32, fieldNumber: Int) throws {
  413. try visitSingularUInt64Field(value: UInt64(value), fieldNumber: fieldNumber)
  414. }
  415. public mutating func visitSingularSInt32Field(value: Int32, fieldNumber: Int) throws {
  416. try visitSingularInt32Field(value: value, fieldNumber: fieldNumber)
  417. }
  418. public mutating func visitSingularSInt64Field(value: Int64, fieldNumber: Int) throws {
  419. try visitSingularInt64Field(value: value, fieldNumber: fieldNumber)
  420. }
  421. public mutating func visitSingularFixed32Field(value: UInt32, fieldNumber: Int) throws {
  422. try visitSingularUInt32Field(value: value, fieldNumber: fieldNumber)
  423. }
  424. public mutating func visitSingularFixed64Field(value: UInt64, fieldNumber: Int) throws {
  425. try visitSingularUInt64Field(value: value, fieldNumber: fieldNumber)
  426. }
  427. public mutating func visitSingularSFixed32Field(value: Int32, fieldNumber: Int) throws {
  428. try visitSingularInt32Field(value: value, fieldNumber: fieldNumber)
  429. }
  430. public mutating func visitSingularSFixed64Field(value: Int64, fieldNumber: Int) throws {
  431. try visitSingularInt64Field(value: value, fieldNumber: fieldNumber)
  432. }
  433. // Default definitions of repeated serializations that just iterate and
  434. // invoke the singular encoding. These "just work" for Protobuf Binary (encoder
  435. // and size visitor), Protobuf Text, and Hash visitors. JSON format stores
  436. // repeated values differently from singular, so overrides these.
  437. public mutating func visitRepeatedFloatField(value: [Float], fieldNumber: Int) throws {
  438. assert(!value.isEmpty)
  439. for v in value {
  440. try visitSingularFloatField(value: v, fieldNumber: fieldNumber)
  441. }
  442. }
  443. public mutating func visitRepeatedDoubleField(value: [Double], fieldNumber: Int) throws {
  444. assert(!value.isEmpty)
  445. for v in value {
  446. try visitSingularDoubleField(value: v, fieldNumber: fieldNumber)
  447. }
  448. }
  449. public mutating func visitRepeatedInt32Field(value: [Int32], fieldNumber: Int) throws {
  450. assert(!value.isEmpty)
  451. for v in value {
  452. try visitSingularInt32Field(value: v, fieldNumber: fieldNumber)
  453. }
  454. }
  455. public mutating func visitRepeatedInt64Field(value: [Int64], fieldNumber: Int) throws {
  456. assert(!value.isEmpty)
  457. for v in value {
  458. try visitSingularInt64Field(value: v, fieldNumber: fieldNumber)
  459. }
  460. }
  461. public mutating func visitRepeatedUInt32Field(value: [UInt32], fieldNumber: Int) throws {
  462. assert(!value.isEmpty)
  463. for v in value {
  464. try visitSingularUInt32Field(value: v, fieldNumber: fieldNumber)
  465. }
  466. }
  467. public mutating func visitRepeatedUInt64Field(value: [UInt64], fieldNumber: Int) throws {
  468. assert(!value.isEmpty)
  469. for v in value {
  470. try visitSingularUInt64Field(value: v, fieldNumber: fieldNumber)
  471. }
  472. }
  473. public mutating func visitRepeatedSInt32Field(value: [Int32], fieldNumber: Int) throws {
  474. assert(!value.isEmpty)
  475. for v in value {
  476. try visitSingularSInt32Field(value: v, fieldNumber: fieldNumber)
  477. }
  478. }
  479. public mutating func visitRepeatedSInt64Field(value: [Int64], fieldNumber: Int) throws {
  480. assert(!value.isEmpty)
  481. for v in value {
  482. try visitSingularSInt64Field(value: v, fieldNumber: fieldNumber)
  483. }
  484. }
  485. public mutating func visitRepeatedFixed32Field(value: [UInt32], fieldNumber: Int) throws {
  486. assert(!value.isEmpty)
  487. for v in value {
  488. try visitSingularFixed32Field(value: v, fieldNumber: fieldNumber)
  489. }
  490. }
  491. public mutating func visitRepeatedFixed64Field(value: [UInt64], fieldNumber: Int) throws {
  492. assert(!value.isEmpty)
  493. for v in value {
  494. try visitSingularFixed64Field(value: v, fieldNumber: fieldNumber)
  495. }
  496. }
  497. public mutating func visitRepeatedSFixed32Field(value: [Int32], fieldNumber: Int) throws {
  498. assert(!value.isEmpty)
  499. for v in value {
  500. try visitSingularSFixed32Field(value: v, fieldNumber: fieldNumber)
  501. }
  502. }
  503. public mutating func visitRepeatedSFixed64Field(value: [Int64], fieldNumber: Int) throws {
  504. assert(!value.isEmpty)
  505. for v in value {
  506. try visitSingularSFixed64Field(value: v, fieldNumber: fieldNumber)
  507. }
  508. }
  509. public mutating func visitRepeatedBoolField(value: [Bool], fieldNumber: Int) throws {
  510. assert(!value.isEmpty)
  511. for v in value {
  512. try visitSingularBoolField(value: v, fieldNumber: fieldNumber)
  513. }
  514. }
  515. public mutating func visitRepeatedStringField(value: [String], fieldNumber: Int) throws {
  516. assert(!value.isEmpty)
  517. for v in value {
  518. try visitSingularStringField(value: v, fieldNumber: fieldNumber)
  519. }
  520. }
  521. public mutating func visitRepeatedBytesField(value: [Data], fieldNumber: Int) throws {
  522. assert(!value.isEmpty)
  523. for v in value {
  524. try visitSingularBytesField(value: v, fieldNumber: fieldNumber)
  525. }
  526. }
  527. public mutating func visitRepeatedEnumField<E: Enum>(value: [E], fieldNumber: Int) throws {
  528. assert(!value.isEmpty)
  529. for v in value {
  530. try visitSingularEnumField(value: v, fieldNumber: fieldNumber)
  531. }
  532. }
  533. public mutating func visitRepeatedMessageField<M: Message>(value: [M], fieldNumber: Int) throws {
  534. assert(!value.isEmpty)
  535. for v in value {
  536. try visitSingularMessageField(value: v, fieldNumber: fieldNumber)
  537. }
  538. }
  539. public mutating func visitRepeatedGroupField<G: Message>(value: [G], fieldNumber: Int) throws {
  540. assert(!value.isEmpty)
  541. for v in value {
  542. try visitSingularGroupField(value: v, fieldNumber: fieldNumber)
  543. }
  544. }
  545. // Default definitions of packed serialization just defer to the
  546. // repeated implementation. This works for Hash and JSON visitors
  547. // (which do not distinguish packed vs. non-packed) but are
  548. // overridden by Protobuf Binary and Text.
  549. public mutating func visitPackedFloatField(value: [Float], fieldNumber: Int) throws {
  550. assert(!value.isEmpty)
  551. try visitRepeatedFloatField(value: value, fieldNumber: fieldNumber)
  552. }
  553. public mutating func visitPackedDoubleField(value: [Double], fieldNumber: Int) throws {
  554. assert(!value.isEmpty)
  555. try visitRepeatedDoubleField(value: value, fieldNumber: fieldNumber)
  556. }
  557. public mutating func visitPackedInt32Field(value: [Int32], fieldNumber: Int) throws {
  558. assert(!value.isEmpty)
  559. try visitRepeatedInt32Field(value: value, fieldNumber: fieldNumber)
  560. }
  561. public mutating func visitPackedInt64Field(value: [Int64], fieldNumber: Int) throws {
  562. assert(!value.isEmpty)
  563. try visitRepeatedInt64Field(value: value, fieldNumber: fieldNumber)
  564. }
  565. public mutating func visitPackedUInt32Field(value: [UInt32], fieldNumber: Int) throws {
  566. assert(!value.isEmpty)
  567. try visitRepeatedUInt32Field(value: value, fieldNumber: fieldNumber)
  568. }
  569. public mutating func visitPackedUInt64Field(value: [UInt64], fieldNumber: Int) throws {
  570. assert(!value.isEmpty)
  571. try visitRepeatedUInt64Field(value: value, fieldNumber: fieldNumber)
  572. }
  573. public mutating func visitPackedSInt32Field(value: [Int32], fieldNumber: Int) throws {
  574. assert(!value.isEmpty)
  575. try visitPackedInt32Field(value: value, fieldNumber: fieldNumber)
  576. }
  577. public mutating func visitPackedSInt64Field(value: [Int64], fieldNumber: Int) throws {
  578. assert(!value.isEmpty)
  579. try visitPackedInt64Field(value: value, fieldNumber: fieldNumber)
  580. }
  581. public mutating func visitPackedFixed32Field(value: [UInt32], fieldNumber: Int) throws {
  582. assert(!value.isEmpty)
  583. try visitPackedUInt32Field(value: value, fieldNumber: fieldNumber)
  584. }
  585. public mutating func visitPackedFixed64Field(value: [UInt64], fieldNumber: Int) throws {
  586. assert(!value.isEmpty)
  587. try visitPackedUInt64Field(value: value, fieldNumber: fieldNumber)
  588. }
  589. public mutating func visitPackedSFixed32Field(value: [Int32], fieldNumber: Int) throws {
  590. assert(!value.isEmpty)
  591. try visitPackedInt32Field(value: value, fieldNumber: fieldNumber)
  592. }
  593. public mutating func visitPackedSFixed64Field(value: [Int64], fieldNumber: Int) throws {
  594. assert(!value.isEmpty)
  595. try visitPackedInt64Field(value: value, fieldNumber: fieldNumber)
  596. }
  597. public mutating func visitPackedBoolField(value: [Bool], fieldNumber: Int) throws {
  598. assert(!value.isEmpty)
  599. try visitRepeatedBoolField(value: value, fieldNumber: fieldNumber)
  600. }
  601. public mutating func visitPackedEnumField<E: Enum>(
  602. value: [E],
  603. fieldNumber: Int
  604. ) throws {
  605. assert(!value.isEmpty)
  606. try visitRepeatedEnumField(value: value, fieldNumber: fieldNumber)
  607. }
  608. // Default handling for Groups is to treat them just like messages.
  609. // This works for Text and Hash, but is overridden by Protobuf Binary
  610. // format (which has a different encoding for groups) and JSON
  611. // (which explicitly ignores all groups).
  612. public mutating func visitSingularGroupField<G: Message>(
  613. value: G,
  614. fieldNumber: Int
  615. ) throws {
  616. try visitSingularMessageField(value: value, fieldNumber: fieldNumber)
  617. }
  618. // Default handling of Extensions as a MessageSet to handing them just
  619. // as plain extensions. Formats that what custom behavior can override
  620. // it.
  621. public mutating func visitExtensionFieldsAsMessageSet(
  622. fields: ExtensionFieldValueSet,
  623. start: Int,
  624. end: Int
  625. ) throws {
  626. try visitExtensionFields(fields: fields, start: start, end: end)
  627. }
  628. // Default handling for Extensions is to forward the traverse to
  629. // the ExtensionFieldValueSet. Formats that don't care about extensions
  630. // can override to avoid it.
  631. /// Called for each extension range.
  632. public mutating func visitExtensionFields(fields: ExtensionFieldValueSet, start: Int, end: Int) throws {
  633. try fields.traverse(visitor: &self, start: start, end: end)
  634. }
  635. }