Decoder.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Sources/SwiftProtobuf/Decoder.swift - Basic field setting
  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. /// In this way, the generated code only knows about schema
  12. /// information; the decoder logic knows how to decode particular
  13. /// wire types based on that information.
  14. ///
  15. // -----------------------------------------------------------------------------
  16. import Foundation
  17. /// Abstract protocol used by the generated code
  18. /// to deserialize data.
  19. ///
  20. /// The generated code looks roughly like this:
  21. ///
  22. /// ```
  23. /// while fieldNumber = try decoder.nextFieldNumber() {
  24. /// switch fieldNumber {
  25. /// case 1: decoder.decodeRepeatedInt32Field(value: &_field)
  26. /// ... etc ...
  27. /// }
  28. /// ```
  29. ///
  30. /// In particular, note that the decoder must provide field _numbers_
  31. /// corresponding to the numbers in the original proto file.
  32. /// For formats such as Protobuf Binary format that encode field numbers
  33. /// directly, this is trivial. Decoders for formats such as Protobuf
  34. /// Text Format or JSON must use auxiliary information attached to
  35. /// the message type to translate string field names to field numbers.
  36. ///
  37. /// For performance, the field decoding provides three separate methods
  38. /// for every primitive type:
  39. /// * Repeated support accepts an inout Array for repeated fields
  40. /// * Singular support that accepts an inout `Optional`, for proto2
  41. /// * Singular support that accepts an inout non-`Optional`, for proto3
  42. ///
  43. /// Note that we don't distinguish "packed" here, since all existing decoders
  44. /// treat "packed" the same as "repeated" at this level. (That is,
  45. /// even when the serializer distinguishes packed and non-packed
  46. /// forms, the deserializer always accepts both.)
  47. ///
  48. /// Generics come into play at only a few points: `Enum`s and `Message`s
  49. /// use a generic type to locate the correct initializer. Maps and
  50. /// extensions use generics to avoid the method explosion of having to
  51. /// support a separate method for every map and extension type. Maps
  52. /// do distinguish `Enum`-valued and `Message`-valued maps to avoid
  53. /// polluting the generated `Enum` and `Message` types with all of the
  54. /// necessary generic methods to support this.
  55. public protocol Decoder {
  56. /// Called by a `oneof` when it already has a value and is being asked to
  57. /// accept a new value. Some formats require `oneof` decoding to fail in this
  58. /// case.
  59. mutating func handleConflictingOneOf() throws
  60. /// Returns the next field number, or nil when the end of the input is
  61. /// reached.
  62. ///
  63. /// For JSON and text format, the decoder translates the field name to a
  64. /// number at this point, based on information it obtained from the message
  65. /// when it was initialized.
  66. mutating func nextFieldNumber() throws -> Int?
  67. /// Decode a float value to non-`Optional` field storage
  68. mutating func decodeSingularFloatField(value: inout Float) throws
  69. /// Decode a float value to `Optional` field storage
  70. mutating func decodeSingularFloatField(value: inout Float?) throws
  71. /// Decode float values to repeated field storage
  72. mutating func decodeRepeatedFloatField(value: inout [Float]) throws
  73. /// Decode a double value to non-`Optional` field storage
  74. mutating func decodeSingularDoubleField(value: inout Double) throws
  75. /// Decode a double value to `Optional` field storage
  76. mutating func decodeSingularDoubleField(value: inout Double?) throws
  77. /// Decode double values to repeated field storage
  78. mutating func decodeRepeatedDoubleField(value: inout [Double]) throws
  79. /// Decode an int32 value to non-`Optional` field storage
  80. mutating func decodeSingularInt32Field(value: inout Int32) throws
  81. /// Decode an int32 value to `Optional` field storage
  82. mutating func decodeSingularInt32Field(value: inout Int32?) throws
  83. /// Decode int32 values to repeated field storage
  84. mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws
  85. /// Decode an int64 value to non-`Optional` field storage
  86. mutating func decodeSingularInt64Field(value: inout Int64) throws
  87. /// Decode an int64 value to `Optional` field storage
  88. mutating func decodeSingularInt64Field(value: inout Int64?) throws
  89. /// Decode int64 values to repeated field storage
  90. mutating func decodeRepeatedInt64Field(value: inout [Int64]) throws
  91. /// Decode a uint32 value to non-`Optional` field storage
  92. mutating func decodeSingularUInt32Field(value: inout UInt32) throws
  93. /// Decode a uint32 value to `Optional` field storage
  94. mutating func decodeSingularUInt32Field(value: inout UInt32?) throws
  95. /// Decode uint32 values to repeated field storage
  96. mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws
  97. /// Decode a uint64 value to non-`Optional` field storage
  98. mutating func decodeSingularUInt64Field(value: inout UInt64) throws
  99. /// Decode a uint64 value to `Optional` field storage
  100. mutating func decodeSingularUInt64Field(value: inout UInt64?) throws
  101. /// Decode uint64 values to repeated field storage
  102. mutating func decodeRepeatedUInt64Field(value: inout [UInt64]) throws
  103. /// Decode an sint32 value to non-`Optional` field storage
  104. mutating func decodeSingularSInt32Field(value: inout Int32) throws
  105. /// Decode an sint32 value to `Optional` field storage
  106. mutating func decodeSingularSInt32Field(value: inout Int32?) throws
  107. /// Decode sint32 values to repeated field storage
  108. mutating func decodeRepeatedSInt32Field(value: inout [Int32]) throws
  109. /// Decode an sint64 value to non-`Optional` field storage
  110. mutating func decodeSingularSInt64Field(value: inout Int64) throws
  111. /// Decode an sint64 value to `Optional` field storage
  112. mutating func decodeSingularSInt64Field(value: inout Int64?) throws
  113. /// Decode sint64 values to repeated field storage
  114. mutating func decodeRepeatedSInt64Field(value: inout [Int64]) throws
  115. /// Decode a fixed32 value to non-`Optional` field storage
  116. mutating func decodeSingularFixed32Field(value: inout UInt32) throws
  117. /// Decode a fixed32 value to `Optional` field storage
  118. mutating func decodeSingularFixed32Field(value: inout UInt32?) throws
  119. /// Decode fixed32 values to repeated field storage
  120. mutating func decodeRepeatedFixed32Field(value: inout [UInt32]) throws
  121. /// Decode a fixed64 value to non-`Optional` field storage
  122. mutating func decodeSingularFixed64Field(value: inout UInt64) throws
  123. /// Decode a fixed64 value to `Optional` field storage
  124. mutating func decodeSingularFixed64Field(value: inout UInt64?) throws
  125. /// Decode fixed64 values to repeated field storage
  126. mutating func decodeRepeatedFixed64Field(value: inout [UInt64]) throws
  127. /// Decode an sfixed32 value to non-`Optional` field storage
  128. mutating func decodeSingularSFixed32Field(value: inout Int32) throws
  129. /// Decode an sfixed32 value to `Optional` field storage
  130. mutating func decodeSingularSFixed32Field(value: inout Int32?) throws
  131. /// Decode sfixed32 values to repeated field storage
  132. mutating func decodeRepeatedSFixed32Field(value: inout [Int32]) throws
  133. /// Decode an sfixed64 value to non-`Optional` field storage
  134. mutating func decodeSingularSFixed64Field(value: inout Int64) throws
  135. /// Decode an sfixed64 value to `Optional` field storage
  136. mutating func decodeSingularSFixed64Field(value: inout Int64?) throws
  137. /// Decode sfixed64 values to repeated field storage
  138. mutating func decodeRepeatedSFixed64Field(value: inout [Int64]) throws
  139. /// Decode a bool value to non-`Optional` field storage
  140. mutating func decodeSingularBoolField(value: inout Bool) throws
  141. /// Decode a bool value to `Optional` field storage
  142. mutating func decodeSingularBoolField(value: inout Bool?) throws
  143. /// Decode bool values to repeated field storage
  144. mutating func decodeRepeatedBoolField(value: inout [Bool]) throws
  145. /// Decode a string value to non-`Optional` field storage
  146. mutating func decodeSingularStringField(value: inout String) throws
  147. /// Decode a string value to `Optional` field storage
  148. mutating func decodeSingularStringField(value: inout String?) throws
  149. /// Decode string values to repeated field storage
  150. mutating func decodeRepeatedStringField(value: inout [String]) throws
  151. /// Decode a bytes value to non-`Optional` field storage
  152. mutating func decodeSingularBytesField(value: inout Data) throws
  153. /// Decode a bytes value to `Optional` field storage
  154. mutating func decodeSingularBytesField(value: inout Data?) throws
  155. /// Decode bytes values to repeated field storage
  156. mutating func decodeRepeatedBytesField(value: inout [Data]) throws
  157. // Decode Enum fields
  158. /// Decode an enum value to non-`Optional` field storage
  159. mutating func decodeSingularEnumField<E: Enum>(value: inout E) throws where E.RawValue == Int
  160. /// Decode an enum value to `Optional` field storage
  161. mutating func decodeSingularEnumField<E: Enum>(value: inout E?) throws where E.RawValue == Int
  162. /// Decode enum values to repeated field storage
  163. mutating func decodeRepeatedEnumField<E: Enum>(value: inout [E]) throws where E.RawValue == Int
  164. // Decode Message fields
  165. /// Decode a message value to `Optional` field storage.
  166. ///
  167. /// Unlike the primitive types, message fields are always stored
  168. /// as Swift `Optional` values.
  169. mutating func decodeSingularMessageField<M: Message>(value: inout M?) throws
  170. /// Decode message values to repeated field storage
  171. mutating func decodeRepeatedMessageField<M: Message>(value: inout [M]) throws
  172. // Decode Group fields
  173. /// Decode a group value to `Optional` field storage.
  174. ///
  175. /// Unlike the primitive types, message fields are always stored
  176. /// as Swift `Optional` values.
  177. /// Note that groups are only used in proto2.
  178. mutating func decodeSingularGroupField<G: Message>(value: inout G?) throws
  179. /// Decode group values to repeated field storage
  180. mutating func decodeRepeatedGroupField<G: Message>(value: inout [G]) throws
  181. // Decode Map fields.
  182. // This is broken into separate methods depending on whether the value
  183. // type is primitive (_ProtobufMap), enum (_ProtobufEnumMap), or message
  184. // (_ProtobufMessageMap)
  185. /// Decode a map whose values are primitive types (including string and bytes)
  186. mutating func decodeMapField<KeyType, ValueType: MapValueType>(
  187. fieldType: _ProtobufMap<KeyType, ValueType>.Type,
  188. value: inout _ProtobufMap<KeyType, ValueType>.BaseType
  189. ) throws
  190. /// Decode a map whose values are protobuf enum types
  191. mutating func decodeMapField<KeyType, ValueType>(
  192. fieldType: _ProtobufEnumMap<KeyType, ValueType>.Type,
  193. value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType
  194. ) throws where ValueType.RawValue == Int
  195. /// Decode a map whose values are protobuf message types
  196. mutating func decodeMapField<KeyType, ValueType>(
  197. fieldType: _ProtobufMessageMap<KeyType, ValueType>.Type,
  198. value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType
  199. ) throws
  200. // Decode extension fields
  201. /// Decode an extension field
  202. mutating func decodeExtensionField(
  203. values: inout ExtensionFieldValueSet,
  204. messageType: any Message.Type,
  205. fieldNumber: Int
  206. ) throws
  207. // Run a decode loop decoding the MessageSet format for Extensions.
  208. mutating func decodeExtensionFieldsAsMessageSet(
  209. values: inout ExtensionFieldValueSet,
  210. messageType: any Message.Type
  211. ) throws
  212. }
  213. /// Most Decoders won't care about Extension handing as in MessageSet
  214. /// format, so provide a default implementation simply looping on the
  215. /// fieldNumbers and feeding through to extension decoding.
  216. extension Decoder {
  217. public mutating func decodeExtensionFieldsAsMessageSet(
  218. values: inout ExtensionFieldValueSet,
  219. messageType: any Message.Type
  220. ) throws {
  221. while let fieldNumber = try self.nextFieldNumber() {
  222. try self.decodeExtensionField(
  223. values: &values,
  224. messageType: messageType,
  225. fieldNumber: fieldNumber
  226. )
  227. }
  228. }
  229. }