Message+BinaryAdditions_Data.swift 11 KB

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