Message+BinaryAdditions_Data.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Sources/SwiftProtobuf/Message+BinaryAdditions_Data.swift - Per-type binary coding
  2. //
  3. // Copyright (c) 2022 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. /// Extensions to `Message` to provide binary coding and decoding using ``Foundation/Data``.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. import Foundation
  15. /// Binary encoding and decoding methods for messages.
  16. extension Message {
  17. /// Creates a new message by decoding the given `Data` value
  18. /// containing a serialized message in Protocol Buffer binary format.
  19. ///
  20. /// - Parameters:
  21. /// - serializedData: The binary-encoded message `Data` to decode.
  22. /// - extensions: An ``ExtensionMap`` used to look up and decode any
  23. /// extensions in this message or messages nested within this message's
  24. /// fields.
  25. /// - partial: If `false` (the default), this method will check
  26. /// ``Message/isInitialized-6abgi`` after decoding to verify that all required
  27. /// fields are present. If any are missing, this method throws
  28. /// ``BinaryDecodingError/missingRequiredFields``.
  29. /// - options: The ``BinaryDecodingOptions`` to use.
  30. /// - Throws: ``BinaryDecodingError`` if decoding fails.
  31. @inlinable
  32. @available(*, deprecated, renamed: "init(serializedBytes:extensions:partial:options:)")
  33. public init(
  34. serializedData data: Data,
  35. extensions: (any ExtensionMap)? = nil,
  36. partial: Bool = false,
  37. options: BinaryDecodingOptions = BinaryDecodingOptions()
  38. ) throws {
  39. self.init()
  40. try merge(serializedBytes: data, extensions: extensions, partial: partial, options: options)
  41. }
  42. /// Creates a new message by decoding the given `Foundation/ContiguousBytes` value
  43. /// containing a serialized message in Protocol Buffer binary format.
  44. ///
  45. /// - Parameters:
  46. /// - contiguousBytes: The binary-encoded message data to decode.
  47. /// - extensions: An ``ExtensionMap`` used to look up and decode any
  48. /// extensions in this message or messages nested within this message's
  49. /// fields.
  50. /// - partial: If `false` (the default), this method will check
  51. /// ``Message/isInitialized-6abgi`` after decoding to verify that all required
  52. /// fields are present. If any are missing, this method throws
  53. /// ``SwiftProtobufError/BinaryDecoding/missingRequiredFields``.
  54. /// - options: The ``BinaryDecodingOptions`` to use.
  55. /// - Throws: ``SwiftProtobufError`` if decoding fails.
  56. @inlinable
  57. @_disfavoredOverload
  58. @available(*, deprecated, renamed: "init(serializedBytes:extensions:partial:options:)")
  59. public init<Bytes: ContiguousBytes>(
  60. contiguousBytes bytes: Bytes,
  61. extensions: (any ExtensionMap)? = nil,
  62. partial: Bool = false,
  63. options: BinaryDecodingOptions = BinaryDecodingOptions()
  64. ) throws {
  65. self.init()
  66. try merge(serializedBytes: bytes, extensions: extensions, partial: partial, options: options)
  67. }
  68. /// Creates a new message by decoding the given `Foundation/ContiguousBytes` value
  69. /// containing a serialized message in Protocol Buffer binary format.
  70. ///
  71. /// - Parameters:
  72. /// - serializedBytes: The binary-encoded message data to decode.
  73. /// - extensions: An ``ExtensionMap`` used to look up and decode any
  74. /// extensions in this message or messages nested within this message's
  75. /// fields.
  76. /// - partial: If `false` (the default), this method will check
  77. /// ``Message/isInitialized-6abgi`` after decoding to verify that all required
  78. /// fields are present. If any are missing, this method throws
  79. /// ``SwiftProtobufError/BinaryDecoding/missingRequiredFields``.
  80. /// - options: The ``BinaryDecodingOptions`` to use.
  81. /// - Throws: ``SwiftProtobufError`` if decoding fails.
  82. @inlinable
  83. @_disfavoredOverload
  84. @available(
  85. *,
  86. deprecated,
  87. message:
  88. "Please conform your Bytes type to `SwiftProtobufContiguousBytes` instead of `Foundation.ContiguousBytes`."
  89. )
  90. public init<Bytes: ContiguousBytes>(
  91. serializedBytes bytes: Bytes,
  92. extensions: (any ExtensionMap)? = nil,
  93. partial: Bool = false,
  94. options: BinaryDecodingOptions = BinaryDecodingOptions()
  95. ) throws {
  96. self.init()
  97. try merge(serializedBytes: bytes, extensions: extensions, partial: partial, options: options)
  98. }
  99. /// Updates the message by decoding the given `Foundation/ContiguousBytes` value
  100. /// containing a serialized message in Protocol Buffer binary format into the
  101. /// receiver.
  102. ///
  103. /// - Note: If this method throws an error, the message may still have been
  104. /// partially mutated by the binary data that was decoded before the error
  105. /// occurred.
  106. ///
  107. /// - Parameters:
  108. /// - contiguousBytes: The binary-encoded message data to decode.
  109. /// - extensions: An ``ExtensionMap`` used to look up and decode any
  110. /// extensions in this message or messages nested within this message's
  111. /// fields.
  112. /// - partial: If `false` (the default), this method will check
  113. /// ``Message/isInitialized-6abgi`` after decoding to verify that all required
  114. /// fields are present. If any are missing, this method throws
  115. /// ``SwiftProtobufError/BinaryDecoding/missingRequiredFields``.
  116. /// - options: The ``BinaryDecodingOptions`` to use.
  117. /// - Throws: ``SwiftProtobufError`` if decoding fails.
  118. @inlinable
  119. @_disfavoredOverload
  120. @available(*, deprecated, renamed: "merge(serializedBytes:extensions:partial:options:)")
  121. public mutating func merge<Bytes: ContiguousBytes>(
  122. contiguousBytes bytes: Bytes,
  123. extensions: (any ExtensionMap)? = nil,
  124. partial: Bool = false,
  125. options: BinaryDecodingOptions = BinaryDecodingOptions()
  126. ) throws {
  127. try bytes.withUnsafeBytes { (body: UnsafeRawBufferPointer) in
  128. try _merge(rawBuffer: body, extensions: extensions, partial: partial, options: options)
  129. }
  130. }
  131. /// Updates the message by decoding the given `Foundation/ContiguousBytes` value
  132. /// containing a serialized message in Protocol Buffer binary format into the
  133. /// receiver.
  134. ///
  135. /// - Note: If this method throws an error, the message may still have been
  136. /// partially mutated by the binary data that was decoded before the error
  137. /// occurred.
  138. ///
  139. /// - Parameters:
  140. /// - serializedBytes: The binary-encoded message data to decode.
  141. /// - extensions: An ``ExtensionMap`` used to look up and decode any
  142. /// extensions in this message or messages nested within this message's
  143. /// fields.
  144. /// - partial: If `false` (the default), this method will check
  145. /// ``Message/isInitialized-6abgi`` after decoding to verify that all required
  146. /// fields are present. If any are missing, this method throws
  147. /// ``SwiftProtobufError/BinaryDecoding/missingRequiredFields``.
  148. /// - options: The ``BinaryDecodingOptions`` to use.
  149. /// - Throws: ``SwiftProtobufError`` if decoding fails.
  150. @inlinable
  151. @_disfavoredOverload
  152. @available(
  153. *,
  154. deprecated,
  155. message:
  156. "Please conform your Bytes type to `SwiftProtobufContiguousBytes` instead of `Foundation.ContiguousBytes`."
  157. )
  158. public mutating func merge<Bytes: ContiguousBytes>(
  159. serializedBytes bytes: Bytes,
  160. extensions: (any ExtensionMap)? = nil,
  161. partial: Bool = false,
  162. options: BinaryDecodingOptions = BinaryDecodingOptions()
  163. ) throws {
  164. try bytes.withUnsafeBytes { (body: UnsafeRawBufferPointer) in
  165. try _merge(rawBuffer: body, extensions: extensions, partial: partial, options: options)
  166. }
  167. }
  168. /// Updates the message by decoding the given `Data` value
  169. /// containing a serialized message in Protocol Buffer binary format into the
  170. /// receiver.
  171. ///
  172. /// - Note: If this method throws an error, the message may still have been
  173. /// partially mutated by the binary data that was decoded before the error
  174. /// occurred.
  175. ///
  176. /// - Parameters:
  177. /// - serializedData: The binary-encoded message data to decode.
  178. /// - extensions: An ``ExtensionMap`` used to look up and decode any
  179. /// extensions in this message or messages nested within this message's
  180. /// fields.
  181. /// - partial: If `false` (the default), this method will check
  182. /// ``Message/isInitialized-6abgi`` after decoding to verify that all required
  183. /// fields are present. If any are missing, this method throws
  184. /// ``BinaryDecodingError/missingRequiredFields``.
  185. /// - options: The ``BinaryDecodingOptions`` to use.
  186. /// - Throws: ``BinaryDecodingError`` if decoding fails.
  187. @inlinable
  188. public mutating func merge(
  189. serializedData data: Data,
  190. extensions: (any ExtensionMap)? = nil,
  191. partial: Bool = false,
  192. options: BinaryDecodingOptions = BinaryDecodingOptions()
  193. ) throws {
  194. try merge(serializedBytes: data, extensions: extensions, partial: partial, options: options)
  195. }
  196. /// Returns a `Data` instance containing the Protocol Buffer binary
  197. /// format serialization of the message.
  198. ///
  199. /// - Parameters:
  200. /// - partial: If `false` (the default), this method will check
  201. /// ``Message/isInitialized-6abgi`` before encoding to verify that all required
  202. /// fields are present. If any are missing, this method throws
  203. /// ``BinaryEncodingError/missingRequiredFields``.
  204. /// - Returns: A `Data` instance containing the binary serialization of the message.
  205. /// - Throws: ``BinaryEncodingError`` if encoding fails.
  206. public func serializedData(partial: Bool = false) throws -> Data {
  207. try serializedBytes(partial: partial, options: BinaryEncodingOptions())
  208. }
  209. /// Returns a `Data` instance containing the Protocol Buffer binary
  210. /// format serialization of the message.
  211. ///
  212. /// - Parameters:
  213. /// - partial: If `false` (the default), this method will check
  214. /// ``Message/isInitialized-6abgi`` before encoding to verify that all required
  215. /// fields are present. If any are missing, this method throws
  216. /// ``BinaryEncodingError/missingRequiredFields``.
  217. /// - options: The ``BinaryEncodingOptions`` to use.
  218. /// - Returns: A `Data` instance containing the binary serialization of the message.
  219. /// - Throws: ``BinaryEncodingError`` if encoding fails.
  220. public func serializedData(
  221. partial: Bool = false,
  222. options: BinaryEncodingOptions = BinaryEncodingOptions()
  223. ) throws -> Data {
  224. try serializedBytes(partial: partial, options: options)
  225. }
  226. }