@@ -32,10 +32,44 @@ public protocol HelperCoder {
3232 /// - Throws: If decoding fails due to corrupted or invalid data.
3333 func decodeIfPresent( from decoder: Decoder ) throws -> Coded ?
3434
35+ /// Decodes a value of the ``Coded`` type from the given `container`
36+ /// and specified `key`.
37+ ///
38+ /// Uses ``decode(from:)`` implementation by default
39+ /// to get value from the `decoder` at the specified key.
40+ ///
41+ /// - Parameters:
42+ /// - container: The container to read data from.
43+ /// - key: The key for the value decoded.
44+ ///
45+ /// - Returns: A value of the ``Coded`` type.
46+ /// - Throws: If decoding fails due to corrupted or invalid data.
47+ func decode< DecodingContainer: KeyedDecodingContainerProtocol > (
48+ from container: DecodingContainer ,
49+ forKey key: DecodingContainer . Key
50+ ) throws -> Coded
51+ /// Decodes an optional value of the ``Coded`` type from
52+ /// the given `container` and specified `key`, if present.
53+ ///
54+ /// Uses ``decodeIfPresent(from:)`` implementation by default
55+ /// to get value if any value exists at specified key,
56+ /// otherwise returns `nil` if any error thrown.
57+ ///
58+ /// - Parameters:
59+ /// - container: The container to read data from.
60+ /// - key: The key for the value decoded.
61+ ///
62+ /// - Returns: An optional value of the ``Coded`` type.
63+ /// - Throws: If decoding fails due to corrupted or invalid data.
64+ func decodeIfPresent< DecodingContainer: KeyedDecodingContainerProtocol > (
65+ from container: DecodingContainer ,
66+ forKey key: DecodingContainer . Key
67+ ) throws -> Coded ?
68+
3569 /// Encodes given value of the ``Coded`` type to the provided `encoder`.
3670 ///
37- /// If the ``Coded`` value confirms to `Encodable`, then encoding is
38- /// performed. Otherwise no data written to the encoder.
71+ /// By default, of the ``Coded`` value confirms to `Encodable`, then
72+ /// encoding is performed. Otherwise no data written to the encoder.
3973 ///
4074 /// - Parameters:
4175 /// - value: The ``Coded`` value to encode.
@@ -46,15 +80,50 @@ public protocol HelperCoder {
4680 /// Encodes given optional value of the ``Coded`` type to the provided
4781 /// `encoder` if it is not `nil`.
4882 ///
49- /// If the ``Coded`` value confirms to `Encodable`, then encoding is
50- /// performed. Otherwise no data written to the encoder.
83+ /// By default, of the ``Coded`` value confirms to `Encodable`, then
84+ /// encoding is performed. Otherwise no data written to the encoder.
5185 ///
5286 /// - Parameters:
5387 /// - value: The optional ``Coded`` value to encode.
5488 /// - encoder: The encoder to write data to.
5589 ///
5690 /// - Throws: If any values are invalid for the given encoder’s format.
5791 func encodeIfPresent( _ value: Coded ? , to encoder: Encoder ) throws
92+
93+ /// Encodes given value of the ``Coded`` type to the provided `container`
94+ /// at the specified `key`.
95+ ///
96+ /// By default, of the ``Coded`` value confirms to `Encodable`, then
97+ /// encoding is performed. Otherwise no data written to the encoder.
98+ ///
99+ /// - Parameters:
100+ /// - value: The ``Coded`` value to encode.
101+ /// - container: The container to write data to.
102+ /// - key: The key to write data at.
103+ ///
104+ /// - Throws: If any values are invalid for the given encoder’s format.
105+ func encode< EncodingContainer: KeyedEncodingContainerProtocol > (
106+ _ value: Coded ,
107+ to container: inout EncodingContainer ,
108+ atKey key: EncodingContainer . Key
109+ ) throws
110+ /// Encodes given optional value of the ``Coded`` type to the provided
111+ /// `container` at the specified `key`, if it is not `nil`.
112+ ///
113+ /// By default, of the ``Coded`` value confirms to `Encodable`, then
114+ /// encoding is performed. Otherwise no data written to the encoder.
115+ ///
116+ /// - Parameters:
117+ /// - value: The optional ``Coded`` value to encode.
118+ /// - container: The container to write data to.
119+ /// - key: The key to write data at.
120+ ///
121+ /// - Throws: If any values are invalid for the given encoder’s format.
122+ func encodeIfPresent< EncodingContainer: KeyedEncodingContainerProtocol > (
123+ _ value: Coded ? ,
124+ to container: inout EncodingContainer ,
125+ atKey key: EncodingContainer . Key
126+ ) throws
58127}
59128
60129public extension HelperCoder {
@@ -73,6 +142,49 @@ public extension HelperCoder {
73142 return try ? self . decode ( from: decoder)
74143 }
75144
145+ /// Decodes a value of the ``HelperCoder/Coded`` type from the given
146+ /// `container` and specified `key`.
147+ ///
148+ /// Uses ``decode(from:)`` implementation by default
149+ /// to get value from the `decoder` at the specified key.
150+ ///
151+ /// - Parameters:
152+ /// - container: The container to read data from.
153+ /// - key: The key for the value decoded.
154+ ///
155+ /// - Returns: A value of the ``HelperCoder/Coded`` type.
156+ /// - Throws: If decoding fails due to corrupted or invalid data.
157+ @inlinable
158+ func decode< DecodingContainer: KeyedDecodingContainerProtocol > (
159+ from container: DecodingContainer ,
160+ forKey key: DecodingContainer . Key
161+ ) throws -> Coded {
162+ return try self . decode ( from: container. superDecoder ( forKey: key) )
163+ }
164+
165+ /// Decodes an optional value of the ``HelperCoder/Coded`` type from
166+ /// the given `container` and specified `key`, if present.
167+ ///
168+ /// Uses ``decodeIfPresent(from:)`` implementation by default
169+ /// to get value if any value exists at specified key,
170+ /// otherwise returns `nil` if any error thrown.
171+ ///
172+ /// - Parameters:
173+ /// - container: The container to read data from.
174+ /// - key: The key for the value decoded.
175+ ///
176+ /// - Returns: An optional value of the ``HelperCoder/Coded`` type.
177+ /// - Throws: If decoding fails due to corrupted or invalid data.
178+ @inlinable
179+ func decodeIfPresent< DecodingContainer: KeyedDecodingContainerProtocol > (
180+ from container: DecodingContainer ,
181+ forKey key: DecodingContainer . Key
182+ ) throws -> Coded ? {
183+ guard let decoder = try ? container. superDecoder ( forKey: key)
184+ else { return nil }
185+ return try self . decodeIfPresent ( from: decoder)
186+ }
187+
76188 /// Encodes given value of the ``HelperCoder/Coded`` type
77189 /// to the provided `encoder`.
78190 ///
@@ -105,6 +217,49 @@ public extension HelperCoder {
105217 guard let value else { return }
106218 try self . encode ( value, to: encoder)
107219 }
220+
221+ /// Encodes given value of the ``Coded`` type to the provided `container`
222+ /// at the specified `key`.
223+ ///
224+ /// By default, of the ``Coded`` value confirms to `Encodable`, then
225+ /// encoding is performed. Otherwise no data written to the encoder.
226+ ///
227+ /// - Parameters:
228+ /// - value: The ``Coded`` value to encode.
229+ /// - container: The container to write data to.
230+ /// - key: The key to write data at.
231+ ///
232+ /// - Throws: If any values are invalid for the given encoder’s format.
233+ @inlinable
234+ func encode< EncodingContainer: KeyedEncodingContainerProtocol > (
235+ _ value: Coded ,
236+ to container: inout EncodingContainer ,
237+ atKey key: EncodingContainer . Key
238+ ) throws {
239+ try self . encode ( value, to: container. superEncoder ( forKey: key) )
240+ }
241+
242+ /// Encodes given optional value of the ``Coded`` type to the provided
243+ /// `container` at the specified `key`, if it is not `nil`.
244+ ///
245+ /// By default, of the ``Coded`` value confirms to `Encodable`, then
246+ /// encoding is performed. Otherwise no data written to the encoder.
247+ ///
248+ /// - Parameters:
249+ /// - value: The optional ``Coded`` value to encode.
250+ /// - container: The container to write data to.
251+ /// - key: The key to write data at.
252+ ///
253+ /// - Throws: If any values are invalid for the given encoder’s format.
254+ @inlinable
255+ func encodeIfPresent< EncodingContainer: KeyedEncodingContainerProtocol > (
256+ _ value: Coded ? ,
257+ to container: inout EncodingContainer ,
258+ atKey key: EncodingContainer . Key
259+ ) throws {
260+ guard let value else { return }
261+ try self . encode ( value, to: & container, atKey: key)
262+ }
108263}
109264
110265public extension HelperCoder where Coded: Encodable {
0 commit comments