@@ -1740,6 +1740,10 @@ public protocol UnkeyedDecodingContainer {
1740
1740
/// A container that can support the storage and direct encoding of a single
1741
1741
/// non-keyed value.
1742
1742
public protocol SingleValueEncodingContainer {
1743
+ /// The path of coding keys taken to get to this point in encoding.
1744
+ /// A `nil` value indicates an unkeyed container.
1745
+ var codingPath : [ CodingKey ? ] { get }
1746
+
1743
1747
/// Encodes a null value.
1744
1748
///
1745
1749
/// - throws: `EncodingError.invalidValue` if a null value is invalid in the current context for this format.
@@ -1854,6 +1858,10 @@ public protocol SingleValueEncodingContainer {
1854
1858
1855
1859
/// A `SingleValueDecodingContainer` is a container which can support the storage and direct decoding of a single non-keyed value.
1856
1860
public protocol SingleValueDecodingContainer {
1861
+ /// The path of coding keys taken to get to this point in encoding.
1862
+ /// A `nil` value indicates an unkeyed container.
1863
+ var codingPath : [ CodingKey ? ] { get }
1864
+
1857
1865
/// Decodes a null value.
1858
1866
///
1859
1867
/// - returns: Whether the encountered value was null.
@@ -2175,6 +2183,47 @@ public enum DecodingError : Error {
2175
2183
}
2176
2184
}
2177
2185
2186
+ // The following extensions allow for easier error construction.
2187
+
2188
+ public extension DecodingError {
2189
+ /// A convenience method which creates a new .dataCorrupted error using a constructed coding path and the given debug description.
2190
+ ///
2191
+ /// Constructs a coding path by appending the given key to the given container's coding path.
2192
+ ///
2193
+ /// - param key: The key which caused the failure.
2194
+ /// - param container: The container in which the corrupted data was accessed.
2195
+ /// - param debugDescription: A description of the error to aid in debugging.
2196
+ static func dataCorruptedError< C : KeyedDecodingContainerProtocol > ( forKey key: C . Key , in container: C , debugDescription: String ) -> DecodingError {
2197
+ let context = DecodingError . Context ( codingPath: container. codingPath + [ key] ,
2198
+ debugDescription: debugDescription)
2199
+ return . dataCorrupted( context)
2200
+ }
2201
+
2202
+ /// A convenience method which creates a new .dataCorrupted error using a constructed coding path and the given debug description.
2203
+ ///
2204
+ /// Constructs a coding path by appending a nil key to the given container's coding path.
2205
+ ///
2206
+ /// - param container: The container in which the corrupted data was accessed.
2207
+ /// - param debugDescription: A description of the error to aid in debugging.
2208
+ static func dataCorruptedError( in container: UnkeyedDecodingContainer , debugDescription: String ) -> DecodingError {
2209
+ let context = DecodingError . Context ( codingPath: container. codingPath + [ nil ] ,
2210
+ debugDescription: debugDescription)
2211
+ return . dataCorrupted( context)
2212
+ }
2213
+
2214
+ /// A convenience method which creates a new .dataCorrupted error using a constructed coding path and the given debug description.
2215
+ ///
2216
+ /// Uses the given container's coding path as the constructed path.
2217
+ ///
2218
+ /// - param container: The container in which the corrupted data was accessed.
2219
+ /// - param debugDescription: A description of the error to aid in debugging.
2220
+ static func dataCorruptedError( in container: SingleValueDecodingContainer , debugDescription: String ) -> DecodingError {
2221
+ let context = DecodingError . Context ( codingPath: container. codingPath,
2222
+ debugDescription: debugDescription)
2223
+ return . dataCorrupted( context)
2224
+ }
2225
+ }
2226
+
2178
2227
//===----------------------------------------------------------------------===//
2179
2228
// Keyed Encoding Container Implementations
2180
2229
//===----------------------------------------------------------------------===//
0 commit comments