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