Files
swift-mirror/test/refactoring/AddCodableImplementation/Outputs/enum/decodable.swift.expected
Allan Shortlidge 8079d9cf45 AST: Fix whitespace for switch statements under -print-ast.
No test changes because the tests for `-print-ast` don't match whitespace.
2023-07-31 21:27:46 -07:00

48 lines
1.5 KiB
Plaintext

enum Payload: Codable {
case plain(String)
case pair(key: String, value: String)
}
enum Payload_D: Decodable {
case plain(String)
case pair(key: String, value: String)
enum CodingKeys: CodingKey {
case plain
case pair
}
enum PlainCodingKeys: CodingKey {
case _0
}
enum PairCodingKeys: CodingKey {
case key
case value
}
init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Payload_D.CodingKeys.self)
var allKeys = ArraySlice(container.allKeys)
guard let onlyKey = allKeys.popFirst(), allKeys.isEmpty else {
throw DecodingError.typeMismatch(Payload_D.self, DecodingError.Context.init(codingPath: container.codingPath, debugDescription: "Invalid number of keys found, expected one.", underlyingError: nil))
}
switch onlyKey {
case .plain:
let nestedContainer = try container.nestedContainer(keyedBy: Payload_D.PlainCodingKeys.self, forKey: Payload_D.CodingKeys.plain)
self = Payload_D.plain(try nestedContainer.decode(String.self, forKey: Payload_D.PlainCodingKeys._0))
case .pair:
let nestedContainer = try container.nestedContainer(keyedBy: Payload_D.PairCodingKeys.self, forKey: Payload_D.CodingKeys.pair)
self = Payload_D.pair(key: try nestedContainer.decode(String.self, forKey: Payload_D.PairCodingKeys.key), value: try nestedContainer.decode(String.self, forKey: Payload_D.PairCodingKeys.value))
}
}
}
enum Payload_E: Encodable {
case plain(String)
case pair(key: String, value: String)
}