1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
enum Payload: Codable {
case plain(String)
case pair(key: String, value: String)
private enum CodingKeys: CodingKey {
case plain
case pair
}
private enum PlainCodingKeys: CodingKey {
case _0
}
private enum PairCodingKeys: CodingKey {
case key
case value
}
init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Payload.CodingKeys.self)
var allKeys = ArraySlice(container.allKeys)
guard let onlyKey = allKeys.popFirst(), allKeys.isEmpty else {
throw DecodingError.typeMismatch(Payload.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.PlainCodingKeys.self, forKey: Payload.CodingKeys.plain)
self = Payload.plain(try nestedContainer.decode(String.self, forKey: Payload.PlainCodingKeys._0))
case .pair:
let nestedContainer = try container.nestedContainer(keyedBy: Payload.PairCodingKeys.self, forKey: Payload.CodingKeys.pair)
self = Payload.pair(key: try nestedContainer.decode(String.self, forKey: Payload.PairCodingKeys.key), value: try nestedContainer.decode(String.self, forKey: Payload.PairCodingKeys.value))
}
}
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: Payload.CodingKeys.self)
switch self {
case .plain(let a0):
var nestedContainer = container.nestedContainer(keyedBy: Payload.PlainCodingKeys.self, forKey: Payload.CodingKeys.plain)
try nestedContainer.encode(a0, forKey: Payload.PlainCodingKeys._0)
case .pair(let key, let value):
var nestedContainer = container.nestedContainer(keyedBy: Payload.PairCodingKeys.self, forKey: Payload.CodingKeys.pair)
try nestedContainer.encode(key, forKey: Payload.PairCodingKeys.key)
try nestedContainer.encode(value, forKey: Payload.PairCodingKeys.value)
}
}
}
enum Payload_D: Decodable {
case plain(String)
case pair(key: String, value: String)
}
enum Payload_E: Encodable {
case plain(String)
case pair(key: String, value: String)
}
|