How would you write the decoding code for the following JSON:
{
"identifier": "1",
"issuer": "visa",
"pattern": [5, [7, 9]]
}
To map it into the following model:
struct Card: Decodable {
let identifier: String
let issuer: String
let pattern: [CardPattern]
}
enum CardPattern: Decodable {
case prefix(Int)
case range(start: Int, end: Int)
}
Notice how the pattern attribute in the json is a collection of two possible values:
Intindicating we should map into aCardPattern.prefixcase[Int]containing two values, indicating we should map into aCardPattern.rangecase (first value isstart, second value isend)